Files
reverse-engineering-anything/src/contracts/advertisedDialectProjection.test.ts
T
morluto b0e0d52a9f refactor(mcp): leave input literal portability to the advertised dialect projection (#11207)
* refactor(mcp): leave input literal portability to the advertised dialect projection

* test(mcp): leave listed-input literal portability to the dialect check

* test(mcp): trigger normalization dependency cases with non-portable patterns

* refactor(mcp): let the dialect projection own input patterns and dependency prose (#11238)

* refactor(mcp): let the dialect projection own input patterns and dependency prose

* refactor(mcp): drop the unused import from the output schema presentation

* test(mcp): force the reduced form by depth now that the projection re-emits patterns

* test(mcp): take advertised input from the one advertised-tool owner after #11268

* test(mcp): check the base64 pad-bit widening on the advertised input, not the registered one
2026-11-16 03:55:27 +08:00

274 lines
8.1 KiB
TypeScript

import { Ajv2020 } from "ajv/dist/2020.js";
import { addFormats } from "@modelcontextprotocol/client/validators/ajv";
import { describe, expect, it } from "vitest";
import { advertisedInputDialectViolations } from "./advertisedDialect.js";
import { projectToAdvertisedDialect } from "./advertisedDialectProjection.js";
const project = (properties: Record<string, unknown>, root = {}) =>
projectToAdvertisedDialect({
$schema: "https://json-schema.org/draft/2020-12/schema",
type: "object",
properties,
additionalProperties: false,
...root,
});
/** Every canonical-valid value stays valid; the projection only widens. */
const expectWidening = (
canonical: Record<string, unknown>,
values: readonly unknown[],
) => {
const { schema } = project({ value: canonical });
const ajv = new Ajv2020({ strict: false });
addFormats(ajv);
const validateCanonical = ajv.compile({
type: "object",
properties: { value: canonical },
});
const validateAdvertised = ajv.compile(schema);
for (const value of values)
if (validateCanonical({ value }))
expect(validateAdvertised({ value }), JSON.stringify(value)).toBe(true);
return schema.properties as Record<string, unknown>;
};
it("does not move a closed allOf member's property scope onto another member", () => {
expectWidening(
{
allOf: [
{ type: "object", properties: { a: { type: "string" } } },
{
type: "object",
properties: { a: { type: "string" }, b: { type: "string" } },
additionalProperties: false,
},
],
},
[{ a: "x", b: "y" }],
);
});
it("does not move an allOf member's tuple tail beside other positions", () => {
expectWidening(
{
type: "array",
allOf: [
{ prefixItems: [{ type: "number" }] },
{ prefixItems: [{ type: "number" }, { type: "string" }], items: false },
],
},
[[1, "ok"]],
);
});
it("does not move a single union branch's closure onto the parent's properties", () => {
expectWidening(
{
type: "object",
properties: { a: { type: "string" } },
anyOf: [
{
type: "object",
properties: { a: { type: "string" }, b: { type: "string" } },
additionalProperties: false,
},
],
},
[{ a: "x", b: "y" }],
);
});
it("leaves objects open when dropped pattern properties admit names", () => {
expectWidening(
{
type: "object",
patternProperties: { "^b$": { type: "string" } },
additionalProperties: false,
},
[{ b: "ok" }],
);
});
it("does not make a union branch's omitted rule into global guidance", () => {
const { guidance } = project({
value: {
anyOf: [
{ type: "string", not: { enum: ["reserved"] } },
{ type: "string", enum: ["reserved"] },
],
},
});
expect(guidance.join(" ")).not.toContain('must not be "reserved"');
});
it("keeps a property named __proto__ as an own property", () => {
const canonical = JSON.parse(
'{"type":"object","properties":{"__proto__":{"type":"string"}},"required":["__proto__"],"additionalProperties":false}',
) as Record<string, unknown>;
const { schema } = projectToAdvertisedDialect(canonical);
const properties = schema.properties as Record<string, unknown>;
expect(Object.getPrototypeOf(properties)).toBe(Object.prototype);
expect(Object.hasOwn(properties, "__proto__")).toBe(true);
expect(properties.__proto__).toEqual({ type: "string" });
expect(schema).toMatchObject({
required: ["__proto__"],
additionalProperties: false,
});
});
describe("portable advertised dialect projection", () => {
it.each([
["string const", { const: "a" }, { type: "string", enum: ["a"] }],
["empty string const", { type: "string", const: "" }, { type: "string" }],
["boolean const", { type: "boolean", const: true }, { type: "boolean" }],
["numeric enum", { enum: [1, 2] }, { type: "integer" }],
[
"integer exclusive bound",
{ type: "integer", exclusiveMinimum: 0, exclusiveMaximum: 10 },
{ type: "integer", minimum: 1, maximum: 9 },
],
[
"number exclusive bound",
{ type: "number", exclusiveMinimum: 0.5 },
{ type: "number", minimum: 0.5 },
],
[
"pattern-only allOf members",
{
type: "string",
allOf: [{ pattern: "^a[a-z]*$" }, { pattern: "^[a-z]*b$" }],
},
{ type: "string", pattern: "^a[a-z]*$" },
],
[
"pattern in its portable form",
{ type: "string", pattern: "^\\d+$" },
{ type: "string", pattern: "^[0-9]+$" },
],
[
"pattern without a portable form",
{ type: "string", pattern: "^😀$" },
{ type: "string" },
],
[
"string literal union",
{ anyOf: [{ type: "string", enum: ["a", "b"] }, { const: "c" }] },
{ type: "string", enum: ["a", "b", "c"] },
],
[
"homogeneous tuple",
{
type: "array",
prefixItems: [{ type: "number" }, { type: "number" }],
minItems: 2,
maxItems: 2,
},
{ type: "array", items: { type: "number" }, minItems: 2, maxItems: 2 },
],
[
"client-unsafe bounds",
{ type: "string", minLength: 1, maxLength: 4096, format: "uuid" },
{ type: "string", minLength: 1 },
],
[
"open map",
{
type: "object",
propertyNames: { pattern: "^k" },
additionalProperties: { type: "string" },
minProperties: 1,
},
{ type: "object" },
],
])("projects a %s exactly or wider", (_label, canonical, expected) => {
const properties = expectWidening(canonical, [
"a",
"c",
"",
true,
0,
1,
2,
5,
0.5,
10,
[1, 2],
{ k: "v" },
"ab",
]);
expect(properties.value).toEqual(expected);
});
it("widens oneOf, types the union and keeps the null branch of nullable values", () => {
const { schema } = project({
view: {
oneOf: [
{ type: "object", properties: { kind: { const: "a" } } },
{ type: "object", properties: { kind: { const: "b" } } },
],
},
limit: { type: ["integer", "null"], minimum: 1 },
});
expect(schema.properties).toEqual({
view: {
type: "object",
anyOf: [
{
type: "object",
properties: { kind: { type: "string", enum: ["a"] } },
},
{
type: "object",
properties: { kind: { type: "string", enum: ["b"] } },
},
],
},
limit: { anyOf: [{ type: "integer", minimum: 1 }, { type: "null" }] },
});
// A nullable value is a typed branch or null, within the dialect.
expect(
advertisedInputDialectViolations({ name: "probe", inputSchema: schema }),
).toEqual([]);
});
it("moves the root description and dropped cross-field constraints to tool guidance", () => {
const projection = project(
{
table: { type: "string" },
row_limit: { type: "integer" },
ids: { type: "array", items: { type: "string" }, uniqueItems: true },
name: { type: "string", not: { enum: ["reserved"] } },
},
{
description: "Root guidance.",
dependentRequired: { row_limit: ["table"] },
if: { required: ["table"] },
then: { required: ["row_limit"] },
},
);
expect(projection.guidance).toEqual([
"Root guidance.",
"When row_limit is provided, also provide table.",
"`ids` entries must be distinct.",
'`name` must not be "reserved".',
]);
expect(projection.schema).not.toHaveProperty("description");
expect(
advertisedInputDialectViolations({
name: "probe",
inputSchema: projection.schema,
}),
).toEqual([]);
});
it("bounds nested descriptions without splitting a surrogate pair", () => {
const description = `${"x".repeat(1022)}😀tail`;
const { schema } = project({ value: { type: "string", description } });
const value = (schema.properties as Record<string, Record<string, string>>)
.value!;
expect(value.description!.length).toBeLessThanOrEqual(1024);
expect(value.description!.endsWith("x…")).toBe(true);
});
});