apcore-js 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/.github/workflows/ci.yml +39 -0
  2. package/CHANGELOG.md +70 -0
  3. package/package.json +4 -2
  4. package/src/acl.ts +21 -8
  5. package/src/async-task.ts +267 -0
  6. package/src/bindings.ts +6 -0
  7. package/src/cancel.ts +32 -0
  8. package/src/context.ts +87 -2
  9. package/src/errors.ts +7 -2
  10. package/src/executor.ts +35 -9
  11. package/src/extensions.ts +265 -0
  12. package/src/index.ts +18 -4
  13. package/src/middleware/manager.ts +1 -1
  14. package/src/observability/context-logger.ts +4 -2
  15. package/src/observability/metrics.ts +4 -2
  16. package/src/observability/tracing.ts +73 -8
  17. package/src/registry/index.ts +1 -0
  18. package/src/registry/registry.ts +229 -5
  19. package/src/registry/scanner.ts +28 -10
  20. package/src/registry/schema-export.ts +10 -3
  21. package/src/schema/loader.ts +29 -15
  22. package/src/schema/ref-resolver.ts +14 -2
  23. package/src/schema/strict.ts +11 -1
  24. package/src/trace-context.ts +102 -0
  25. package/tests/async-task.test.ts +335 -0
  26. package/tests/integration/test-acl-safety.test.ts +2 -1
  27. package/tests/observability/test-metrics.test.ts +98 -1
  28. package/tests/observability/test-tracing.test.ts +173 -1
  29. package/tests/registry/test-registry.test.ts +1258 -1
  30. package/tests/registry/test-schema-export.test.ts +131 -1
  31. package/tests/schema/test-loader.test.ts +366 -2
  32. package/tests/schema/test-ref-resolver.test.ts +427 -2
  33. package/tests/schema/test-strict.test.ts +209 -0
  34. package/tests/test-acl.test.ts +218 -1
  35. package/tests/test-cancel.test.ts +71 -0
  36. package/tests/test-context.test.ts +115 -0
  37. package/tests/test-errors.test.ts +448 -5
  38. package/tests/test-extensions.test.ts +310 -0
  39. package/tests/test-trace-context.test.ts +251 -0
  40. package/tests/utils/test-pattern.test.ts +109 -0
@@ -1,6 +1,9 @@
1
- import { describe, it, expect } from 'vitest';
1
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2
+ import { mkdtempSync, writeFileSync, mkdirSync, rmSync } from 'node:fs';
3
+ import { join } from 'node:path';
4
+ import { tmpdir } from 'node:os';
2
5
  import { RefResolver } from '../../src/schema/ref-resolver.js';
3
- import { SchemaCircularRefError, SchemaNotFoundError } from '../../src/errors.js';
6
+ import { SchemaCircularRefError, SchemaNotFoundError, SchemaParseError } from '../../src/errors.js';
4
7
 
