ts-procedures 5.10.0 → 5.10.2
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/agent_config/bin/postinstall.mjs +3 -3
- package/agent_config/bin/setup.mjs +22 -11
- package/agent_config/claude-code/agents/ts-procedures-architect.md +2 -2
- package/agent_config/claude-code/skills/{guide → ts-procedures}/SKILL.md +1 -1
- package/agent_config/claude-code/skills/{review → ts-procedures-review}/SKILL.md +3 -3
- package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/SKILL.md +2 -2
- package/agent_config/lib/install-claude.mjs +35 -87
- package/build/src/client/call.d.ts +14 -0
- package/build/src/client/call.js +47 -0
- package/build/src/client/call.js.map +1 -0
- package/build/src/client/call.test.d.ts +1 -0
- package/build/src/client/call.test.js +124 -0
- package/build/src/client/call.test.js.map +1 -0
- package/build/src/client/errors.d.ts +25 -0
- package/build/src/client/errors.js +33 -0
- package/build/src/client/errors.js.map +1 -0
- package/build/src/client/errors.test.d.ts +1 -0
- package/build/src/client/errors.test.js +41 -0
- package/build/src/client/errors.test.js.map +1 -0
- package/build/src/client/fetch-adapter.d.ts +12 -0
- package/build/src/client/fetch-adapter.js +156 -0
- package/build/src/client/fetch-adapter.js.map +1 -0
- package/build/src/client/fetch-adapter.test.d.ts +1 -0
- package/build/src/client/fetch-adapter.test.js +271 -0
- package/build/src/client/fetch-adapter.test.js.map +1 -0
- package/build/src/client/hooks.d.ts +17 -0
- package/build/src/client/hooks.js +40 -0
- package/build/src/client/hooks.js.map +1 -0
- package/build/src/client/hooks.test.d.ts +1 -0
- package/build/src/client/hooks.test.js +163 -0
- package/build/src/client/hooks.test.js.map +1 -0
- package/build/src/client/index.d.ts +22 -0
- package/build/src/client/index.js +67 -0
- package/build/src/client/index.js.map +1 -0
- package/build/src/client/index.test.d.ts +1 -0
- package/build/src/client/index.test.js +231 -0
- package/build/src/client/index.test.js.map +1 -0
- package/build/src/client/request-builder.d.ts +13 -0
- package/build/src/client/request-builder.js +53 -0
- package/build/src/client/request-builder.js.map +1 -0
- package/build/src/client/request-builder.test.d.ts +1 -0
- package/build/src/client/request-builder.test.js +160 -0
- package/build/src/client/request-builder.test.js.map +1 -0
- package/build/src/client/stream.d.ts +27 -0
- package/build/src/client/stream.js +118 -0
- package/build/src/client/stream.js.map +1 -0
- package/build/src/client/stream.test.d.ts +1 -0
- package/build/src/client/stream.test.js +228 -0
- package/build/src/client/stream.test.js.map +1 -0
- package/build/src/client/types.d.ts +78 -0
- package/build/src/client/types.js +3 -0
- package/build/src/client/types.js.map +1 -0
- package/build/src/codegen/bin/cli.d.ts +45 -0
- package/build/src/codegen/bin/cli.js +246 -0
- package/build/src/codegen/bin/cli.js.map +1 -0
- package/build/src/codegen/bin/cli.test.d.ts +1 -0
- package/build/src/codegen/bin/cli.test.js +220 -0
- package/build/src/codegen/bin/cli.test.js.map +1 -0
- package/build/src/codegen/constants.d.ts +1 -0
- package/build/src/codegen/constants.js +2 -0
- package/build/src/codegen/constants.js.map +1 -0
- package/build/src/codegen/e2e.test.d.ts +1 -0
- package/build/src/codegen/e2e.test.js +464 -0
- package/build/src/codegen/e2e.test.js.map +1 -0
- package/build/src/codegen/emit-client-runtime.d.ts +9 -0
- package/build/src/codegen/emit-client-runtime.js +99 -0
- package/build/src/codegen/emit-client-runtime.js.map +1 -0
- package/build/src/codegen/emit-client-runtime.test.d.ts +1 -0
- package/build/src/codegen/emit-client-runtime.test.js +78 -0
- package/build/src/codegen/emit-client-runtime.test.js.map +1 -0
- package/build/src/codegen/emit-client-types.d.ts +8 -0
- package/build/src/codegen/emit-client-types.js +25 -0
- package/build/src/codegen/emit-client-types.js.map +1 -0
- package/build/src/codegen/emit-client-types.test.d.ts +1 -0
- package/build/src/codegen/emit-client-types.test.js +33 -0
- package/build/src/codegen/emit-client-types.test.js.map +1 -0
- package/build/src/codegen/emit-errors.d.ts +19 -0
- package/build/src/codegen/emit-errors.js +59 -0
- package/build/src/codegen/emit-errors.js.map +1 -0
- package/build/src/codegen/emit-errors.test.d.ts +1 -0
- package/build/src/codegen/emit-errors.test.js +175 -0
- package/build/src/codegen/emit-errors.test.js.map +1 -0
- package/build/src/codegen/emit-index.d.ts +12 -0
- package/build/src/codegen/emit-index.js +41 -0
- package/build/src/codegen/emit-index.js.map +1 -0
- package/build/src/codegen/emit-index.test.d.ts +1 -0
- package/build/src/codegen/emit-index.test.js +106 -0
- package/build/src/codegen/emit-index.test.js.map +1 -0
- package/build/src/codegen/emit-scope.d.ts +15 -0
- package/build/src/codegen/emit-scope.js +299 -0
- package/build/src/codegen/emit-scope.js.map +1 -0
- package/build/src/codegen/emit-scope.test.d.ts +1 -0
- package/build/src/codegen/emit-scope.test.js +559 -0
- package/build/src/codegen/emit-scope.test.js.map +1 -0
- package/build/src/codegen/emit-types.d.ts +43 -0
- package/build/src/codegen/emit-types.js +111 -0
- package/build/src/codegen/emit-types.js.map +1 -0
- package/build/src/codegen/emit-types.test.d.ts +1 -0
- package/build/src/codegen/emit-types.test.js +184 -0
- package/build/src/codegen/emit-types.test.js.map +1 -0
- package/build/src/codegen/group-routes.d.ts +23 -0
- package/build/src/codegen/group-routes.js +46 -0
- package/build/src/codegen/group-routes.js.map +1 -0
- package/build/src/codegen/group-routes.test.d.ts +1 -0
- package/build/src/codegen/group-routes.test.js +131 -0
- package/build/src/codegen/group-routes.test.js.map +1 -0
- package/build/src/codegen/index.d.ts +15 -0
- package/build/src/codegen/index.js +16 -0
- package/build/src/codegen/index.js.map +1 -0
- package/build/src/codegen/naming.d.ts +7 -0
- package/build/src/codegen/naming.js +21 -0
- package/build/src/codegen/naming.js.map +1 -0
- package/build/src/codegen/naming.test.d.ts +1 -0
- package/build/src/codegen/naming.test.js +40 -0
- package/build/src/codegen/naming.test.js.map +1 -0
- package/build/src/codegen/pipeline.d.ts +17 -0
- package/build/src/codegen/pipeline.js +78 -0
- package/build/src/codegen/pipeline.js.map +1 -0
- package/build/src/codegen/pipeline.test.d.ts +1 -0
- package/build/src/codegen/pipeline.test.js +269 -0
- package/build/src/codegen/pipeline.test.js.map +1 -0
- package/build/src/codegen/resolve-envelope.d.ts +7 -0
- package/build/src/codegen/resolve-envelope.js +46 -0
- package/build/src/codegen/resolve-envelope.js.map +1 -0
- package/build/src/codegen/resolve-envelope.test.d.ts +1 -0
- package/build/src/codegen/resolve-envelope.test.js +69 -0
- package/build/src/codegen/resolve-envelope.test.js.map +1 -0
- package/build/src/errors.d.ts +33 -0
- package/build/src/errors.js +91 -0
- package/build/src/errors.js.map +1 -0
- package/build/src/errors.test.d.ts +1 -0
- package/build/src/errors.test.js +122 -0
- package/build/src/errors.test.js.map +1 -0
- package/build/src/exports.d.ts +7 -0
- package/build/src/exports.js +8 -0
- package/build/src/exports.js.map +1 -0
- package/build/src/implementations/http/doc-registry.d.ts +12 -0
- package/build/src/implementations/http/doc-registry.js +114 -0
- package/build/src/implementations/http/doc-registry.js.map +1 -0
- package/build/src/implementations/http/doc-registry.test.d.ts +1 -0
- package/build/src/implementations/http/doc-registry.test.js +347 -0
- package/build/src/implementations/http/doc-registry.test.js.map +1 -0
- package/build/src/implementations/http/express-rpc/index.d.ts +94 -0
- package/build/src/implementations/http/express-rpc/index.js +185 -0
- package/build/src/implementations/http/express-rpc/index.js.map +1 -0
- package/build/src/implementations/http/express-rpc/index.test.d.ts +1 -0
- package/build/src/implementations/http/express-rpc/index.test.js +684 -0
- package/build/src/implementations/http/express-rpc/index.test.js.map +1 -0
- package/build/src/implementations/http/express-rpc/types.d.ts +11 -0
- package/build/src/implementations/http/express-rpc/types.js +2 -0
- package/build/src/implementations/http/express-rpc/types.js.map +1 -0
- package/build/src/implementations/http/hono-api/index.d.ts +102 -0
- package/build/src/implementations/http/hono-api/index.js +341 -0
- package/build/src/implementations/http/hono-api/index.js.map +1 -0
- package/build/src/implementations/http/hono-api/index.test.d.ts +1 -0
- package/build/src/implementations/http/hono-api/index.test.js +992 -0
- package/build/src/implementations/http/hono-api/index.test.js.map +1 -0
- package/build/src/implementations/http/hono-api/types.d.ts +13 -0
- package/build/src/implementations/http/hono-api/types.js +2 -0
- package/build/src/implementations/http/hono-api/types.js.map +1 -0
- package/build/src/implementations/http/hono-rpc/index.d.ts +92 -0
- package/build/src/implementations/http/hono-rpc/index.js +161 -0
- package/build/src/implementations/http/hono-rpc/index.js.map +1 -0
- package/build/src/implementations/http/hono-rpc/index.test.d.ts +1 -0
- package/build/src/implementations/http/hono-rpc/index.test.js +803 -0
- package/build/src/implementations/http/hono-rpc/index.test.js.map +1 -0
- package/build/src/implementations/http/hono-rpc/types.d.ts +11 -0
- package/build/src/implementations/http/hono-rpc/types.js +2 -0
- package/build/src/implementations/http/hono-rpc/types.js.map +1 -0
- package/build/src/implementations/http/hono-stream/index.d.ts +120 -0
- package/build/src/implementations/http/hono-stream/index.js +309 -0
- package/build/src/implementations/http/hono-stream/index.js.map +1 -0
- package/build/src/implementations/http/hono-stream/index.test.d.ts +1 -0
- package/build/src/implementations/http/hono-stream/index.test.js +1356 -0
- package/build/src/implementations/http/hono-stream/index.test.js.map +1 -0
- package/build/src/implementations/http/hono-stream/types.d.ts +15 -0
- package/build/src/implementations/http/hono-stream/types.js +2 -0
- package/build/src/implementations/http/hono-stream/types.js.map +1 -0
- package/build/src/implementations/types.d.ts +142 -0
- package/build/src/implementations/types.js +2 -0
- package/build/src/implementations/types.js.map +1 -0
- package/build/src/index.d.ts +165 -0
- package/build/src/index.js +253 -0
- package/build/src/index.js.map +1 -0
- package/build/src/index.test.d.ts +1 -0
- package/build/src/index.test.js +890 -0
- package/build/src/index.test.js.map +1 -0
- package/build/src/schema/compute-schema.d.ts +35 -0
- package/build/src/schema/compute-schema.js +41 -0
- package/build/src/schema/compute-schema.js.map +1 -0
- package/build/src/schema/compute-schema.test.d.ts +1 -0
- package/build/src/schema/compute-schema.test.js +107 -0
- package/build/src/schema/compute-schema.test.js.map +1 -0
- package/build/src/schema/extract-json-schema.d.ts +2 -0
- package/build/src/schema/extract-json-schema.js +12 -0
- package/build/src/schema/extract-json-schema.js.map +1 -0
- package/build/src/schema/extract-json-schema.test.d.ts +1 -0
- package/build/src/schema/extract-json-schema.test.js +23 -0
- package/build/src/schema/extract-json-schema.test.js.map +1 -0
- package/build/src/schema/parser.d.ts +28 -0
- package/build/src/schema/parser.js +170 -0
- package/build/src/schema/parser.js.map +1 -0
- package/build/src/schema/parser.test.d.ts +1 -0
- package/build/src/schema/parser.test.js +120 -0
- package/build/src/schema/parser.test.js.map +1 -0
- package/build/src/schema/resolve-schema-lib.d.ts +12 -0
- package/build/src/schema/resolve-schema-lib.js +11 -0
- package/build/src/schema/resolve-schema-lib.js.map +1 -0
- package/build/src/schema/resolve-schema-lib.test.d.ts +1 -0
- package/build/src/schema/resolve-schema-lib.test.js +17 -0
- package/build/src/schema/resolve-schema-lib.test.js.map +1 -0
- package/build/src/schema/types.d.ts +8 -0
- package/build/src/schema/types.js +2 -0
- package/build/src/schema/types.js.map +1 -0
- package/build/src/stack-utils.d.ts +25 -0
- package/build/src/stack-utils.js +95 -0
- package/build/src/stack-utils.js.map +1 -0
- package/build/src/stack-utils.test.d.ts +1 -0
- package/build/src/stack-utils.test.js +80 -0
- package/build/src/stack-utils.test.js.map +1 -0
- package/docs/ai-agent-setup.md +7 -6
- package/package.json +1 -1
- /package/agent_config/claude-code/skills/{guide → ts-procedures}/anti-patterns.md +0 -0
- /package/agent_config/claude-code/skills/{guide → ts-procedures}/api-reference.md +0 -0
- /package/agent_config/claude-code/skills/{guide → ts-procedures}/patterns.md +0 -0
- /package/agent_config/claude-code/skills/{review → ts-procedures-review}/checklist.md +0 -0
- /package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/templates/client.md +0 -0
- /package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/templates/express-rpc.md +0 -0
- /package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/templates/hono-api.md +0 -0
- /package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/templates/hono-rpc.md +0 -0
- /package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/templates/hono-stream.md +0 -0
- /package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/templates/procedure.md +0 -0
- /package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/templates/stream-procedure.md +0 -0
|
@@ -0,0 +1,890 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import { describe, expect, test } from 'vitest';
|
|
3
|
+
import { Procedures } from './index.js';
|
|
4
|
+
import { v } from 'suretype';
|
|
5
|
+
import { Type } from 'typebox';
|
|
6
|
+
import { ProcedureError, ProcedureValidationError } from './errors.js';
|
|
7
|
+
describe('Procedures', () => {
|
|
8
|
+
test('Procedures', () => {
|
|
9
|
+
const result = Procedures({
|
|
10
|
+
onCreate: () => {
|
|
11
|
+
return undefined;
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
expect(result).toHaveProperty('Create');
|
|
15
|
+
});
|
|
16
|
+
test('Procedures generic context & extended config', () => {
|
|
17
|
+
const { Create } = Procedures();
|
|
18
|
+
const { info } = Create('TestProcedure', {
|
|
19
|
+
// should not throw type errors
|
|
20
|
+
customProp: 'customProp',
|
|
21
|
+
}, async (ctx) => {
|
|
22
|
+
// should not throw type errors
|
|
23
|
+
return ctx.authToken;
|
|
24
|
+
});
|
|
25
|
+
expect(info.customProp).toEqual('customProp');
|
|
26
|
+
expect(info.optionalProp).toEqual(undefined);
|
|
27
|
+
});
|
|
28
|
+
test('Create Single Procedures', () => {
|
|
29
|
+
const { procedure: procedure1, info: info1 } = Procedures().Create('test1', {}, async () => {
|
|
30
|
+
return '1';
|
|
31
|
+
});
|
|
32
|
+
const { procedure: procedure2, info: info2 } = Procedures().Create('test2', {}, async () => {
|
|
33
|
+
return '2';
|
|
34
|
+
});
|
|
35
|
+
expect(procedure1).toBeDefined();
|
|
36
|
+
expect(info1).toBeDefined();
|
|
37
|
+
expect(procedure2).toBeDefined();
|
|
38
|
+
expect(info2).toBeDefined();
|
|
39
|
+
});
|
|
40
|
+
test('Procedures - Create call', () => new Promise((done) => {
|
|
41
|
+
let mockHttpCall;
|
|
42
|
+
const { Create } = Procedures({
|
|
43
|
+
onCreate: ({ handler }) => {
|
|
44
|
+
mockHttpCall = handler;
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
Create('Handler', {
|
|
48
|
+
schema: {
|
|
49
|
+
params: v.object({ name: v.string() }),
|
|
50
|
+
returnType: v.string(),
|
|
51
|
+
},
|
|
52
|
+
}, async (ctx, params) => {
|
|
53
|
+
expect(params).toEqual({ name: 'name' });
|
|
54
|
+
done();
|
|
55
|
+
return 'name';
|
|
56
|
+
});
|
|
57
|
+
mockHttpCall({}, { name: 'name' });
|
|
58
|
+
}));
|
|
59
|
+
test('Procedures - Create call w/ Typebox', () => new Promise((done) => {
|
|
60
|
+
let mockHttpCall;
|
|
61
|
+
const { Create } = Procedures({
|
|
62
|
+
onCreate: ({ handler, config, name }) => {
|
|
63
|
+
mockHttpCall = handler;
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
Create('Handler', {
|
|
67
|
+
schema: {
|
|
68
|
+
params: Type.Object({ name: Type.Optional(Type.String()) }),
|
|
69
|
+
returnType: Type.String(),
|
|
70
|
+
},
|
|
71
|
+
}, async (ctx, params) => {
|
|
72
|
+
expect(params).toEqual({ name: 'name' });
|
|
73
|
+
done();
|
|
74
|
+
return 'name';
|
|
75
|
+
});
|
|
76
|
+
mockHttpCall({}, { name: 'name' });
|
|
77
|
+
}));
|
|
78
|
+
test('Procedures - Create returns a handler to call/test the Procedure registration', async () => {
|
|
79
|
+
let ProcedureRegisteredCbHandler;
|
|
80
|
+
const { Create } = Procedures({
|
|
81
|
+
onCreate: (Procedure) => {
|
|
82
|
+
ProcedureRegisteredCbHandler = Procedure.handler;
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
const { NamedExportHandler, procedure, info } = Create('NamedExportHandler', {
|
|
86
|
+
description: 'Handler description',
|
|
87
|
+
schema: {
|
|
88
|
+
params: v.object({ number: v.number() }),
|
|
89
|
+
},
|
|
90
|
+
}, async (ctx, params) => {
|
|
91
|
+
return params.number;
|
|
92
|
+
});
|
|
93
|
+
expect(NamedExportHandler).toBeDefined();
|
|
94
|
+
expect(procedure).toBeDefined();
|
|
95
|
+
expect(ProcedureRegisteredCbHandler).toEqual(NamedExportHandler);
|
|
96
|
+
expect(ProcedureRegisteredCbHandler).toEqual(procedure);
|
|
97
|
+
const result = NamedExportHandler({}, { number: 1 });
|
|
98
|
+
expect(result).toBeDefined();
|
|
99
|
+
expect(result).toBeInstanceOf(Promise);
|
|
100
|
+
await expect(result).resolves.toEqual(1);
|
|
101
|
+
expect(info).toBeDefined();
|
|
102
|
+
expect(info).toBeInstanceOf(Object);
|
|
103
|
+
expect(info.schema).toHaveProperty('params');
|
|
104
|
+
expect(info.schema.params).toEqual({
|
|
105
|
+
type: 'object',
|
|
106
|
+
properties: { number: { type: 'number' } },
|
|
107
|
+
});
|
|
108
|
+
expect(info).toHaveProperty('description');
|
|
109
|
+
expect(info.description).toEqual('Handler description');
|
|
110
|
+
});
|
|
111
|
+
test('Procedures - Create params validation w/ no params provided', () => new Promise((done) => {
|
|
112
|
+
let mockHttpCall;
|
|
113
|
+
const { Create } = Procedures({
|
|
114
|
+
onCreate: ({ handler, config, name }) => {
|
|
115
|
+
mockHttpCall = (callParams) => {
|
|
116
|
+
if (config.validation?.params) {
|
|
117
|
+
const { errors } = config.validation.params(callParams);
|
|
118
|
+
if (errors && 'message' in errors[0]) {
|
|
119
|
+
expect(errors[0].message).toEqual('must be object');
|
|
120
|
+
done();
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
handler(callParams, {});
|
|
125
|
+
};
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
Create('test', {
|
|
129
|
+
schema: {
|
|
130
|
+
params: v.object({}),
|
|
131
|
+
},
|
|
132
|
+
}, async () => {
|
|
133
|
+
done();
|
|
134
|
+
});
|
|
135
|
+
mockHttpCall();
|
|
136
|
+
}));
|
|
137
|
+
test('Procedures - Create params validation w/ missing params', async () => new Promise((done) => {
|
|
138
|
+
let mockHttpCall;
|
|
139
|
+
const { Create } = Procedures({
|
|
140
|
+
onCreate: async ({ handler, config, name }) => {
|
|
141
|
+
mockHttpCall = async (callParams) => {
|
|
142
|
+
if (config.validation?.params) {
|
|
143
|
+
const { errors } = config.validation.params(callParams);
|
|
144
|
+
expect(errors).toBeDefined();
|
|
145
|
+
expect(errors?.length).toEqual(2);
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
await handler(callParams, {});
|
|
149
|
+
}
|
|
150
|
+
catch (e) {
|
|
151
|
+
expect(e).instanceof(ProcedureValidationError);
|
|
152
|
+
expect(e.errors.length).toEqual(2);
|
|
153
|
+
done();
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
Create('test', {
|
|
159
|
+
schema: {
|
|
160
|
+
params: v.object({
|
|
161
|
+
name: v.string().required(),
|
|
162
|
+
id: v.number().required(),
|
|
163
|
+
email: v.string(),
|
|
164
|
+
}),
|
|
165
|
+
},
|
|
166
|
+
}, async () => {
|
|
167
|
+
return;
|
|
168
|
+
});
|
|
169
|
+
mockHttpCall({});
|
|
170
|
+
}));
|
|
171
|
+
test('Procedures - Create call provides ctx to handler', () => new Promise((done) => {
|
|
172
|
+
let mockHttpCall;
|
|
173
|
+
const { Create } = Procedures({
|
|
174
|
+
onCreate: ({ handler }) => {
|
|
175
|
+
mockHttpCall = () => handler({ testCtx: 'testCtx' });
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
Create('test', {}, async (ctx, params) => {
|
|
179
|
+
expect(ctx.testCtx).toEqual('testCtx');
|
|
180
|
+
done();
|
|
181
|
+
});
|
|
182
|
+
mockHttpCall();
|
|
183
|
+
}));
|
|
184
|
+
test('Procedure handler can throw local ctx error and is caught', async () => {
|
|
185
|
+
const { Create } = Procedures();
|
|
186
|
+
const { TestProcedureHandlerError } = Create('TestProcedureHandlerError', {}, async (ctx) => {
|
|
187
|
+
throw ctx.error('Local context error');
|
|
188
|
+
});
|
|
189
|
+
try {
|
|
190
|
+
await TestProcedureHandlerError({}, {});
|
|
191
|
+
}
|
|
192
|
+
catch (e) {
|
|
193
|
+
expect(e).toBeInstanceOf(ProcedureError);
|
|
194
|
+
expect(e.message).toEqual('Local context error');
|
|
195
|
+
expect(e.procedureName).toEqual('TestProcedureHandlerError');
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
test('Procedures - getRegisteredProcedures', () => {
|
|
199
|
+
const { Create, getProcedures } = Procedures({
|
|
200
|
+
onCreate: () => {
|
|
201
|
+
return undefined;
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
Create('test-docs', {
|
|
205
|
+
schema: {
|
|
206
|
+
params: v.object({ name: v.string().required() }),
|
|
207
|
+
returnType: v.string(),
|
|
208
|
+
},
|
|
209
|
+
}, async () => {
|
|
210
|
+
return 'test-docs';
|
|
211
|
+
});
|
|
212
|
+
const procedures = getProcedures();
|
|
213
|
+
const testDocsProcedure = procedures.find((p) => p.name === 'test-docs');
|
|
214
|
+
expect(testDocsProcedure).toBeDefined();
|
|
215
|
+
expect(testDocsProcedure?.config?.schema).toEqual({
|
|
216
|
+
params: {
|
|
217
|
+
type: 'object',
|
|
218
|
+
properties: {
|
|
219
|
+
name: {
|
|
220
|
+
type: 'string',
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
required: ['name'],
|
|
224
|
+
},
|
|
225
|
+
returnType: {
|
|
226
|
+
type: 'string',
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
test('Procedures - context() throws', async () => {
|
|
231
|
+
const { Create } = Procedures();
|
|
232
|
+
function validateAuthToken(token) {
|
|
233
|
+
return token === 'valid-token';
|
|
234
|
+
}
|
|
235
|
+
const { CheckIsAuthenticated } = Create('CheckIsAuthenticated', {
|
|
236
|
+
schema: {
|
|
237
|
+
returnType: v.string(),
|
|
238
|
+
},
|
|
239
|
+
}, async (ctx) => {
|
|
240
|
+
if (!validateAuthToken(ctx.authToken)) {
|
|
241
|
+
throw ctx.error('Invalid auth token');
|
|
242
|
+
}
|
|
243
|
+
return 'User authentication is valid';
|
|
244
|
+
});
|
|
245
|
+
await expect(CheckIsAuthenticated({ authToken: 'valid-token' }, {})).resolves.toEqual('User authentication is valid');
|
|
246
|
+
await expect(CheckIsAuthenticated({ authToken: 'not-valid-token' }, {})).rejects.toThrowError(ProcedureError);
|
|
247
|
+
});
|
|
248
|
+
test('Procedures - duplicate registration throws before schema computation', () => {
|
|
249
|
+
const { Create } = Procedures();
|
|
250
|
+
Create('DuplicateTest', {}, async () => 'first');
|
|
251
|
+
expect(() => {
|
|
252
|
+
Create('DuplicateTest', {}, async () => 'second');
|
|
253
|
+
}).toThrow('Procedure with name DuplicateTest is already registered');
|
|
254
|
+
});
|
|
255
|
+
test('Procedures - wrapped errors preserve cause', async () => {
|
|
256
|
+
const { Create } = Procedures();
|
|
257
|
+
const originalError = new Error('Database connection failed');
|
|
258
|
+
originalError.code = 'ECONNREFUSED';
|
|
259
|
+
const { TestCause } = Create('TestCause', {}, async () => {
|
|
260
|
+
throw originalError;
|
|
261
|
+
});
|
|
262
|
+
try {
|
|
263
|
+
await TestCause({}, {});
|
|
264
|
+
}
|
|
265
|
+
catch (e) {
|
|
266
|
+
expect(e).toBeInstanceOf(ProcedureError);
|
|
267
|
+
expect(e.cause).toBe(originalError);
|
|
268
|
+
expect(e.cause.code).toBe('ECONNREFUSED');
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
test('Procedures - getProcedure returns specific procedure', () => {
|
|
272
|
+
const { Create, getProcedure } = Procedures();
|
|
273
|
+
Create('FindMe', {}, async () => 'found');
|
|
274
|
+
const proc = getProcedure('FindMe');
|
|
275
|
+
expect(proc).toBeDefined();
|
|
276
|
+
expect(proc?.name).toBe('FindMe');
|
|
277
|
+
expect(getProcedure('NotFound')).toBeUndefined();
|
|
278
|
+
});
|
|
279
|
+
test('Procedures - removeProcedure allows re-registration', () => {
|
|
280
|
+
const { Create, removeProcedure, getProcedure } = Procedures();
|
|
281
|
+
Create('Removable', {}, async () => 'v1');
|
|
282
|
+
expect(getProcedure('Removable')).toBeDefined();
|
|
283
|
+
const removed = removeProcedure('Removable');
|
|
284
|
+
expect(removed).toBe(true);
|
|
285
|
+
expect(getProcedure('Removable')).toBeUndefined();
|
|
286
|
+
// Can now re-register
|
|
287
|
+
Create('Removable', {}, async () => 'v2');
|
|
288
|
+
expect(getProcedure('Removable')).toBeDefined();
|
|
289
|
+
});
|
|
290
|
+
test('Procedures - clear removes all procedures', () => {
|
|
291
|
+
const { Create, getProcedures, clear } = Procedures();
|
|
292
|
+
Create('One', {}, async () => '1');
|
|
293
|
+
Create('Two', {}, async () => '2');
|
|
294
|
+
expect(getProcedures().length).toBe(2);
|
|
295
|
+
clear();
|
|
296
|
+
expect(getProcedures().length).toBe(0);
|
|
297
|
+
});
|
|
298
|
+
test('Procedures - ctx.error still works after optimization', async () => {
|
|
299
|
+
const { Create } = Procedures();
|
|
300
|
+
const { ErrorTest } = Create('ErrorTest', {}, async (ctx) => {
|
|
301
|
+
throw ctx.error('Custom error message', { code: 'ERR_001' });
|
|
302
|
+
});
|
|
303
|
+
try {
|
|
304
|
+
await ErrorTest({}, {});
|
|
305
|
+
}
|
|
306
|
+
catch (e) {
|
|
307
|
+
expect(e).toBeInstanceOf(ProcedureError);
|
|
308
|
+
expect(e.message).toBe('Custom error message');
|
|
309
|
+
expect(e.procedureName).toBe('ErrorTest');
|
|
310
|
+
expect(e.meta).toEqual({ code: 'ERR_001' });
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
test('Create passes through external signal from context', async () => {
|
|
314
|
+
const { Create } = Procedures();
|
|
315
|
+
const externalAc = new AbortController();
|
|
316
|
+
let capturedSignal = null;
|
|
317
|
+
const { WithSignal } = Create('WithSignal', {}, async (ctx) => {
|
|
318
|
+
capturedSignal = ctx.signal;
|
|
319
|
+
return 'done';
|
|
320
|
+
});
|
|
321
|
+
await WithSignal({ signal: externalAc.signal }, {});
|
|
322
|
+
expect(capturedSignal).toBe(externalAc.signal);
|
|
323
|
+
expect(capturedSignal.aborted).toBe(false);
|
|
324
|
+
});
|
|
325
|
+
test('Create external signal reflects abort from caller', async () => {
|
|
326
|
+
const { Create } = Procedures();
|
|
327
|
+
const externalAc = new AbortController();
|
|
328
|
+
let capturedSignal = null;
|
|
329
|
+
const { AbortedSignal } = Create('AbortedSignal', {}, async (ctx) => {
|
|
330
|
+
capturedSignal = ctx.signal;
|
|
331
|
+
expect(ctx.signal.aborted).toBe(true);
|
|
332
|
+
return 'done';
|
|
333
|
+
});
|
|
334
|
+
externalAc.abort();
|
|
335
|
+
await AbortedSignal({ signal: externalAc.signal }, {});
|
|
336
|
+
expect(capturedSignal.aborted).toBe(true);
|
|
337
|
+
});
|
|
338
|
+
test('Create external signal cancels in-flight async work', async () => {
|
|
339
|
+
const { Create } = Procedures();
|
|
340
|
+
const externalAc = new AbortController();
|
|
341
|
+
let wasAbortedDuringWork = false;
|
|
342
|
+
const ready = Promise.withResolvers();
|
|
343
|
+
const { LongWork } = Create('LongWork', {}, async (ctx) => {
|
|
344
|
+
ready.resolve();
|
|
345
|
+
await new Promise((resolve) => {
|
|
346
|
+
ctx.signal.addEventListener('abort', () => {
|
|
347
|
+
wasAbortedDuringWork = true;
|
|
348
|
+
resolve();
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
return 'done';
|
|
352
|
+
});
|
|
353
|
+
const p = LongWork({ signal: externalAc.signal }, {});
|
|
354
|
+
await ready.promise;
|
|
355
|
+
externalAc.abort();
|
|
356
|
+
await p;
|
|
357
|
+
expect(wasAbortedDuringWork).toBe(true);
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
describe('Procedures - Definition Location in Errors', () => {
|
|
361
|
+
test('ProcedureValidationError includes definition location', async () => {
|
|
362
|
+
const { Create } = Procedures();
|
|
363
|
+
const { TestValidation } = Create('TestValidation', {
|
|
364
|
+
schema: {
|
|
365
|
+
params: v.object({ name: v.string().required() }),
|
|
366
|
+
},
|
|
367
|
+
}, async (ctx, params) => {
|
|
368
|
+
return params.name;
|
|
369
|
+
});
|
|
370
|
+
try {
|
|
371
|
+
// @ts-expect-error - intentionally passing invalid params
|
|
372
|
+
await TestValidation({}, {}); // Missing required 'name' param
|
|
373
|
+
}
|
|
374
|
+
catch (e) {
|
|
375
|
+
expect(e).toBeInstanceOf(ProcedureValidationError);
|
|
376
|
+
expect(e.definedAt).toBeDefined();
|
|
377
|
+
expect(e.definedAt.file).toContain('index.test.ts');
|
|
378
|
+
expect(e.definedAt.line).toBeGreaterThan(0);
|
|
379
|
+
expect(e.definedAt.column).toBeGreaterThan(0);
|
|
380
|
+
expect(e.stack).toContain('--- Procedure "TestValidation" defined at ---');
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
test('ctx.error() includes definition location', async () => {
|
|
384
|
+
const { Create } = Procedures();
|
|
385
|
+
const { TestCtxError } = Create('TestCtxError', {}, async (ctx) => {
|
|
386
|
+
throw ctx.error('Custom error');
|
|
387
|
+
});
|
|
388
|
+
try {
|
|
389
|
+
await TestCtxError({}, {});
|
|
390
|
+
}
|
|
391
|
+
catch (e) {
|
|
392
|
+
expect(e).toBeInstanceOf(ProcedureError);
|
|
393
|
+
expect(e.definedAt).toBeDefined();
|
|
394
|
+
expect(e.definedAt.file).toContain('index.test.ts');
|
|
395
|
+
expect(e.getDefinitionLocation()).toBeDefined();
|
|
396
|
+
expect(e.stack).toContain('--- Procedure "TestCtxError" defined at ---');
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
test('wrapped errors include definition location', async () => {
|
|
400
|
+
const { Create } = Procedures();
|
|
401
|
+
const { TestWrappedError } = Create('TestWrappedError', {}, async () => {
|
|
402
|
+
throw new Error('Original error');
|
|
403
|
+
});
|
|
404
|
+
try {
|
|
405
|
+
await TestWrappedError({}, {});
|
|
406
|
+
}
|
|
407
|
+
catch (e) {
|
|
408
|
+
expect(e).toBeInstanceOf(ProcedureError);
|
|
409
|
+
expect(e.definedAt).toBeDefined();
|
|
410
|
+
expect(e.definedAt.file).toContain('index.test.ts');
|
|
411
|
+
expect(e.message).toContain('Error in handler for TestWrappedError');
|
|
412
|
+
expect(e.cause).toBeInstanceOf(Error);
|
|
413
|
+
expect(e.cause.message).toBe('Original error');
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
test('getDefinitionLocation returns formatted string', async () => {
|
|
417
|
+
const { Create } = Procedures();
|
|
418
|
+
const { TestGetLocation } = Create('TestGetLocation', {
|
|
419
|
+
schema: {
|
|
420
|
+
params: v.object({ id: v.number().required() }),
|
|
421
|
+
},
|
|
422
|
+
}, async (ctx, params) => {
|
|
423
|
+
return params.id;
|
|
424
|
+
});
|
|
425
|
+
try {
|
|
426
|
+
// @ts-expect-error - intentionally passing invalid params
|
|
427
|
+
await TestGetLocation({}, {}); // Missing required 'id' param
|
|
428
|
+
}
|
|
429
|
+
catch (e) {
|
|
430
|
+
const location = e.getDefinitionLocation();
|
|
431
|
+
expect(location).toBeDefined();
|
|
432
|
+
expect(location).toMatch(/index\.test\.ts:\d+:\d+/);
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
test('error stack shows procedure definition location at the end', async () => {
|
|
436
|
+
const { Create } = Procedures();
|
|
437
|
+
const { TestStackFormat } = Create('TestStackFormat', {
|
|
438
|
+
schema: {
|
|
439
|
+
params: v.object({ value: v.string().required() }),
|
|
440
|
+
},
|
|
441
|
+
}, async (ctx, params) => {
|
|
442
|
+
return params.value;
|
|
443
|
+
});
|
|
444
|
+
try {
|
|
445
|
+
// @ts-expect-error - intentionally passing invalid params
|
|
446
|
+
await TestStackFormat({}, {});
|
|
447
|
+
}
|
|
448
|
+
catch (e) {
|
|
449
|
+
// Verify it's a validation error
|
|
450
|
+
expect(e.name).toBe('ProcedureValidationError');
|
|
451
|
+
expect(e).toBeInstanceOf(ProcedureValidationError);
|
|
452
|
+
// Stack should contain the error message and definition location
|
|
453
|
+
expect(e.stack).toContain('Validation error for TestStackFormat');
|
|
454
|
+
expect(e.stack).toContain('--- Procedure "TestStackFormat" defined at ---');
|
|
455
|
+
// The definition section should be at the end of the stack
|
|
456
|
+
const stackLines = e.stack.split('\n');
|
|
457
|
+
const definitionIndex = stackLines.findIndex((line) => line.includes('--- Procedure "TestStackFormat" defined at ---'));
|
|
458
|
+
expect(definitionIndex).toBeGreaterThan(0);
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
});
|
|
462
|
+
describe('Streaming Procedures - CreateStream', () => {
|
|
463
|
+
test('CreateStream creates a streaming procedure', async () => {
|
|
464
|
+
const { CreateStream } = Procedures();
|
|
465
|
+
const { StreamTest, procedure, info } = CreateStream('StreamTest', {
|
|
466
|
+
description: 'Test streaming procedure',
|
|
467
|
+
schema: {
|
|
468
|
+
params: v.object({ count: v.number().required() }),
|
|
469
|
+
yieldType: v.object({ value: v.number().required() }),
|
|
470
|
+
},
|
|
471
|
+
}, async function* (ctx, params) {
|
|
472
|
+
for (let i = 0; i < params.count; i++) {
|
|
473
|
+
yield { value: i };
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
expect(StreamTest).toBeDefined();
|
|
477
|
+
expect(procedure).toBeDefined();
|
|
478
|
+
expect(info.isStream).toBe(true);
|
|
479
|
+
expect(info.name).toBe('StreamTest');
|
|
480
|
+
expect(info.description).toBe('Test streaming procedure');
|
|
481
|
+
expect(info.schema.params).toEqual({
|
|
482
|
+
type: 'object',
|
|
483
|
+
properties: { count: { type: 'number' } },
|
|
484
|
+
required: ['count'],
|
|
485
|
+
});
|
|
486
|
+
expect(info.schema.yieldType).toEqual({
|
|
487
|
+
type: 'object',
|
|
488
|
+
properties: { value: { type: 'number' } },
|
|
489
|
+
required: ['value'],
|
|
490
|
+
});
|
|
491
|
+
// Collect yielded values
|
|
492
|
+
const values = [];
|
|
493
|
+
for await (const val of StreamTest({}, { count: 3 })) {
|
|
494
|
+
values.push(val);
|
|
495
|
+
}
|
|
496
|
+
expect(values).toEqual([{ value: 0 }, { value: 1 }, { value: 2 }]);
|
|
497
|
+
});
|
|
498
|
+
test('CreateStream validates params', async () => {
|
|
499
|
+
const { CreateStream } = Procedures();
|
|
500
|
+
const { StreamWithParams } = CreateStream('StreamWithParams', {
|
|
501
|
+
schema: {
|
|
502
|
+
params: v.object({ name: v.string().required() }),
|
|
503
|
+
yieldType: v.string(),
|
|
504
|
+
},
|
|
505
|
+
}, async function* (ctx, params) {
|
|
506
|
+
yield params.name;
|
|
507
|
+
});
|
|
508
|
+
// Missing required param should throw ProcedureValidationError
|
|
509
|
+
try {
|
|
510
|
+
// @ts-expect-error - intentionally passing invalid params
|
|
511
|
+
for await (const _val of StreamWithParams({}, {})) {
|
|
512
|
+
// Should not reach here
|
|
513
|
+
}
|
|
514
|
+
expect.fail('Should have thrown');
|
|
515
|
+
}
|
|
516
|
+
catch (e) {
|
|
517
|
+
expect(e).toBeInstanceOf(ProcedureValidationError);
|
|
518
|
+
expect(e.message).toContain('Validation error for StreamWithParams');
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
test('CreateStream with validateYields validates each yielded value', async () => {
|
|
522
|
+
const { CreateStream } = Procedures();
|
|
523
|
+
const { ProcedureYieldValidationError } = await import('./errors.js');
|
|
524
|
+
const { StreamValidateYields } = CreateStream('StreamValidateYields', {
|
|
525
|
+
schema: {
|
|
526
|
+
yieldType: v.object({ id: v.number().required() }),
|
|
527
|
+
},
|
|
528
|
+
validateYields: true,
|
|
529
|
+
}, async function* () {
|
|
530
|
+
yield { id: 1 }; // Valid
|
|
531
|
+
yield { id: 'not-a-number' }; // Invalid - should throw
|
|
532
|
+
});
|
|
533
|
+
const values = [];
|
|
534
|
+
try {
|
|
535
|
+
for await (const val of StreamValidateYields({}, {})) {
|
|
536
|
+
values.push(val);
|
|
537
|
+
}
|
|
538
|
+
expect.fail('Should have thrown on invalid yield');
|
|
539
|
+
}
|
|
540
|
+
catch (e) {
|
|
541
|
+
expect(e).toBeInstanceOf(ProcedureYieldValidationError);
|
|
542
|
+
expect(e.message).toContain('Yield validation error for StreamValidateYields');
|
|
543
|
+
expect(values).toEqual([{ id: 1 }]); // First value was valid
|
|
544
|
+
}
|
|
545
|
+
});
|
|
546
|
+
test('CreateStream without validateYields does not validate yields', async () => {
|
|
547
|
+
const { CreateStream } = Procedures();
|
|
548
|
+
const { StreamNoValidate } = CreateStream('StreamNoValidate', {
|
|
549
|
+
schema: {
|
|
550
|
+
yieldType: v.object({ id: v.number().required() }),
|
|
551
|
+
},
|
|
552
|
+
// validateYields defaults to false
|
|
553
|
+
}, async function* () {
|
|
554
|
+
yield { id: 1 };
|
|
555
|
+
yield { id: 'not-a-number' }; // Invalid but won't throw
|
|
556
|
+
});
|
|
557
|
+
const values = [];
|
|
558
|
+
for await (const val of StreamNoValidate({}, {})) {
|
|
559
|
+
values.push(val);
|
|
560
|
+
}
|
|
561
|
+
expect(values).toEqual([{ id: 1 }, { id: 'not-a-number' }]);
|
|
562
|
+
});
|
|
563
|
+
test('CreateStream ctx.error throws ProcedureError', async () => {
|
|
564
|
+
const { CreateStream } = Procedures();
|
|
565
|
+
const { StreamError } = CreateStream('StreamError', {}, async function* (ctx) {
|
|
566
|
+
yield 'first';
|
|
567
|
+
throw ctx.error('Custom stream error', { code: 'STREAM_ERR' });
|
|
568
|
+
});
|
|
569
|
+
const values = [];
|
|
570
|
+
try {
|
|
571
|
+
for await (const val of StreamError({}, {})) {
|
|
572
|
+
values.push(val);
|
|
573
|
+
}
|
|
574
|
+
expect.fail('Should have thrown');
|
|
575
|
+
}
|
|
576
|
+
catch (e) {
|
|
577
|
+
expect(e).toBeInstanceOf(ProcedureError);
|
|
578
|
+
expect(e.message).toBe('Custom stream error');
|
|
579
|
+
expect(e.procedureName).toBe('StreamError');
|
|
580
|
+
expect(e.meta).toEqual({ code: 'STREAM_ERR' });
|
|
581
|
+
expect(values).toEqual(['first']);
|
|
582
|
+
}
|
|
583
|
+
});
|
|
584
|
+
test('CreateStream provides ctx.signal for abort handling', async () => {
|
|
585
|
+
const { CreateStream } = Procedures();
|
|
586
|
+
let signalReceived = false;
|
|
587
|
+
const { StreamWithSignal } = CreateStream('StreamWithSignal', {}, async function* (ctx) {
|
|
588
|
+
expect(ctx.signal).toBeDefined();
|
|
589
|
+
expect(ctx.signal).toBeInstanceOf(AbortSignal);
|
|
590
|
+
signalReceived = true;
|
|
591
|
+
yield 'value';
|
|
592
|
+
});
|
|
593
|
+
for await (const _val of StreamWithSignal({}, {})) {
|
|
594
|
+
// consume
|
|
595
|
+
}
|
|
596
|
+
expect(signalReceived).toBe(true);
|
|
597
|
+
});
|
|
598
|
+
test('CreateStream appears in getProcedures with isStream flag', () => {
|
|
599
|
+
const { Create, CreateStream, getProcedures } = Procedures();
|
|
600
|
+
Create('RegularProc', {}, async () => 'regular');
|
|
601
|
+
CreateStream('StreamProc', {}, async function* () {
|
|
602
|
+
yield 'stream';
|
|
603
|
+
});
|
|
604
|
+
const procs = getProcedures();
|
|
605
|
+
expect(procs.length).toBe(2);
|
|
606
|
+
const regular = procs.find((p) => p.name === 'RegularProc');
|
|
607
|
+
const stream = procs.find((p) => p.name === 'StreamProc');
|
|
608
|
+
expect(regular?.isStream).toBeUndefined();
|
|
609
|
+
expect(stream?.isStream).toBe(true);
|
|
610
|
+
});
|
|
611
|
+
test('CreateStream onCreate callback receives isStream flag', () => new Promise((done) => {
|
|
612
|
+
let receivedProc;
|
|
613
|
+
const { CreateStream } = Procedures({
|
|
614
|
+
onCreate: (proc) => {
|
|
615
|
+
receivedProc = proc;
|
|
616
|
+
},
|
|
617
|
+
});
|
|
618
|
+
CreateStream('OnCreateStream', {}, async function* () {
|
|
619
|
+
yield 'test';
|
|
620
|
+
});
|
|
621
|
+
expect(receivedProc).toBeDefined();
|
|
622
|
+
expect(receivedProc.isStream).toBe(true);
|
|
623
|
+
expect(receivedProc.name).toBe('OnCreateStream');
|
|
624
|
+
done();
|
|
625
|
+
}));
|
|
626
|
+
test('CreateStream duplicate registration throws', () => {
|
|
627
|
+
const { CreateStream } = Procedures();
|
|
628
|
+
CreateStream('DuplicateStream', {}, async function* () {
|
|
629
|
+
yield 'first';
|
|
630
|
+
});
|
|
631
|
+
expect(() => {
|
|
632
|
+
CreateStream('DuplicateStream', {}, async function* () {
|
|
633
|
+
yield 'second';
|
|
634
|
+
});
|
|
635
|
+
}).toThrow('Procedure with name DuplicateStream is already registered');
|
|
636
|
+
});
|
|
637
|
+
test('CreateStream error includes definition location', async () => {
|
|
638
|
+
const { CreateStream } = Procedures();
|
|
639
|
+
const { StreamErrorLocation } = CreateStream('StreamErrorLocation', {
|
|
640
|
+
schema: {
|
|
641
|
+
params: v.object({ id: v.number().required() }),
|
|
642
|
+
},
|
|
643
|
+
}, async function* () {
|
|
644
|
+
yield 'test';
|
|
645
|
+
});
|
|
646
|
+
try {
|
|
647
|
+
// @ts-expect-error - intentionally passing invalid params
|
|
648
|
+
for await (const _val of StreamErrorLocation({}, {})) {
|
|
649
|
+
// consume
|
|
650
|
+
}
|
|
651
|
+
expect.fail('Should have thrown');
|
|
652
|
+
}
|
|
653
|
+
catch (e) {
|
|
654
|
+
expect(e.definedAt).toBeDefined();
|
|
655
|
+
expect(e.definedAt.file).toContain('index.test.ts');
|
|
656
|
+
expect(e.stack).toContain('--- Procedure "StreamErrorLocation" defined at ---');
|
|
657
|
+
}
|
|
658
|
+
});
|
|
659
|
+
test('CreateStream with Typebox schema', async () => {
|
|
660
|
+
const { CreateStream } = Procedures();
|
|
661
|
+
const { TypeboxStream } = CreateStream('TypeboxStream', {
|
|
662
|
+
schema: {
|
|
663
|
+
params: Type.Object({ limit: Type.Number() }),
|
|
664
|
+
yieldType: Type.Object({ data: Type.String() }),
|
|
665
|
+
},
|
|
666
|
+
}, async function* (ctx, params) {
|
|
667
|
+
for (let i = 0; i < params.limit; i++) {
|
|
668
|
+
yield { data: `item-${i}` };
|
|
669
|
+
}
|
|
670
|
+
});
|
|
671
|
+
const values = [];
|
|
672
|
+
for await (const val of TypeboxStream({}, { limit: 2 })) {
|
|
673
|
+
values.push(val);
|
|
674
|
+
}
|
|
675
|
+
expect(values).toEqual([{ data: 'item-0' }, { data: 'item-1' }]);
|
|
676
|
+
});
|
|
677
|
+
test('CreateStream with context type', async () => {
|
|
678
|
+
const { CreateStream } = Procedures();
|
|
679
|
+
const { ContextStream } = CreateStream('ContextStream', {}, async function* (ctx) {
|
|
680
|
+
// ctx should have both userId and error
|
|
681
|
+
expect(ctx.userId).toBe('user-123');
|
|
682
|
+
expect(ctx.error).toBeDefined();
|
|
683
|
+
expect(ctx.signal).toBeDefined();
|
|
684
|
+
yield ctx.userId;
|
|
685
|
+
});
|
|
686
|
+
const values = [];
|
|
687
|
+
for await (const val of ContextStream({ userId: 'user-123' }, {})) {
|
|
688
|
+
values.push(val);
|
|
689
|
+
}
|
|
690
|
+
expect(values).toEqual(['user-123']);
|
|
691
|
+
});
|
|
692
|
+
test('CreateStream wrapped errors preserve cause', async () => {
|
|
693
|
+
const { CreateStream } = Procedures();
|
|
694
|
+
const originalError = new Error('Stream underlying error');
|
|
695
|
+
originalError.code = 'STREAM_FAIL';
|
|
696
|
+
const { StreamCause } = CreateStream('StreamCause', {},
|
|
697
|
+
// eslint-disable-next-line require-yield
|
|
698
|
+
async function* () {
|
|
699
|
+
throw originalError;
|
|
700
|
+
});
|
|
701
|
+
try {
|
|
702
|
+
for await (const _val of StreamCause({}, {})) {
|
|
703
|
+
// consume
|
|
704
|
+
}
|
|
705
|
+
expect.fail('Should have thrown');
|
|
706
|
+
}
|
|
707
|
+
catch (e) {
|
|
708
|
+
expect(e).toBeInstanceOf(ProcedureError);
|
|
709
|
+
expect(e.cause).toBe(originalError);
|
|
710
|
+
expect(e.cause.code).toBe('STREAM_FAIL');
|
|
711
|
+
}
|
|
712
|
+
});
|
|
713
|
+
test('CreateStream with extended config', () => {
|
|
714
|
+
const { CreateStream } = Procedures();
|
|
715
|
+
const { info } = CreateStream('ExtendedStream', {
|
|
716
|
+
scope: 'api',
|
|
717
|
+
version: 1,
|
|
718
|
+
description: 'Extended config stream',
|
|
719
|
+
}, async function* () {
|
|
720
|
+
yield 'data';
|
|
721
|
+
});
|
|
722
|
+
expect(info.scope).toBe('api');
|
|
723
|
+
expect(info.version).toBe(1);
|
|
724
|
+
expect(info.description).toBe('Extended config stream');
|
|
725
|
+
expect(info.isStream).toBe(true);
|
|
726
|
+
});
|
|
727
|
+
test('CreateStream signal.aborted becomes true after generator completes', async () => {
|
|
728
|
+
const { CreateStream } = Procedures();
|
|
729
|
+
let capturedSignal = null;
|
|
730
|
+
const { AbortStream } = CreateStream('AbortStream', {}, async function* (ctx) {
|
|
731
|
+
capturedSignal = ctx.signal;
|
|
732
|
+
yield 'value';
|
|
733
|
+
});
|
|
734
|
+
// Consume the generator
|
|
735
|
+
for await (const _val of AbortStream({}, {})) {
|
|
736
|
+
// consume
|
|
737
|
+
}
|
|
738
|
+
// After generator completes, signal should be aborted
|
|
739
|
+
expect(capturedSignal).not.toBeNull();
|
|
740
|
+
expect(capturedSignal.aborted).toBe(true);
|
|
741
|
+
});
|
|
742
|
+
test('CreateStream signal.reason is stream-completed after normal completion', async () => {
|
|
743
|
+
const { CreateStream } = Procedures();
|
|
744
|
+
let capturedSignal = null;
|
|
745
|
+
const { ReasonStream } = CreateStream('ReasonStream', {}, async function* (ctx) {
|
|
746
|
+
capturedSignal = ctx.signal;
|
|
747
|
+
yield 'value';
|
|
748
|
+
});
|
|
749
|
+
for await (const _val of ReasonStream({}, {})) {
|
|
750
|
+
// consume
|
|
751
|
+
}
|
|
752
|
+
expect(capturedSignal.reason).toBe('stream-completed');
|
|
753
|
+
});
|
|
754
|
+
test('CreateStream combines external signal with internal signal', async () => {
|
|
755
|
+
const { CreateStream } = Procedures();
|
|
756
|
+
const externalAc = new AbortController();
|
|
757
|
+
let capturedSignal = null;
|
|
758
|
+
const { CombinedStream } = CreateStream('CombinedStream', {}, async function* (ctx) {
|
|
759
|
+
capturedSignal = ctx.signal;
|
|
760
|
+
// Combined signal is a new object, not the raw external signal
|
|
761
|
+
expect(ctx.signal).not.toBe(externalAc.signal);
|
|
762
|
+
yield 'value';
|
|
763
|
+
});
|
|
764
|
+
// Abort external before consuming — combined signal should reflect it
|
|
765
|
+
externalAc.abort('client-disconnected');
|
|
766
|
+
for await (const _val of CombinedStream({ signal: externalAc.signal }, {})) {
|
|
767
|
+
// consume
|
|
768
|
+
}
|
|
769
|
+
expect(capturedSignal.aborted).toBe(true);
|
|
770
|
+
// Reason comes from external abort, not internal 'stream-completed'
|
|
771
|
+
expect(capturedSignal.reason).toBe('client-disconnected');
|
|
772
|
+
});
|
|
773
|
+
});
|
|
774
|
+
describe('isPrevalidated context property', () => {
|
|
775
|
+
test('Create skips validation when ctx.isPrevalidated is true', async () => {
|
|
776
|
+
const { Create } = Procedures();
|
|
777
|
+
const { SkipValidation } = Create('SkipValidation', {
|
|
778
|
+
schema: {
|
|
779
|
+
params: v.object({ name: v.string().required() }),
|
|
780
|
+
},
|
|
781
|
+
}, async (ctx, params) => {
|
|
782
|
+
return params;
|
|
783
|
+
});
|
|
784
|
+
// Without isPrevalidated, missing required param would throw
|
|
785
|
+
// With isPrevalidated: true, validation is skipped
|
|
786
|
+
const result = await SkipValidation({ isPrevalidated: true }, {});
|
|
787
|
+
expect(result).toEqual({});
|
|
788
|
+
});
|
|
789
|
+
test('Create validates when ctx.isPrevalidated is false', async () => {
|
|
790
|
+
const { Create } = Procedures();
|
|
791
|
+
const { ValidateParams } = Create('ValidateParams', {
|
|
792
|
+
schema: {
|
|
793
|
+
params: v.object({ name: v.string().required() }),
|
|
794
|
+
},
|
|
795
|
+
}, async (ctx, params) => {
|
|
796
|
+
return params;
|
|
797
|
+
});
|
|
798
|
+
// With isPrevalidated: false, validation should still run
|
|
799
|
+
try {
|
|
800
|
+
await ValidateParams({ isPrevalidated: false }, {});
|
|
801
|
+
expect.fail('Should have thrown validation error');
|
|
802
|
+
}
|
|
803
|
+
catch (e) {
|
|
804
|
+
expect(e).toBeInstanceOf(ProcedureValidationError);
|
|
805
|
+
}
|
|
806
|
+
});
|
|
807
|
+
test('Create validates when ctx.isPrevalidated is undefined', async () => {
|
|
808
|
+
const { Create } = Procedures();
|
|
809
|
+
const { ValidateUndefined } = Create('ValidateUndefined', {
|
|
810
|
+
schema: {
|
|
811
|
+
params: v.object({ id: v.number().required() }),
|
|
812
|
+
},
|
|
813
|
+
}, async (ctx, params) => {
|
|
814
|
+
return params;
|
|
815
|
+
});
|
|
816
|
+
// Without isPrevalidated property, validation should run
|
|
817
|
+
try {
|
|
818
|
+
await ValidateUndefined({}, {});
|
|
819
|
+
expect.fail('Should have thrown validation error');
|
|
820
|
+
}
|
|
821
|
+
catch (e) {
|
|
822
|
+
expect(e).toBeInstanceOf(ProcedureValidationError);
|
|
823
|
+
}
|
|
824
|
+
});
|
|
825
|
+
test('CreateStream skips validation when ctx.isPrevalidated is true', async () => {
|
|
826
|
+
const { CreateStream } = Procedures();
|
|
827
|
+
const { StreamSkipValidation } = CreateStream('StreamSkipValidation', {
|
|
828
|
+
schema: {
|
|
829
|
+
params: v.object({ count: v.number().required() }),
|
|
830
|
+
},
|
|
831
|
+
}, async function* (ctx, params) {
|
|
832
|
+
yield { received: params };
|
|
833
|
+
});
|
|
834
|
+
// With isPrevalidated: true, validation is skipped even with invalid params
|
|
835
|
+
const values = [];
|
|
836
|
+
for await (const val of StreamSkipValidation({ isPrevalidated: true }, {})) {
|
|
837
|
+
values.push(val);
|
|
838
|
+
}
|
|
839
|
+
expect(values).toEqual([{ received: {} }]);
|
|
840
|
+
});
|
|
841
|
+
test('CreateStream validates when ctx.isPrevalidated is false', async () => {
|
|
842
|
+
const { CreateStream } = Procedures();
|
|
843
|
+
const { StreamValidate } = CreateStream('StreamValidate', {
|
|
844
|
+
schema: {
|
|
845
|
+
params: v.object({ count: v.number().required() }),
|
|
846
|
+
},
|
|
847
|
+
}, async function* (ctx, params) {
|
|
848
|
+
yield { received: params };
|
|
849
|
+
});
|
|
850
|
+
// With isPrevalidated: false, validation should run
|
|
851
|
+
try {
|
|
852
|
+
for await (const _val of StreamValidate({ isPrevalidated: false }, {})) {
|
|
853
|
+
// consume
|
|
854
|
+
}
|
|
855
|
+
expect.fail('Should have thrown validation error');
|
|
856
|
+
}
|
|
857
|
+
catch (e) {
|
|
858
|
+
expect(e).toBeInstanceOf(ProcedureValidationError);
|
|
859
|
+
}
|
|
860
|
+
});
|
|
861
|
+
test('CreateStream validates when ctx.isPrevalidated is undefined', async () => {
|
|
862
|
+
const { CreateStream } = Procedures();
|
|
863
|
+
const { StreamValidateUndefined } = CreateStream('StreamValidateUndefined', {
|
|
864
|
+
schema: {
|
|
865
|
+
params: v.object({ value: v.string().required() }),
|
|
866
|
+
},
|
|
867
|
+
}, async function* (ctx, params) {
|
|
868
|
+
yield params;
|
|
869
|
+
});
|
|
870
|
+
// Without isPrevalidated property, validation should run
|
|
871
|
+
try {
|
|
872
|
+
for await (const _val of StreamValidateUndefined({}, {})) {
|
|
873
|
+
// consume
|
|
874
|
+
}
|
|
875
|
+
expect.fail('Should have thrown validation error');
|
|
876
|
+
}
|
|
877
|
+
catch (e) {
|
|
878
|
+
expect(e).toBeInstanceOf(ProcedureValidationError);
|
|
879
|
+
}
|
|
880
|
+
});
|
|
881
|
+
test('isPrevalidated is not exposed in handler context type', async () => {
|
|
882
|
+
const { Create } = Procedures();
|
|
883
|
+
Create('CheckCtxType', {}, async (ctx) => {
|
|
884
|
+
// @ts-expect-error - isPrevalidated should not be on handler context type
|
|
885
|
+
void ctx.isPrevalidated;
|
|
886
|
+
return 'done';
|
|
887
|
+
});
|
|
888
|
+
});
|
|
889
|
+
});
|
|
890
|
+
//# sourceMappingURL=index.test.js.map
|