apcore-js 0.1.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.
- package/.claude/settings.local.json +11 -0
- package/.gitmessage +60 -0
- package/.pre-commit-config.yaml +28 -0
- package/CHANGELOG.md +47 -0
- package/CLAUDE.md +68 -0
- package/README.md +131 -0
- package/apcore-logo.svg +79 -0
- package/package.json +37 -0
- package/planning/acl-system/overview.md +54 -0
- package/planning/acl-system/plan.md +92 -0
- package/planning/acl-system/state.json +76 -0
- package/planning/acl-system/tasks/acl-core.md +226 -0
- package/planning/acl-system/tasks/acl-rule.md +92 -0
- package/planning/acl-system/tasks/conditional-rules.md +259 -0
- package/planning/acl-system/tasks/pattern-matching.md +152 -0
- package/planning/acl-system/tasks/yaml-loading.md +271 -0
- package/planning/core-executor/overview.md +53 -0
- package/planning/core-executor/plan.md +88 -0
- package/planning/core-executor/state.json +76 -0
- package/planning/core-executor/tasks/async-support.md +106 -0
- package/planning/core-executor/tasks/execution-pipeline.md +113 -0
- package/planning/core-executor/tasks/redaction.md +85 -0
- package/planning/core-executor/tasks/safety-checks.md +65 -0
- package/planning/core-executor/tasks/setup.md +75 -0
- package/planning/decorator-bindings/overview.md +62 -0
- package/planning/decorator-bindings/plan.md +104 -0
- package/planning/decorator-bindings/state.json +87 -0
- package/planning/decorator-bindings/tasks/binding-directory.md +79 -0
- package/planning/decorator-bindings/tasks/binding-loader.md +148 -0
- package/planning/decorator-bindings/tasks/explicit-schemas.md +85 -0
- package/planning/decorator-bindings/tasks/function-module.md +127 -0
- package/planning/decorator-bindings/tasks/module-factory.md +89 -0
- package/planning/decorator-bindings/tasks/schema-modes.md +142 -0
- package/planning/middleware-system/overview.md +48 -0
- package/planning/middleware-system/plan.md +102 -0
- package/planning/middleware-system/state.json +65 -0
- package/planning/middleware-system/tasks/adapters.md +170 -0
- package/planning/middleware-system/tasks/base.md +115 -0
- package/planning/middleware-system/tasks/logging-middleware.md +304 -0
- package/planning/middleware-system/tasks/manager.md +313 -0
- package/planning/observability/overview.md +53 -0
- package/planning/observability/plan.md +119 -0
- package/planning/observability/state.json +98 -0
- package/planning/observability/tasks/context-logger.md +201 -0
- package/planning/observability/tasks/exporters.md +121 -0
- package/planning/observability/tasks/metrics-collector.md +162 -0
- package/planning/observability/tasks/metrics-middleware.md +141 -0
- package/planning/observability/tasks/obs-logging-middleware.md +179 -0
- package/planning/observability/tasks/span-model.md +120 -0
- package/planning/observability/tasks/tracing-middleware.md +179 -0
- package/planning/overview.md +81 -0
- package/planning/registry-system/overview.md +57 -0
- package/planning/registry-system/plan.md +114 -0
- package/planning/registry-system/state.json +109 -0
- package/planning/registry-system/tasks/dependencies.md +157 -0
- package/planning/registry-system/tasks/entry-point.md +148 -0
- package/planning/registry-system/tasks/metadata.md +198 -0
- package/planning/registry-system/tasks/registry-core.md +323 -0
- package/planning/registry-system/tasks/scanner.md +172 -0
- package/planning/registry-system/tasks/schema-export.md +261 -0
- package/planning/registry-system/tasks/types.md +124 -0
- package/planning/registry-system/tasks/validation.md +177 -0
- package/planning/schema-system/overview.md +56 -0
- package/planning/schema-system/plan.md +121 -0
- package/planning/schema-system/state.json +98 -0
- package/planning/schema-system/tasks/exporter.md +153 -0
- package/planning/schema-system/tasks/loader.md +106 -0
- package/planning/schema-system/tasks/ref-resolver.md +133 -0
- package/planning/schema-system/tasks/strict-mode.md +140 -0
- package/planning/schema-system/tasks/typebox-generation.md +133 -0
- package/planning/schema-system/tasks/types-and-annotations.md +160 -0
- package/planning/schema-system/tasks/validator.md +149 -0
- package/src/acl.ts +188 -0
- package/src/bindings.ts +208 -0
- package/src/config.ts +24 -0
- package/src/context.ts +75 -0
- package/src/decorator.ts +110 -0
- package/src/errors.ts +369 -0
- package/src/executor.ts +348 -0
- package/src/index.ts +81 -0
- package/src/middleware/adapters.ts +54 -0
- package/src/middleware/base.ts +33 -0
- package/src/middleware/index.ts +6 -0
- package/src/middleware/logging.ts +103 -0
- package/src/middleware/manager.ts +105 -0
- package/src/module.ts +41 -0
- package/src/observability/context-logger.ts +201 -0
- package/src/observability/index.ts +4 -0
- package/src/observability/metrics.ts +212 -0
- package/src/observability/tracing.ts +187 -0
- package/src/registry/dependencies.ts +99 -0
- package/src/registry/entry-point.ts +64 -0
- package/src/registry/index.ts +8 -0
- package/src/registry/metadata.ts +111 -0
- package/src/registry/registry.ts +314 -0
- package/src/registry/scanner.ts +150 -0
- package/src/registry/schema-export.ts +177 -0
- package/src/registry/types.ts +32 -0
- package/src/registry/validation.ts +38 -0
- package/src/schema/annotations.ts +67 -0
- package/src/schema/exporter.ts +93 -0
- package/src/schema/index.ts +14 -0
- package/src/schema/loader.ts +270 -0
- package/src/schema/ref-resolver.ts +235 -0
- package/src/schema/strict.ts +128 -0
- package/src/schema/types.ts +73 -0
- package/src/schema/validator.ts +82 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/pattern.ts +30 -0
- package/tests/helpers.ts +30 -0
- package/tests/integration/test-acl-safety.test.ts +268 -0
- package/tests/integration/test-binding-executor.test.ts +194 -0
- package/tests/integration/test-e2e-flow.test.ts +117 -0
- package/tests/integration/test-error-propagation.test.ts +259 -0
- package/tests/integration/test-middleware-chain.test.ts +120 -0
- package/tests/integration/test-observability-integration.test.ts +438 -0
- package/tests/observability/test-context-logger.test.ts +123 -0
- package/tests/observability/test-metrics.test.ts +89 -0
- package/tests/observability/test-tracing.test.ts +131 -0
- package/tests/registry/test-dependencies.test.ts +70 -0
- package/tests/registry/test-entry-point.test.ts +133 -0
- package/tests/registry/test-metadata.test.ts +265 -0
- package/tests/registry/test-registry.test.ts +140 -0
- package/tests/registry/test-scanner.test.ts +257 -0
- package/tests/registry/test-schema-export.test.ts +224 -0
- package/tests/registry/test-validation.test.ts +75 -0
- package/tests/schema/test-loader.test.ts +97 -0
- package/tests/schema/test-ref-resolver.test.ts +105 -0
- package/tests/schema/test-strict.test.ts +139 -0
- package/tests/schema/test-validator.test.ts +64 -0
- package/tests/test-acl.test.ts +206 -0
- package/tests/test-bindings.test.ts +227 -0
- package/tests/test-config.test.ts +76 -0
- package/tests/test-context.test.ts +151 -0
- package/tests/test-decorator.test.ts +173 -0
- package/tests/test-errors.test.ts +204 -0
- package/tests/test-executor.test.ts +252 -0
- package/tests/test-middleware-manager.test.ts +185 -0
- package/tests/test-middleware.test.ts +86 -0
- package/tsconfig.build.json +8 -0
- package/tsconfig.json +20 -0
- package/vitest.config.ts +18 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { Type } from '@sinclair/typebox';
|
|
3
|
+
import { Registry } from '../../src/registry/registry.js';
|
|
4
|
+
import { FunctionModule } from '../../src/decorator.js';
|
|
5
|
+
import { InvalidInputError, ModuleNotFoundError } from '../../src/errors.js';
|
|
6
|
+
|
|
7
|
+
function createMod(id: string): FunctionModule {
|
|
8
|
+
return new FunctionModule({
|
|
9
|
+
execute: () => ({ ok: true }),
|
|
10
|
+
moduleId: id,
|
|
11
|
+
inputSchema: Type.Object({}),
|
|
12
|
+
outputSchema: Type.Object({ ok: Type.Boolean() }),
|
|
13
|
+
description: `Module ${id}`,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe('Registry', () => {
|
|
18
|
+
it('creates empty registry', () => {
|
|
19
|
+
const registry = new Registry();
|
|
20
|
+
expect(registry.count).toBe(0);
|
|
21
|
+
expect(registry.list()).toEqual([]);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('register and get module', () => {
|
|
25
|
+
const registry = new Registry();
|
|
26
|
+
const mod = createMod('test.a');
|
|
27
|
+
registry.register('test.a', mod);
|
|
28
|
+
expect(registry.get('test.a')).toBe(mod);
|
|
29
|
+
expect(registry.has('test.a')).toBe(true);
|
|
30
|
+
expect(registry.count).toBe(1);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('get returns null for unknown module', () => {
|
|
34
|
+
const registry = new Registry();
|
|
35
|
+
expect(registry.get('unknown')).toBeNull();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('get throws for empty string', () => {
|
|
39
|
+
const registry = new Registry();
|
|
40
|
+
expect(() => registry.get('')).toThrow(ModuleNotFoundError);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('register throws for empty moduleId', () => {
|
|
44
|
+
const registry = new Registry();
|
|
45
|
+
expect(() => registry.register('', createMod('x'))).toThrow(InvalidInputError);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('register throws for duplicate moduleId', () => {
|
|
49
|
+
const registry = new Registry();
|
|
50
|
+
registry.register('test.a', createMod('test.a'));
|
|
51
|
+
expect(() => registry.register('test.a', createMod('test.a'))).toThrow(InvalidInputError);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('unregister removes module', () => {
|
|
55
|
+
const registry = new Registry();
|
|
56
|
+
registry.register('test.a', createMod('test.a'));
|
|
57
|
+
const removed = registry.unregister('test.a');
|
|
58
|
+
expect(removed).toBe(true);
|
|
59
|
+
expect(registry.has('test.a')).toBe(false);
|
|
60
|
+
expect(registry.count).toBe(0);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('unregister returns false for unknown module', () => {
|
|
64
|
+
const registry = new Registry();
|
|
65
|
+
expect(registry.unregister('nonexistent')).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('list returns sorted module IDs', () => {
|
|
69
|
+
const registry = new Registry();
|
|
70
|
+
registry.register('b.mod', createMod('b.mod'));
|
|
71
|
+
registry.register('a.mod', createMod('a.mod'));
|
|
72
|
+
registry.register('c.mod', createMod('c.mod'));
|
|
73
|
+
expect(registry.list()).toEqual(['a.mod', 'b.mod', 'c.mod']);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('list filters by prefix', () => {
|
|
77
|
+
const registry = new Registry();
|
|
78
|
+
registry.register('foo.a', createMod('foo.a'));
|
|
79
|
+
registry.register('foo.b', createMod('foo.b'));
|
|
80
|
+
registry.register('bar.a', createMod('bar.a'));
|
|
81
|
+
expect(registry.list({ prefix: 'foo.' })).toEqual(['foo.a', 'foo.b']);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('moduleIds returns sorted IDs', () => {
|
|
85
|
+
const registry = new Registry();
|
|
86
|
+
registry.register('z.mod', createMod('z.mod'));
|
|
87
|
+
registry.register('a.mod', createMod('a.mod'));
|
|
88
|
+
expect(registry.moduleIds).toEqual(['a.mod', 'z.mod']);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('iter returns entries', () => {
|
|
92
|
+
const registry = new Registry();
|
|
93
|
+
registry.register('test.a', createMod('test.a'));
|
|
94
|
+
const entries = [...registry.iter()];
|
|
95
|
+
expect(entries).toHaveLength(1);
|
|
96
|
+
expect(entries[0][0]).toBe('test.a');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('on register event fires', () => {
|
|
100
|
+
const registry = new Registry();
|
|
101
|
+
const events: string[] = [];
|
|
102
|
+
registry.on('register', (id) => events.push(id));
|
|
103
|
+
registry.register('test.a', createMod('test.a'));
|
|
104
|
+
expect(events).toEqual(['test.a']);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('on unregister event fires', () => {
|
|
108
|
+
const registry = new Registry();
|
|
109
|
+
const events: string[] = [];
|
|
110
|
+
registry.on('unregister', (id) => events.push(id));
|
|
111
|
+
registry.register('test.a', createMod('test.a'));
|
|
112
|
+
registry.unregister('test.a');
|
|
113
|
+
expect(events).toEqual(['test.a']);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('on throws for invalid event', () => {
|
|
117
|
+
const registry = new Registry();
|
|
118
|
+
expect(() => registry.on('invalid', () => {})).toThrow(InvalidInputError);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('getDefinition returns descriptor', () => {
|
|
122
|
+
const registry = new Registry();
|
|
123
|
+
const mod = createMod('test.a');
|
|
124
|
+
registry.register('test.a', mod);
|
|
125
|
+
const def = registry.getDefinition('test.a');
|
|
126
|
+
expect(def).not.toBeNull();
|
|
127
|
+
expect(def!.moduleId).toBe('test.a');
|
|
128
|
+
expect(def!.description).toBe('Module test.a');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('getDefinition returns null for unknown module', () => {
|
|
132
|
+
const registry = new Registry();
|
|
133
|
+
expect(registry.getDefinition('nonexistent')).toBeNull();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('clearCache does not throw', () => {
|
|
137
|
+
const registry = new Registry();
|
|
138
|
+
registry.clearCache();
|
|
139
|
+
});
|
|
140
|
+
});
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
mkdtempSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
writeFileSync,
|
|
6
|
+
rmSync,
|
|
7
|
+
chmodSync,
|
|
8
|
+
} from 'node:fs';
|
|
9
|
+
import { tmpdir } from 'node:os';
|
|
10
|
+
import { join, sep } from 'node:path';
|
|
11
|
+
import { scanExtensions, scanMultiRoot } from '../../src/registry/scanner.js';
|
|
12
|
+
import { ConfigNotFoundError, ConfigError } from '../../src/errors.js';
|
|
13
|
+
|
|
14
|
+
let tempDir: string;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
tempDir = mkdtempSync(join(tmpdir(), 'scanner-test-'));
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
rmSync(tempDir, { recursive: true, force: true });
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
function touch(relativePath: string, content = ''): string {
|
|
25
|
+
const full = join(tempDir, relativePath);
|
|
26
|
+
const dir = full.substring(0, full.lastIndexOf(sep));
|
|
27
|
+
mkdirSync(dir, { recursive: true });
|
|
28
|
+
writeFileSync(full, content);
|
|
29
|
+
return full;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe('scanExtensions', () => {
|
|
33
|
+
it('discovers .ts and .js files and returns correct DiscoveredModule shape', () => {
|
|
34
|
+
touch('alpha.ts');
|
|
35
|
+
touch('beta.js');
|
|
36
|
+
|
|
37
|
+
const results = scanExtensions(tempDir);
|
|
38
|
+
expect(results).toHaveLength(2);
|
|
39
|
+
const ids = results.map((r) => r.canonicalId).sort();
|
|
40
|
+
expect(ids).toEqual(['alpha', 'beta']);
|
|
41
|
+
|
|
42
|
+
for (const mod of results) {
|
|
43
|
+
expect(mod.filePath).toBeTruthy();
|
|
44
|
+
expect(mod.canonicalId).toBeTruthy();
|
|
45
|
+
expect(mod.metaPath).toBeNull();
|
|
46
|
+
expect(mod.namespace).toBeNull();
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('builds dot-notation canonicalId from nested paths', () => {
|
|
51
|
+
touch('sub/module.ts');
|
|
52
|
+
touch('deep/nested/handler.js');
|
|
53
|
+
|
|
54
|
+
const results = scanExtensions(tempDir);
|
|
55
|
+
const ids = results.map((r) => r.canonicalId).sort();
|
|
56
|
+
expect(ids).toEqual(['deep.nested.handler', 'sub.module']);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('skips .d.ts declaration files', () => {
|
|
60
|
+
touch('real.ts');
|
|
61
|
+
touch('types.d.ts');
|
|
62
|
+
|
|
63
|
+
const results = scanExtensions(tempDir);
|
|
64
|
+
expect(results).toHaveLength(1);
|
|
65
|
+
expect(results[0].canonicalId).toBe('real');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('skips .test.ts, .test.js, .spec.ts, and .spec.js files', () => {
|
|
69
|
+
touch('handler.ts');
|
|
70
|
+
touch('handler.test.ts');
|
|
71
|
+
touch('handler.test.js');
|
|
72
|
+
touch('handler.spec.ts');
|
|
73
|
+
touch('handler.spec.js');
|
|
74
|
+
|
|
75
|
+
const results = scanExtensions(tempDir);
|
|
76
|
+
expect(results).toHaveLength(1);
|
|
77
|
+
expect(results[0].canonicalId).toBe('handler');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('skips dot-prefixed and underscore-prefixed entries', () => {
|
|
81
|
+
touch('.hidden/secret.ts');
|
|
82
|
+
touch('_private/internal.ts');
|
|
83
|
+
touch('.env.ts');
|
|
84
|
+
touch('_helper.ts');
|
|
85
|
+
touch('visible.ts');
|
|
86
|
+
|
|
87
|
+
const results = scanExtensions(tempDir);
|
|
88
|
+
expect(results).toHaveLength(1);
|
|
89
|
+
expect(results[0].canonicalId).toBe('visible');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('skips node_modules and __pycache__ directories', () => {
|
|
93
|
+
touch('node_modules/pkg/index.ts');
|
|
94
|
+
touch('__pycache__/cached.ts');
|
|
95
|
+
touch('real.ts');
|
|
96
|
+
|
|
97
|
+
const results = scanExtensions(tempDir);
|
|
98
|
+
expect(results).toHaveLength(1);
|
|
99
|
+
expect(results[0].canonicalId).toBe('real');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('ignores non-.ts/.js files', () => {
|
|
103
|
+
touch('readme.md');
|
|
104
|
+
touch('config.yaml');
|
|
105
|
+
touch('data.json');
|
|
106
|
+
touch('valid.ts');
|
|
107
|
+
|
|
108
|
+
const results = scanExtensions(tempDir);
|
|
109
|
+
expect(results).toHaveLength(1);
|
|
110
|
+
expect(results[0].canonicalId).toBe('valid');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('detects companion _meta.yaml files', () => {
|
|
114
|
+
touch('handler.ts');
|
|
115
|
+
touch('handler_meta.yaml', 'description: a handler');
|
|
116
|
+
|
|
117
|
+
const results = scanExtensions(tempDir);
|
|
118
|
+
expect(results).toHaveLength(1);
|
|
119
|
+
expect(results[0].metaPath).toBe(join(tempDir, 'handler_meta.yaml'));
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('sets metaPath to null when no companion _meta.yaml exists', () => {
|
|
123
|
+
touch('handler.ts');
|
|
124
|
+
touch('handler_meta.json', '{}');
|
|
125
|
+
|
|
126
|
+
const results = scanExtensions(tempDir);
|
|
127
|
+
expect(results).toHaveLength(1);
|
|
128
|
+
expect(results[0].metaPath).toBeNull();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('respects maxDepth parameter', () => {
|
|
132
|
+
touch('level1.ts');
|
|
133
|
+
touch('a/level2.ts');
|
|
134
|
+
touch('a/b/level3.ts');
|
|
135
|
+
|
|
136
|
+
const resultsDepth1 = scanExtensions(tempDir, 1);
|
|
137
|
+
expect(resultsDepth1.map((r) => r.canonicalId).sort()).toEqual(['level1']);
|
|
138
|
+
|
|
139
|
+
const resultsDepth2 = scanExtensions(tempDir, 2);
|
|
140
|
+
expect(resultsDepth2.map((r) => r.canonicalId).sort()).toEqual(['a.level2', 'level1']);
|
|
141
|
+
|
|
142
|
+
const resultsAll = scanExtensions(tempDir, 8);
|
|
143
|
+
expect(resultsAll.map((r) => r.canonicalId).sort()).toEqual(['a.b.level3', 'a.level2', 'level1']);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('throws ConfigNotFoundError for non-existent root directory', () => {
|
|
147
|
+
const bogus = join(tempDir, 'does-not-exist');
|
|
148
|
+
expect(() => scanExtensions(bogus)).toThrow(ConfigNotFoundError);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('deduplicates by canonicalId (first file wins)', () => {
|
|
152
|
+
touch('handler.ts');
|
|
153
|
+
touch('handler.js');
|
|
154
|
+
|
|
155
|
+
const results = scanExtensions(tempDir);
|
|
156
|
+
expect(results).toHaveLength(1);
|
|
157
|
+
expect(results[0].canonicalId).toBe('handler');
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('handles deeply nested structures correctly', () => {
|
|
161
|
+
touch('a/b/c/d/e/module.ts');
|
|
162
|
+
|
|
163
|
+
const results = scanExtensions(tempDir);
|
|
164
|
+
expect(results).toHaveLength(1);
|
|
165
|
+
expect(results[0].canonicalId).toBe('a.b.c.d.e.module');
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('returns empty array for an empty directory', () => {
|
|
169
|
+
const results = scanExtensions(tempDir);
|
|
170
|
+
expect(results).toEqual([]);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('filePath is an absolute path to the discovered file', () => {
|
|
174
|
+
touch('mod.ts');
|
|
175
|
+
|
|
176
|
+
const results = scanExtensions(tempDir);
|
|
177
|
+
expect(results).toHaveLength(1);
|
|
178
|
+
expect(results[0].filePath).toBe(join(tempDir, 'mod.ts'));
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe('scanMultiRoot', () => {
|
|
183
|
+
it('prefixes canonicalId with namespace', () => {
|
|
184
|
+
const rootA = join(tempDir, 'rootA');
|
|
185
|
+
mkdirSync(rootA);
|
|
186
|
+
writeFileSync(join(rootA, 'handler.ts'), '');
|
|
187
|
+
|
|
188
|
+
const results = scanMultiRoot([{ root: rootA, namespace: 'ns1' }]);
|
|
189
|
+
expect(results).toHaveLength(1);
|
|
190
|
+
expect(results[0].canonicalId).toBe('ns1.handler');
|
|
191
|
+
expect(results[0].namespace).toBe('ns1');
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('auto-uses directory basename as namespace when not specified', () => {
|
|
195
|
+
const rootDir = join(tempDir, 'myextensions');
|
|
196
|
+
mkdirSync(rootDir);
|
|
197
|
+
writeFileSync(join(rootDir, 'action.ts'), '');
|
|
198
|
+
|
|
199
|
+
const results = scanMultiRoot([{ root: rootDir }]);
|
|
200
|
+
expect(results).toHaveLength(1);
|
|
201
|
+
expect(results[0].canonicalId).toBe('myextensions.action');
|
|
202
|
+
expect(results[0].namespace).toBe('myextensions');
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('throws ConfigError for duplicate namespaces', () => {
|
|
206
|
+
const rootA = join(tempDir, 'a');
|
|
207
|
+
const rootB = join(tempDir, 'b');
|
|
208
|
+
mkdirSync(rootA);
|
|
209
|
+
mkdirSync(rootB);
|
|
210
|
+
|
|
211
|
+
expect(() =>
|
|
212
|
+
scanMultiRoot([
|
|
213
|
+
{ root: rootA, namespace: 'dup' },
|
|
214
|
+
{ root: rootB, namespace: 'dup' },
|
|
215
|
+
]),
|
|
216
|
+
).toThrow(ConfigError);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('merges results from multiple roots', () => {
|
|
220
|
+
const rootA = join(tempDir, 'rootA');
|
|
221
|
+
const rootB = join(tempDir, 'rootB');
|
|
222
|
+
mkdirSync(rootA);
|
|
223
|
+
mkdirSync(rootB);
|
|
224
|
+
writeFileSync(join(rootA, 'foo.ts'), '');
|
|
225
|
+
writeFileSync(join(rootB, 'bar.ts'), '');
|
|
226
|
+
|
|
227
|
+
const results = scanMultiRoot([
|
|
228
|
+
{ root: rootA, namespace: 'a' },
|
|
229
|
+
{ root: rootB, namespace: 'b' },
|
|
230
|
+
]);
|
|
231
|
+
expect(results).toHaveLength(2);
|
|
232
|
+
const ids = results.map((r) => r.canonicalId).sort();
|
|
233
|
+
expect(ids).toEqual(['a.foo', 'b.bar']);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('propagates ConfigNotFoundError for non-existent root', () => {
|
|
237
|
+
const bogus = join(tempDir, 'nonexistent');
|
|
238
|
+
expect(() => scanMultiRoot([{ root: bogus, namespace: 'ns' }])).toThrow(ConfigNotFoundError);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('handles empty roots array', () => {
|
|
242
|
+
const results = scanMultiRoot([]);
|
|
243
|
+
expect(results).toEqual([]);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('preserves metaPath through multi-root scan', () => {
|
|
247
|
+
const rootDir = join(tempDir, 'ext');
|
|
248
|
+
mkdirSync(rootDir);
|
|
249
|
+
writeFileSync(join(rootDir, 'mod.ts'), '');
|
|
250
|
+
writeFileSync(join(rootDir, 'mod_meta.yaml'), 'description: test');
|
|
251
|
+
|
|
252
|
+
const results = scanMultiRoot([{ root: rootDir, namespace: 'pkg' }]);
|
|
253
|
+
expect(results).toHaveLength(1);
|
|
254
|
+
expect(results[0].metaPath).toBe(join(rootDir, 'mod_meta.yaml'));
|
|
255
|
+
expect(results[0].canonicalId).toBe('pkg.mod');
|
|
256
|
+
});
|
|
257
|
+
});
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { Type } from '@sinclair/typebox';
|
|
3
|
+
import { Registry } from '../../src/registry/registry.js';
|
|
4
|
+
import { FunctionModule } from '../../src/decorator.js';
|
|
5
|
+
import { ModuleNotFoundError } from '../../src/errors.js';
|
|
6
|
+
import {
|
|
7
|
+
getSchema,
|
|
8
|
+
exportSchema,
|
|
9
|
+
getAllSchemas,
|
|
10
|
+
exportAllSchemas,
|
|
11
|
+
} from '../../src/registry/schema-export.js';
|
|
12
|
+
|
|
13
|
+
const inputSchema = Type.Object({
|
|
14
|
+
prompt: Type.String({ description: 'The input prompt' }),
|
|
15
|
+
temperature: Type.Optional(Type.Number({ description: 'Sampling temperature' })),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const outputSchema = Type.Object({
|
|
19
|
+
text: Type.String(),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
function createModule(
|
|
23
|
+
id: string,
|
|
24
|
+
overrides?: Partial<ConstructorParameters<typeof FunctionModule>[0]>,
|
|
25
|
+
): FunctionModule {
|
|
26
|
+
return new FunctionModule({
|
|
27
|
+
execute: () => ({ text: 'hello' }),
|
|
28
|
+
moduleId: id,
|
|
29
|
+
inputSchema,
|
|
30
|
+
outputSchema,
|
|
31
|
+
description: 'A test module. It does many things.\nSecond paragraph.',
|
|
32
|
+
tags: ['ai', 'test'],
|
|
33
|
+
version: '2.0.0',
|
|
34
|
+
annotations: {
|
|
35
|
+
readonly: true,
|
|
36
|
+
destructive: false,
|
|
37
|
+
idempotent: true,
|
|
38
|
+
requiresApproval: false,
|
|
39
|
+
openWorld: false,
|
|
40
|
+
},
|
|
41
|
+
examples: [
|
|
42
|
+
{
|
|
43
|
+
title: 'Basic example',
|
|
44
|
+
inputs: { prompt: 'hi' },
|
|
45
|
+
output: { text: 'hello' },
|
|
46
|
+
description: 'Simple greeting',
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
...overrides,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function makeRegistry(...modules: Array<[string, FunctionModule]>): Registry {
|
|
54
|
+
const registry = new Registry();
|
|
55
|
+
for (const [id, mod] of modules) {
|
|
56
|
+
registry.register(id, mod);
|
|
57
|
+
}
|
|
58
|
+
return registry;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
describe('getSchema', () => {
|
|
62
|
+
it('returns null for unregistered module', () => {
|
|
63
|
+
const registry = new Registry();
|
|
64
|
+
expect(getSchema(registry, 'no.such.module')).toBeNull();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('returns schema record with all expected fields', () => {
|
|
68
|
+
const mod = createModule('test.gen');
|
|
69
|
+
const registry = makeRegistry(['test.gen', mod]);
|
|
70
|
+
|
|
71
|
+
const schema = getSchema(registry, 'test.gen');
|
|
72
|
+
expect(schema).not.toBeNull();
|
|
73
|
+
expect(schema!['module_id']).toBe('test.gen');
|
|
74
|
+
expect(schema!['description']).toBe('A test module. It does many things.\nSecond paragraph.');
|
|
75
|
+
expect(schema!['version']).toBe('2.0.0');
|
|
76
|
+
expect(schema!['tags']).toEqual(['ai', 'test']);
|
|
77
|
+
expect(schema!['input_schema']).toBeDefined();
|
|
78
|
+
expect(schema!['output_schema']).toBeDefined();
|
|
79
|
+
expect(schema!['examples']).toHaveLength(1);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('copies tags array to prevent mutation', () => {
|
|
83
|
+
const mod = createModule('test.tags');
|
|
84
|
+
const registry = makeRegistry(['test.tags', mod]);
|
|
85
|
+
|
|
86
|
+
const schema = getSchema(registry, 'test.tags');
|
|
87
|
+
const tags = schema!['tags'] as string[];
|
|
88
|
+
tags.push('injected');
|
|
89
|
+
|
|
90
|
+
expect(mod.tags).toEqual(['ai', 'test']);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('returns empty array for tags when module has null tags', () => {
|
|
94
|
+
const mod = createModule('test.notags', { tags: null });
|
|
95
|
+
const registry = makeRegistry(['test.notags', mod]);
|
|
96
|
+
const schema = getSchema(registry, 'test.notags');
|
|
97
|
+
expect(schema!['tags']).toEqual([]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('returns null annotations when module has no annotations', () => {
|
|
101
|
+
const mod = createModule('test.noanno', { annotations: null });
|
|
102
|
+
const registry = makeRegistry(['test.noanno', mod]);
|
|
103
|
+
const schema = getSchema(registry, 'test.noanno');
|
|
104
|
+
expect(schema!['annotations']).toBeNull();
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe('exportSchema', () => {
|
|
109
|
+
it('returns JSON string by default', () => {
|
|
110
|
+
const mod = createModule('test.json');
|
|
111
|
+
const registry = makeRegistry(['test.json', mod]);
|
|
112
|
+
|
|
113
|
+
const result = exportSchema(registry, 'test.json');
|
|
114
|
+
const parsed = JSON.parse(result);
|
|
115
|
+
expect(parsed['module_id']).toBe('test.json');
|
|
116
|
+
expect(parsed['version']).toBe('2.0.0');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('returns YAML string when format is yaml', () => {
|
|
120
|
+
const mod = createModule('test.yaml');
|
|
121
|
+
const registry = makeRegistry(['test.yaml', mod]);
|
|
122
|
+
|
|
123
|
+
const result = exportSchema(registry, 'test.yaml', 'yaml');
|
|
124
|
+
expect(result).toContain('module_id:');
|
|
125
|
+
expect(result).toContain('test.yaml');
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('throws ModuleNotFoundError for unregistered module', () => {
|
|
129
|
+
const registry = new Registry();
|
|
130
|
+
expect(() => exportSchema(registry, 'no.such.module')).toThrow(ModuleNotFoundError);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('applies strict mode to input and output schemas', () => {
|
|
134
|
+
const mod = createModule('test.strict');
|
|
135
|
+
const registry = makeRegistry(['test.strict', mod]);
|
|
136
|
+
|
|
137
|
+
const result = exportSchema(registry, 'test.strict', 'json', true);
|
|
138
|
+
const parsed = JSON.parse(result);
|
|
139
|
+
expect((parsed['input_schema'] as Record<string, unknown>)['additionalProperties']).toBe(false);
|
|
140
|
+
expect((parsed['output_schema'] as Record<string, unknown>)['additionalProperties']).toBe(false);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('compact mode truncates description at first sentence boundary', () => {
|
|
144
|
+
const mod = createModule('test.compact');
|
|
145
|
+
const registry = makeRegistry(['test.compact', mod]);
|
|
146
|
+
|
|
147
|
+
const result = exportSchema(registry, 'test.compact', 'json', false, true);
|
|
148
|
+
const parsed = JSON.parse(result);
|
|
149
|
+
expect(parsed['description']).toBe('A test module.');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('compact mode removes examples and documentation', () => {
|
|
153
|
+
const mod = createModule('test.compact.ex', { documentation: 'Full docs' });
|
|
154
|
+
const registry = makeRegistry(['test.compact.ex', mod]);
|
|
155
|
+
|
|
156
|
+
const result = exportSchema(registry, 'test.compact.ex', 'json', false, true);
|
|
157
|
+
const parsed = JSON.parse(result);
|
|
158
|
+
expect(parsed['examples']).toBeUndefined();
|
|
159
|
+
expect(parsed['documentation']).toBeUndefined();
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('strict takes precedence over compact when both are true', () => {
|
|
163
|
+
const mod = createModule('test.both');
|
|
164
|
+
const registry = makeRegistry(['test.both', mod]);
|
|
165
|
+
|
|
166
|
+
const result = exportSchema(registry, 'test.both', 'json', true, true);
|
|
167
|
+
const parsed = JSON.parse(result);
|
|
168
|
+
expect((parsed['input_schema'] as Record<string, unknown>)['additionalProperties']).toBe(false);
|
|
169
|
+
expect(parsed['description']).toBe('A test module. It does many things.\nSecond paragraph.');
|
|
170
|
+
expect(parsed['examples']).toBeDefined();
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe('getAllSchemas', () => {
|
|
175
|
+
it('returns empty object for empty registry', () => {
|
|
176
|
+
const registry = new Registry();
|
|
177
|
+
expect(getAllSchemas(registry)).toEqual({});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('returns all module schemas keyed by module id', () => {
|
|
181
|
+
const modA = createModule('alpha');
|
|
182
|
+
const modB = createModule('beta', { version: '3.0.0' });
|
|
183
|
+
const registry = makeRegistry(['alpha', modA], ['beta', modB]);
|
|
184
|
+
|
|
185
|
+
const result = getAllSchemas(registry);
|
|
186
|
+
expect(Object.keys(result).sort()).toEqual(['alpha', 'beta']);
|
|
187
|
+
expect(result['alpha']['module_id']).toBe('alpha');
|
|
188
|
+
expect(result['beta']['version']).toBe('3.0.0');
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
describe('exportAllSchemas', () => {
|
|
193
|
+
it('serializes all schemas to JSON', () => {
|
|
194
|
+
const modA = createModule('a.mod');
|
|
195
|
+
const modB = createModule('b.mod');
|
|
196
|
+
const registry = makeRegistry(['a.mod', modA], ['b.mod', modB]);
|
|
197
|
+
|
|
198
|
+
const result = exportAllSchemas(registry);
|
|
199
|
+
const parsed = JSON.parse(result);
|
|
200
|
+
expect(Object.keys(parsed).sort()).toEqual(['a.mod', 'b.mod']);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('supports YAML format', () => {
|
|
204
|
+
const mod = createModule('yaml.mod');
|
|
205
|
+
const registry = makeRegistry(['yaml.mod', mod]);
|
|
206
|
+
|
|
207
|
+
const result = exportAllSchemas(registry, 'yaml');
|
|
208
|
+
expect(result).toContain('yaml.mod');
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('applies strict mode to all schemas', () => {
|
|
212
|
+
const modA = createModule('strict.a');
|
|
213
|
+
const registry = makeRegistry(['strict.a', modA]);
|
|
214
|
+
|
|
215
|
+
const result = exportAllSchemas(registry, 'json', true);
|
|
216
|
+
const parsed = JSON.parse(result);
|
|
217
|
+
expect((parsed['strict.a']['input_schema'] as Record<string, unknown>)['additionalProperties']).toBe(false);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('returns empty JSON object for empty registry', () => {
|
|
221
|
+
const registry = new Registry();
|
|
222
|
+
expect(JSON.parse(exportAllSchemas(registry))).toEqual({});
|
|
223
|
+
});
|
|
224
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { Type } from '@sinclair/typebox';
|
|
3
|
+
import { validateModule } from '../../src/registry/validation.js';
|
|
4
|
+
|
|
5
|
+
describe('validateModule', () => {
|
|
6
|
+
it('valid module returns no errors', () => {
|
|
7
|
+
const mod = {
|
|
8
|
+
inputSchema: Type.Object({}),
|
|
9
|
+
outputSchema: Type.Object({}),
|
|
10
|
+
description: 'A test module',
|
|
11
|
+
execute: () => ({}),
|
|
12
|
+
};
|
|
13
|
+
expect(validateModule(mod)).toEqual([]);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('missing inputSchema reports error', () => {
|
|
17
|
+
const mod = {
|
|
18
|
+
outputSchema: Type.Object({}),
|
|
19
|
+
description: 'test',
|
|
20
|
+
execute: () => ({}),
|
|
21
|
+
};
|
|
22
|
+
const errors = validateModule(mod);
|
|
23
|
+
expect(errors.length).toBeGreaterThan(0);
|
|
24
|
+
expect(errors.some(e => e.includes('inputSchema'))).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('missing outputSchema reports error', () => {
|
|
28
|
+
const mod = {
|
|
29
|
+
inputSchema: Type.Object({}),
|
|
30
|
+
description: 'test',
|
|
31
|
+
execute: () => ({}),
|
|
32
|
+
};
|
|
33
|
+
const errors = validateModule(mod);
|
|
34
|
+
expect(errors.length).toBeGreaterThan(0);
|
|
35
|
+
expect(errors.some(e => e.includes('outputSchema'))).toBe(true);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('missing description reports error', () => {
|
|
39
|
+
const mod = {
|
|
40
|
+
inputSchema: Type.Object({}),
|
|
41
|
+
outputSchema: Type.Object({}),
|
|
42
|
+
execute: () => ({}),
|
|
43
|
+
};
|
|
44
|
+
const errors = validateModule(mod);
|
|
45
|
+
expect(errors.length).toBeGreaterThan(0);
|
|
46
|
+
expect(errors.some(e => e.includes('description'))).toBe(true);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('missing execute reports error', () => {
|
|
50
|
+
const mod = {
|
|
51
|
+
inputSchema: Type.Object({}),
|
|
52
|
+
outputSchema: Type.Object({}),
|
|
53
|
+
description: 'test',
|
|
54
|
+
};
|
|
55
|
+
const errors = validateModule(mod);
|
|
56
|
+
expect(errors.length).toBeGreaterThan(0);
|
|
57
|
+
expect(errors.some(e => e.includes('execute'))).toBe(true);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('completely empty object reports multiple errors', () => {
|
|
61
|
+
const errors = validateModule({});
|
|
62
|
+
expect(errors.length).toBe(4);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('non-function execute reports error', () => {
|
|
66
|
+
const mod = {
|
|
67
|
+
inputSchema: Type.Object({}),
|
|
68
|
+
outputSchema: Type.Object({}),
|
|
69
|
+
description: 'test',
|
|
70
|
+
execute: 'not-a-function',
|
|
71
|
+
};
|
|
72
|
+
const errors = validateModule(mod);
|
|
73
|
+
expect(errors.some(e => e.includes('execute'))).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
});
|