apcore-js 0.3.0 → 0.4.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.
@@ -2,7 +2,7 @@ import { describe, it, expect } from 'vitest';
2
2
  import { Type } from '@sinclair/typebox';
3
3
  import { Registry } from '../../src/registry/registry.js';
4
4
  import { FunctionModule } from '../../src/decorator.js';
5
- import { ModuleNotFoundError } from '../../src/errors.js';
5
+ import { InvalidInputError, ModuleNotFoundError } from '../../src/errors.js';
6
6
  import {
7
7
  getSchema,
8
8
  exportSchema,
@@ -218,8 +218,138 @@ describe('exportAllSchemas', () => {
218
218
  expect((parsed['strict.a']['input_schema'] as Record<string, unknown>)['additionalProperties']).toBe(false);
219
219
  });
220
220
 
221
+ it('applies compact mode to all schemas', () => {
222
+ const mod = createModule('compact.all');
223
+ const registry = makeRegistry(['compact.all', mod]);
224
+
225
+ const result = exportAllSchemas(registry, 'json', false, true);
226
+ const parsed = JSON.parse(result);
227
+ expect(parsed['compact.all']['description']).toBe('A test module.');
228
+ expect(parsed['compact.all']['examples']).toBeUndefined();
229
+ });
230
+
221
231
  it('returns empty JSON object for empty registry', () => {
222
232
  const registry = new Registry();
223
233
  expect(JSON.parse(exportAllSchemas(registry))).toEqual({});
224
234
  });
225
235
  });
