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,131 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { Context } from '../../src/context.js';
|
|
3
|
+
import {
|
|
4
|
+
createSpan,
|
|
5
|
+
InMemoryExporter,
|
|
6
|
+
StdoutExporter,
|
|
7
|
+
TracingMiddleware,
|
|
8
|
+
} from '../../src/observability/tracing.js';
|
|
9
|
+
|
|
10
|
+
describe('Span', () => {
|
|
11
|
+
it('createSpan creates span with defaults', () => {
|
|
12
|
+
const span = createSpan({
|
|
13
|
+
traceId: 'trace-1',
|
|
14
|
+
name: 'test.span',
|
|
15
|
+
startTime: 100,
|
|
16
|
+
});
|
|
17
|
+
expect(span.traceId).toBe('trace-1');
|
|
18
|
+
expect(span.name).toBe('test.span');
|
|
19
|
+
expect(span.startTime).toBe(100);
|
|
20
|
+
expect(span.spanId).toBeDefined();
|
|
21
|
+
expect(span.parentSpanId).toBeNull();
|
|
22
|
+
expect(span.status).toBe('ok');
|
|
23
|
+
expect(span.events).toEqual([]);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('InMemoryExporter', () => {
|
|
28
|
+
it('collects and retrieves spans', () => {
|
|
29
|
+
const exporter = new InMemoryExporter();
|
|
30
|
+
const span = createSpan({ traceId: 't1', name: 'test', startTime: 0 });
|
|
31
|
+
exporter.export(span);
|
|
32
|
+
expect(exporter.getSpans()).toHaveLength(1);
|
|
33
|
+
expect(exporter.getSpans()[0].traceId).toBe('t1');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('respects max_spans limit', () => {
|
|
37
|
+
const exporter = new InMemoryExporter(3);
|
|
38
|
+
for (let i = 0; i < 5; i++) {
|
|
39
|
+
exporter.export(createSpan({ traceId: `t${i}`, name: 'test', startTime: i }));
|
|
40
|
+
}
|
|
41
|
+
const spans = exporter.getSpans();
|
|
42
|
+
expect(spans).toHaveLength(3);
|
|
43
|
+
expect(spans[0].traceId).toBe('t2');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('clear removes all spans', () => {
|
|
47
|
+
const exporter = new InMemoryExporter();
|
|
48
|
+
exporter.export(createSpan({ traceId: 't1', name: 'test', startTime: 0 }));
|
|
49
|
+
exporter.clear();
|
|
50
|
+
expect(exporter.getSpans()).toHaveLength(0);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe('TracingMiddleware', () => {
|
|
55
|
+
it('creates and exports spans on success', () => {
|
|
56
|
+
const exporter = new InMemoryExporter();
|
|
57
|
+
const mw = new TracingMiddleware(exporter);
|
|
58
|
+
const ctx = Context.create();
|
|
59
|
+
|
|
60
|
+
mw.before('mod.a', {}, ctx);
|
|
61
|
+
mw.after('mod.a', {}, { result: 'ok' }, ctx);
|
|
62
|
+
|
|
63
|
+
const spans = exporter.getSpans();
|
|
64
|
+
expect(spans).toHaveLength(1);
|
|
65
|
+
expect(spans[0].name).toBe('apcore.module.execute');
|
|
66
|
+
expect(spans[0].status).toBe('ok');
|
|
67
|
+
expect(spans[0].attributes['moduleId']).toBe('mod.a');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('creates error spans', () => {
|
|
71
|
+
const exporter = new InMemoryExporter();
|
|
72
|
+
const mw = new TracingMiddleware(exporter);
|
|
73
|
+
const ctx = Context.create();
|
|
74
|
+
|
|
75
|
+
mw.before('mod.err', {}, ctx);
|
|
76
|
+
mw.onError('mod.err', {}, new Error('fail'), ctx);
|
|
77
|
+
|
|
78
|
+
const spans = exporter.getSpans();
|
|
79
|
+
expect(spans).toHaveLength(1);
|
|
80
|
+
expect(spans[0].status).toBe('error');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('supports nested spans with parent chain', () => {
|
|
84
|
+
const exporter = new InMemoryExporter();
|
|
85
|
+
const mw = new TracingMiddleware(exporter);
|
|
86
|
+
const ctx = Context.create();
|
|
87
|
+
|
|
88
|
+
mw.before('mod.outer', {}, ctx);
|
|
89
|
+
mw.before('mod.inner', {}, ctx);
|
|
90
|
+
mw.after('mod.inner', {}, {}, ctx);
|
|
91
|
+
mw.after('mod.outer', {}, {}, ctx);
|
|
92
|
+
|
|
93
|
+
const spans = exporter.getSpans();
|
|
94
|
+
expect(spans).toHaveLength(2);
|
|
95
|
+
// inner span has outer as parent
|
|
96
|
+
expect(spans[0].parentSpanId).toBe(spans[1].spanId);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('off strategy does not export', () => {
|
|
100
|
+
const exporter = new InMemoryExporter();
|
|
101
|
+
const mw = new TracingMiddleware(exporter, 1.0, 'off');
|
|
102
|
+
const ctx = Context.create();
|
|
103
|
+
|
|
104
|
+
mw.before('mod.a', {}, ctx);
|
|
105
|
+
mw.after('mod.a', {}, {}, ctx);
|
|
106
|
+
|
|
107
|
+
expect(exporter.getSpans()).toHaveLength(0);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('error_first exports errors even when not sampled', () => {
|
|
111
|
+
const exporter = new InMemoryExporter();
|
|
112
|
+
const mw = new TracingMiddleware(exporter, 0.0, 'error_first');
|
|
113
|
+
const ctx = Context.create();
|
|
114
|
+
|
|
115
|
+
mw.before('mod.a', {}, ctx);
|
|
116
|
+
mw.onError('mod.a', {}, new Error('fail'), ctx);
|
|
117
|
+
|
|
118
|
+
expect(exporter.getSpans()).toHaveLength(1);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('throws on invalid sampling rate', () => {
|
|
122
|
+
const exporter = new InMemoryExporter();
|
|
123
|
+
expect(() => new TracingMiddleware(exporter, -0.1)).toThrow();
|
|
124
|
+
expect(() => new TracingMiddleware(exporter, 1.5)).toThrow();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('throws on invalid sampling strategy', () => {
|
|
128
|
+
const exporter = new InMemoryExporter();
|
|
129
|
+
expect(() => new TracingMiddleware(exporter, 1.0, 'invalid')).toThrow();
|
|
130
|
+
});
|
|
131
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { resolveDependencies } from '../../src/registry/dependencies.js';
|
|
3
|
+
import { CircularDependencyError, ModuleLoadError } from '../../src/errors.js';
|
|
4
|
+
|
|
5
|
+
describe('resolveDependencies', () => {
|
|
6
|
+
it('returns empty for empty input', () => {
|
|
7
|
+
expect(resolveDependencies([])).toEqual([]);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('returns single module', () => {
|
|
11
|
+
const result = resolveDependencies([['mod.a', []]]);
|
|
12
|
+
expect(result).toEqual(['mod.a']);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('resolves linear dependency chain', () => {
|
|
16
|
+
const modules: Array<[string, Array<{ moduleId: string; optional: boolean; version: string | null }>]> = [
|
|
17
|
+
['mod.b', [{ moduleId: 'mod.a', optional: false, version: null }]],
|
|
18
|
+
['mod.a', []],
|
|
19
|
+
];
|
|
20
|
+
const result = resolveDependencies(modules);
|
|
21
|
+
expect(result.indexOf('mod.a')).toBeLessThan(result.indexOf('mod.b'));
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('resolves diamond dependency', () => {
|
|
25
|
+
const modules: Array<[string, Array<{ moduleId: string; optional: boolean; version: string | null }>]> = [
|
|
26
|
+
['mod.d', [{ moduleId: 'mod.b', optional: false, version: null }, { moduleId: 'mod.c', optional: false, version: null }]],
|
|
27
|
+
['mod.b', [{ moduleId: 'mod.a', optional: false, version: null }]],
|
|
28
|
+
['mod.c', [{ moduleId: 'mod.a', optional: false, version: null }]],
|
|
29
|
+
['mod.a', []],
|
|
30
|
+
];
|
|
31
|
+
const result = resolveDependencies(modules);
|
|
32
|
+
expect(result.indexOf('mod.a')).toBeLessThan(result.indexOf('mod.b'));
|
|
33
|
+
expect(result.indexOf('mod.a')).toBeLessThan(result.indexOf('mod.c'));
|
|
34
|
+
expect(result.indexOf('mod.b')).toBeLessThan(result.indexOf('mod.d'));
|
|
35
|
+
expect(result.indexOf('mod.c')).toBeLessThan(result.indexOf('mod.d'));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('throws CircularDependencyError on cycle', () => {
|
|
39
|
+
const modules: Array<[string, Array<{ moduleId: string; optional: boolean; version: string | null }>]> = [
|
|
40
|
+
['mod.a', [{ moduleId: 'mod.b', optional: false, version: null }]],
|
|
41
|
+
['mod.b', [{ moduleId: 'mod.a', optional: false, version: null }]],
|
|
42
|
+
];
|
|
43
|
+
expect(() => resolveDependencies(modules)).toThrow(CircularDependencyError);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('throws ModuleLoadError for missing required dependency', () => {
|
|
47
|
+
const modules: Array<[string, Array<{ moduleId: string; optional: boolean; version: string | null }>]> = [
|
|
48
|
+
['mod.a', [{ moduleId: 'mod.missing', optional: false, version: null }]],
|
|
49
|
+
];
|
|
50
|
+
expect(() => resolveDependencies(modules)).toThrow(ModuleLoadError);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('skips optional missing dependencies', () => {
|
|
54
|
+
const modules: Array<[string, Array<{ moduleId: string; optional: boolean; version: string | null }>]> = [
|
|
55
|
+
['mod.a', [{ moduleId: 'mod.missing', optional: true, version: null }]],
|
|
56
|
+
];
|
|
57
|
+
const result = resolveDependencies(modules);
|
|
58
|
+
expect(result).toEqual(['mod.a']);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('independent modules in deterministic order', () => {
|
|
62
|
+
const modules: Array<[string, Array<{ moduleId: string; optional: boolean; version: string | null }>]> = [
|
|
63
|
+
['mod.c', []],
|
|
64
|
+
['mod.a', []],
|
|
65
|
+
['mod.b', []],
|
|
66
|
+
];
|
|
67
|
+
const result = resolveDependencies(modules);
|
|
68
|
+
expect(result).toEqual(['mod.a', 'mod.b', 'mod.c']);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { mkdtempSync, writeFileSync, rmSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { pathToFileURL } from 'node:url';
|
|
6
|
+
import { snakeToPascal, resolveEntryPoint } from '../../src/registry/entry-point.js';
|
|
7
|
+
import { ModuleLoadError } from '../../src/errors.js';
|
|
8
|
+
|
|
9
|
+
function validModuleSource(name: string): string {
|
|
10
|
+
return `{
|
|
11
|
+
inputSchema: { type: 'object' },
|
|
12
|
+
outputSchema: { type: 'object' },
|
|
13
|
+
description: '${name} module',
|
|
14
|
+
execute: function() { return {}; },
|
|
15
|
+
}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('snakeToPascal', () => {
|
|
19
|
+
it('returns empty string for empty input', () => {
|
|
20
|
+
expect(snakeToPascal('')).toBe('');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('capitalises a single word', () => {
|
|
24
|
+
expect(snakeToPascal('hello')).toBe('Hello');
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('converts two-word snake_case', () => {
|
|
28
|
+
expect(snakeToPascal('hello_world')).toBe('HelloWorld');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('converts multi-word snake_case', () => {
|
|
32
|
+
expect(snakeToPascal('my_module_name')).toBe('MyModuleName');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('capitalises a word with no underscores', () => {
|
|
36
|
+
expect(snakeToPascal('already')).toBe('Already');
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('resolveEntryPoint', () => {
|
|
41
|
+
let tmpDir: string;
|
|
42
|
+
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
tmpDir = mkdtempSync(join(tmpdir(), 'apcore-entry-test-'));
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterEach(() => {
|
|
48
|
+
rmSync(tmpDir, { recursive: true, force: true });
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
function fileUrl(filePath: string): string {
|
|
52
|
+
return pathToFileURL(filePath).href;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
it('throws ModuleLoadError for a non-existent file path', async () => {
|
|
56
|
+
const badPath = fileUrl(join(tmpDir, 'does_not_exist.js'));
|
|
57
|
+
await expect(resolveEntryPoint(badPath)).rejects.toThrow(ModuleLoadError);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('finds default export that satisfies isModuleClass', async () => {
|
|
61
|
+
const filePath = join(tmpDir, 'default_mod.mjs');
|
|
62
|
+
writeFileSync(filePath, `const mod = ${validModuleSource('Default')};\nexport default mod;\n`);
|
|
63
|
+
const result = await resolveEntryPoint(fileUrl(filePath));
|
|
64
|
+
expect(result).toBeDefined();
|
|
65
|
+
expect((result as Record<string, unknown>)['description']).toBe('Default module');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('finds a single named export that satisfies isModuleClass', async () => {
|
|
69
|
+
const filePath = join(tmpDir, 'named_mod.mjs');
|
|
70
|
+
writeFileSync(filePath, `export const MyModule = ${validModuleSource('Named')};\n`);
|
|
71
|
+
const result = await resolveEntryPoint(fileUrl(filePath));
|
|
72
|
+
expect(result).toBeDefined();
|
|
73
|
+
expect((result as Record<string, unknown>)['description']).toBe('Named module');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('throws ModuleLoadError when no export satisfies isModuleClass', async () => {
|
|
77
|
+
const filePath = join(tmpDir, 'no_valid.mjs');
|
|
78
|
+
writeFileSync(filePath, `export const notAModule = { foo: 'bar' };\nexport const alsoNot = 42;\n`);
|
|
79
|
+
await expect(resolveEntryPoint(fileUrl(filePath))).rejects.toThrow(ModuleLoadError);
|
|
80
|
+
await expect(resolveEntryPoint(fileUrl(filePath))).rejects.toThrow(/No Module subclass found/);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('throws ModuleLoadError when multiple exports satisfy isModuleClass', async () => {
|
|
84
|
+
const filePath = join(tmpDir, 'ambiguous.mjs');
|
|
85
|
+
writeFileSync(filePath, `export const ModuleA = ${validModuleSource('A')};\nexport const ModuleB = ${validModuleSource('B')};\n`);
|
|
86
|
+
await expect(resolveEntryPoint(fileUrl(filePath))).rejects.toThrow(ModuleLoadError);
|
|
87
|
+
await expect(resolveEntryPoint(fileUrl(filePath))).rejects.toThrow(/Ambiguous entry point/);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('uses meta entry_point to find a specific named class', async () => {
|
|
91
|
+
const filePath = join(tmpDir, 'meta_override.mjs');
|
|
92
|
+
writeFileSync(filePath, `export const Alpha = ${validModuleSource('Alpha')};\nexport const Beta = ${validModuleSource('Beta')};\n`);
|
|
93
|
+
const meta = { entry_point: 'some.path:Beta' };
|
|
94
|
+
const result = await resolveEntryPoint(fileUrl(filePath), meta);
|
|
95
|
+
expect((result as Record<string, unknown>)['description']).toBe('Beta module');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('throws ModuleLoadError when meta entry_point class does not exist', async () => {
|
|
99
|
+
const filePath = join(tmpDir, 'meta_missing.mjs');
|
|
100
|
+
writeFileSync(filePath, `export const Alpha = ${validModuleSource('Alpha')};\n`);
|
|
101
|
+
const meta = { entry_point: 'mod:NonExistent' };
|
|
102
|
+
await expect(resolveEntryPoint(fileUrl(filePath), meta)).rejects.toThrow(ModuleLoadError);
|
|
103
|
+
await expect(resolveEntryPoint(fileUrl(filePath), meta)).rejects.toThrow(/Entry point class 'NonExistent' not found/);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('prefers default export over named exports when both are valid', async () => {
|
|
107
|
+
const filePath = join(tmpDir, 'default_priority.mjs');
|
|
108
|
+
writeFileSync(filePath, `const def = ${validModuleSource('DefaultPriority')};\nexport default def;\nexport const Named = ${validModuleSource('NamedSibling')};\n`);
|
|
109
|
+
const result = await resolveEntryPoint(fileUrl(filePath));
|
|
110
|
+
expect((result as Record<string, unknown>)['description']).toBe('DefaultPriority module');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('falls back to auto-infer when meta is null', async () => {
|
|
114
|
+
const filePath = join(tmpDir, 'null_meta.mjs');
|
|
115
|
+
writeFileSync(filePath, `export const OnlyModule = ${validModuleSource('OnlyMeta')};\n`);
|
|
116
|
+
const result = await resolveEntryPoint(fileUrl(filePath), null);
|
|
117
|
+
expect((result as Record<string, unknown>)['description']).toBe('OnlyMeta module');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('ignores exports missing execute function', async () => {
|
|
121
|
+
const filePath = join(tmpDir, 'missing_execute.mjs');
|
|
122
|
+
writeFileSync(filePath, `export const Incomplete = {\n inputSchema: { type: 'object' },\n outputSchema: { type: 'object' },\n description: 'no execute',\n};\nexport const Complete = ${validModuleSource('Complete')};\n`);
|
|
123
|
+
const result = await resolveEntryPoint(fileUrl(filePath));
|
|
124
|
+
expect((result as Record<string, unknown>)['description']).toBe('Complete module');
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('ignores primitive and null exports', async () => {
|
|
128
|
+
const filePath = join(tmpDir, 'primitives.mjs');
|
|
129
|
+
writeFileSync(filePath, `export const aNumber = 42;\nexport const aString = 'hello';\nexport const aNull = null;\nexport const Valid = ${validModuleSource('Valid')};\n`);
|
|
130
|
+
const result = await resolveEntryPoint(fileUrl(filePath));
|
|
131
|
+
expect((result as Record<string, unknown>)['description']).toBe('Valid module');
|
|
132
|
+
});
|
|
133
|
+
});
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { mkdtempSync, writeFileSync, rmSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import {
|
|
6
|
+
loadMetadata,
|
|
7
|
+
parseDependencies,
|
|
8
|
+
mergeModuleMetadata,
|
|
9
|
+
loadIdMap,
|
|
10
|
+
} from '../../src/registry/metadata.js';
|
|
11
|
+
import { ConfigError, ConfigNotFoundError } from '../../src/errors.js';
|
|
12
|
+
|
|
13
|
+
describe('loadMetadata', () => {
|
|
14
|
+
let tmpDir: string;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
tmpDir = mkdtempSync(join(tmpdir(), 'metadata-test-'));
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
rmSync(tmpDir, { recursive: true, force: true });
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('returns empty object for non-existent file', () => {
|
|
25
|
+
const result = loadMetadata(join(tmpDir, 'does_not_exist.yaml'));
|
|
26
|
+
expect(result).toEqual({});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('parses valid YAML and returns record', () => {
|
|
30
|
+
const metaPath = join(tmpDir, '_meta.yaml');
|
|
31
|
+
writeFileSync(metaPath, 'description: hello\nversion: "2.0.0"\ntags:\n - alpha\n - beta\n');
|
|
32
|
+
const result = loadMetadata(metaPath);
|
|
33
|
+
expect(result).toEqual({
|
|
34
|
+
description: 'hello',
|
|
35
|
+
version: '2.0.0',
|
|
36
|
+
tags: ['alpha', 'beta'],
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('returns empty object for null YAML content', () => {
|
|
41
|
+
const metaPath = join(tmpDir, '_meta.yaml');
|
|
42
|
+
writeFileSync(metaPath, '');
|
|
43
|
+
const result = loadMetadata(metaPath);
|
|
44
|
+
expect(result).toEqual({});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('returns empty object for YAML file containing only null', () => {
|
|
48
|
+
const metaPath = join(tmpDir, '_meta.yaml');
|
|
49
|
+
writeFileSync(metaPath, 'null\n');
|
|
50
|
+
const result = loadMetadata(metaPath);
|
|
51
|
+
expect(result).toEqual({});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('throws ConfigError for invalid YAML syntax', () => {
|
|
55
|
+
const metaPath = join(tmpDir, '_meta.yaml');
|
|
56
|
+
writeFileSync(metaPath, ':\n :\n bad: {{{\n');
|
|
57
|
+
expect(() => loadMetadata(metaPath)).toThrow(ConfigError);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('throws ConfigError if YAML content is a list instead of mapping', () => {
|
|
61
|
+
const metaPath = join(tmpDir, '_meta.yaml');
|
|
62
|
+
writeFileSync(metaPath, '- item1\n- item2\n');
|
|
63
|
+
expect(() => loadMetadata(metaPath)).toThrow(ConfigError);
|
|
64
|
+
expect(() => loadMetadata(metaPath)).toThrow(/must be a YAML mapping/);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('parseDependencies', () => {
|
|
69
|
+
it('returns empty array for empty input', () => {
|
|
70
|
+
expect(parseDependencies([])).toEqual([]);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('returns empty array for null/undefined input', () => {
|
|
74
|
+
expect(parseDependencies(null as unknown as Array<Record<string, unknown>>)).toEqual([]);
|
|
75
|
+
expect(parseDependencies(undefined as unknown as Array<Record<string, unknown>>)).toEqual([]);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('parses dependencies with moduleId, version, optional', () => {
|
|
79
|
+
const raw = [
|
|
80
|
+
{ module_id: 'core.auth', version: '1.2.0', optional: true },
|
|
81
|
+
{ module_id: 'core.db', version: '3.0.0', optional: false },
|
|
82
|
+
];
|
|
83
|
+
const result = parseDependencies(raw);
|
|
84
|
+
expect(result).toEqual([
|
|
85
|
+
{ moduleId: 'core.auth', version: '1.2.0', optional: true },
|
|
86
|
+
{ moduleId: 'core.db', version: '3.0.0', optional: false },
|
|
87
|
+
]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('skips entries without module_id', () => {
|
|
91
|
+
const raw = [
|
|
92
|
+
{ module_id: 'valid.module' },
|
|
93
|
+
{ version: '1.0.0' },
|
|
94
|
+
{ optional: true },
|
|
95
|
+
{},
|
|
96
|
+
];
|
|
97
|
+
const result = parseDependencies(raw);
|
|
98
|
+
expect(result).toHaveLength(1);
|
|
99
|
+
expect(result[0].moduleId).toBe('valid.module');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('defaults version to null and optional to false', () => {
|
|
103
|
+
const raw = [{ module_id: 'some.module' }];
|
|
104
|
+
const result = parseDependencies(raw);
|
|
105
|
+
expect(result).toEqual([
|
|
106
|
+
{ moduleId: 'some.module', version: null, optional: false },
|
|
107
|
+
]);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('handles mixed entries with partial fields', () => {
|
|
111
|
+
const raw = [
|
|
112
|
+
{ module_id: 'a', version: '1.0.0' },
|
|
113
|
+
{ module_id: 'b', optional: true },
|
|
114
|
+
{ module_id: 'c' },
|
|
115
|
+
];
|
|
116
|
+
const result = parseDependencies(raw);
|
|
117
|
+
expect(result).toEqual([
|
|
118
|
+
{ moduleId: 'a', version: '1.0.0', optional: false },
|
|
119
|
+
{ moduleId: 'b', version: null, optional: true },
|
|
120
|
+
{ moduleId: 'c', version: null, optional: false },
|
|
121
|
+
]);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe('mergeModuleMetadata', () => {
|
|
126
|
+
it('YAML values win over code values for all top-level fields', () => {
|
|
127
|
+
const moduleObj = {
|
|
128
|
+
description: 'code desc',
|
|
129
|
+
name: 'code-name',
|
|
130
|
+
tags: ['code-tag'],
|
|
131
|
+
version: '1.0.0',
|
|
132
|
+
annotations: { codeAnnot: true },
|
|
133
|
+
examples: [{ code: 'code-example' }],
|
|
134
|
+
documentation: 'code docs',
|
|
135
|
+
metadata: { codeKey: 'codeVal' },
|
|
136
|
+
};
|
|
137
|
+
const meta = {
|
|
138
|
+
description: 'yaml desc',
|
|
139
|
+
name: 'yaml-name',
|
|
140
|
+
tags: ['yaml-tag'],
|
|
141
|
+
version: '2.0.0',
|
|
142
|
+
annotations: { yamlAnnot: true },
|
|
143
|
+
examples: [{ code: 'yaml-example' }],
|
|
144
|
+
documentation: 'yaml docs',
|
|
145
|
+
metadata: { yamlKey: 'yamlVal' },
|
|
146
|
+
};
|
|
147
|
+
const result = mergeModuleMetadata(moduleObj, meta);
|
|
148
|
+
expect(result['description']).toBe('yaml desc');
|
|
149
|
+
expect(result['name']).toBe('yaml-name');
|
|
150
|
+
expect(result['tags']).toEqual(['yaml-tag']);
|
|
151
|
+
expect(result['version']).toBe('2.0.0');
|
|
152
|
+
expect(result['annotations']).toEqual({ yamlAnnot: true });
|
|
153
|
+
expect(result['examples']).toEqual([{ code: 'yaml-example' }]);
|
|
154
|
+
expect(result['documentation']).toBe('yaml docs');
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('code values used as fallback when YAML is empty', () => {
|
|
158
|
+
const moduleObj = {
|
|
159
|
+
description: 'code desc',
|
|
160
|
+
name: 'code-name',
|
|
161
|
+
tags: ['code-tag'],
|
|
162
|
+
version: '1.0.0',
|
|
163
|
+
metadata: { codeKey: 'codeVal' },
|
|
164
|
+
};
|
|
165
|
+
const meta: Record<string, unknown> = {};
|
|
166
|
+
const result = mergeModuleMetadata(moduleObj, meta);
|
|
167
|
+
expect(result['description']).toBe('code desc');
|
|
168
|
+
expect(result['name']).toBe('code-name');
|
|
169
|
+
expect(result['tags']).toEqual(['code-tag']);
|
|
170
|
+
expect(result['version']).toBe('1.0.0');
|
|
171
|
+
expect(result['metadata']).toEqual({ codeKey: 'codeVal' });
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('metadata records are shallow-merged with YAML spread over code', () => {
|
|
175
|
+
const moduleObj = { metadata: { shared: 'from-code', codeOnly: 'value' } };
|
|
176
|
+
const meta = { metadata: { shared: 'from-yaml', yamlOnly: 'value' } };
|
|
177
|
+
const result = mergeModuleMetadata(moduleObj, meta);
|
|
178
|
+
expect(result['metadata']).toEqual({
|
|
179
|
+
shared: 'from-yaml',
|
|
180
|
+
codeOnly: 'value',
|
|
181
|
+
yamlOnly: 'value',
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('default values used when both code and YAML are absent', () => {
|
|
186
|
+
const result = mergeModuleMetadata({}, {});
|
|
187
|
+
expect(result['description']).toBe('');
|
|
188
|
+
expect(result['name']).toBeNull();
|
|
189
|
+
expect(result['tags']).toEqual([]);
|
|
190
|
+
expect(result['version']).toBe('1.0.0');
|
|
191
|
+
expect(result['annotations']).toBeNull();
|
|
192
|
+
expect(result['examples']).toEqual([]);
|
|
193
|
+
expect(result['metadata']).toEqual({});
|
|
194
|
+
expect(result['documentation']).toBeNull();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('YAML empty array for tags overrides code tags', () => {
|
|
198
|
+
const moduleObj = { tags: ['code-tag'] };
|
|
199
|
+
const meta = { tags: [] };
|
|
200
|
+
const result = mergeModuleMetadata(moduleObj, meta);
|
|
201
|
+
expect(result['tags']).toEqual([]);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe('loadIdMap', () => {
|
|
206
|
+
let tmpDir: string;
|
|
207
|
+
|
|
208
|
+
beforeEach(() => {
|
|
209
|
+
tmpDir = mkdtempSync(join(tmpdir(), 'idmap-test-'));
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
afterEach(() => {
|
|
213
|
+
rmSync(tmpDir, { recursive: true, force: true });
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('throws ConfigNotFoundError for non-existent file', () => {
|
|
217
|
+
expect(() => loadIdMap(join(tmpDir, 'nonexistent.yaml'))).toThrow(ConfigNotFoundError);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('throws ConfigError for invalid YAML syntax', () => {
|
|
221
|
+
const idMapPath = join(tmpDir, 'id_map.yaml');
|
|
222
|
+
writeFileSync(idMapPath, ':\n bad: {{{\n');
|
|
223
|
+
expect(() => loadIdMap(idMapPath)).toThrow(ConfigError);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it('throws ConfigError when mappings key is missing', () => {
|
|
227
|
+
const idMapPath = join(tmpDir, 'id_map.yaml');
|
|
228
|
+
writeFileSync(idMapPath, 'some_key: value\n');
|
|
229
|
+
expect(() => loadIdMap(idMapPath)).toThrow(ConfigError);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('throws ConfigError when mappings is not an array', () => {
|
|
233
|
+
const idMapPath = join(tmpDir, 'id_map.yaml');
|
|
234
|
+
writeFileSync(idMapPath, 'mappings:\n key: value\n');
|
|
235
|
+
expect(() => loadIdMap(idMapPath)).toThrow(ConfigError);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('parses valid mappings with file, id, and class fields', () => {
|
|
239
|
+
const idMapPath = join(tmpDir, 'id_map.yaml');
|
|
240
|
+
writeFileSync(
|
|
241
|
+
idMapPath,
|
|
242
|
+
['mappings:', ' - file: module_a.ts', ' id: custom.module.a', ' class: ModuleA', ' - file: module_b.ts', ' id: custom.module.b', ''].join('\n'),
|
|
243
|
+
);
|
|
244
|
+
const result = loadIdMap(idMapPath);
|
|
245
|
+
expect(result['module_a.ts']).toEqual({ id: 'custom.module.a', class: 'ModuleA' });
|
|
246
|
+
expect(result['module_b.ts']).toEqual({ id: 'custom.module.b', class: null });
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('skips entries without file field', () => {
|
|
250
|
+
const idMapPath = join(tmpDir, 'id_map.yaml');
|
|
251
|
+
writeFileSync(
|
|
252
|
+
idMapPath,
|
|
253
|
+
['mappings:', ' - file: valid.ts', ' id: valid.id', ' - id: orphan.id', ''].join('\n'),
|
|
254
|
+
);
|
|
255
|
+
const result = loadIdMap(idMapPath);
|
|
256
|
+
expect(Object.keys(result)).toEqual(['valid.ts']);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('handles empty mappings array', () => {
|
|
260
|
+
const idMapPath = join(tmpDir, 'id_map.yaml');
|
|
261
|
+
writeFileSync(idMapPath, 'mappings: []\n');
|
|
262
|
+
const result = loadIdMap(idMapPath);
|
|
263
|
+
expect(result).toEqual({});
|
|
264
|
+
});
|
|
265
|
+
});
|