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