236
+
237
+ describe('exportSchema with profile', () => {
238
+ it('exports with mcp profile and returns valid JSON with MCP shape', () => {
239
+ const mod = createModule('mcp.mod');
240
+ const registry = makeRegistry(['mcp.mod', mod]);
241
+
242
+ const result = exportSchema(registry, 'mcp.mod', 'json', false, false, 'mcp');
243
+ const parsed = JSON.parse(result);
244
+ expect(parsed['name']).toBeDefined();
245
+ expect(parsed['description']).toBeDefined();
246
+ expect(parsed['inputSchema']).toBeDefined();
247
+ expect(parsed['annotations']).toBeDefined();
248
+ expect(parsed['annotations']['readOnlyHint']).toBe(true);
249
+ expect(parsed['annotations']['destructiveHint']).toBe(false);
250
+ });
251
+
252
+ it('exports with openai profile and returns function tool shape', () => {
253
+ const mod = createModule('openai.mod');
254
+ const registry = makeRegistry(['openai.mod', mod]);
255
+
256
+ const result = exportSchema(registry, 'openai.mod', 'json', false, false, 'openai');
257
+ const parsed = JSON.parse(result);
258
+ expect(parsed['type']).toBe('function');
259
+ expect(parsed['function']).toBeDefined();
260
+ expect(parsed['function']['name']).toBe('openai_mod');
261
+ expect(parsed['function']['description']).toBeDefined();
262
+ expect(parsed['function']['parameters']).toBeDefined();
263
+ expect(parsed['function']['strict']).toBe(true);
264
+ });
265
+
266
+ it('exports with anthropic profile and returns tool shape with input_examples', () => {
267
+ const mod = createModule('anthro.mod');
268
+ const registry = makeRegistry(['anthro.mod', mod]);
269
+
270
+ const result = exportSchema(registry, 'anthro.mod', 'json', false, false, 'anthropic');
271
+ const parsed = JSON.parse(result);
272
+ expect(parsed['name']).toBe('anthro_mod');
273
+ expect(parsed['description']).toBeDefined();
274
+ expect(parsed['input_schema']).toBeDefined();
275
+ expect(parsed['input_examples']).toBeDefined();
276
+ expect((parsed['input_examples'] as unknown[]).length).toBe(1);
277
+ });
278
+
279
+ it('exports with generic profile and returns module_id and schema fields', () => {
280
+ const mod = createModule('generic.mod');
281
+ const registry = makeRegistry(['generic.mod', mod]);
282
+
283
+ const result = exportSchema(registry, 'generic.mod', 'json', false, false, 'generic');
284
+ const parsed = JSON.parse(result);
285
+ expect(parsed['module_id']).toBe('generic.mod');
286
+ expect(parsed['description']).toBeDefined();
287
+ expect(parsed['input_schema']).toBeDefined();
288
+ expect(parsed['output_schema']).toBeDefined();
289
+ expect(parsed['definitions']).toBeDefined();
290
+ });
291
+
292
+ it('exports with profile using yaml format', () => {
293
+ const mod = createModule('mcp.yaml');
294
+ const registry = makeRegistry(['mcp.yaml', mod]);
295
+
296
+ const result = exportSchema(registry, 'mcp.yaml', 'yaml', false, false, 'mcp');
297
+ expect(result).toContain('name:');
298
+ expect(result).toContain('inputSchema:');
299
+ });
300
+
301
+ it('throws InvalidInputError for an unrecognized profile name', () => {
302
+ const mod = createModule('bad.profile');
303
+ const registry = makeRegistry(['bad.profile', mod]);
304
+
305
+ expect(() =>
306
+ exportSchema(registry, 'bad.profile', 'json', false, false, 'not_a_real_profile'),
307
+ ).toThrow(InvalidInputError);
308
+ });
309
+
310
+ it('includes the invalid profile name in the error message', () => {
311
+ const mod = createModule('err.profile');
312
+ const registry = makeRegistry(['err.profile', mod]);
313
+
314
+ expect(() =>
315
+ exportSchema(registry, 'err.profile', 'json', false, false, 'bogus'),
316
+ ).toThrowError(/bogus/);
317
+ });
318
+ });
319
+
320
+ describe('truncateDescription edge cases', () => {
321
+ it('returns the full string when there is no dot-space or newline', () => {
322
+ const mod = createModule('no.boundary', {
323
+ description: 'A simple description with no sentence boundary',
324
+ });
325
+ const registry = makeRegistry(['no.boundary', mod]);
326
+
327
+ const result = exportSchema(registry, 'no.boundary', 'json', false, true);
328
+ const parsed = JSON.parse(result);
329
+ expect(parsed['description']).toBe('A simple description with no sentence boundary');
330
+ });
331
+
332
+ it('truncates at the earlier boundary when both dot-space and newline are present', () => {
333
+ // newline comes before dot-space: "Line one\nSecond sentence. More text."
334
+ const mod = createModule('newline.first', {
335
+ description: 'Line one\nSecond sentence. More text.',
336
+ });
337
+ const registry = makeRegistry(['newline.first', mod]);
338
+
339
+ const result = exportSchema(registry, 'newline.first', 'json', false, true);
340
+ const parsed = JSON.parse(result);
341
+ expect(parsed['description']).toBe('Line one');
342
+ });
343
+
344
+ it('truncates at dot-space when it comes before a newline', () => {
345
+ // dot-space before newline: "First sentence. Second line\nThird."
346
+ const mod = createModule('dotspace.first', {
347
+ description: 'First sentence. Second line\nThird.',
348
+ });
349
+ const registry = makeRegistry(['dotspace.first', mod]);
350
+
351
+ const result = exportSchema(registry, 'dotspace.first', 'json', false, true);
352
+ const parsed = JSON.parse(result);
353
+ expect(parsed['description']).toBe('First sentence.');
354
+ });
355
+ });
@@ -1,7 +1,371 @@
1
- import { describe, it, expect } from 'vitest';
1
+ import { mkdirSync, writeFileSync, rmSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { tmpdir } from 'node:os';
4
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2
5
  import { Type } from '@sinclair/typebox';
3
6
  import { Value } from '@sinclair/typebox/value';
4
- import { jsonSchemaToTypeBox } from '../../src/schema/loader.js';
7
+ import { Config } from '../../src/config.js';
8
+ import { SchemaNotFoundError, SchemaParseError } from '../../src/errors.js';
9
+ import { SchemaLoader, jsonSchemaToTypeBox } from '../../src/schema/loader.js';
10
+
11
+ describe('SchemaLoader', () => {
12
+ let tmpDir: string;
13
+ let schemasDir: string;
14
+
15
+ beforeEach(() => {
16
+ tmpDir = join(tmpdir(), `apcore-test-loader-${Date.now()}-${Math.random().toString(36).slice(2)}`);
17
+ schemasDir = join(tmpDir, 'schemas');
18
+ mkdirSync(schemasDir, { recursive: true });
19
+ });
20
+
21
+ afterEach(() => {
22
+ rmSync(tmpDir, { recursive: true, force: true });
23
+ });
24
+
25
+ function writeSchema(relPath: string, content: string): void {
26
+ const fullPath = join(schemasDir, relPath);
27
+ const dir = fullPath.replace(/\/[^/]+$/, '');
28
+ mkdirSync(dir, { recursive: true });
29
+ writeFileSync(fullPath, content, 'utf-8');
30
+ }
31
+
32
+ function makeConfig(overrides?: Record<string, unknown>): Config {
33
+ return new Config({
34
+ schema: { root: schemasDir, strategy: 'yaml_first', ...overrides },
35
+ });
36
+ }
37
+
38
+ describe('load', () => {
39
+ it('loads a valid YAML schema file', () => {
40
+ writeSchema('greeter.schema.yaml', `
41
+ description: A greeter module
42
+ input_schema:
43
+ type: object
44
+ properties:
45
+ name:
46
+ type: string
47
+ required:
48
+ - name
49
+ output_schema:
50
+ type: object
51
+ properties:
52
+ message:
53
+ type: string
54
+ `);
55
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
56
+ const sd = loader.load('greeter');
57
+
58
+ expect(sd.moduleId).toBe('greeter');
59
+ expect(sd.description).toBe('A greeter module');
60
+ expect(sd.inputSchema).toEqual({
61
+ type: 'object',
62
+ properties: { name: { type: 'string' } },
63
+ required: ['name'],
64
+ });
65
+ expect(sd.version).toBe('1.0.0');
66
+ });
67
+
68
+ it('loads a schema with dot-separated module ID (nested path)', () => {
69
+ writeSchema('math/add.schema.yaml', `
70
+ description: Add numbers
71
+ input_schema:
72
+ type: object
73
+ properties:
74
+ a:
75
+ type: number
76
+ output_schema:
77
+ type: object
78
+ properties:
79
+ result:
80
+ type: number
81
+ `);
82
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
83
+ const sd = loader.load('math.add');
84
+
85
+ expect(sd.moduleId).toBe('math.add');
86
+ expect(sd.description).toBe('Add numbers');
87
+ });
88
+
89
+ it('throws SchemaNotFoundError for non-existent schema', () => {
90
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
91
+ expect(() => loader.load('nonexistent')).toThrow(SchemaNotFoundError);
92
+ });
93
+
94
+ it('throws SchemaParseError for invalid YAML', () => {
95
+ writeSchema('bad.schema.yaml', '{ invalid yaml:: [');
96
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
97
+ expect(() => loader.load('bad')).toThrow(SchemaParseError);
98
+ });
99
+
100
+ it('throws SchemaParseError for empty file', () => {
101
+ writeSchema('empty.schema.yaml', '');
102
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
103
+ expect(() => loader.load('empty')).toThrow(SchemaParseError);
104
+ });
105
+
106
+ it('throws SchemaParseError for array YAML', () => {
107
+ writeSchema('arr.schema.yaml', '- item1\n- item2\n');
108
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
109
+ expect(() => loader.load('arr')).toThrow(SchemaParseError);
110
+ });
111
+
112
+ it('throws SchemaParseError when required field is missing', () => {
113
+ writeSchema('noinput.schema.yaml', `
114
+ description: Missing input_schema
115
+ output_schema:
116
+ type: object
117
+ `);
118
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
119
+ expect(() => loader.load('noinput')).toThrow(SchemaParseError);
120
+ expect(() => loader.load('noinput')).toThrow(/Missing required field/);
121
+ });
122
+
123
+ it('merges definitions and $defs', () => {
124
+ writeSchema('withdefs.schema.yaml', `
125
+ description: Has definitions
126
+ input_schema:
127
+ type: object
128
+ output_schema:
129
+ type: object
130
+ definitions:
131
+ Foo:
132
+ type: string
133
+ $defs:
134
+ Bar:
135
+ type: integer
136
+ `);
137
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
138
+ const sd = loader.load('withdefs');
139
+ expect(sd.definitions).toEqual({ Foo: { type: 'string' }, Bar: { type: 'integer' } });
140
+ });
141
+
142
+ it('returns cached result on second call', () => {
143
+ writeSchema('cached.schema.yaml', `
144
+ description: Cached
145
+ input_schema:
146
+ type: object
147
+ output_schema:
148
+ type: object
149
+ `);
150
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
151
+ const first = loader.load('cached');
152
+ const second = loader.load('cached');
153
+ expect(first).toBe(second);
154
+ });
155
+
156
+ it('preserves optional fields (version, documentation, errorSchema)', () => {
157
+ writeSchema('full.schema.yaml', `
158
+ description: Full schema
159
+ version: "2.0.0"
160
+ documentation: "Some docs"
161
+ input_schema:
162
+ type: object
163
+ output_schema:
164
+ type: object
165
+ error_schema:
166
+ type: object
167
+ properties:
168
+ code:
169
+ type: string
170
+ `);
171
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
172
+ const sd = loader.load('full');
173
+ expect(sd.version).toBe('2.0.0');
174
+ expect(sd.documentation).toBe('Some docs');
175
+ expect(sd.errorSchema).toEqual({ type: 'object', properties: { code: { type: 'string' } } });
176
+ });
177
+ });
178
+
179
+ describe('resolve', () => {
180
+ it('resolves a schema definition into TypeBox schemas', () => {
181
+ writeSchema('resolvable.schema.yaml', `
182
+ description: Resolvable
183
+ input_schema:
184
+ type: object
185
+ properties:
186
+ query:
187
+ type: string
188
+ required:
189
+ - query
190
+ output_schema:
191
+ type: object
192
+ properties:
193
+ result:
194
+ type: string
195
+ `);
196
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
197
+ const sd = loader.load('resolvable');
198
+ const [inputRs, outputRs] = loader.resolve(sd);
199
+
200
+ expect(inputRs.moduleId).toBe('resolvable');
201
+ expect(inputRs.direction).toBe('input');
202
+ expect(Value.Check(inputRs.schema, { query: 'hello' })).toBe(true);
203
+ expect(Value.Check(inputRs.schema, {})).toBe(false);
204
+
205
+ expect(outputRs.moduleId).toBe('resolvable');
206
+ expect(outputRs.direction).toBe('output');
207
+ expect(Value.Check(outputRs.schema, { result: 'world' })).toBe(true);
208
+ });
209
+ });
210
+
211
+ describe('getSchema', () => {
212
+ const validYaml = `
213
+ description: Test module
214
+ input_schema:
215
+ type: object
216
+ properties:
217
+ x:
218
+ type: string
219
+ required:
220
+ - x
221
+ output_schema:
222
+ type: object
223
+ properties:
224
+ y:
225
+ type: string
226
+ `;
227
+
228
+ it('uses yaml_first strategy and finds YAML', () => {
229
+ writeSchema('mod.schema.yaml', validYaml);
230
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
231
+ const [inputRs, outputRs] = loader.getSchema('mod');
232
+
233
+ expect(inputRs.direction).toBe('input');
234
+ expect(outputRs.direction).toBe('output');
235
+ expect(Value.Check(inputRs.schema, { x: 'hi' })).toBe(true);
236
+ });
237
+
238
+ it('uses yaml_first strategy and falls back to native when YAML not found', () => {
239
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
240
+ const nativeInput = Type.Object({ a: Type.String() });
241
+ const nativeOutput = Type.Object({ b: Type.Number() });
242
+
243
+ const [inputRs, outputRs] = loader.getSchema('missing', nativeInput, nativeOutput);
244
+
245
+ expect(inputRs.direction).toBe('input');
246
+ expect(inputRs.moduleId).toBe('missing');
247
+ expect(Value.Check(inputRs.schema, { a: 'test' })).toBe(true);
248
+ expect(outputRs.direction).toBe('output');
249
+ expect(Value.Check(outputRs.schema, { b: 42 })).toBe(true);
250
+ });
251
+
252
+ it('uses yaml_first strategy and throws when YAML not found and no native schemas', () => {
253
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
254
+ expect(() => loader.getSchema('missing')).toThrow(SchemaNotFoundError);
255
+ });
256
+
257
+ it('uses native_first strategy and prefers native when available', () => {
258
+ writeSchema('native.schema.yaml', validYaml);
259
+ const config = makeConfig({ strategy: 'native_first' });
260
+ const loader = new SchemaLoader(config, schemasDir);
261
+
262
+ const nativeInput = Type.Object({ custom: Type.Boolean() });
263
+ const nativeOutput = Type.Object({ out: Type.Boolean() });
264
+
265
+ const [inputRs] = loader.getSchema('native', nativeInput, nativeOutput);
266
+ // Should use native, not YAML
267
+ expect(Value.Check(inputRs.schema, { custom: true })).toBe(true);
268
+ expect(Value.Check(inputRs.schema, { x: 'string' })).toBe(false);
269
+ });
270
+
271
+ it('uses native_first strategy and falls back to YAML when no native', () => {
272
+ writeSchema('fallback.schema.yaml', validYaml);
273
+ const config = makeConfig({ strategy: 'native_first' });
274
+ const loader = new SchemaLoader(config, schemasDir);
275
+
276
+ const [inputRs] = loader.getSchema('fallback');
277
+ expect(Value.Check(inputRs.schema, { x: 'hi' })).toBe(true);
278
+ });
279
+
280
+ it('uses yaml_only strategy', () => {
281
+ writeSchema('yamlonly.schema.yaml', validYaml);
282
+ const config = makeConfig({ strategy: 'yaml_only' });
283
+ const loader = new SchemaLoader(config, schemasDir);
284
+
285
+ const [inputRs] = loader.getSchema('yamlonly');
286
+ expect(Value.Check(inputRs.schema, { x: 'hi' })).toBe(true);
287
+ });
288
+
289
+ it('uses yaml_only strategy and throws when YAML not found', () => {
290
+ const config = makeConfig({ strategy: 'yaml_only' });
291
+ const loader = new SchemaLoader(config, schemasDir);
292
+
293
+ const nativeInput = Type.Object({ a: Type.String() });
294
+ const nativeOutput = Type.Object({ b: Type.Number() });
295
+ // yaml_only ignores native schemas
296
+ expect(() => loader.getSchema('nope', nativeInput, nativeOutput)).toThrow(SchemaNotFoundError);
297
+ });
298
+
299
+ it('caches getSchema results', () => {
300
+ writeSchema('cacheme.schema.yaml', validYaml);
301
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
302
+
303
+ const first = loader.getSchema('cacheme');
304
+ const second = loader.getSchema('cacheme');
305
+ expect(first).toBe(second);
306
+ });
307
+ });
308
+
309
+ describe('clearCache', () => {
310
+ it('clears all caches so next load/getSchema reloads from disk', () => {
311
+ writeSchema('clearable.schema.yaml', `
312
+ description: Clearable
313
+ input_schema:
314
+ type: object
315
+ properties:
316
+ v:
317
+ type: string
318
+ output_schema:
319
+ type: object
320
+ `);
321
+ const loader = new SchemaLoader(makeConfig(), schemasDir);
322
+
323
+ const sd1 = loader.load('clearable');
324
+ expect(sd1.description).toBe('Clearable');
325
+
326
+ // Overwrite file
327
+ writeSchema('clearable.schema.yaml', `
328
+ description: Updated
329
+ input_schema:
330
+ type: object
331
+ output_schema:
332
+ type: object
333
+ `);
334
+
335
+ // Without clear, returns cached
336
+ const sd2 = loader.load('clearable');
337
+ expect(sd2.description).toBe('Clearable');
338
+
339
+ // After clear, reloads from disk
340
+ loader.clearCache();
341
+ const sd3 = loader.load('clearable');
342
+ expect(sd3.description).toBe('Updated');
343
+ });
344
+ });
345
+
346
+ describe('constructor', () => {
347
+ it('uses config schema.root when schemasDir not provided', () => {
348
+ writeSchema('fromconfig.schema.yaml', `
349
+ description: From config
350
+ input_schema:
351
+ type: object
352
+ output_schema:
353
+ type: object
354
+ `);
355
+ const config = new Config({ schema: { root: schemasDir } });
356
+ const loader = new SchemaLoader(config);
357
+ const sd = loader.load('fromconfig');
358
+ expect(sd.description).toBe('From config');
359
+ });
360
+
361
+ it('uses default ./schemas when config has no schema.root', () => {
362
+ const config = new Config({});
363
+ // This just constructs without error; actual path may not exist
364
+ const loader = new SchemaLoader(config);
365
+ expect(loader).toBeInstanceOf(SchemaLoader);
366
+ });
367
+ });
368
+ });
5
369
 
6
370
  describe('jsonSchemaToTypeBox', () => {
7
371
  it('converts string type', () => {