apcore-js 0.4.0 → 0.6.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/dist/acl.d.ts +27 -0
- package/dist/acl.d.ts.map +1 -0
- package/dist/acl.js +175 -0
- package/dist/acl.js.map +1 -0
- package/dist/async-task.d.ts +90 -0
- package/dist/async-task.d.ts.map +1 -0
- package/dist/async-task.js +215 -0
- package/dist/async-task.js.map +1 -0
- package/dist/bindings.d.ts +12 -0
- package/dist/bindings.d.ts.map +1 -0
- package/dist/bindings.js +185 -0
- package/dist/bindings.js.map +1 -0
- package/dist/cancel.d.ts +14 -0
- package/dist/cancel.d.ts.map +1 -0
- package/dist/cancel.js +27 -0
- package/dist/cancel.js.map +1 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +23 -0
- package/dist/config.js.map +1 -0
- package/dist/context.d.ts +50 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +87 -0
- package/dist/context.js.map +1 -0
- package/dist/decorator.d.ts +57 -0
- package/dist/decorator.d.ts.map +1 -0
- package/dist/decorator.js +74 -0
- package/dist/decorator.js.map +1 -0
- package/dist/errors.d.ts +215 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +246 -0
- package/dist/errors.js.map +1 -0
- package/dist/executor.d.ts +67 -0
- package/dist/executor.d.ts.map +1 -0
- package/dist/executor.js +372 -0
- package/dist/executor.js.map +1 -0
- package/dist/extensions.d.ts +58 -0
- package/dist/extensions.d.ts.map +1 -0
- package/dist/extensions.js +220 -0
- package/dist/extensions.js.map +1 -0
- package/{src/index.ts → dist/index.d.ts} +14 -59
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware/adapters.d.ts +18 -0
- package/dist/middleware/adapters.d.ts.map +1 -0
- package/dist/middleware/adapters.js +25 -0
- package/dist/middleware/adapters.js.map +1 -0
- package/dist/middleware/base.d.ts +10 -0
- package/dist/middleware/base.d.ts.map +1 -0
- package/dist/middleware/base.js +15 -0
- package/dist/middleware/base.js.map +1 -0
- package/{src/middleware/index.ts → dist/middleware/index.d.ts} +1 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/middleware/index.js +5 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/middleware/logging.d.ts +25 -0
- package/dist/middleware/logging.d.ts.map +1 -0
- package/dist/middleware/logging.js +64 -0
- package/dist/middleware/logging.js.map +1 -0
- package/dist/middleware/manager.d.ts +21 -0
- package/dist/middleware/manager.d.ts.map +1 -0
- package/dist/middleware/manager.js +77 -0
- package/dist/middleware/manager.js.map +1 -0
- package/dist/module.d.ts +31 -0
- package/dist/module.d.ts.map +1 -0
- package/dist/module.js +12 -0
- package/dist/module.js.map +1 -0
- package/dist/observability/context-logger.d.ts +54 -0
- package/dist/observability/context-logger.d.ts.map +1 -0
- package/dist/observability/context-logger.js +151 -0
- package/dist/observability/context-logger.js.map +1 -0
- package/{src/observability/index.ts → dist/observability/index.d.ts} +1 -0
- package/dist/observability/index.d.ts.map +1 -0
- package/dist/observability/index.js +4 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/observability/metrics.d.ts +30 -0
- package/dist/observability/metrics.d.ts.map +1 -0
- package/dist/observability/metrics.js +177 -0
- package/dist/observability/metrics.js.map +1 -0
- package/dist/observability/tracing.d.ts +62 -0
- package/dist/observability/tracing.d.ts.map +1 -0
- package/dist/observability/tracing.js +184 -0
- package/dist/observability/tracing.js.map +1 -0
- package/dist/registry/dependencies.d.ts +6 -0
- package/dist/registry/dependencies.d.ts.map +1 -0
- package/dist/registry/dependencies.js +83 -0
- package/dist/registry/dependencies.js.map +1 -0
- package/dist/registry/entry-point.d.ts +6 -0
- package/dist/registry/entry-point.d.ts.map +1 -0
- package/dist/registry/entry-point.js +55 -0
- package/dist/registry/entry-point.js.map +1 -0
- package/{src/registry/index.ts → dist/registry/index.d.ts} +2 -0
- package/dist/registry/index.d.ts.map +1 -0
- package/dist/registry/index.js +8 -0
- package/dist/registry/index.js.map +1 -0
- package/dist/registry/metadata.d.ts +9 -0
- package/dist/registry/metadata.d.ts.map +1 -0
- package/dist/registry/metadata.js +105 -0
- package/dist/registry/metadata.js.map +1 -0
- package/dist/registry/registry.d.ts +102 -0
- package/dist/registry/registry.d.ts.map +1 -0
- package/dist/registry/registry.js +534 -0
- package/dist/registry/registry.js.map +1 -0
- package/dist/registry/scanner.d.ts +7 -0
- package/dist/registry/scanner.d.ts.map +1 -0
- package/dist/registry/scanner.js +164 -0
- package/dist/registry/scanner.js.map +1 -0
- package/dist/registry/schema-export.d.ts +9 -0
- package/dist/registry/schema-export.d.ts.map +1 -0
- package/dist/registry/schema-export.js +132 -0
- package/dist/registry/schema-export.js.map +1 -0
- package/dist/registry/types.d.ts +29 -0
- package/dist/registry/types.d.ts.map +1 -0
- package/dist/registry/types.js +5 -0
- package/dist/registry/types.js.map +1 -0
- package/dist/registry/validation.d.ts +9 -0
- package/dist/registry/validation.d.ts.map +1 -0
- package/dist/registry/validation.js +33 -0
- package/dist/registry/validation.js.map +1 -0
- package/dist/schema/annotations.d.ts +8 -0
- package/dist/schema/annotations.d.ts.map +1 -0
- package/dist/schema/annotations.js +52 -0
- package/dist/schema/annotations.js.map +1 -0
- package/dist/schema/exporter.d.ts +13 -0
- package/dist/schema/exporter.d.ts.map +1 -0
- package/dist/schema/exporter.js +71 -0
- package/dist/schema/exporter.js.map +1 -0
- package/dist/schema/index.d.ts +9 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/{src/schema/index.ts → dist/schema/index.js} +1 -7
- package/dist/schema/index.js.map +1 -0
- package/dist/schema/loader.d.ts +30 -0
- package/dist/schema/loader.d.ts.map +1 -0
- package/dist/schema/loader.js +260 -0
- package/dist/schema/loader.js.map +1 -0
- package/dist/schema/ref-resolver.d.ts +19 -0
- package/dist/schema/ref-resolver.d.ts.map +1 -0
- package/dist/schema/ref-resolver.js +212 -0
- package/dist/schema/ref-resolver.js.map +1 -0
- package/dist/schema/strict.d.ts +7 -0
- package/dist/schema/strict.d.ts.map +1 -0
- package/dist/schema/strict.js +127 -0
- package/dist/schema/strict.js.map +1 -0
- package/dist/schema/types.d.ts +53 -0
- package/dist/schema/types.d.ts.map +1 -0
- package/dist/schema/types.js +31 -0
- package/dist/schema/types.js.map +1 -0
- package/dist/schema/validator.d.ts +16 -0
- package/dist/schema/validator.d.ts.map +1 -0
- package/dist/schema/validator.js +71 -0
- package/dist/schema/validator.js.map +1 -0
- package/dist/trace-context.d.ts +35 -0
- package/dist/trace-context.d.ts.map +1 -0
- package/dist/trace-context.js +86 -0
- package/dist/trace-context.js.map +1 -0
- package/dist/utils/index.d.ts +11 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +32 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/pattern.d.ts +5 -0
- package/dist/utils/pattern.d.ts.map +1 -0
- package/dist/utils/pattern.js +31 -0
- package/dist/utils/pattern.js.map +1 -0
- package/package.json +8 -2
- package/.claude/settings.local.json +0 -12
- package/.github/workflows/ci.yml +0 -39
- package/.gitmessage +0 -60
- package/.pre-commit-config.yaml +0 -28
- package/CHANGELOG.md +0 -183
- package/CLAUDE.md +0 -68
- package/apcore-logo.svg +0 -79
- package/planning/acl-system/overview.md +0 -54
- package/planning/acl-system/plan.md +0 -92
- package/planning/acl-system/state.json +0 -76
- package/planning/acl-system/tasks/acl-core.md +0 -226
- package/planning/acl-system/tasks/acl-rule.md +0 -92
- package/planning/acl-system/tasks/conditional-rules.md +0 -259
- package/planning/acl-system/tasks/pattern-matching.md +0 -152
- package/planning/acl-system/tasks/yaml-loading.md +0 -271
- package/planning/core-executor/overview.md +0 -53
- package/planning/core-executor/plan.md +0 -88
- package/planning/core-executor/state.json +0 -76
- package/planning/core-executor/tasks/async-support.md +0 -106
- package/planning/core-executor/tasks/execution-pipeline.md +0 -113
- package/planning/core-executor/tasks/redaction.md +0 -85
- package/planning/core-executor/tasks/safety-checks.md +0 -65
- package/planning/core-executor/tasks/setup.md +0 -75
- package/planning/decorator-bindings/overview.md +0 -62
- package/planning/decorator-bindings/plan.md +0 -104
- package/planning/decorator-bindings/state.json +0 -87
- package/planning/decorator-bindings/tasks/binding-directory.md +0 -79
- package/planning/decorator-bindings/tasks/binding-loader.md +0 -148
- package/planning/decorator-bindings/tasks/explicit-schemas.md +0 -85
- package/planning/decorator-bindings/tasks/function-module.md +0 -127
- package/planning/decorator-bindings/tasks/module-factory.md +0 -89
- package/planning/decorator-bindings/tasks/schema-modes.md +0 -142
- package/planning/middleware-system/overview.md +0 -48
- package/planning/middleware-system/plan.md +0 -102
- package/planning/middleware-system/state.json +0 -65
- package/planning/middleware-system/tasks/adapters.md +0 -170
- package/planning/middleware-system/tasks/base.md +0 -115
- package/planning/middleware-system/tasks/logging-middleware.md +0 -304
- package/planning/middleware-system/tasks/manager.md +0 -313
- package/planning/observability/overview.md +0 -53
- package/planning/observability/plan.md +0 -119
- package/planning/observability/state.json +0 -98
- package/planning/observability/tasks/context-logger.md +0 -201
- package/planning/observability/tasks/exporters.md +0 -121
- package/planning/observability/tasks/metrics-collector.md +0 -162
- package/planning/observability/tasks/metrics-middleware.md +0 -141
- package/planning/observability/tasks/obs-logging-middleware.md +0 -179
- package/planning/observability/tasks/span-model.md +0 -120
- package/planning/observability/tasks/tracing-middleware.md +0 -179
- package/planning/overview.md +0 -81
- package/planning/registry-system/overview.md +0 -57
- package/planning/registry-system/plan.md +0 -114
- package/planning/registry-system/state.json +0 -109
- package/planning/registry-system/tasks/dependencies.md +0 -157
- package/planning/registry-system/tasks/entry-point.md +0 -148
- package/planning/registry-system/tasks/metadata.md +0 -198
- package/planning/registry-system/tasks/registry-core.md +0 -323
- package/planning/registry-system/tasks/scanner.md +0 -172
- package/planning/registry-system/tasks/schema-export.md +0 -261
- package/planning/registry-system/tasks/types.md +0 -124
- package/planning/registry-system/tasks/validation.md +0 -177
- package/planning/schema-system/overview.md +0 -56
- package/planning/schema-system/plan.md +0 -121
- package/planning/schema-system/state.json +0 -98
- package/planning/schema-system/tasks/exporter.md +0 -153
- package/planning/schema-system/tasks/loader.md +0 -106
- package/planning/schema-system/tasks/ref-resolver.md +0 -133
- package/planning/schema-system/tasks/strict-mode.md +0 -140
- package/planning/schema-system/tasks/typebox-generation.md +0 -133
- package/planning/schema-system/tasks/types-and-annotations.md +0 -160
- package/planning/schema-system/tasks/validator.md +0 -149
- package/src/acl.ts +0 -200
- package/src/bindings.ts +0 -207
- package/src/config.ts +0 -24
- package/src/context.ts +0 -78
- package/src/decorator.ts +0 -110
- package/src/errors.ts +0 -425
- package/src/executor.ts +0 -475
- package/src/middleware/adapters.ts +0 -54
- package/src/middleware/base.ts +0 -33
- package/src/middleware/logging.ts +0 -103
- package/src/middleware/manager.ts +0 -105
- package/src/module.ts +0 -43
- package/src/observability/context-logger.ts +0 -203
- package/src/observability/metrics.ts +0 -214
- package/src/observability/tracing.ts +0 -188
- package/src/registry/dependencies.ts +0 -99
- package/src/registry/entry-point.ts +0 -64
- package/src/registry/metadata.ts +0 -111
- package/src/registry/registry.ts +0 -360
- package/src/registry/scanner.ts +0 -168
- package/src/registry/schema-export.ts +0 -181
- package/src/registry/types.ts +0 -32
- package/src/registry/validation.ts +0 -38
- package/src/schema/annotations.ts +0 -68
- package/src/schema/exporter.ts +0 -90
- package/src/schema/loader.ts +0 -273
- package/src/schema/ref-resolver.ts +0 -244
- package/src/schema/strict.ts +0 -136
- package/src/schema/types.ts +0 -73
- package/src/schema/validator.ts +0 -82
- package/src/utils/index.ts +0 -5
- package/src/utils/pattern.ts +0 -30
- package/tests/helpers.ts +0 -30
- package/tests/integration/test-acl-safety.test.ts +0 -269
- package/tests/integration/test-binding-executor.test.ts +0 -194
- package/tests/integration/test-e2e-flow.test.ts +0 -117
- package/tests/integration/test-error-propagation.test.ts +0 -259
- package/tests/integration/test-middleware-chain.test.ts +0 -120
- package/tests/integration/test-observability-integration.test.ts +0 -438
- package/tests/observability/test-context-logger.test.ts +0 -123
- package/tests/observability/test-metrics.test.ts +0 -186
- package/tests/observability/test-tracing.test.ts +0 -131
- package/tests/registry/test-dependencies.test.ts +0 -70
- package/tests/registry/test-entry-point.test.ts +0 -133
- package/tests/registry/test-metadata.test.ts +0 -265
- package/tests/registry/test-registry.test.ts +0 -1008
- package/tests/registry/test-scanner.test.ts +0 -257
- package/tests/registry/test-schema-export.test.ts +0 -355
- package/tests/registry/test-validation.test.ts +0 -75
- package/tests/schema/test-annotations.test.ts +0 -137
- package/tests/schema/test-exporter.test.ts +0 -172
- package/tests/schema/test-loader.test.ts +0 -461
- package/tests/schema/test-ref-resolver.test.ts +0 -530
- package/tests/schema/test-strict.test.ts +0 -348
- package/tests/schema/test-validator.test.ts +0 -64
- package/tests/test-acl.test.ts +0 -423
- package/tests/test-bindings.test.ts +0 -227
- package/tests/test-config.test.ts +0 -76
- package/tests/test-context.test.ts +0 -151
- package/tests/test-decorator.test.ts +0 -173
- package/tests/test-errors.test.ts +0 -647
- package/tests/test-executor-stream.test.ts +0 -208
- package/tests/test-executor.test.ts +0 -252
- package/tests/test-logging-middleware.test.ts +0 -150
- package/tests/test-middleware-manager.test.ts +0 -185
- package/tests/test-middleware.test.ts +0 -86
- package/tests/utils/test-pattern.test.ts +0 -109
- package/tsconfig.build.json +0 -8
- package/tsconfig.json +0 -20
- package/vitest.config.ts +0 -18
|
@@ -1,208 +0,0 @@
|
|
|
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 { ModuleNotFoundError } from '../src/errors.js';
|
|
8
|
-
|
|
9
|
-
function createSimpleModule(id: string): FunctionModule {
|
|
10
|
-
return new FunctionModule({
|
|
11
|
-
execute: (inputs) => ({ greeting: `Hello, ${inputs['name'] ?? 'world'}!` }),
|
|
12
|
-
moduleId: id,
|
|
13
|
-
inputSchema: Type.Object({ name: Type.Optional(Type.String()) }),
|
|
14
|
-
outputSchema: Type.Object({ greeting: Type.String() }),
|
|
15
|
-
description: 'Greet module',
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Creates a module with a stream() async generator that yields chunks.
|
|
21
|
-
*/
|
|
22
|
-
function createStreamingModule(id: string): FunctionModule & { stream: (inputs: Record<string, unknown>) => AsyncGenerator<Record<string, unknown>> } {
|
|
23
|
-
const mod = new FunctionModule({
|
|
24
|
-
execute: (inputs) => ({ greeting: `Hello, ${inputs['name'] ?? 'world'}!` }),
|
|
25
|
-
moduleId: id,
|
|
26
|
-
inputSchema: Type.Object({ name: Type.Optional(Type.String()) }),
|
|
27
|
-
outputSchema: Type.Object({ greeting: Type.String() }),
|
|
28
|
-
description: 'Streaming greet module',
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
// Attach a stream method to the module
|
|
32
|
-
const streamingMod = mod as FunctionModule & { stream: (inputs: Record<string, unknown>) => AsyncGenerator<Record<string, unknown>> };
|
|
33
|
-
streamingMod.stream = async function* (inputs: Record<string, unknown>): AsyncGenerator<Record<string, unknown>> {
|
|
34
|
-
const name = (inputs['name'] as string) ?? 'world';
|
|
35
|
-
yield { greeting: `Hello, ` };
|
|
36
|
-
yield { greeting: `${name}` };
|
|
37
|
-
yield { greeting: `!` };
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
return streamingMod;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async function collectChunks(gen: AsyncGenerator<Record<string, unknown>>): Promise<Record<string, unknown>[]> {
|
|
44
|
-
const chunks: Record<string, unknown>[] = [];
|
|
45
|
-
for await (const chunk of gen) {
|
|
46
|
-
chunks.push(chunk);
|
|
47
|
-
}
|
|
48
|
-
return chunks;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
describe('Executor.stream()', () => {
|
|
52
|
-
it('falls back to single chunk when module has no stream()', async () => {
|
|
53
|
-
const registry = new Registry();
|
|
54
|
-
const mod = createSimpleModule('greet');
|
|
55
|
-
registry.register('greet', mod);
|
|
56
|
-
|
|
57
|
-
const executor = new Executor({ registry });
|
|
58
|
-
const chunks = await collectChunks(executor.stream('greet', { name: 'Alice' }));
|
|
59
|
-
|
|
60
|
-
expect(chunks).toHaveLength(1);
|
|
61
|
-
expect(chunks[0]['greeting']).toBe('Hello, Alice!');
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('yields multiple chunks from streaming module', async () => {
|
|
65
|
-
const registry = new Registry();
|
|
66
|
-
const mod = createStreamingModule('greet');
|
|
67
|
-
registry.register('greet', mod);
|
|
68
|
-
|
|
69
|
-
const executor = new Executor({ registry });
|
|
70
|
-
const chunks = await collectChunks(executor.stream('greet', { name: 'Bob' }));
|
|
71
|
-
|
|
72
|
-
expect(chunks.length).toBeGreaterThan(1);
|
|
73
|
-
expect(chunks[0]['greeting']).toBe('Hello, ');
|
|
74
|
-
expect(chunks[1]['greeting']).toBe('Bob');
|
|
75
|
-
expect(chunks[2]['greeting']).toBe('!');
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('throws ModuleNotFoundError for unknown module', async () => {
|
|
79
|
-
const registry = new Registry();
|
|
80
|
-
const executor = new Executor({ registry });
|
|
81
|
-
|
|
82
|
-
const chunks: Record<string, unknown>[] = [];
|
|
83
|
-
await expect(async () => {
|
|
84
|
-
for await (const chunk of executor.stream('nonexistent')) {
|
|
85
|
-
chunks.push(chunk);
|
|
86
|
-
}
|
|
87
|
-
}).rejects.toThrow(ModuleNotFoundError);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('runs before-middleware before streaming and after-middleware on accumulated result', async () => {
|
|
91
|
-
const registry = new Registry();
|
|
92
|
-
const mod = createStreamingModule('echo');
|
|
93
|
-
registry.register('echo', mod);
|
|
94
|
-
|
|
95
|
-
const calls: string[] = [];
|
|
96
|
-
class TrackingMiddleware extends Middleware {
|
|
97
|
-
override before() { calls.push('before'); return null; }
|
|
98
|
-
override after() { calls.push('after'); return null; }
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const executor = new Executor({ registry, middlewares: [new TrackingMiddleware()] });
|
|
102
|
-
const chunks = await collectChunks(executor.stream('echo', { name: 'Test' }));
|
|
103
|
-
|
|
104
|
-
expect(chunks.length).toBeGreaterThan(0);
|
|
105
|
-
expect(calls).toContain('before');
|
|
106
|
-
expect(calls).toContain('after');
|
|
107
|
-
// before must come first
|
|
108
|
-
expect(calls.indexOf('before')).toBeLessThan(calls.indexOf('after'));
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it('runs before-middleware before fallback and after-middleware on result', async () => {
|
|
112
|
-
const registry = new Registry();
|
|
113
|
-
const mod = createSimpleModule('echo');
|
|
114
|
-
registry.register('echo', mod);
|
|
115
|
-
|
|
116
|
-
const calls: string[] = [];
|
|
117
|
-
class TrackingMiddleware extends Middleware {
|
|
118
|
-
override before() { calls.push('before'); return null; }
|
|
119
|
-
override after() { calls.push('after'); return null; }
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const executor = new Executor({ registry, middlewares: [new TrackingMiddleware()] });
|
|
123
|
-
const chunks = await collectChunks(executor.stream('echo', { name: 'Test' }));
|
|
124
|
-
|
|
125
|
-
expect(chunks).toHaveLength(1);
|
|
126
|
-
expect(calls).toEqual(['before', 'after']);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it('handles middleware error recovery in streaming mode', async () => {
|
|
130
|
-
const registry = new Registry();
|
|
131
|
-
const failMod = new FunctionModule({
|
|
132
|
-
execute: () => { throw new Error('stream-boom'); },
|
|
133
|
-
moduleId: 'fail',
|
|
134
|
-
inputSchema: Type.Object({}),
|
|
135
|
-
outputSchema: Type.Object({}),
|
|
136
|
-
description: 'Failing module',
|
|
137
|
-
});
|
|
138
|
-
registry.register('fail', failMod);
|
|
139
|
-
|
|
140
|
-
class RecoveryMiddleware extends Middleware {
|
|
141
|
-
override onError() { return { recovered: true }; }
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const executor = new Executor({ registry, middlewares: [new RecoveryMiddleware()] });
|
|
145
|
-
// Non-streaming module fallback with error should recover
|
|
146
|
-
const chunks = await collectChunks(executor.stream('fail'));
|
|
147
|
-
expect(chunks).toHaveLength(1);
|
|
148
|
-
expect(chunks[0]['recovered']).toBe(true);
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
it('accumulates chunks via shallow merge for after-middleware', async () => {
|
|
152
|
-
const registry = new Registry();
|
|
153
|
-
const mod = {
|
|
154
|
-
description: 'multi-key streaming module',
|
|
155
|
-
inputSchema: Type.Object({ prefix: Type.String() }),
|
|
156
|
-
outputSchema: Type.Object({ a: Type.Optional(Type.String()), b: Type.Optional(Type.String()) }),
|
|
157
|
-
execute: async (inputs: Record<string, unknown>) => ({ a: `${inputs['prefix']}_a`, b: `${inputs['prefix']}_b` }),
|
|
158
|
-
async *stream(inputs: Record<string, unknown>) {
|
|
159
|
-
yield { a: `${inputs['prefix']}_a` };
|
|
160
|
-
yield { b: `${inputs['prefix']}_b` };
|
|
161
|
-
},
|
|
162
|
-
};
|
|
163
|
-
registry.register('multi', mod);
|
|
164
|
-
|
|
165
|
-
let afterOutput: Record<string, unknown> | null = null;
|
|
166
|
-
const executor = new Executor({ registry });
|
|
167
|
-
executor.useAfter((_mid, _inputs, output) => {
|
|
168
|
-
afterOutput = { ...output };
|
|
169
|
-
return null;
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
const chunks: Record<string, unknown>[] = [];
|
|
173
|
-
for await (const chunk of executor.stream('multi', { prefix: 'test' })) {
|
|
174
|
-
chunks.push(chunk);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
expect(chunks).toHaveLength(2);
|
|
178
|
-
expect(chunks[0]).toEqual({ a: 'test_a' });
|
|
179
|
-
expect(chunks[1]).toEqual({ b: 'test_b' });
|
|
180
|
-
// After-middleware should receive the MERGED result
|
|
181
|
-
expect(afterOutput).toEqual({ a: 'test_a', b: 'test_b' });
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
it('validates output schema on accumulated streaming result', async () => {
|
|
185
|
-
const registry = new Registry();
|
|
186
|
-
const mod = new FunctionModule({
|
|
187
|
-
execute: () => ({ greeting: 'fallback' }),
|
|
188
|
-
moduleId: 'validated',
|
|
189
|
-
inputSchema: Type.Object({}),
|
|
190
|
-
outputSchema: Type.Object({ greeting: Type.String() }),
|
|
191
|
-
description: 'Validated module',
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
// Attach a stream that produces valid accumulated output
|
|
195
|
-
const streamingMod = mod as FunctionModule & { stream: (inputs: Record<string, unknown>) => AsyncGenerator<Record<string, unknown>> };
|
|
196
|
-
streamingMod.stream = async function* (): AsyncGenerator<Record<string, unknown>> {
|
|
197
|
-
yield { greeting: 'chunk1' };
|
|
198
|
-
yield { greeting: 'chunk2' };
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
registry.register('validated', streamingMod);
|
|
202
|
-
|
|
203
|
-
const executor = new Executor({ registry });
|
|
204
|
-
// The last chunk is used as the accumulated output for validation
|
|
205
|
-
const chunks = await collectChunks(executor.stream('validated'));
|
|
206
|
-
expect(chunks).toHaveLength(2);
|
|
207
|
-
});
|
|
208
|
-
});
|
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { Type } from '@sinclair/typebox';
|
|
3
|
-
import { Context, createIdentity } from '../src/context.js';
|
|
4
|
-
import { Executor, redactSensitive, REDACTED_VALUE } from '../src/executor.js';
|
|
5
|
-
import { FunctionModule } from '../src/decorator.js';
|
|
6
|
-
import { Registry } from '../src/registry/registry.js';
|
|
7
|
-
import { ACL } from '../src/acl.js';
|
|
8
|
-
import { Middleware } from '../src/middleware/base.js';
|
|
9
|
-
import {
|
|
10
|
-
ModuleNotFoundError,
|
|
11
|
-
ACLDeniedError,
|
|
12
|
-
CallDepthExceededError,
|
|
13
|
-
CircularCallError,
|
|
14
|
-
CallFrequencyExceededError,
|
|
15
|
-
SchemaValidationError,
|
|
16
|
-
} from '../src/errors.js';
|
|
17
|
-
|
|
18
|
-
function createSimpleModule(id: string): FunctionModule {
|
|
19
|
-
return new FunctionModule({
|
|
20
|
-
execute: (inputs) => ({ greeting: `Hello, ${inputs['name'] ?? 'world'}!` }),
|
|
21
|
-
moduleId: id,
|
|
22
|
-
inputSchema: Type.Object({ name: Type.Optional(Type.String()) }),
|
|
23
|
-
outputSchema: Type.Object({ greeting: Type.String() }),
|
|
24
|
-
description: 'Greet module',
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
describe('redactSensitive', () => {
|
|
29
|
-
it('redacts fields marked x-sensitive', () => {
|
|
30
|
-
const data = { name: 'Alice', password: 'secret123' };
|
|
31
|
-
const schema = {
|
|
32
|
-
properties: {
|
|
33
|
-
name: { type: 'string' },
|
|
34
|
-
password: { type: 'string', 'x-sensitive': true },
|
|
35
|
-
},
|
|
36
|
-
};
|
|
37
|
-
const result = redactSensitive(data, schema);
|
|
38
|
-
expect(result['name']).toBe('Alice');
|
|
39
|
-
expect(result['password']).toBe(REDACTED_VALUE);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('redacts _secret_ prefix keys', () => {
|
|
43
|
-
const data = { _secret_token: 'abc123', name: 'Bob' };
|
|
44
|
-
const schema = { properties: { name: { type: 'string' } } };
|
|
45
|
-
const result = redactSensitive(data, schema);
|
|
46
|
-
expect(result['_secret_token']).toBe(REDACTED_VALUE);
|
|
47
|
-
expect(result['name']).toBe('Bob');
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('does not modify original data', () => {
|
|
51
|
-
const data = { password: 'secret' };
|
|
52
|
-
const schema = { properties: { password: { type: 'string', 'x-sensitive': true } } };
|
|
53
|
-
redactSensitive(data, schema);
|
|
54
|
-
expect(data['password']).toBe('secret');
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('handles nested objects', () => {
|
|
58
|
-
const data = { user: { name: 'Alice', token: 'abc' } };
|
|
59
|
-
const schema = {
|
|
60
|
-
properties: {
|
|
61
|
-
user: {
|
|
62
|
-
type: 'object',
|
|
63
|
-
properties: {
|
|
64
|
-
name: { type: 'string' },
|
|
65
|
-
token: { type: 'string', 'x-sensitive': true },
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
};
|
|
70
|
-
const result = redactSensitive(data, schema);
|
|
71
|
-
expect((result['user'] as Record<string, unknown>)['name']).toBe('Alice');
|
|
72
|
-
expect((result['user'] as Record<string, unknown>)['token']).toBe(REDACTED_VALUE);
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
describe('Executor', () => {
|
|
77
|
-
it('executes a simple module', async () => {
|
|
78
|
-
const registry = new Registry();
|
|
79
|
-
const mod = createSimpleModule('greet');
|
|
80
|
-
registry.register('greet', mod);
|
|
81
|
-
|
|
82
|
-
const executor = new Executor({ registry });
|
|
83
|
-
const result = await executor.call('greet', { name: 'Alice' });
|
|
84
|
-
expect(result['greeting']).toBe('Hello, Alice!');
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('throws ModuleNotFoundError for unknown module', async () => {
|
|
88
|
-
const registry = new Registry();
|
|
89
|
-
const executor = new Executor({ registry });
|
|
90
|
-
|
|
91
|
-
await expect(executor.call('nonexistent')).rejects.toThrow(ModuleNotFoundError);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('validates input against schema', async () => {
|
|
95
|
-
const registry = new Registry();
|
|
96
|
-
const mod = new FunctionModule({
|
|
97
|
-
execute: (inputs) => ({ result: inputs['count'] }),
|
|
98
|
-
moduleId: 'strict',
|
|
99
|
-
inputSchema: Type.Object({ count: Type.Number() }),
|
|
100
|
-
outputSchema: Type.Object({ result: Type.Number() }),
|
|
101
|
-
description: 'Strict module',
|
|
102
|
-
});
|
|
103
|
-
registry.register('strict', mod);
|
|
104
|
-
|
|
105
|
-
const executor = new Executor({ registry });
|
|
106
|
-
await expect(executor.call('strict', { count: 'not-a-number' })).rejects.toThrow(SchemaValidationError);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('enforces ACL deny', async () => {
|
|
110
|
-
const registry = new Registry();
|
|
111
|
-
registry.register('secret', createSimpleModule('secret'));
|
|
112
|
-
|
|
113
|
-
const acl = new ACL([
|
|
114
|
-
{ callers: ['@external'], targets: ['secret'], effect: 'deny', description: 'deny all' },
|
|
115
|
-
], 'deny');
|
|
116
|
-
|
|
117
|
-
const executor = new Executor({ registry, acl });
|
|
118
|
-
await expect(executor.call('secret')).rejects.toThrow(ACLDeniedError);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it('enforces ACL allow', async () => {
|
|
122
|
-
const registry = new Registry();
|
|
123
|
-
registry.register('public', createSimpleModule('public'));
|
|
124
|
-
|
|
125
|
-
const acl = new ACL([
|
|
126
|
-
{ callers: ['*'], targets: ['*'], effect: 'allow', description: 'allow all' },
|
|
127
|
-
], 'deny');
|
|
128
|
-
|
|
129
|
-
const executor = new Executor({ registry, acl });
|
|
130
|
-
const result = await executor.call('public', { name: 'World' });
|
|
131
|
-
expect(result['greeting']).toBe('Hello, World!');
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
it('calls middleware before and after', async () => {
|
|
135
|
-
const registry = new Registry();
|
|
136
|
-
registry.register('echo', createSimpleModule('echo'));
|
|
137
|
-
|
|
138
|
-
const calls: string[] = [];
|
|
139
|
-
class TrackingMiddleware extends Middleware {
|
|
140
|
-
override before() { calls.push('before'); return null; }
|
|
141
|
-
override after() { calls.push('after'); return null; }
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const executor = new Executor({ registry, middlewares: [new TrackingMiddleware()] });
|
|
145
|
-
await executor.call('echo');
|
|
146
|
-
expect(calls).toEqual(['before', 'after']);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it('runs middleware onError on execution failure', async () => {
|
|
150
|
-
const registry = new Registry();
|
|
151
|
-
const failMod = new FunctionModule({
|
|
152
|
-
execute: () => { throw new Error('boom'); },
|
|
153
|
-
moduleId: 'fail',
|
|
154
|
-
inputSchema: Type.Object({}),
|
|
155
|
-
outputSchema: Type.Object({}),
|
|
156
|
-
description: 'Failing module',
|
|
157
|
-
});
|
|
158
|
-
registry.register('fail', failMod);
|
|
159
|
-
|
|
160
|
-
let errorSeen = false;
|
|
161
|
-
class ErrorTracker extends Middleware {
|
|
162
|
-
override onError() { errorSeen = true; return null; }
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const executor = new Executor({ registry, middlewares: [new ErrorTracker()] });
|
|
166
|
-
await expect(executor.call('fail')).rejects.toThrow('boom');
|
|
167
|
-
expect(errorSeen).toBe(true);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it('middleware onError can recover', async () => {
|
|
171
|
-
const registry = new Registry();
|
|
172
|
-
const failMod = new FunctionModule({
|
|
173
|
-
execute: () => { throw new Error('boom'); },
|
|
174
|
-
moduleId: 'fail',
|
|
175
|
-
inputSchema: Type.Object({}),
|
|
176
|
-
outputSchema: Type.Object({}),
|
|
177
|
-
description: 'Failing module',
|
|
178
|
-
});
|
|
179
|
-
registry.register('fail', failMod);
|
|
180
|
-
|
|
181
|
-
class RecoveryMiddleware extends Middleware {
|
|
182
|
-
override onError() { return { recovered: true }; }
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const executor = new Executor({ registry, middlewares: [new RecoveryMiddleware()] });
|
|
186
|
-
const result = await executor.call('fail');
|
|
187
|
-
expect(result['recovered']).toBe(true);
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
it('validate() checks inputs without executing', () => {
|
|
191
|
-
const registry = new Registry();
|
|
192
|
-
const mod = new FunctionModule({
|
|
193
|
-
execute: () => ({ ok: true }),
|
|
194
|
-
moduleId: 'v',
|
|
195
|
-
inputSchema: Type.Object({ x: Type.Number() }),
|
|
196
|
-
outputSchema: Type.Object({ ok: Type.Boolean() }),
|
|
197
|
-
description: 'Validate test',
|
|
198
|
-
});
|
|
199
|
-
registry.register('v', mod);
|
|
200
|
-
|
|
201
|
-
const executor = new Executor({ registry });
|
|
202
|
-
const valid = executor.validate('v', { x: 42 });
|
|
203
|
-
expect(valid.valid).toBe(true);
|
|
204
|
-
|
|
205
|
-
const invalid = executor.validate('v', { x: 'not-a-number' });
|
|
206
|
-
expect(invalid.valid).toBe(false);
|
|
207
|
-
expect(invalid.errors.length).toBeGreaterThan(0);
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
it('auto-creates context when none provided', async () => {
|
|
211
|
-
const registry = new Registry();
|
|
212
|
-
registry.register('ctx', createSimpleModule('ctx'));
|
|
213
|
-
|
|
214
|
-
const executor = new Executor({ registry });
|
|
215
|
-
const result = await executor.call('ctx');
|
|
216
|
-
expect(result['greeting']).toBeDefined();
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
it('uses provided context', async () => {
|
|
220
|
-
const registry = new Registry();
|
|
221
|
-
let capturedCtx: Context | null = null;
|
|
222
|
-
const mod = new FunctionModule({
|
|
223
|
-
execute: (_inputs, ctx) => { capturedCtx = ctx; return { ok: true }; },
|
|
224
|
-
moduleId: 'ctx_test',
|
|
225
|
-
inputSchema: Type.Object({}),
|
|
226
|
-
outputSchema: Type.Object({ ok: Type.Boolean() }),
|
|
227
|
-
description: 'Context capture',
|
|
228
|
-
});
|
|
229
|
-
registry.register('ctx_test', mod);
|
|
230
|
-
|
|
231
|
-
const executor = new Executor({ registry });
|
|
232
|
-
const ctx = Context.create(executor, createIdentity('user1'));
|
|
233
|
-
await executor.call('ctx_test', {}, ctx);
|
|
234
|
-
|
|
235
|
-
expect(capturedCtx).not.toBeNull();
|
|
236
|
-
expect(capturedCtx!.traceId).toBe(ctx.traceId);
|
|
237
|
-
expect(capturedCtx!.identity?.id).toBe('user1');
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
it('use/remove middleware chaining', () => {
|
|
241
|
-
const registry = new Registry();
|
|
242
|
-
const executor = new Executor({ registry });
|
|
243
|
-
const mw = new Middleware();
|
|
244
|
-
|
|
245
|
-
const result = executor.use(mw);
|
|
246
|
-
expect(result).toBe(executor);
|
|
247
|
-
expect(executor.middlewares).toHaveLength(1);
|
|
248
|
-
|
|
249
|
-
executor.remove(mw);
|
|
250
|
-
expect(executor.middlewares).toHaveLength(0);
|
|
251
|
-
});
|
|
252
|
-
});
|
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for middleware/logging.ts — LoggingMiddleware.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { describe, it, expect, vi } from 'vitest';
|
|
6
|
-
import { LoggingMiddleware } from '../src/middleware/logging.js';
|
|
7
|
-
import type { Logger } from '../src/middleware/logging.js';
|
|
8
|
-
import { Context } from '../src/context.js';
|
|
9
|
-
|
|
10
|
-
function makeContext(moduleId: string = 'test.mod'): Context {
|
|
11
|
-
const ctx = Context.create(null);
|
|
12
|
-
return ctx.child(moduleId);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function makeLogger(): Logger & { infoCalls: Array<[string, unknown]>; errorCalls: Array<[string, unknown]> } {
|
|
16
|
-
const logger = {
|
|
17
|
-
infoCalls: [] as Array<[string, unknown]>,
|
|
18
|
-
errorCalls: [] as Array<[string, unknown]>,
|
|
19
|
-
info(message: string, extra?: Record<string, unknown>) {
|
|
20
|
-
logger.infoCalls.push([message, extra]);
|
|
21
|
-
},
|
|
22
|
-
error(message: string, extra?: Record<string, unknown>) {
|
|
23
|
-
logger.errorCalls.push([message, extra]);
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
return logger;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
describe('LoggingMiddleware', () => {
|
|
30
|
-
describe('before', () => {
|
|
31
|
-
it('logs module start with inputs when logInputs is true', () => {
|
|
32
|
-
const logger = makeLogger();
|
|
33
|
-
const mw = new LoggingMiddleware({ logger, logInputs: true });
|
|
34
|
-
const ctx = makeContext('my.module');
|
|
35
|
-
const result = mw.before('my.module', { key: 'val' }, ctx);
|
|
36
|
-
expect(result).toBeNull();
|
|
37
|
-
expect(logger.infoCalls).toHaveLength(1);
|
|
38
|
-
expect(logger.infoCalls[0][0]).toContain('START my.module');
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('does not log when logInputs is false', () => {
|
|
42
|
-
const logger = makeLogger();
|
|
43
|
-
const mw = new LoggingMiddleware({ logger, logInputs: false });
|
|
44
|
-
const ctx = makeContext();
|
|
45
|
-
mw.before('mod', { key: 'val' }, ctx);
|
|
46
|
-
expect(logger.infoCalls).toHaveLength(0);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('stores start time in context data', () => {
|
|
50
|
-
const logger = makeLogger();
|
|
51
|
-
const mw = new LoggingMiddleware({ logger });
|
|
52
|
-
const ctx = makeContext();
|
|
53
|
-
mw.before('mod', {}, ctx);
|
|
54
|
-
expect(typeof ctx.data['_logging_mw_start']).toBe('number');
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('uses redacted inputs when available', () => {
|
|
58
|
-
const logger = makeLogger();
|
|
59
|
-
const mw = new LoggingMiddleware({ logger, logInputs: true });
|
|
60
|
-
const ctx = makeContext();
|
|
61
|
-
ctx.redactedInputs = { key: '***REDACTED***' };
|
|
62
|
-
mw.before('mod', { key: 'secret' }, ctx);
|
|
63
|
-
const extra = logger.infoCalls[0][1] as Record<string, unknown>;
|
|
64
|
-
expect(extra['inputs']).toEqual({ key: '***REDACTED***' });
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
describe('after', () => {
|
|
69
|
-
it('logs module end with duration when logOutputs is true', () => {
|
|
70
|
-
const logger = makeLogger();
|
|
71
|
-
const mw = new LoggingMiddleware({ logger, logOutputs: true });
|
|
72
|
-
const ctx = makeContext('my.module');
|
|
73
|
-
ctx.data['_logging_mw_start'] = performance.now() - 100;
|
|
74
|
-
const result = mw.after('my.module', {}, { result: 'ok' }, ctx);
|
|
75
|
-
expect(result).toBeNull();
|
|
76
|
-
expect(logger.infoCalls).toHaveLength(1);
|
|
77
|
-
expect(logger.infoCalls[0][0]).toContain('END my.module');
|
|
78
|
-
expect(logger.infoCalls[0][0]).toMatch(/\d+\.\d+ms/);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('does not log when logOutputs is false', () => {
|
|
82
|
-
const logger = makeLogger();
|
|
83
|
-
const mw = new LoggingMiddleware({ logger, logOutputs: false });
|
|
84
|
-
const ctx = makeContext();
|
|
85
|
-
mw.after('mod', {}, { result: 'ok' }, ctx);
|
|
86
|
-
expect(logger.infoCalls).toHaveLength(0);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
it('handles missing start time gracefully', () => {
|
|
90
|
-
const logger = makeLogger();
|
|
91
|
-
const mw = new LoggingMiddleware({ logger, logOutputs: true });
|
|
92
|
-
const ctx = makeContext();
|
|
93
|
-
mw.after('mod', {}, { result: 'ok' }, ctx);
|
|
94
|
-
expect(logger.infoCalls).toHaveLength(1);
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
describe('onError', () => {
|
|
99
|
-
it('logs error with redacted inputs when logErrors is true', () => {
|
|
100
|
-
const logger = makeLogger();
|
|
101
|
-
const mw = new LoggingMiddleware({ logger, logErrors: true });
|
|
102
|
-
const ctx = makeContext('my.module');
|
|
103
|
-
ctx.redactedInputs = { safe: 'data' };
|
|
104
|
-
const error = new Error('something broke');
|
|
105
|
-
const result = mw.onError('my.module', { secret: 'val' }, error, ctx);
|
|
106
|
-
expect(result).toBeNull();
|
|
107
|
-
expect(logger.errorCalls).toHaveLength(1);
|
|
108
|
-
expect(logger.errorCalls[0][0]).toContain('ERROR my.module');
|
|
109
|
-
const extra = logger.errorCalls[0][1] as Record<string, unknown>;
|
|
110
|
-
expect(extra['inputs']).toEqual({ safe: 'data' });
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('does not log when logErrors is false', () => {
|
|
114
|
-
const logger = makeLogger();
|
|
115
|
-
const mw = new LoggingMiddleware({ logger, logErrors: false });
|
|
116
|
-
const ctx = makeContext();
|
|
117
|
-
mw.onError('mod', {}, new Error('fail'), ctx);
|
|
118
|
-
expect(logger.errorCalls).toHaveLength(0);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it('uses raw inputs when redactedInputs is null', () => {
|
|
122
|
-
const logger = makeLogger();
|
|
123
|
-
const mw = new LoggingMiddleware({ logger, logErrors: true });
|
|
124
|
-
const ctx = makeContext();
|
|
125
|
-
mw.onError('mod', { raw: 'data' }, new Error('fail'), ctx);
|
|
126
|
-
const extra = logger.errorCalls[0][1] as Record<string, unknown>;
|
|
127
|
-
expect(extra['inputs']).toEqual({ raw: 'data' });
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
describe('defaults', () => {
|
|
132
|
-
it('uses default logger when none provided', () => {
|
|
133
|
-
const mw = new LoggingMiddleware();
|
|
134
|
-
const ctx = makeContext();
|
|
135
|
-
// Should not throw
|
|
136
|
-
expect(() => mw.before('mod', {}, ctx)).not.toThrow();
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it('enables all logging by default', () => {
|
|
140
|
-
const logger = makeLogger();
|
|
141
|
-
const mw = new LoggingMiddleware({ logger });
|
|
142
|
-
const ctx = makeContext();
|
|
143
|
-
mw.before('mod', {}, ctx);
|
|
144
|
-
mw.after('mod', {}, { r: 1 }, ctx);
|
|
145
|
-
mw.onError('mod', {}, new Error('e'), ctx);
|
|
146
|
-
expect(logger.infoCalls).toHaveLength(2);
|
|
147
|
-
expect(logger.errorCalls).toHaveLength(1);
|
|
148
|
-
});
|
|
149
|
-
});
|
|
150
|
-
});
|