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,259 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { Type } from '@sinclair/typebox';
|
|
3
|
+
import { Executor } from '../../src/executor.js';
|
|
4
|
+
import { FunctionModule } from '../../src/decorator.js';
|
|
5
|
+
import { Registry } from '../../src/registry/registry.js';
|
|
6
|
+
import { Middleware } from '../../src/middleware/base.js';
|
|
7
|
+
import { Context, createIdentity } from '../../src/context.js';
|
|
8
|
+
import { InMemoryExporter, TracingMiddleware } from '../../src/observability/tracing.js';
|
|
9
|
+
import { MetricsCollector, MetricsMiddleware } from '../../src/observability/metrics.js';
|
|
10
|
+
import {
|
|
11
|
+
ModuleNotFoundError,
|
|
12
|
+
SchemaValidationError,
|
|
13
|
+
ACLDeniedError,
|
|
14
|
+
} from '../../src/errors.js';
|
|
15
|
+
import { ACL } from '../../src/acl.js';
|
|
16
|
+
|
|
17
|
+
describe('Error Propagation', () => {
|
|
18
|
+
it('ModuleNotFoundError for non-existent module', async () => {
|
|
19
|
+
const registry = new Registry();
|
|
20
|
+
const executor = new Executor({ registry });
|
|
21
|
+
|
|
22
|
+
await expect(executor.call('non.existent')).rejects.toThrow(ModuleNotFoundError);
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
await executor.call('non.existent');
|
|
26
|
+
} catch (error) {
|
|
27
|
+
expect(error).toBeInstanceOf(ModuleNotFoundError);
|
|
28
|
+
expect((error as ModuleNotFoundError).details['moduleId']).toBe('non.existent');
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('SchemaValidationError on invalid input', async () => {
|
|
33
|
+
const registry = new Registry();
|
|
34
|
+
registry.register('validate.input', new FunctionModule({
|
|
35
|
+
execute: (inputs) => ({ result: 'ok' }),
|
|
36
|
+
moduleId: 'validate.input',
|
|
37
|
+
inputSchema: Type.Object({ name: Type.String() }),
|
|
38
|
+
outputSchema: Type.Object({ result: Type.String() }),
|
|
39
|
+
description: 'Input validation test',
|
|
40
|
+
}));
|
|
41
|
+
|
|
42
|
+
const executor = new Executor({ registry });
|
|
43
|
+
|
|
44
|
+
await expect(
|
|
45
|
+
executor.call('validate.input', { name: 123 }),
|
|
46
|
+
).rejects.toThrow(SchemaValidationError);
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
await executor.call('validate.input', { name: 123 });
|
|
50
|
+
} catch (error) {
|
|
51
|
+
expect(error).toBeInstanceOf(SchemaValidationError);
|
|
52
|
+
const details = (error as SchemaValidationError).details;
|
|
53
|
+
const errors = details['errors'] as Array<Record<string, unknown>>;
|
|
54
|
+
expect(errors.length).toBeGreaterThan(0);
|
|
55
|
+
expect(errors[0]).toHaveProperty('field');
|
|
56
|
+
expect(errors[0]).toHaveProperty('message');
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('SchemaValidationError on invalid output', async () => {
|
|
61
|
+
const registry = new Registry();
|
|
62
|
+
registry.register('validate.output', new FunctionModule({
|
|
63
|
+
execute: () => ({ count: 'not_a_number' }),
|
|
64
|
+
moduleId: 'validate.output',
|
|
65
|
+
inputSchema: Type.Object({}),
|
|
66
|
+
outputSchema: Type.Object({ count: Type.Number() }),
|
|
67
|
+
description: 'Output validation test',
|
|
68
|
+
}));
|
|
69
|
+
|
|
70
|
+
const executor = new Executor({ registry });
|
|
71
|
+
|
|
72
|
+
await expect(
|
|
73
|
+
executor.call('validate.output', {}),
|
|
74
|
+
).rejects.toThrow(SchemaValidationError);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('ACLDeniedError with tracing captures error span', async () => {
|
|
78
|
+
const registry = new Registry();
|
|
79
|
+
registry.register('protected', new FunctionModule({
|
|
80
|
+
execute: () => ({ data: 'secret' }),
|
|
81
|
+
moduleId: 'protected',
|
|
82
|
+
inputSchema: Type.Object({}),
|
|
83
|
+
outputSchema: Type.Object({ data: Type.String() }),
|
|
84
|
+
description: 'Protected module',
|
|
85
|
+
}));
|
|
86
|
+
|
|
87
|
+
const acl = new ACL([
|
|
88
|
+
{ callers: ['@external'], targets: ['protected'], effect: 'deny', description: 'block externals' },
|
|
89
|
+
], 'allow');
|
|
90
|
+
|
|
91
|
+
const exporter = new InMemoryExporter();
|
|
92
|
+
const executor = new Executor({
|
|
93
|
+
registry,
|
|
94
|
+
middlewares: [new TracingMiddleware(exporter)],
|
|
95
|
+
acl,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// ACL check happens BEFORE middleware before(), so tracing won't capture it
|
|
99
|
+
// The span is created in before() but ACL check is at step 4 (after middleware before)
|
|
100
|
+
// Actually looking at executor.call: step 4 is ACL, step 6 is middleware before
|
|
101
|
+
// Wait - step 6 is middleware before, but step 4 (ACL) happens before middleware
|
|
102
|
+
// So tracing middleware won't have a span for ACL errors
|
|
103
|
+
// Let's just verify the error is thrown
|
|
104
|
+
await expect(executor.call('protected')).rejects.toThrow(ACLDeniedError);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('middleware onError recovery returns fallback output', async () => {
|
|
108
|
+
const registry = new Registry();
|
|
109
|
+
registry.register('failing', new FunctionModule({
|
|
110
|
+
execute: () => { throw new Error('Module failed'); },
|
|
111
|
+
moduleId: 'failing',
|
|
112
|
+
inputSchema: Type.Object({}),
|
|
113
|
+
outputSchema: Type.Object({ recovered: Type.Boolean() }),
|
|
114
|
+
description: 'Failing module',
|
|
115
|
+
}));
|
|
116
|
+
|
|
117
|
+
class RecoveryMiddleware extends Middleware {
|
|
118
|
+
override onError(
|
|
119
|
+
_moduleId: string,
|
|
120
|
+
_inputs: Record<string, unknown>,
|
|
121
|
+
_error: Error,
|
|
122
|
+
_context: Context,
|
|
123
|
+
): Record<string, unknown> | null {
|
|
124
|
+
return { recovered: true };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const executor = new Executor({ registry, middlewares: [new RecoveryMiddleware()] });
|
|
129
|
+
const result = await executor.call('failing', {});
|
|
130
|
+
expect(result).toEqual({ recovered: true });
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('middleware onError cascade: reverse order, first recovery wins', async () => {
|
|
134
|
+
const registry = new Registry();
|
|
135
|
+
registry.register('failing', new FunctionModule({
|
|
136
|
+
execute: () => { throw new Error('Module failed'); },
|
|
137
|
+
moduleId: 'failing',
|
|
138
|
+
inputSchema: Type.Object({}),
|
|
139
|
+
outputSchema: Type.Object({}),
|
|
140
|
+
description: 'Failing module',
|
|
141
|
+
}));
|
|
142
|
+
|
|
143
|
+
const callOrder: string[] = [];
|
|
144
|
+
|
|
145
|
+
class MW1 extends Middleware {
|
|
146
|
+
override onError(
|
|
147
|
+
_moduleId: string,
|
|
148
|
+
_inputs: Record<string, unknown>,
|
|
149
|
+
_error: Error,
|
|
150
|
+
_context: Context,
|
|
151
|
+
): Record<string, unknown> | null {
|
|
152
|
+
callOrder.push('mw1');
|
|
153
|
+
return { recoveredBy: 'mw1' };
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
class MW2 extends Middleware {
|
|
158
|
+
override onError(
|
|
159
|
+
_moduleId: string,
|
|
160
|
+
_inputs: Record<string, unknown>,
|
|
161
|
+
_error: Error,
|
|
162
|
+
_context: Context,
|
|
163
|
+
): Record<string, unknown> | null {
|
|
164
|
+
callOrder.push('mw2');
|
|
165
|
+
return { recoveredBy: 'mw2' };
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const executor = new Executor({ registry, middlewares: [new MW1(), new MW2()] });
|
|
170
|
+
const result = await executor.call('failing', {});
|
|
171
|
+
|
|
172
|
+
// onError is called in reverse order: MW2 first, then MW1
|
|
173
|
+
// First non-null return wins (MW2)
|
|
174
|
+
expect(callOrder[0]).toBe('mw2');
|
|
175
|
+
expect(result).toEqual({ recoveredBy: 'mw2' });
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('MetricsMiddleware records error metrics', async () => {
|
|
179
|
+
const registry = new Registry();
|
|
180
|
+
registry.register('error.mod', new FunctionModule({
|
|
181
|
+
execute: () => { throw new Error('Test error'); },
|
|
182
|
+
moduleId: 'error.mod',
|
|
183
|
+
inputSchema: Type.Object({}),
|
|
184
|
+
outputSchema: Type.Object({}),
|
|
185
|
+
description: 'Error module',
|
|
186
|
+
}));
|
|
187
|
+
|
|
188
|
+
const metrics = new MetricsCollector();
|
|
189
|
+
const executor = new Executor({ registry, middlewares: [new MetricsMiddleware(metrics)] });
|
|
190
|
+
|
|
191
|
+
await expect(executor.call('error.mod', {})).rejects.toThrow('Test error');
|
|
192
|
+
|
|
193
|
+
const snap = metrics.snapshot();
|
|
194
|
+
const counters = snap['counters'] as Record<string, number>;
|
|
195
|
+
expect(counters['apcore_module_calls_total|module_id=error.mod,status=error']).toBe(1);
|
|
196
|
+
expect(counters['apcore_module_errors_total|error_code=Error,module_id=error.mod']).toBe(1);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('full observability stack captures error metrics and tracing', async () => {
|
|
200
|
+
const registry = new Registry();
|
|
201
|
+
registry.register('obs.error', new FunctionModule({
|
|
202
|
+
execute: () => { throw new Error('Observable error'); },
|
|
203
|
+
moduleId: 'obs.error',
|
|
204
|
+
inputSchema: Type.Object({}),
|
|
205
|
+
outputSchema: Type.Object({}),
|
|
206
|
+
description: 'Observable error module',
|
|
207
|
+
}));
|
|
208
|
+
|
|
209
|
+
const metrics = new MetricsCollector();
|
|
210
|
+
const exporter = new InMemoryExporter();
|
|
211
|
+
const executor = new Executor({
|
|
212
|
+
registry,
|
|
213
|
+
middlewares: [new MetricsMiddleware(metrics), new TracingMiddleware(exporter)],
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
await expect(executor.call('obs.error', {})).rejects.toThrow('Observable error');
|
|
217
|
+
|
|
218
|
+
// Check metrics
|
|
219
|
+
const snap = metrics.snapshot();
|
|
220
|
+
const counters = snap['counters'] as Record<string, number>;
|
|
221
|
+
expect(counters['apcore_module_calls_total|module_id=obs.error,status=error']).toBe(1);
|
|
222
|
+
|
|
223
|
+
// Check tracing
|
|
224
|
+
const spans = exporter.getSpans();
|
|
225
|
+
expect(spans).toHaveLength(1);
|
|
226
|
+
expect(spans[0].status).toBe('error');
|
|
227
|
+
expect(spans[0].attributes['error_code']).toBe('Error');
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('SchemaValidationError includes field path, code, and message', async () => {
|
|
231
|
+
const registry = new Registry();
|
|
232
|
+
registry.register('multi.validate', new FunctionModule({
|
|
233
|
+
execute: (inputs) => ({ result: 'ok' }),
|
|
234
|
+
moduleId: 'multi.validate',
|
|
235
|
+
inputSchema: Type.Object({
|
|
236
|
+
name: Type.String(),
|
|
237
|
+
age: Type.Number(),
|
|
238
|
+
}),
|
|
239
|
+
outputSchema: Type.Object({ result: Type.String() }),
|
|
240
|
+
description: 'Multi-field validation',
|
|
241
|
+
}));
|
|
242
|
+
|
|
243
|
+
const executor = new Executor({ registry });
|
|
244
|
+
|
|
245
|
+
try {
|
|
246
|
+
await executor.call('multi.validate', { name: 123, age: 'not_a_number' });
|
|
247
|
+
expect.unreachable('should have thrown');
|
|
248
|
+
} catch (error) {
|
|
249
|
+
expect(error).toBeInstanceOf(SchemaValidationError);
|
|
250
|
+
const errors = (error as SchemaValidationError).details['errors'] as Array<Record<string, unknown>>;
|
|
251
|
+
expect(errors.length).toBeGreaterThanOrEqual(2);
|
|
252
|
+
for (const err of errors) {
|
|
253
|
+
expect(err).toHaveProperty('field');
|
|
254
|
+
expect(err).toHaveProperty('code');
|
|
255
|
+
expect(err).toHaveProperty('message');
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
});
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { Type } from '@sinclair/typebox';
|
|
3
|
+
import { Context } from '../../src/context.js';
|
|
4
|
+
import { Executor } from '../../src/executor.js';
|
|
5
|
+
import { FunctionModule } from '../../src/decorator.js';
|
|
6
|
+
import { Registry } from '../../src/registry/registry.js';
|
|
7
|
+
import { Middleware } from '../../src/middleware/base.js';
|
|
8
|
+
import { BeforeMiddleware, AfterMiddleware } from '../../src/middleware/adapters.js';
|
|
9
|
+
|
|
10
|
+
function createEchoModule(): FunctionModule {
|
|
11
|
+
return new FunctionModule({
|
|
12
|
+
execute: (inputs) => ({ value: inputs['x'] ?? 'default' }),
|
|
13
|
+
moduleId: 'echo',
|
|
14
|
+
inputSchema: Type.Object({ x: Type.Optional(Type.String()) }),
|
|
15
|
+
outputSchema: Type.Object({ value: Type.String() }),
|
|
16
|
+
description: 'Echo module',
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe('Middleware Chain', () => {
|
|
21
|
+
it('before middlewares run in order, after in reverse', async () => {
|
|
22
|
+
const registry = new Registry();
|
|
23
|
+
registry.register('echo', createEchoModule());
|
|
24
|
+
|
|
25
|
+
const order: string[] = [];
|
|
26
|
+
|
|
27
|
+
class MW1 extends Middleware {
|
|
28
|
+
override before() { order.push('before-1'); return null; }
|
|
29
|
+
override after() { order.push('after-1'); return null; }
|
|
30
|
+
}
|
|
31
|
+
class MW2 extends Middleware {
|
|
32
|
+
override before() { order.push('before-2'); return null; }
|
|
33
|
+
override after() { order.push('after-2'); return null; }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const executor = new Executor({ registry, middlewares: [new MW1(), new MW2()] });
|
|
37
|
+
await executor.call('echo', { x: 'test' });
|
|
38
|
+
|
|
39
|
+
expect(order).toEqual(['before-1', 'before-2', 'after-2', 'after-1']);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('before middleware can transform inputs', async () => {
|
|
43
|
+
const registry = new Registry();
|
|
44
|
+
registry.register('echo', createEchoModule());
|
|
45
|
+
|
|
46
|
+
class InputTransform extends Middleware {
|
|
47
|
+
override before(
|
|
48
|
+
_moduleId: string,
|
|
49
|
+
_inputs: Record<string, unknown>,
|
|
50
|
+
_context: Context,
|
|
51
|
+
): Record<string, unknown> {
|
|
52
|
+
return { x: 'transformed' };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const executor = new Executor({ registry, middlewares: [new InputTransform()] });
|
|
57
|
+
const result = await executor.call('echo', { x: 'original' });
|
|
58
|
+
expect(result['value']).toBe('transformed');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('after middleware can transform output', async () => {
|
|
62
|
+
const registry = new Registry();
|
|
63
|
+
registry.register('echo', createEchoModule());
|
|
64
|
+
|
|
65
|
+
class Transform extends Middleware {
|
|
66
|
+
override after(
|
|
67
|
+
_moduleId: string,
|
|
68
|
+
_inputs: Record<string, unknown>,
|
|
69
|
+
output: Record<string, unknown>,
|
|
70
|
+
): Record<string, unknown> {
|
|
71
|
+
return { value: `transformed-${output['value']}` };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const executor = new Executor({ registry, middlewares: [new Transform()] });
|
|
76
|
+
const result = await executor.call('echo', { x: 'hello' });
|
|
77
|
+
expect(result['value']).toBe('transformed-hello');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('adapter middlewares work', async () => {
|
|
81
|
+
const registry = new Registry();
|
|
82
|
+
registry.register('echo', createEchoModule());
|
|
83
|
+
|
|
84
|
+
const calls: string[] = [];
|
|
85
|
+
const beforeMw = new BeforeMiddleware((_moduleId, _inputs, _ctx) => {
|
|
86
|
+
calls.push('before-adapter');
|
|
87
|
+
return null;
|
|
88
|
+
});
|
|
89
|
+
const afterMw = new AfterMiddleware((_moduleId, _inputs, _output, _ctx) => {
|
|
90
|
+
calls.push('after-adapter');
|
|
91
|
+
return null;
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const executor = new Executor({ registry, middlewares: [beforeMw, afterMw] });
|
|
95
|
+
await executor.call('echo', { x: 'test' });
|
|
96
|
+
|
|
97
|
+
expect(calls).toEqual(['before-adapter', 'after-adapter']);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('use/remove middleware at runtime', async () => {
|
|
101
|
+
const registry = new Registry();
|
|
102
|
+
registry.register('echo', createEchoModule());
|
|
103
|
+
|
|
104
|
+
const calls: string[] = [];
|
|
105
|
+
class Tracker extends Middleware {
|
|
106
|
+
override before() { calls.push('tracked'); return null; }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const executor = new Executor({ registry });
|
|
110
|
+
const mw = new Tracker();
|
|
111
|
+
executor.use(mw);
|
|
112
|
+
|
|
113
|
+
await executor.call('echo', { x: 'a' });
|
|
114
|
+
expect(calls).toEqual(['tracked']);
|
|
115
|
+
|
|
116
|
+
executor.remove(mw);
|
|
117
|
+
await executor.call('echo', { x: 'b' });
|
|
118
|
+
expect(calls).toEqual(['tracked']); // Still just 1 call
|
|
119
|
+
});
|
|
120
|
+
});
|