From c9f84f06c4bc8ee6e47d709fc53523888fe658cd Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sat, 5 Dec 2026 18:50:58 +0800 Subject: [PATCH] test(contracts): share closed object schema fixtures (#17035) --- src/contracts/toolInputReferences.test.ts | 95 +++++++++-------------- 1 file changed, 35 insertions(+), 60 deletions(-) diff --git a/src/contracts/toolInputReferences.test.ts b/src/contracts/toolInputReferences.test.ts index 1dc2085f663..5875059d3ae 100644 --- a/src/contracts/toolInputReferences.test.ts +++ b/src/contracts/toolInputReferences.test.ts @@ -18,6 +18,16 @@ import { import { drafts, validatorFor } from "./advertisedSchemaValidator.fixture.js"; import { JAVASCRIPT_APPLICATION_EVIDENCE_EXAMPLE } from "./javascript/javascriptRuntimeReconciliationExample.js"; +const closedObjectSchema = ( + properties: Record, + required: string[], +) => ({ + type: "object", + properties, + required, + additionalProperties: false, +}); + const contractFrom = (family: readonly ToolContract[], name: string) => { const contract = family.find((entry) => entry.name === name); if (contract === undefined) @@ -301,12 +311,7 @@ describe("root input presentation", () => { ["__proto__", { type: "string" }], ["constructor", { type: "number" }], ]); - const branch = { - type: "object", - properties: fields, - required: ["__proto__"], - additionalProperties: false, - }; + const branch = closedObjectSchema(fields, ["__proto__"]); const presented = presentInputJsonSchema( { anyOf: [branch, branch] }, (property) => `Parameter ${property}`, @@ -553,24 +558,20 @@ describe("advertised Boolean schema composition", () => { properties: { selection: { oneOf: [ - { - type: "object", - properties: { + closedObjectSchema( + { source: { type: "string" }, mode: { type: "boolean", const: false }, }, - required: ["source", "mode"], - additionalProperties: false, - }, - { - type: "object", - properties: { + ["source", "mode"], + ), + closedObjectSchema( + { target: { type: "string" }, mode: { type: "boolean", const: true }, }, - required: ["target", "mode"], - additionalProperties: false, - }, + ["target", "mode"], + ), ], }, }, @@ -692,18 +693,8 @@ it.each(["$ref", "$dynamicRef"])( (keyword) => { const source = { anyOf: [ - { - type: "object", - properties: { end: { const: "done" } }, - required: ["end"], - additionalProperties: false, - }, - { - type: "object", - properties: { next: { [keyword]: "#" } }, - required: ["next"], - additionalProperties: false, - }, + closedObjectSchema({ end: { const: "done" } }, ["end"]), + closedObjectSchema({ next: { [keyword]: "#" } }, ["next"]), ], }; const presented = presentInputJsonSchema(source, () => undefined); @@ -724,25 +715,16 @@ describe("root union default presentation", () => { it("preserves a required prototype-named field in a group-only object", () => { const canonical = { anyOf: [ - { - type: "object", - properties: { text: { type: "string" } }, - required: ["text"], - additionalProperties: false, - }, - { - type: "object", - properties: { - config: { - type: "object", - properties: { ["__proto__"]: { type: "string", default: "" } }, - required: ["__proto__"], - additionalProperties: false, - }, + closedObjectSchema({ text: { type: "string" } }, ["text"]), + closedObjectSchema( + { + config: closedObjectSchema( + { ["__proto__"]: { type: "string", default: "" } }, + ["__proto__"], + ), }, - required: ["config"], - additionalProperties: false, - }, + ["config"], + ), ], }; const advertised = presentInputJsonSchema(canonical, () => undefined); @@ -816,15 +798,9 @@ describe("root union default presentation", () => { ? "http://json-schema.org/draft-07/schema#" : "https://json-schema.org/draft/2020-12/schema", anyOf: [ - { - type: "object", - properties: { text: { type: "string" } }, - required: ["text"], - additionalProperties: false, - }, - { - type: "object", - properties: { + closedObjectSchema({ text: { type: "string" } }, ["text"]), + closedObjectSchema( + { normalization: { $id: "https://example.test/normalization", type: "object", @@ -835,9 +811,8 @@ describe("root union default presentation", () => { default: { rules: { type: "string" } }, }, }, - required: ["normalization"], - additionalProperties: false, - }, + ["normalization"], + ), ], }; const advertised = presentInputJsonSchema(canonical, () => undefined);