@abyss-project/runtime 3.5.0 → 3.7.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.
Files changed (59) hide show
  1. package/.eslintrc.js +32 -32
  2. package/.prettierrc +5 -5
  3. package/README.md +88 -88
  4. package/dist/engine/engine-source.js +1 -1
  5. package/dist/engine/run-in-isolate.js +5 -2
  6. package/dist/engine/run-in-isolate.js.map +1 -1
  7. package/dist/interpreter/interpreter.js +19 -4
  8. package/dist/interpreter/interpreter.js.map +1 -1
  9. package/dist/interpreter/interpreter.types.d.ts +3 -0
  10. package/dist/interpreter/interpreter.types.js.map +1 -1
  11. package/dist/runner-protocol/execution-protocol.types.d.ts +12 -0
  12. package/dist/runner-protocol/execution-protocol.types.js +2 -1
  13. package/dist/runner-protocol/execution-protocol.types.js.map +1 -1
  14. package/jest.config.ts +11 -11
  15. package/package.json +15 -10
  16. package/scripts/build-engine.cjs +70 -70
  17. package/src/__tests__/engine-bundle.test.ts +121 -121
  18. package/src/__tests__/surface.test.ts +70 -66
  19. package/src/access/__tests__/host-match.test.ts +24 -24
  20. package/src/access/access.types.ts +25 -25
  21. package/src/access/host-match.ts +13 -13
  22. package/src/cost/__tests__/execution-cost.test.ts +196 -196
  23. package/src/cost/execution-cost.ts +186 -186
  24. package/src/definition/__tests__/validate-definition.test.ts +354 -354
  25. package/src/definition/definition.limits.ts +9 -9
  26. package/src/definition/definition.schema.ts +172 -172
  27. package/src/definition/definition.types.ts +171 -171
  28. package/src/definition/validate-definition.ts +206 -206
  29. package/src/definition/walk-steps.ts +44 -44
  30. package/src/discord/__tests__/discord-event-input.test.ts +89 -89
  31. package/src/discord/discord.types.ts +639 -639
  32. package/src/engine/__tests__/run-in-isolate.test.ts +468 -468
  33. package/src/engine/engine-entry.ts +3 -3
  34. package/src/engine/engine-global.ts +2 -2
  35. package/src/engine/run-in-isolate.ts +419 -404
  36. package/src/errors/runtime-error-code.enum.ts +37 -37
  37. package/src/index.ts +32 -32
  38. package/src/interpreter/__tests__/interpreter.test.ts +854 -854
  39. package/src/interpreter/__tests__/log-respond-resume.test.ts +271 -223
  40. package/src/interpreter/interpreter-error.ts +12 -12
  41. package/src/interpreter/interpreter.ts +483 -461
  42. package/src/interpreter/interpreter.types.ts +204 -192
  43. package/src/interpreter/sequence-path.ts +67 -67
  44. package/src/interpreter/test-host.ts +85 -85
  45. package/src/runner-protocol/evaluation.types.ts +21 -21
  46. package/src/runner-protocol/execution-protocol.types.ts +260 -231
  47. package/src/schedule/schedule.types.ts +100 -100
  48. package/src/server/__tests__/code-runner-session.test.ts +311 -311
  49. package/src/server/code-runner-error.ts +12 -12
  50. package/src/server/code-runner-session.ts +310 -310
  51. package/src/server/index.ts +11 -11
  52. package/src/steps/__tests__/global-references.test.ts +133 -133
  53. package/src/steps/execute-step.ts +186 -186
  54. package/src/steps/global-references.ts +48 -48
  55. package/src/steps/step-access.ts +56 -56
  56. package/src/steps/step-fields.ts +87 -87
  57. package/src/steps/value-source.ts +69 -69
  58. package/tsconfig.check.json +15 -15
  59. package/tsconfig.json +35 -35