5
8
  describe('RefResolver', () => {
6
9
  it('resolves local $ref', () => {
@@ -103,3 +106,425 @@ describe('RefResolver', () => {
103
106
  expect(resolved).toEqual(schema);
104
107
  });
105
108
  });
109
+
110
+ describe('RefResolver - apcore:// URI resolution', () => {
111
+ let tmpDir: string;
112
+
113
+ beforeEach(() => {
114
+ tmpDir = mkdtempSync(join(tmpdir(), 'apcore-ref-test-'));
115
+ });
116
+
117
+ afterEach(() => {
118
+ rmSync(tmpDir, { recursive: true, force: true });
119
+ });
120
+
121
+ it('resolves apcore:// URI to schema file and JSON pointer', () => {
122
+ const schemasDir = join(tmpDir, 'schemas');
123
+ mkdirSync(schemasDir, { recursive: true });
124
+ writeFileSync(
125
+ join(schemasDir, 'foo.schema.yaml'),
126
+ 'type: object\nproperties:\n name:\n type: string\n',
127
+ );
128
+
129
+ const resolver = new RefResolver(schemasDir);
130
+ const schema = {
131
+ type: 'object',
132
+ properties: {
133
+ field: { $ref: 'apcore://foo/properties/name' },
134
+ },
135
+ };
136
+ const resolved = resolver.resolve(schema);
137
+ const props = resolved['properties'] as Record<string, unknown>;
138
+ expect(props['field']).toEqual({ type: 'string' });
139
+ });
140
+
141
+ it('resolves apcore:// URI without pointer to full document', () => {
142
+ const schemasDir = join(tmpDir, 'schemas');
143
+ mkdirSync(schemasDir, { recursive: true });
144
+ writeFileSync(
145
+ join(schemasDir, 'bar.schema.yaml'),
146
+ 'type: string\ndescription: a bar\n',
147
+ );
148
+
149
+ const resolver = new RefResolver(schemasDir);
150
+ const schema = {
151
+ type: 'object',
152
+ properties: {
153
+ x: { $ref: 'apcore://bar' },
154
+ },
155
+ };
156
+ const resolved = resolver.resolve(schema);
157
+ const props = resolved['properties'] as Record<string, unknown>;
158
+ expect(props['x']).toEqual({ type: 'string', description: 'a bar' });
159
+ });
160
+ });
161
+
162
+ describe('RefResolver - array element ref resolution', () => {
163
+ it('resolves $ref inside array elements', () => {
164
+ const resolver = new RefResolver('/tmp/schemas');
165
+ const schema = {
166
+ definitions: {
167
+ Tag: { type: 'string' },
168
+ },
169
+ type: 'object',
170
+ properties: {
171
+ tags: {
172
+ type: 'array',
173
+ items: [
174
+ { $ref: '#/definitions/Tag' },
175
+ { $ref: '#/definitions/Tag' },
176
+ ],
177
+ },
178
+ },
179
+ };
180
+ const resolved = resolver.resolve(schema);
181
+ const props = resolved['properties'] as Record<string, Record<string, unknown>>;
182
+ const items = props['tags']['items'] as Record<string, unknown>[];
183
+ expect(items[0]).toEqual({ type: 'string' });
184
+ expect(items[1]).toEqual({ type: 'string' });
185
+ });
186
+
187
+ it('resolves $ref inside oneOf array', () => {
188
+ const resolver = new RefResolver('/tmp/schemas');
189
+ const schema = {
190
+ definitions: {
191
+ Str: { type: 'string' },
192
+ Num: { type: 'number' },
193
+ },
194
+ type: 'object',
195
+ properties: {
196
+ value: {
197
+ oneOf: [
198
+ { $ref: '#/definitions/Str' },
199
+ { $ref: '#/definitions/Num' },
200
+ ],
201
+ },
202
+ },
203
+ };
204
+ const resolved = resolver.resolve(schema);
205
+ const props = resolved['properties'] as Record<string, Record<string, unknown>>;
206
+ const oneOf = props['value']['oneOf'] as Record<string, unknown>[];
207
+ expect(oneOf[0]).toEqual({ type: 'string' });
208
+ expect(oneOf[1]).toEqual({ type: 'number' });
209
+ });
210
+ });
211
+
212
+ describe('RefResolver - file loading edge cases', () => {
213
+ let tmpDir: string;
214
+
215
+ beforeEach(() => {
216
+ tmpDir = mkdtempSync(join(tmpdir(), 'apcore-ref-load-'));
217
+ });
218
+
219
+ afterEach(() => {
220
+ rmSync(tmpDir, { recursive: true, force: true });
221
+ });
222
+
223
+ it('handles empty YAML files gracefully', () => {
224
+ const schemasDir = join(tmpDir, 'schemas');
225
+ mkdirSync(schemasDir, { recursive: true });
226
+ writeFileSync(join(schemasDir, 'empty.schema.yaml'), '');
227
+
228
+ const resolver = new RefResolver(schemasDir);
229
+ const schema = {
230
+ type: 'object',
231
+ properties: {
232
+ x: { $ref: 'empty.schema.yaml' },
233
+ },
234
+ };
235
+ const resolved = resolver.resolve(schema);
236
+ const props = resolved['properties'] as Record<string, unknown>;
237
+ expect(props['x']).toEqual({});
238
+ });
239
+
240
+ it('handles YAML file with only whitespace', () => {
241
+ const schemasDir = join(tmpDir, 'schemas');
242
+ mkdirSync(schemasDir, { recursive: true });
243
+ writeFileSync(join(schemasDir, 'whitespace.schema.yaml'), ' \n \n ');
244
+
245
+ const resolver = new RefResolver(schemasDir);
246
+ const schema = {
247
+ type: 'object',
248
+ properties: {
249
+ x: { $ref: 'whitespace.schema.yaml' },
250
+ },
251
+ };
252
+ const resolved = resolver.resolve(schema);
253
+ const props = resolved['properties'] as Record<string, unknown>;
254
+ expect(props['x']).toEqual({});
255
+ });
256
+
257
+ it('throws SchemaParseError for invalid YAML', () => {
258
+ const schemasDir = join(tmpDir, 'schemas');
259
+ mkdirSync(schemasDir, { recursive: true });
260
+ writeFileSync(join(schemasDir, 'bad.schema.yaml'), ':\n - :\n :\n invalid: [}');
261
+
262
+ const resolver = new RefResolver(schemasDir);
263
+ const schema = {
264
+ type: 'object',
265
+ properties: {
266
+ x: { $ref: 'bad.schema.yaml' },
267
+ },
268
+ };
269
+ expect(() => resolver.resolve(schema)).toThrow(SchemaParseError);
270
+ });
271
+
272
+ it('throws SchemaParseError when YAML file is a scalar not a mapping', () => {
273
+ const schemasDir = join(tmpDir, 'schemas');
274
+ mkdirSync(schemasDir, { recursive: true });
275
+ writeFileSync(join(schemasDir, 'scalar.schema.yaml'), '"just a string"');
276
+
277
+ const resolver = new RefResolver(schemasDir);
278
+ const schema = {
279
+ type: 'object',
280
+ properties: {
281
+ x: { $ref: 'scalar.schema.yaml' },
282
+ },
283
+ };
284
+ expect(() => resolver.resolve(schema)).toThrow(SchemaParseError);
285
+ });
286
+
287
+ it('throws SchemaParseError when YAML file is a list not a mapping', () => {
288
+ const schemasDir = join(tmpDir, 'schemas');
289
+ mkdirSync(schemasDir, { recursive: true });
290
+ writeFileSync(join(schemasDir, 'list.schema.yaml'), '- one\n- two\n');
291
+
292
+ const resolver = new RefResolver(schemasDir);
293
+ const schema = {
294
+ type: 'object',
295
+ properties: {
296
+ x: { $ref: 'list.schema.yaml' },
297
+ },
298
+ };
299
+ expect(() => resolver.resolve(schema)).toThrow(SchemaParseError);
300
+ });
301
+
302
+ it('throws SchemaNotFoundError for missing file', () => {
303
+ const schemasDir = join(tmpDir, 'schemas');
304
+ mkdirSync(schemasDir, { recursive: true });
305
+
306
+ const resolver = new RefResolver(schemasDir);
307
+ const schema = {
308
+ type: 'object',
309
+ properties: {
310
+ x: { $ref: 'nonexistent.yaml' },
311
+ },
312
+ };
313
+ expect(() => resolver.resolve(schema)).toThrow(SchemaNotFoundError);
314
+ });
315
+ });
316
+
317
+ describe('RefResolver - path traversal guard', () => {
318
+ let tmpDir: string;
319
+
320
+ beforeEach(() => {
321
+ tmpDir = mkdtempSync(join(tmpdir(), 'apcore-ref-guard-'));
322
+ });
323
+
324
+ afterEach(() => {
325
+ rmSync(tmpDir, { recursive: true, force: true });
326
+ });
327
+
328
+ it('throws SchemaNotFoundError when ref resolves outside schemas dir', () => {
329
+ const schemasDir = join(tmpDir, 'schemas');
330
+ mkdirSync(schemasDir, { recursive: true });
331
+
332
+ const resolver = new RefResolver(schemasDir);
333
+ const schema = {
334
+ type: 'object',
335
+ properties: {
336
+ x: { $ref: '../../etc/passwd' },
337
+ },
338
+ };
339
+ expect(() => resolver.resolve(schema)).toThrow(SchemaNotFoundError);
340
+ expect(() => resolver.resolve(schema)).toThrow(/resolves outside schemas directory/);
341
+ });
342
+
343
+ it('throws SchemaNotFoundError for file#pointer ref outside schemas dir', () => {
344
+ const schemasDir = join(tmpDir, 'schemas');
345
+ mkdirSync(schemasDir, { recursive: true });
346
+
347
+ const resolver = new RefResolver(schemasDir);
348
+ const schema = {
349
+ type: 'object',
350
+ properties: {
351
+ x: { $ref: '../outside.yaml#/foo' },
352
+ },
353
+ };
354
+ expect(() => resolver.resolve(schema)).toThrow(SchemaNotFoundError);
355
+ expect(() => resolver.resolve(schema)).toThrow(/resolves outside schemas directory/);
356
+ });
357
+ });
358
+
359
+ describe('RefResolver - clearCache', () => {
360
+ let tmpDir: string;
361
+
362
+ beforeEach(() => {
363
+ tmpDir = mkdtempSync(join(tmpdir(), 'apcore-ref-cache-'));
364
+ });
365
+
366
+ afterEach(() => {
367
+ rmSync(tmpDir, { recursive: true, force: true });
368
+ });
369
+
370
+ it('clears cached file content so subsequent resolve re-reads files', () => {
371
+ const schemasDir = join(tmpDir, 'schemas');
372
+ mkdirSync(schemasDir, { recursive: true });
373
+ writeFileSync(join(schemasDir, 'mutable.schema.yaml'), 'type: string\n');
374
+
375
+ const resolver = new RefResolver(schemasDir);
376
+
377
+ const schema1 = { type: 'object', properties: { x: { $ref: 'mutable.schema.yaml' } } };
378
+ const resolved1 = resolver.resolve(schema1);
379
+ const props1 = resolved1['properties'] as Record<string, Record<string, unknown>>;
380
+ expect(props1['x']['type']).toBe('string');
381
+
382
+ writeFileSync(join(schemasDir, 'mutable.schema.yaml'), 'type: integer\n');
383
+
384
+ // Without clearing cache, old value is served
385
+ const schema2 = { type: 'object', properties: { x: { $ref: 'mutable.schema.yaml' } } };
386
+ const resolvedCached = resolver.resolve(schema2);
387
+ const propsCached = resolvedCached['properties'] as Record<string, Record<string, unknown>>;
388
+ expect(propsCached['x']['type']).toBe('string');
389
+
390
+ // After clearing, new value is read
391
+ resolver.clearCache();
392
+ const schema3 = { type: 'object', properties: { x: { $ref: 'mutable.schema.yaml' } } };
393
+ const resolvedFresh = resolver.resolve(schema3);
394
+ const propsFresh = resolvedFresh['properties'] as Record<string, Record<string, unknown>>;
395
+ expect(propsFresh['x']['type']).toBe('integer');
396
+ });
397
+ });
398
+
399
+ describe('RefResolver - nested $ref resolution', () => {
400
+ it('resolves a $ref that points to another $ref (chain)', () => {
401
+ const resolver = new RefResolver('/tmp/schemas');
402
+ const schema = {
403
+ definitions: {
404
+ Alias: { $ref: '#/definitions/Actual' },
405
+ Actual: { type: 'number' },
406
+ },
407
+ type: 'object',
408
+ properties: {
409
+ val: { $ref: '#/definitions/Alias' },
410
+ },
411
+ };
412
+ const resolved = resolver.resolve(schema);
413
+ const props = resolved['properties'] as Record<string, Record<string, unknown>>;
414
+ expect(props['val']).toEqual({ type: 'number' });
415
+ });
416
+
417
+ it('resolves sibling keys alongside $ref', () => {
418
+ const resolver = new RefResolver('/tmp/schemas');
419
+ const schema = {
420
+ definitions: {
421
+ Base: { type: 'string' },
422
+ },
423
+ type: 'object',
424
+ properties: {
425
+ val: { $ref: '#/definitions/Base', description: 'overridden' },
426
+ },
427
+ };
428
+ const resolved = resolver.resolve(schema);
429
+ const props = resolved['properties'] as Record<string, Record<string, unknown>>;
430
+ expect(props['val']['type']).toBe('string');
431
+ expect(props['val']['description']).toBe('overridden');
432
+ });
433
+ });
434
+
435
+ describe('RefResolver - cross-file $ref', () => {
436
+ let tmpDir: string;
437
+
438
+ beforeEach(() => {
439
+ tmpDir = mkdtempSync(join(tmpdir(), 'apcore-ref-cross-'));
440
+ });
441
+
442
+ afterEach(() => {
443
+ rmSync(tmpDir, { recursive: true, force: true });
444
+ });
445
+
446
+ it('resolves file.yaml#/pointer format', () => {
447
+ const schemasDir = join(tmpDir, 'schemas');
448
+ mkdirSync(schemasDir, { recursive: true });
449
+ writeFileSync(
450
+ join(schemasDir, 'shared.yaml'),
451
+ 'definitions:\n Email:\n type: string\n format: email\n',
452
+ );
453
+
454
+ const resolver = new RefResolver(schemasDir);
455
+ const schema = {
456
+ type: 'object',
457
+ properties: {
458
+ email: { $ref: 'shared.yaml#/definitions/Email' },
459
+ },
460
+ };
461
+ const resolved = resolver.resolve(schema);
462
+ const props = resolved['properties'] as Record<string, Record<string, unknown>>;
463
+ expect(props['email']).toEqual({ type: 'string', format: 'email' });
464
+ });
465
+
466
+ it('resolves file.yaml without pointer to full document', () => {
467
+ const schemasDir = join(tmpDir, 'schemas');
468
+ mkdirSync(schemasDir, { recursive: true });
469
+ writeFileSync(
470
+ join(schemasDir, 'simple.yaml'),
471
+ 'type: boolean\n',
472
+ );
473
+
474
+ const resolver = new RefResolver(schemasDir);
475
+ const schema = {
476
+ type: 'object',
477
+ properties: {
478
+ flag: { $ref: 'simple.yaml' },
479
+ },
480
+ };
481
+ const resolved = resolver.resolve(schema);
482
+ const props = resolved['properties'] as Record<string, Record<string, unknown>>;
483
+ expect(props['flag']).toEqual({ type: 'boolean' });
484
+ });
485
+
486
+ it('resolves cross-file ref with currentFile context', () => {
487
+ const schemasDir = join(tmpDir, 'schemas');
488
+ const subDir = join(schemasDir, 'sub');
489
+ mkdirSync(subDir, { recursive: true });
490
+ writeFileSync(
491
+ join(schemasDir, 'types.yaml'),
492
+ 'definitions:\n Id:\n type: integer\n',
493
+ );
494
+ writeFileSync(
495
+ join(subDir, 'model.yaml'),
496
+ 'type: object\nproperties:\n id:\n $ref: "../types.yaml#/definitions/Id"\n',
497
+ );
498
+
499
+ const resolver = new RefResolver(schemasDir);
500
+ const schema = {
501
+ type: 'object',
502
+ properties: {
503
+ model: { $ref: 'sub/model.yaml' },
504
+ },
505
+ };
506
+ const resolved = resolver.resolve(schema);
507
+ const props = resolved['properties'] as Record<string, Record<string, unknown>>;
508
+ const model = props['model'] as Record<string, unknown>;
509
+ const modelProps = model['properties'] as Record<string, Record<string, unknown>>;
510
+ expect(modelProps['id']).toEqual({ type: 'integer' });
511
+ });
512
+
513
+ it('resolves YAML with null-parsed content as empty object', () => {
514
+ const schemasDir = join(tmpDir, 'schemas');
515
+ mkdirSync(schemasDir, { recursive: true });
516
+ // YAML "null" keyword parses to null
517
+ writeFileSync(join(schemasDir, 'nulldoc.yaml'), 'null\n');
518
+
519
+ const resolver = new RefResolver(schemasDir);
520
+ const schema = {
521
+ type: 'object',
522
+ properties: {
523
+ x: { $ref: 'nulldoc.yaml' },
524
+ },
525
+ };
526
+ const resolved = resolver.resolve(schema);
527
+ const props = resolved['properties'] as Record<string, unknown>;
528
+ expect(props['x']).toEqual({});
529
+ });
530
+ });
@@ -103,6 +103,202 @@ describe('applyLlmDescriptions', () => {
103
103
  });
104
104
  });
