Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 162 lines 6.44 KB
Raw
1 /**
2 * Property-based acceptance parity for scalar JSON Schema conversion.
3 *
4 * The oracle is Ajv in Draft 7 mode: for every generated scalar schema and
5 * instance, the converted Zod schema must accept exactly what the oracle
6 * accepts.
7 *
8 * The generator deliberately stays inside the semantics the converter claims
9 * to support today; shrinking an exclusion is the way to turn a fixed
10 * behavior into a permanent regression guard. The remaining exclusions are
11 * deliberate divergences or oracle limits, not open bugs:
12 *
13 * - `multipleOf` is always an integer: the converter checks decimal
14 * multiples through decimal scaling (see the `primitive-float-multiple-of`
15 * corpus case) while the oracle uses raw float modulo
16 * - subnormal numbers are skipped under `multipleOf`: the oracle divides in
17 * floating point, and `-5e-324 / 5` underflows to `-0`, so Ajv calls a
18 * subnormal a multiple of any small integer. The converter's exact check
19 * rejects it (see "does not treat a subnormal as an integer multiple" in
20 * semantic-regressions.test.ts)
21 * - draft-4 boolean `exclusiveMinimum`/`exclusiveMaximum` are never
22 * generated: they are not part of Draft 7, so the oracle cannot express
23 * them (the `primitive-draft4-boolean-exclusive-bounds` corpus case covers
24 * them)
25 */
26 import Ajv from 'ajv';
27 import fc from 'fast-check';
28 import { describe, expect, it } from 'vitest';
29
30 import type { JsonSchema, Serializable } from '../src/types';
31 import { builders } from './support/builders';
32
33 const SCALAR_TYPES = ['string', 'integer', 'number', 'boolean', 'null'] as const;
34 type ScalarType = (typeof SCALAR_TYPES)[number];
35
36 const PATTERN_POOL = ['^[a-z]+$', '^[A-Z]{2}$', '[0-9]', '^a.*z$', '^(?=.*[a-y])a'] as const;
37 const scalarValue: fc.Arbitrary<Serializable> = fc.oneof(
38 fc.constant(null),
39 fc.boolean(),
40 fc.integer({ min: -100, max: 100 }),
41 fc.double({ min: -100, max: 100, noNaN: true, noDefaultInfinity: true }),
42 fc.string({
43 unit: fc.constantFrom(...'abcdefghijklmnopqrstuvwxyz', '\u{1D11E}'),
44 maxLength: 6,
45 })
46 );
47
48 type SchemaRecord = Record<string, unknown>;
49
50 const scalarSchema: fc.Arbitrary<SchemaRecord> = fc
51 .record(
52 {
53 types: fc.option(
54 fc.uniqueArray(fc.constantFrom<ScalarType>(...SCALAR_TYPES), {
55 minLength: 1,
56 maxLength: 3,
57 }),
58 { nil: undefined }
59 ),
60 collapseSingle: fc.boolean(),
61 literalKind: fc.constantFrom('none', 'enum', 'const', 'both'),
62 enumValues: fc.uniqueArray(scalarValue, {
63 minLength: 1,
64 maxLength: 4,
65 comparator: 'SameValue',
66 }),
67 constValue: scalarValue,
68 minLength: fc.option(fc.integer({ min: 0, max: 4 }), { nil: undefined }),
69 maxLength: fc.option(fc.integer({ min: 0, max: 6 }), { nil: undefined }),
70 pattern: fc.option(fc.constantFrom(...PATTERN_POOL), { nil: undefined }),
71 minimum: fc.option(fc.integer({ min: -20, max: 20 }), { nil: undefined }),
72 maximum: fc.option(fc.integer({ min: -20, max: 20 }), { nil: undefined }),
73 exclusiveMinimum: fc.option(fc.integer({ min: -20, max: 20 }), { nil: undefined }),
74 exclusiveMaximum: fc.option(fc.integer({ min: -20, max: 20 }), { nil: undefined }),
75 multipleOf: fc.option(fc.integer({ min: 1, max: 5 }), { nil: undefined }),
76 },
77 { noNullPrototype: true }
78 )
79 .map(parts => {
80 const schema: SchemaRecord = {};
81 const declared: ScalarType[] = parts.types ?? [];
82
83 if (parts.types) {
84 schema.type = parts.types.length === 1 && parts.collapseSingle ? parts.types[0] : parts.types;
85 }
86 if (parts.literalKind === 'enum' || parts.literalKind === 'both')
87 schema.enum = parts.enumValues;
88 if (parts.literalKind === 'const' || parts.literalKind === 'both')
89 schema.const = parts.constValue;
90
91 // Constraints attach independently of the declared type (or its absence):
92 // Draft 7 scopes every scalar keyword to matching instance types anyway.
93 void declared;
94 for (const key of [
95 'minLength',
96 'maxLength',
97 'pattern',
98 'minimum',
99 'maximum',
100 'exclusiveMinimum',
101 'exclusiveMaximum',
102 'multipleOf',
103 ] as const) {
104 if (parts[key] !== undefined) schema[key] = parts[key];
105 }
106 return schema;
107 });
108
109 const schemaAndInstance: fc.Arbitrary<{ schema: SchemaRecord; value: Serializable }> =
110 scalarSchema.chain(schema => {
111 const interesting: Serializable[] = [];
112 if (Array.isArray(schema.enum)) interesting.push(...(schema.enum as Serializable[]));
113 if ('const' in schema) interesting.push(schema.const as Serializable);
114 for (const key of ['minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum'] as const) {
115 const bound = schema[key];
116 if (typeof bound === 'number') interesting.push(bound, bound + 1, bound - 1);
117 }
118 const pools = [scalarValue];
119 if (interesting.length > 0) pools.push(fc.constantFrom(...interesting));
120 return fc.oneof(...pools).map(value => ({ schema, value }));
121 });
122
123 const ajv = new Ajv({ strict: false });
124
125 /** Smallest positive normal double; anything nonzero below it is subnormal. */
126 const MIN_NORMAL = 2 ** -1022;
127 const isSubnormal = (value: Serializable): boolean =>
128 typeof value === 'number' && value !== 0 && Math.abs(value) < MIN_NORMAL;
129
130 describe.each(builders)('scalar conversion matches the Draft 7 oracle ($name)', builder => {
131 it('accepts exactly what Ajv accepts, standalone and object-wrapped', () => {
132 fc.assert(
133 fc.property(schemaAndInstance, ({ schema, value }) => {
134 fc.pre(!(schema.multipleOf !== undefined && isSubnormal(value)));
135
136 const objectSchema = {
137 type: 'object',
138 properties: { value: schema },
139 required: ['value'],
140 additionalProperties: false,
141 };
142 const instance = { value };
143
144 const oracleScalar = ajv.compile(schema)(value);
145 const zodScalar = builder.convert(schema as JsonSchema).safeParse(value).success;
146 expect(
147 zodScalar,
148 `standalone schema=${JSON.stringify(schema)} value=${JSON.stringify(value)}`
149 ).toBe(oracleScalar);
150
151 const oracleObject = ajv.compile(objectSchema)(instance);
152 const zodObject = builder.convert(objectSchema as JsonSchema).safeParse(instance).success;
153 expect(
154 zodObject,
155 `wrapped schema=${JSON.stringify(schema)} value=${JSON.stringify(value)}`
156 ).toBe(oracleObject);
157 }),
158 { numRuns: 300 }
159 );
160 // 300 Ajv-compiled runs sit close to vitest's 5s default on CI runners.
161 }, 30_000);
162 });