@@ -1,354 +1,354 @@
1
- import Joi from 'joi';
2
- import { AccessKind } from '../../access/access.types';
3
- import {
4
- CURRENT_FORMAT_VERSION,
5
- HttpMethod,
6
- LogLevel,
7
- StepType,
8
- StopStatus,
9
- } from '../definition.types';
10
- import type { HostActionCatalog } from '../definition.types';
11
- import { MAX_WAIT_DURATION_MS } from '../definition.schema';
12
- import { validateDefinition } from '../validate-definition';
13
- import { walkSteps } from '../walk-steps';
14
-
15
- const catalog: HostActionCatalog = new Map([
16
- [
17
- 'send-email',
18
- {
19
- name: 'send-email',
20
- inputSchema: Joi.object({ to: Joi.string().required(), subject: Joi.string() }),
21
- },
22
- ],
23
- ]);
24
-
25
- const clone = <T>(value: T): T => JSON.parse(JSON.stringify(value));
26
-
27
- const validDefinition = () => ({
28
- formatVersion: CURRENT_FORMAT_VERSION,
29
- accesses: [
30
- { kind: AccessKind.HTTP, hosts: ['api.example.com', '*.hooks.example.com'] },
31
- { kind: AccessKind.HOST_ACTION, action: 'send-email' },
32
- ],
33
- steps: [
34
- { id: 'init', type: StepType.SET, variable: 'count', value: 0 },
35
- {
36
- id: 'check',
37
- type: StepType.CONDITION,
38
- if: { $expression: 'input.amount > 10' },
39
- then: [
40
- {
41
- id: 'fetch',
42
- type: StepType.HTTP,
43
- method: HttpMethod.POST,
44
- url: 'https://api.example.com/v1/items',
45
- headers: { Authorization: 'Bearer {{ input.token }}', 'X-List': ['a', { b: 1 }] },
46
- body: { items: [1, 2, { $expression: 'input.items' }] },
47
- timeoutMs: 5000,
48
- },
49
- {
50
- id: 'mail',
51
- type: StepType.HOST,
52
- action: 'send-email',
53
- input: { to: '{{ input.email }}', subject: 'Hello' },
54
- },
55
- ],
56
- else: [
57
- { id: 'pause', type: StepType.WAIT, durationMs: 1000 },
58
- {
59
- id: 'note',
60
- type: StepType.LOG,
61
- level: LogLevel.WARN,
62
- message: 'small amount: {{ input.amount }}',
63
- data: { amount: { $expression: 'input.amount' } },
64
- },
65
- {
66
- id: 'reply',
67
- type: StepType.RESPOND,
68
- status: 202,
69
- headers: { 'Content-Type': 'application/json' },
70
- body: { accepted: true },
71
- },
72
- ],
73
- },
74
- {
75
- id: 'route',
76
- type: StepType.SWITCH,
77
- value: '{{ input.kind }}',
78
- cases: [
79
- {
80
- match: 'a',
81
- steps: [
82
- { id: 'compute', type: StepType.CODE, source: 'return input.n * 2;' },
83
- { id: 'later', type: StepType.WAIT, until: '2026-12-31T00:00:00Z' },
84
- ],
85
- },
86
- {
87
- match: null,
88
- steps: [
89
- {
90
- id: 'hook',
91
- type: StepType.HTTP,
92
- method: HttpMethod.GET,
93
- url: 'https://x.hooks.example.com/ping',
94
- },
95
- ],
96
- },
97
- ],
98
- default: [{ id: 'fail', type: StepType.STOP, status: StopStatus.FAILED, output: 'unknown' }],
99
- },
100
- { id: 'done', type: StepType.STOP, status: StopStatus.SUCCEEDED },
101
- ],
102
- });
103
-
104
- const errorsOf = (raw: unknown) => {
105
- const result = validateDefinition(raw, catalog);
106
- if (result.ok) {
107
- throw new Error('expected a refusal');
108
- }
109
- return result.errors;
110
- };
111
-
112
- const pathsOf = (raw: unknown) => errorsOf(raw).map((error) => error.path);
113
-
114
- describe('validateDefinition', () => {
115
- it('accepts a complete definition with the 10 step types and nested branches', () => {
116
- const raw = validDefinition();
117
- const result = validateDefinition(raw, catalog);
118
- expect(result).toEqual({ ok: true, definition: raw });
119
- const types = new Set<string>();
120
- walkSteps(raw.steps as never, (step) => types.add(step.type));
121
- expect([...types].sort()).toEqual(Object.values(StepType).sort());
122
- });
123
-
124
- it('rejects an unknown formatVersion at "formatVersion"', () => {
125
- const raw = { ...validDefinition(), formatVersion: 2 };
126
- expect(pathsOf(raw)).toEqual(['formatVersion']);
127
- });
128
-
129
- it('rejects a duplicated id on the second occurrence', () => {
130
- const raw = validDefinition();
131
- (raw.steps[1] as any).then[0].id = 'init';
132
- expect(pathsOf(raw)).toEqual(['steps[1].then[0].id']);
133
- });
134
-
135
- it('rejects __proto__, constructor and prototype as step id and as SET variable', () => {
136
- for (const name of ['__proto__', 'constructor', 'prototype']) {
137
- const asId = validDefinition();
138
- (asId.steps[0] as any).id = name;
139
- expect(pathsOf(asId)).toEqual(['steps[0].id']);
140
- const asVariable = validDefinition();
141
- (asVariable.steps[0] as any).variable = name;
142
- expect(pathsOf(asVariable)).toEqual(['steps[0].variable']);
143
- }
144
- });
145
-
146
- it('bounds a literal WAIT duration to MAX_WAIT_DURATION_MS', () => {
147
- expect(MAX_WAIT_DURATION_MS).toBe(30 * 24 * 60 * 60 * 1000);
148
- const at = (durationMs: unknown) => ({
149
- ...validDefinition(),
150
- steps: [{ id: 'w', type: StepType.WAIT, durationMs }],
151
- });
152
- expect(validateDefinition(at(MAX_WAIT_DURATION_MS), catalog).ok).toBe(true);
153
- expect(validateDefinition(at({ $expression: 'input.d' }), catalog).ok).toBe(true);
154
- expect(validateDefinition(at('{{ input.d }}'), catalog).ok).toBe(true);
155
- for (const bad of [MAX_WAIT_DURATION_MS + 1, -1, 1.5]) {
156
- expect(pathsOf(at(bad))).toEqual(['steps[0].durationMs']);
157
- }
158
- });
159
-
160
- it('requires a literal WAIT until to be an ISO 8601 date', () => {
161
- const at = (until: unknown) => ({
162
- ...validDefinition(),
163
- steps: [{ id: 'w', type: StepType.WAIT, until }],
164
- });
165
- expect(validateDefinition(at('2030-01-01T00:00:00.000Z'), catalog).ok).toBe(true);
166
- expect(validateDefinition(at('{{ input.when }}'), catalog).ok).toBe(true);
167
- expect(validateDefinition(at({ $expression: 'input.when' }), catalog).ok).toBe(true);
168
- expect(pathsOf(at('tomorrow'))).toEqual(['steps[0].until']);
169
- expect(pathsOf(at(12))).toEqual(['steps[0].until']);
170
- });
171
-
172
- it('requires a LOG level among info, warn, error and a message', () => {
173
- const at = (step: Record<string, unknown>) => ({
174
- ...validDefinition(),
175
- steps: [{ id: 'l', type: StepType.LOG, ...step }],
176
- });
177
- expect(
178
- validateDefinition(at({ level: 'info', message: { $expression: 'input' } }), catalog).ok,
179
- ).toBe(true);
180
- expect(pathsOf(at({ level: 'debug', message: 'x' }))).toEqual(['steps[0].level']);
181
- expect(pathsOf(at({ level: 'error' }))).toEqual(['steps[0].message']);
182
- });
183
-
184
- it('requires a RESPOND status, a literal one from 200 to 599', () => {
185
- const at = (status: unknown) => ({
186
- ...validDefinition(),
187
- steps: [{ id: 'r', type: StepType.RESPOND, status }],
188
- });
189
- for (const good of [200, 201, 599, '{{ input.status }}', { $expression: 'input.status' }]) {
190
- expect(validateDefinition(at(good), catalog).ok).toBe(true);
191
- }
192
- for (const bad of [100, 199, 600, 200.5, 'ok']) {
193
- expect(pathsOf(at(bad))).toEqual(['steps[0].status']);
194
- }
195
- const missing = { ...validDefinition(), steps: [{ id: 'r', type: StepType.RESPOND }] };
196
- expect(pathsOf(missing)).toEqual(['steps[0].status']);
197
- });
198
-
199
- it('rejects an action absent from the catalog at the path of "action"', () => {
200
- const raw = validDefinition();
201
- (raw.steps[1] as any).then[1].action = 'send-sms';
202
- const paths = pathsOf(raw);
203
- expect(paths).toContain('steps[1].then[1].action');
204
- });
205
-
206
- it('rejects an input key outside the action schema', () => {
207
- const raw = validDefinition();
208
- (raw.steps[1] as any).then[1].input.cc = 'x@y.z';
209
- expect(pathsOf(raw)).toEqual(['steps[1].then[1].input.cc']);
210
- });
211
-
212
- it('rejects an HTTP step towards an undeclared host', () => {
213
- const raw = validDefinition();
214
- (raw.steps[1] as any).then[0].url = 'https://evil.example.org/x';
215
- expect(pathsOf(raw)).toEqual(['steps[1].then[0]']);
216
- });
217
-
218
- it('does not let a wildcard declaration cover the apex host', () => {
219
- const raw = validDefinition();
220
- (raw.steps[2] as any).cases[1].steps[0].url = 'https://hooks.example.com/ping';
221
- expect(pathsOf(raw)).toEqual(['steps[2].cases[1].steps[0]']);
222
- });
223
-
224
- it('skips the host check when the URL is a template', () => {
225
- const raw = validDefinition();
226
- (raw.steps[1] as any).then[0].url = 'https://{{ input.host }}/x';
227
- expect(validateDefinition(raw, catalog).ok).toBe(true);
228
- });
229
-
230
- it('requires an HTTP declaration for any HTTP step', () => {
231
- const raw = validDefinition();
232
- raw.accesses = raw.accesses.filter((access) => access.kind !== AccessKind.HTTP);
233
- (raw.steps[1] as any).then[0].url = '{{ input.url }}';
234
- expect(pathsOf(raw)).toContain('steps[1].then[0]');
235
- });
236
-
237
- it('rejects a HOST step whose action is not declared', () => {
238
- const raw = validDefinition();
239
- raw.accesses = raw.accesses.filter((access) => access.kind !== AccessKind.HOST_ACTION);
240
- expect(pathsOf(raw)).toEqual(['steps[1].then[1]']);
241
- });
242
-
243
- it('rejects a WAIT with both durationMs and until', () => {
244
- const raw = validDefinition();
245
- (raw.steps[1] as any).else[0].until = '2026-12-31T00:00:00Z';
246
- expect(pathsOf(raw)).toEqual(['steps[1].else[0]']);
247
- });
248
-
249
- it('rejects a WAIT with neither durationMs nor until', () => {
250
- const raw = validDefinition();
251
- delete (raw.steps[1] as any).else[0].durationMs;
252
- expect(pathsOf(raw)).toEqual(['steps[1].else[0]']);
253
- });
254
-
255
- it('rejects an object carrying $expression and another key', () => {
256
- const raw = validDefinition();
257
- (raw.steps[0] as any).value = { $expression: '1 + 1', other: 2 };
258
- expect(pathsOf(raw)).toEqual(['steps[0].value']);
259
- });
260
-
261
- it('rejects a $expression that is not a string', () => {
262
- const raw = validDefinition();
263
- (raw.steps[0] as any).value = { $expression: 3 };
264
- expect(pathsOf(raw)).toEqual(['steps[0].value']);
265
- });
266
-
267
- it('rejects an invalid SET variable, an empty id and an unknown STOP status', () => {
268
- const raw = validDefinition();
269
- (raw.steps[0] as any).variable = 'not valid';
270
- (raw.steps[3] as any).id = '';
271
- (raw.steps[2] as any).default[0].status = 'MAYBE';
272
- expect(pathsOf(raw).sort()).toEqual(
273
- ['steps[0].variable', 'steps[2].default[0].status', 'steps[3].id'].sort(),
274
- );
275
- });
276
-
277
- it('rejects a SWITCH without case', () => {
278
- const raw = validDefinition();
279
- (raw.steps[2] as any).cases = [];
280
- expect(pathsOf(raw)).toEqual(['steps[2].cases']);
281
- });
282
-
283
- it('bounds the HTTP timeout to a positive integer of at most 30000', () => {
284
- const tooLong = validDefinition();
285
- (tooLong.steps[1] as any).then[0].timeoutMs = 30001;
286
- expect(pathsOf(tooLong)).toEqual(['steps[1].then[0].timeoutMs']);
287
- const fractional = validDefinition();
288
- (fractional.steps[1] as any).then[0].timeoutMs = 1.5;
289
- expect(pathsOf(fractional)).toEqual(['steps[1].then[0].timeoutMs']);
290
- });
291
-
292
- it('rejects HTTP headers that are not value sources', () => {
293
- const raw = validDefinition();
294
- (raw.steps[1] as any).then[0].headers = { 'X-Bad': { $expression: 'a', extra: 1 } };
295
- expect(pathsOf(raw)).toEqual(['steps[1].then[0].headers.X-Bad']);
296
- });
297
-
298
- it('rejects a malformed access declaration', () => {
299
- const raw = validDefinition();
300
- (raw.accesses[0] as any).hosts = ['https://api.example.com/'];
301
- (raw.accesses as any[]).push({ kind: 'FTP' });
302
- const paths = pathsOf(raw);
303
- expect(paths).toContain('accesses[0].hosts[0]');
304
- expect(paths).toContain('accesses[2].kind');
305
- });
306
-
307
- it('rejects an unknown step type and a non-object step', () => {
308
- const raw = validDefinition();
309
- (raw.steps as any[]).push({ id: 'x', type: 'LOOP' }, 'oops');
310
- expect(pathsOf(raw)).toEqual(['steps[4].type', 'steps[5]']);
311
- });
312
-
313
- it('collects every error instead of stopping at the first', () => {
314
- const raw = validDefinition();
315
- (raw.steps[1] as any).then[1].action = 'send-sms';
316
- (raw.steps[1] as any).then[0].url = 'https://evil.example.org/x';
317
- (raw.steps[0] as any).variable = '1x';
318
- expect(errorsOf(raw).length).toBeGreaterThanOrEqual(3);
319
- });
320
-
321
- it('rejects a document that is not an object', () => {
322
- expect(pathsOf('nope')).toEqual(['']);
323
- expect(pathsOf(null)).toEqual(['']);
324
- });
325
-
326
- it('does not mutate its input', () => {
327
- const raw = validDefinition();
328
- const copy = clone(raw);
329
- validateDefinition(raw, catalog);
330
- expect(raw).toEqual(copy);
331
- });
332
- });
333
-
334
- describe('walkSteps', () => {
335
- it('visits steps in document order with their JSON path', () => {
336
- const visited: string[] = [];
337
- walkSteps(validDefinition().steps as never, (step, path) => visited.push(`${path}:${step.id}`));
338
- expect(visited).toEqual([
339
- 'steps[0]:init',
340
- 'steps[1]:check',
341
- 'steps[1].then[0]:fetch',
342
- 'steps[1].then[1]:mail',
343
- 'steps[1].else[0]:pause',
344
- 'steps[1].else[1]:note',
345
- 'steps[1].else[2]:reply',
346
- 'steps[2]:route',
347
- 'steps[2].cases[0].steps[0]:compute',
348
- 'steps[2].cases[0].steps[1]:later',
349
- 'steps[2].cases[1].steps[0]:hook',
350
- 'steps[2].default[0]:fail',
351
- 'steps[3]:done',
352
- ]);
353
- });
354
- });
1
+ import Joi from 'joi';
2
+ import { AccessKind } from '../../access/access.types';
3
+ import {
4
+ CURRENT_FORMAT_VERSION,
5
+ HttpMethod,
6
+ LogLevel,
7
+ StepType,
8
+ StopStatus,
9
+ } from '../definition.types';
10
+ import type { HostActionCatalog } from '../definition.types';
11
+ import { MAX_WAIT_DURATION_MS } from '../definition.schema';
12
+ import { validateDefinition } from '../validate-definition';
13
+ import { walkSteps } from '../walk-steps';
14
+
15
+ const catalog: HostActionCatalog = new Map([
16
+ [
17
+ 'send-email',
18
+ {
19
+ name: 'send-email',
20
+ inputSchema: Joi.object({ to: Joi.string().required(), subject: Joi.string() }),
21
+ },
22
+ ],
23
+ ]);
24
+
25
+ const clone = <T>(value: T): T => JSON.parse(JSON.stringify(value));
26
+
27
+ const validDefinition = () => ({
28
+ formatVersion: CURRENT_FORMAT_VERSION,
29
+ accesses: [
30
+ { kind: AccessKind.HTTP, hosts: ['api.example.com', '*.hooks.example.com'] },
31
+ { kind: AccessKind.HOST_ACTION, action: 'send-email' },
32
+ ],
33
+ steps: [
34
+ { id: 'init', type: StepType.SET, variable: 'count', value: 0 },
35
+ {
36
+ id: 'check',
37
+ type: StepType.CONDITION,
38
+ if: { $expression: 'input.amount > 10' },
39
+ then: [
40
+ {
41
+ id: 'fetch',
42
+ type: StepType.HTTP,
43
+ method: HttpMethod.POST,
44
+ url: 'https://api.example.com/v1/items',
45
+ headers: { Authorization: 'Bearer {{ input.token }}', 'X-List': ['a', { b: 1 }] },
46
+ body: { items: [1, 2, { $expression: 'input.items' }] },
47
+ timeoutMs: 5000,
48
+ },
49
+ {
50
+ id: 'mail',
51
+ type: StepType.HOST,
52
+ action: 'send-email',
53
+ input: { to: '{{ input.email }}', subject: 'Hello' },
54
+ },
55
+ ],
56
+ else: [
57
+ { id: 'pause', type: StepType.WAIT, durationMs: 1000 },
58
+ {
59
+ id: 'note',
60
+ type: StepType.LOG,
61
+ level: LogLevel.WARN,
62
+ message: 'small amount: {{ input.amount }}',
63
+ data: { amount: { $expression: 'input.amount' } },
64
+ },
65
+ {
66
+ id: 'reply',
67
+ type: StepType.RESPOND,
68
+ status: 202,
69
+ headers: { 'Content-Type': 'application/json' },
70
+ body: { accepted: true },
71
+ },
72
+ ],
73
+ },
74
+ {
75
+ id: 'route',
76
+ type: StepType.SWITCH,
77
+ value: '{{ input.kind }}',
78
+ cases: [
79
+ {
80
+ match: 'a',
81
+ steps: [
82
+ { id: 'compute', type: StepType.CODE, source: 'return input.n * 2;' },
83
+ { id: 'later', type: StepType.WAIT, until: '2026-12-31T00:00:00Z' },
84
+ ],
85
+ },
86
+ {
87
+ match: null,
88
+ steps: [
89
+ {
90
+ id: 'hook',
91
+ type: StepType.HTTP,
92
+ method: HttpMethod.GET,
93
+ url: 'https://x.hooks.example.com/ping',
94
+ },
95
+ ],
96
+ },
97
+ ],
98
+ default: [{ id: 'fail', type: StepType.STOP, status: StopStatus.FAILED, output: 'unknown' }],
99
+ },
100
+ { id: 'done', type: StepType.STOP, status: StopStatus.SUCCEEDED },
101
+ ],
102
+ });
103
+
104
+ const errorsOf = (raw: unknown) => {
105
+ const result = validateDefinition(raw, catalog);
106
+ if (result.ok) {
107
+ throw new Error('expected a refusal');
108
+ }
109
+ return result.errors;
110
+ };
111
+
112
+ const pathsOf = (raw: unknown) => errorsOf(raw).map((error) => error.path);
113
+
114
+ describe('validateDefinition', () => {
115
+ it('accepts a complete definition with the 10 step types and nested branches', () => {
116
+ const raw = validDefinition();
117
+ const result = validateDefinition(raw, catalog);
118
+ expect(result).toEqual({ ok: true, definition: raw });
119
+ const types = new Set<string>();
120
+ walkSteps(raw.steps as never, (step) => types.add(step.type));
121
+ expect([...types].sort()).toEqual(Object.values(StepType).sort());
122
+ });
123
+
124
+ it('rejects an unknown formatVersion at "formatVersion"', () => {
125
+ const raw = { ...validDefinition(), formatVersion: 2 };
126
+ expect(pathsOf(raw)).toEqual(['formatVersion']);
127
+ });
128
+
129
+ it('rejects a duplicated id on the second occurrence', () => {
130
+ const raw = validDefinition();
131
+ (raw.steps[1] as any).then[0].id = 'init';
132
+ expect(pathsOf(raw)).toEqual(['steps[1].then[0].id']);
133
+ });
134
+
135
+ it('rejects __proto__, constructor and prototype as step id and as SET variable', () => {
136
+ for (const name of ['__proto__', 'constructor', 'prototype']) {
137
+ const asId = validDefinition();
138
+ (asId.steps[0] as any).id = name;
139
+ expect(pathsOf(asId)).toEqual(['steps[0].id']);
140
+ const asVariable = validDefinition();
141
+ (asVariable.steps[0] as any).variable = name;
142
+ expect(pathsOf(asVariable)).toEqual(['steps[0].variable']);
143
+ }
144
+ });
145
+
146
+ it('bounds a literal WAIT duration to MAX_WAIT_DURATION_MS', () => {
147
+ expect(MAX_WAIT_DURATION_MS).toBe(30 * 24 * 60 * 60 * 1000);
148
+ const at = (durationMs: unknown) => ({
149
+ ...validDefinition(),
150
+ steps: [{ id: 'w', type: StepType.WAIT, durationMs }],
151
+ });
152
+ expect(validateDefinition(at(MAX_WAIT_DURATION_MS), catalog).ok).toBe(true);
153
+ expect(validateDefinition(at({ $expression: 'input.d' }), catalog).ok).toBe(true);
154
+ expect(validateDefinition(at('{{ input.d }}'), catalog).ok).toBe(true);
155
+ for (const bad of [MAX_WAIT_DURATION_MS + 1, -1, 1.5]) {
156
+ expect(pathsOf(at(bad))).toEqual(['steps[0].durationMs']);
157
+ }
158
+ });
159
+
160
+ it('requires a literal WAIT until to be an ISO 8601 date', () => {
161
+ const at = (until: unknown) => ({
162
+ ...validDefinition(),
163
+ steps: [{ id: 'w', type: StepType.WAIT, until }],
164
+ });
165
+ expect(validateDefinition(at('2030-01-01T00:00:00.000Z'), catalog).ok).toBe(true);
166
+ expect(validateDefinition(at('{{ input.when }}'), catalog).ok).toBe(true);
167
+ expect(validateDefinition(at({ $expression: 'input.when' }), catalog).ok).toBe(true);
168
+ expect(pathsOf(at('tomorrow'))).toEqual(['steps[0].until']);
169
+ expect(pathsOf(at(12))).toEqual(['steps[0].until']);
170
+ });
171
+
172
+ it('requires a LOG level among info, warn, error and a message', () => {
173
+ const at = (step: Record<string, unknown>) => ({
174
+ ...validDefinition(),
175
+ steps: [{ id: 'l', type: StepType.LOG, ...step }],
176
+ });
177
+ expect(
178
+ validateDefinition(at({ level: 'info', message: { $expression: 'input' } }), catalog).ok,
179
+ ).toBe(true);
180
+ expect(pathsOf(at({ level: 'debug', message: 'x' }))).toEqual(['steps[0].level']);
181
+ expect(pathsOf(at({ level: 'error' }))).toEqual(['steps[0].message']);
182
+ });
183
+
184
+ it('requires a RESPOND status, a literal one from 200 to 599', () => {
185
+ const at = (status: unknown) => ({
186
+ ...validDefinition(),
187
+ steps: [{ id: 'r', type: StepType.RESPOND, status }],
188
+ });
189
+ for (const good of [200, 201, 599, '{{ input.status }}', { $expression: 'input.status' }]) {
190
+ expect(validateDefinition(at(good), catalog).ok).toBe(true);
191
+ }
192
+ for (const bad of [100, 199, 600, 200.5, 'ok']) {
193
+ expect(pathsOf(at(bad))).toEqual(['steps[0].status']);
194
+ }
195
+ const missing = { ...validDefinition(), steps: [{ id: 'r', type: StepType.RESPOND }] };
196
+ expect(pathsOf(missing)).toEqual(['steps[0].status']);
197
+ });
198
+
199
+ it('rejects an action absent from the catalog at the path of "action"', () => {
200
+ const raw = validDefinition();
201
+ (raw.steps[1] as any).then[1].action = 'send-sms';
202
+ const paths = pathsOf(raw);
203
+ expect(paths).toContain('steps[1].then[1].action');
204
+ });
205
+
206
+ it('rejects an input key outside the action schema', () => {
207
+ const raw = validDefinition();
208
+ (raw.steps[1] as any).then[1].input.cc = 'x@y.z';
209
+ expect(pathsOf(raw)).toEqual(['steps[1].then[1].input.cc']);
210
+ });
211
+
212
+ it('rejects an HTTP step towards an undeclared host', () => {
213
+ const raw = validDefinition();
214
+ (raw.steps[1] as any).then[0].url = 'https://evil.example.org/x';
215
+ expect(pathsOf(raw)).toEqual(['steps[1].then[0]']);
216
+ });
217
+
218
+ it('does not let a wildcard declaration cover the apex host', () => {
219
+ const raw = validDefinition();
220
+ (raw.steps[2] as any).cases[1].steps[0].url = 'https://hooks.example.com/ping';
221
+ expect(pathsOf(raw)).toEqual(['steps[2].cases[1].steps[0]']);
222
+ });
223
+
224
+ it('skips the host check when the URL is a template', () => {
225
+ const raw = validDefinition();
226
+ (raw.steps[1] as any).then[0].url = 'https://{{ input.host }}/x';
227
+ expect(validateDefinition(raw, catalog).ok).toBe(true);
228
+ });
229
+
230
+ it('requires an HTTP declaration for any HTTP step', () => {
231
+ const raw = validDefinition();
232
+ raw.accesses = raw.accesses.filter((access) => access.kind !== AccessKind.HTTP);
233
+ (raw.steps[1] as any).then[0].url = '{{ input.url }}';
234
+ expect(pathsOf(raw)).toContain('steps[1].then[0]');
235
+ });
236
+
237
+ it('rejects a HOST step whose action is not declared', () => {
238
+ const raw = validDefinition();
239
+ raw.accesses = raw.accesses.filter((access) => access.kind !== AccessKind.HOST_ACTION);
240
+ expect(pathsOf(raw)).toEqual(['steps[1].then[1]']);
241
+ });
242
+
243
+ it('rejects a WAIT with both durationMs and until', () => {
244
+ const raw = validDefinition();
245
+ (raw.steps[1] as any).else[0].until = '2026-12-31T00:00:00Z';
246
+ expect(pathsOf(raw)).toEqual(['steps[1].else[0]']);
247
+ });
248
+
249
+ it('rejects a WAIT with neither durationMs nor until', () => {
250
+ const raw = validDefinition();
251
+ delete (raw.steps[1] as any).else[0].durationMs;
252
+ expect(pathsOf(raw)).toEqual(['steps[1].else[0]']);
253
+ });
254
+
255
+ it('rejects an object carrying $expression and another key', () => {
256
+ const raw = validDefinition();
257
+ (raw.steps[0] as any).value = { $expression: '1 + 1', other: 2 };
258
+ expect(pathsOf(raw)).toEqual(['steps[0].value']);
259
+ });
260
+
261
+ it('rejects a $expression that is not a string', () => {
262
+ const raw = validDefinition();
263
+ (raw.steps[0] as any).value = { $expression: 3 };
264
+ expect(pathsOf(raw)).toEqual(['steps[0].value']);
265
+ });
266
+
267
+ it('rejects an invalid SET variable, an empty id and an unknown STOP status', () => {
268
+ const raw = validDefinition();
269
+ (raw.steps[0] as any).variable = 'not valid';
270
+ (raw.steps[3] as any).id = '';
271
+ (raw.steps[2] as any).default[0].status = 'MAYBE';
272
+ expect(pathsOf(raw).sort()).toEqual(
273
+ ['steps[0].variable', 'steps[2].default[0].status', 'steps[3].id'].sort(),
274
+ );
275
+ });
276
+
277
+ it('rejects a SWITCH without case', () => {
278
+ const raw = validDefinition();
279
+ (raw.steps[2] as any).cases = [];
280
+ expect(pathsOf(raw)).toEqual(['steps[2].cases']);
281
+ });
282
+
283
+ it('bounds the HTTP timeout to a positive integer of at most 30000', () => {
284
+ const tooLong = validDefinition();
285
+ (tooLong.steps[1] as any).then[0].timeoutMs = 30001;
286
+ expect(pathsOf(tooLong)).toEqual(['steps[1].then[0].timeoutMs']);
287
+ const fractional = validDefinition();
288
+ (fractional.steps[1] as any).then[0].timeoutMs = 1.5;
289
+ expect(pathsOf(fractional)).toEqual(['steps[1].then[0].timeoutMs']);
290
+ });
291
+
292
+ it('rejects HTTP headers that are not value sources', () => {
293
+ const raw = validDefinition();
294
+ (raw.steps[1] as any).then[0].headers = { 'X-Bad': { $expression: 'a', extra: 1 } };
295
+ expect(pathsOf(raw)).toEqual(['steps[1].then[0].headers.X-Bad']);
296
+ });
297
+
298
+ it('rejects a malformed access declaration', () => {
299
+ const raw = validDefinition();
300
+ (raw.accesses[0] as any).hosts = ['https://api.example.com/'];
301
+ (raw.accesses as any[]).push({ kind: 'FTP' });
302
+ const paths = pathsOf(raw);
303
+ expect(paths).toContain('accesses[0].hosts[0]');
304
+ expect(paths).toContain('accesses[2].kind');
305
+ });
306
+
307
+ it('rejects an unknown step type and a non-object step', () => {
308
+ const raw = validDefinition();
309
+ (raw.steps as any[]).push({ id: 'x', type: 'LOOP' }, 'oops');
310
+ expect(pathsOf(raw)).toEqual(['steps[4].type', 'steps[5]']);
311
+ });
312
+
313
+ it('collects every error instead of stopping at the first', () => {
314
+ const raw = validDefinition();
315
+ (raw.steps[1] as any).then[1].action = 'send-sms';
316
+ (raw.steps[1] as any).then[0].url = 'https://evil.example.org/x';
317
+ (raw.steps[0] as any).variable = '1x';
318
+ expect(errorsOf(raw).length).toBeGreaterThanOrEqual(3);
319
+ });
320
+
321
+ it('rejects a document that is not an object', () => {
322
+ expect(pathsOf('nope')).toEqual(['']);
323
+ expect(pathsOf(null)).toEqual(['']);
324
+ });
325
+
326
+ it('does not mutate its input', () => {
327
+ const raw = validDefinition();
328
+ const copy = clone(raw);
329
+ validateDefinition(raw, catalog);
330
+ expect(raw).toEqual(copy);
331
+ });
332
+ });
333
+
334
+ describe('walkSteps', () => {
335
+ it('visits steps in document order with their JSON path', () => {
336
+ const visited: string[] = [];
337
+ walkSteps(validDefinition().steps as never, (step, path) => visited.push(`${path}:${step.id}`));
338
+ expect(visited).toEqual([
339
+ 'steps[0]:init',
340
+ 'steps[1]:check',
341
+ 'steps[1].then[0]:fetch',
342
+ 'steps[1].then[1]:mail',
343
+ 'steps[1].else[0]:pause',
344
+ 'steps[1].else[1]:note',
345
+ 'steps[1].else[2]:reply',
346
+ 'steps[2]:route',
347
+ 'steps[2].cases[0].steps[0]:compute',
348
+ 'steps[2].cases[0].steps[1]:later',
349
+ 'steps[2].cases[1].steps[0]:hook',
350
+ 'steps[2].default[0]:fail',
351
+ 'steps[3]:done',
352
+ ]);
353
+ });
354
+ });