105
105
 
106
+ describe('toStrictSchema - definitions and combinators', () => {
107
+ it('recurses into definitions block', () => {
108
+ const schema = {
109
+ type: 'object',
110
+ properties: {
111
+ name: { type: 'string' },
112
+ },
113
+ required: ['name'],
114
+ definitions: {
115
+ Address: {
116
+ type: 'object',
117
+ properties: {
118
+ city: { type: 'string' },
119
+ zip: { type: 'string' },
120
+ },
121
+ required: ['city'],
122
+ },
123
+ },
124
+ };
125
+ const strict = toStrictSchema(schema);
126
+ const defs = strict['definitions'] as Record<string, Record<string, unknown>>;
127
+ expect(defs['Address']['additionalProperties']).toBe(false);
128
+ const addrProps = defs['Address']['properties'] as Record<string, Record<string, unknown>>;
129
+ expect(addrProps['zip']['type']).toEqual(['string', 'null']);
130
+ });
131
+
132
+ it('recurses into $defs block', () => {
133
+ const schema = {
134
+ type: 'object',
135
+ properties: { x: { type: 'string' } },
136
+ $defs: {
137
+ Inner: {
138
+ type: 'object',
139
+ properties: {
140
+ a: { type: 'integer' },
141
+ b: { type: 'string' },
142
+ },
143
+ required: ['a'],
144
+ },
145
+ },
146
+ };
147
+ const strict = toStrictSchema(schema);
148
+ const defs = strict['$defs'] as Record<string, Record<string, unknown>>;
149
+ expect(defs['Inner']['additionalProperties']).toBe(false);
150
+ const innerProps = defs['Inner']['properties'] as Record<string, Record<string, unknown>>;
151
+ expect(innerProps['b']['type']).toEqual(['string', 'null']);
152
+ });
153
+
154
+ it('recurses into oneOf/anyOf/allOf variants', () => {
155
+ const schema = {
156
+ type: 'object',
157
+ properties: {
158
+ value: {
159
+ oneOf: [
160
+ {
161
+ type: 'object',
162
+ properties: { a: { type: 'string' } },
163
+ },
164
+ ],
165
+ },
166
+ },
167
+ };
168
+ const strict = toStrictSchema(schema);
169
+ const props = strict['properties'] as Record<string, Record<string, unknown>>;
170
+ const oneOf = props['value']['oneOf'] as Record<string, unknown>[];
171
+ expect(oneOf[0]['additionalProperties']).toBe(false);
172
+ });
173
+
174
+ it('appends null to existing oneOf for optional property', () => {
175
+ const schema = {
176
+ type: 'object',
177
+ properties: {
178
+ value: {
179
+ oneOf: [
180
+ { type: 'string' },
181
+ { type: 'number' },
182
+ ],
183
+ },
184
+ },
185
+ required: [],
186
+ };
187
+ const strict = toStrictSchema(schema);
188
+ const props = strict['properties'] as Record<string, Record<string, unknown>>;
189
+ const oneOf = props['value']['oneOf'] as Record<string, unknown>[];
190
+ expect(oneOf).toHaveLength(3);
191
+ expect(oneOf[2]).toEqual({ type: 'null' });
192
+ });
193
+
194
+ it('appends null to existing anyOf for optional property', () => {
195
+ const schema = {
196
+ type: 'object',
197
+ properties: {
198
+ value: {
199
+ anyOf: [
200
+ { type: 'string' },
201
+ { type: 'integer' },
202
+ ],
203
+ },
204
+ },
205
+ required: [],
206
+ };
207
+ const strict = toStrictSchema(schema);
208
+ const props = strict['properties'] as Record<string, Record<string, unknown>>;
209
+ const anyOf = props['value']['anyOf'] as Record<string, unknown>[];
210
+ expect(anyOf).toHaveLength(3);
211
+ expect(anyOf[2]).toEqual({ type: 'null' });
212
+ });
213
+
214
+ it('does not double-add null to oneOf that already has null', () => {
215
+ const schema = {
216
+ type: 'object',
217
+ properties: {
218
+ value: {
219
+ oneOf: [
220
+ { type: 'string' },
221
+ { type: 'null' },
222
+ ],
223
+ },
224
+ },
225
+ required: [],
226
+ };
227
+ const strict = toStrictSchema(schema);
228
+ const props = strict['properties'] as Record<string, Record<string, unknown>>;
229
+ const oneOf = props['value']['oneOf'] as Record<string, unknown>[];
230
+ expect(oneOf).toHaveLength(2);
231
+ });
232
+
233
+ it('does not add null to already-nullable type array', () => {
234
+ const schema = {
235
+ type: 'object',
236
+ properties: {
237
+ value: { type: ['string', 'null'] },
238
+ },
239
+ required: [],
240
+ };
241
+ const strict = toStrictSchema(schema);
242
+ const props = strict['properties'] as Record<string, Record<string, unknown>>;
243
+ expect(props['value']['type']).toEqual(['string', 'null']);
244
+ });
245
+ });
246
+
247
+ describe('applyLlmDescriptions - definitions and combinators', () => {
248
+ it('recurses into definitions block', () => {
249
+ const schema: Record<string, unknown> = {
250
+ definitions: {
251
+ Foo: { description: 'old', 'x-llm-description': 'new' },
252
+ },
253
+ };
254
+ applyLlmDescriptions(schema);
255
+ const defs = schema['definitions'] as Record<string, Record<string, unknown>>;
256
+ expect(defs['Foo']['description']).toBe('new');
257
+ });
258
+
259
+ it('recurses into $defs block', () => {
260
+ const schema: Record<string, unknown> = {
261
+ $defs: {
262
+ Bar: { description: 'old', 'x-llm-description': 'updated' },
263
+ },
264
+ };
265
+ applyLlmDescriptions(schema);
266
+ const defs = schema['$defs'] as Record<string, Record<string, unknown>>;
267
+ expect(defs['Bar']['description']).toBe('updated');
268
+ });
269
+
270
+ it('recurses into items', () => {
271
+ const schema: Record<string, unknown> = {
272
+ type: 'array',
273
+ items: { 'x-llm-description': 'item desc' },
274
+ };
275
+ applyLlmDescriptions(schema);
276
+ const items = schema['items'] as Record<string, unknown>;
277
+ expect(items['description']).toBe('item desc');
278
+ });
279
+
280
+ it('recurses into oneOf/anyOf/allOf', () => {
281
+ const schema: Record<string, unknown> = {
282
+ oneOf: [
283
+ { 'x-llm-description': 'variant A' },
284
+ { 'x-llm-description': 'variant B' },
285
+ ],
286
+ };
287
+ applyLlmDescriptions(schema);
288
+ const oneOf = schema['oneOf'] as Record<string, unknown>[];
289
+ expect(oneOf[0]['description']).toBe('variant A');
290
+ expect(oneOf[1]['description']).toBe('variant B');
291
+ });
292
+
293
+ it('sets description even when no prior description exists', () => {
294
+ const schema: Record<string, unknown> = {
295
+ 'x-llm-description': 'brand new',
296
+ };
297
+ applyLlmDescriptions(schema);
298
+ expect(schema['description']).toBe('brand new');
299
+ });
300
+ });
301
+
106
302
  describe('stripExtensions', () => {
107
303
  it('removes x- prefixed keys', () => {
108
304
  const schema: Record<string, unknown> = {
@@ -136,4 +332,17 @@ describe('stripExtensions', () => {
136
332
  const props = schema['properties'] as Record<string, Record<string, unknown>>;
137
333
  expect(props['name']['x-sensitive']).toBeUndefined();
138
334
  });
335
+
336
+ it('recurses into arrays', () => {
337
+ const schema: Record<string, unknown> = {
338
+ oneOf: [
339
+ { type: 'string', 'x-remove': true },
340
+ { type: 'number', default: 0 },
341
+ ],
342
+ };
343
+ stripExtensions(schema);
344
+ const oneOf = schema['oneOf'] as Record<string, unknown>[];
345
+ expect(oneOf[0]['x-remove']).toBeUndefined();
346
+ expect(oneOf[1]['default']).toBeUndefined();
347
+ });
139
348
  });