@multiplier-labs/stepflow 0.3.1

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 (76) hide show
  1. package/.github/workflows/publish.yml +30 -0
  2. package/README.md +408 -0
  3. package/dist/chunk-FQCT3I2A.js +1512 -0
  4. package/dist/chunk-FQCT3I2A.js.map +1 -0
  5. package/dist/chunk-GXY66V7Q.js +111 -0
  6. package/dist/chunk-GXY66V7Q.js.map +1 -0
  7. package/dist/chunk-U5QGN3RT.js +715 -0
  8. package/dist/chunk-U5QGN3RT.js.map +1 -0
  9. package/dist/chunk-XCF4PLN7.js +575 -0
  10. package/dist/chunk-XCF4PLN7.js.map +1 -0
  11. package/dist/events/index.d.ts +346 -0
  12. package/dist/events/index.js +11 -0
  13. package/dist/events/index.js.map +1 -0
  14. package/dist/index-CPfGsmrp.d.ts +567 -0
  15. package/dist/index.d.ts +748 -0
  16. package/dist/index.js +1707 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/scheduler/index.d.ts +6 -0
  19. package/dist/scheduler/index.js +12 -0
  20. package/dist/scheduler/index.js.map +1 -0
  21. package/dist/storage/index.d.ts +402 -0
  22. package/dist/storage/index.js +13 -0
  23. package/dist/storage/index.js.map +1 -0
  24. package/dist/types-BVShq7zT.d.ts +150 -0
  25. package/dist/types-BlwFEGzj.d.ts +276 -0
  26. package/dist/types-DzkEqnQJ.d.ts +75 -0
  27. package/docs/github-packages-setup.md +97 -0
  28. package/docs/stepflow-api-reference.md +1662 -0
  29. package/package.json +85 -0
  30. package/src/core/engine.test.ts +1421 -0
  31. package/src/core/engine.ts +717 -0
  32. package/src/core/index.ts +7 -0
  33. package/src/core/orchestrator.ts +590 -0
  34. package/src/core/types.ts +242 -0
  35. package/src/events/index.ts +8 -0
  36. package/src/events/memory.test.ts +109 -0
  37. package/src/events/memory.ts +95 -0
  38. package/src/events/socketio.test.ts +428 -0
  39. package/src/events/socketio.ts +269 -0
  40. package/src/events/types.ts +121 -0
  41. package/src/events/webhook.test.ts +695 -0
  42. package/src/events/webhook.ts +591 -0
  43. package/src/index.ts +270 -0
  44. package/src/planning/index.ts +52 -0
  45. package/src/planning/planner.ts +601 -0
  46. package/src/planning/planning.test.ts +790 -0
  47. package/src/planning/registry.test.ts +445 -0
  48. package/src/planning/registry.ts +369 -0
  49. package/src/planning/types.ts +570 -0
  50. package/src/scheduler/cron.test.ts +480 -0
  51. package/src/scheduler/cron.ts +449 -0
  52. package/src/scheduler/index.ts +18 -0
  53. package/src/scheduler/postgres-persistence.test.ts +906 -0
  54. package/src/scheduler/postgres-persistence.ts +546 -0
  55. package/src/scheduler/sqlite-persistence.test.ts +235 -0
  56. package/src/scheduler/sqlite-persistence.ts +268 -0
  57. package/src/scheduler/types.ts +78 -0
  58. package/src/storage/index.ts +27 -0
  59. package/src/storage/memory.test.ts +399 -0
  60. package/src/storage/memory.ts +248 -0
  61. package/src/storage/postgres.ts +1550 -0
  62. package/src/storage/sqlite.test.ts +593 -0
  63. package/src/storage/sqlite.ts +662 -0
  64. package/src/storage/types.ts +368 -0
  65. package/src/utils/errors.test.ts +152 -0
  66. package/src/utils/errors.ts +191 -0
  67. package/src/utils/id.ts +21 -0
  68. package/src/utils/index.ts +8 -0
  69. package/src/utils/logger.test.ts +127 -0
  70. package/src/utils/logger.ts +96 -0
  71. package/src/utils/postgres-deps.ts +63 -0
  72. package/src/utils/retry.test.ts +197 -0
  73. package/src/utils/retry.ts +121 -0
  74. package/tsconfig.json +21 -0
  75. package/tsup.config.ts +14 -0
  76. package/vitest.config.ts +9 -0
@@ -0,0 +1,790 @@
1
+ import { describe, it, expect, beforeEach } from "vitest";
2
+ import {
3
+ MemoryRecipeRegistry,
4
+ MemoryStepHandlerRegistry,
5
+ createRegistry,
6
+ } from "./registry";
7
+ import { RuleBasedPlanner } from "./planner";
8
+ import type { Recipe, RecipeCondition, PlanningContext } from "./types";
9
+
10
+ describe("MemoryRecipeRegistry", () => {
11
+ let registry: MemoryRecipeRegistry;
12
+
13
+ beforeEach(() => {
14
+ registry = new MemoryRecipeRegistry();
15
+ });
16
+
17
+ const createTestRecipe = (overrides: Partial<Recipe> = {}): Recipe => ({
18
+ id: "test.recipe",
19
+ name: "Test Recipe",
20
+ workflowKind: "test.workflow",
21
+ variant: "default",
22
+ steps: [{ key: "step1", name: "Step 1", handlerRef: "handlers.step1" }],
23
+ ...overrides,
24
+ });
25
+
26
+ describe("register", () => {
27
+ it("should register a recipe", () => {
28
+ const recipe = createTestRecipe();
29
+ registry.register(recipe);
30
+
31
+ expect(registry.has("test.recipe")).toBe(true);
32
+ expect(registry.get("test.recipe")).toEqual(recipe);
33
+ });
34
+
35
+ it("should throw if recipe already registered", () => {
36
+ const recipe = createTestRecipe();
37
+ registry.register(recipe);
38
+
39
+ expect(() => registry.register(recipe)).toThrow(
40
+ "Recipe 'test.recipe' is already registered",
41
+ );
42
+ });
43
+
44
+ it("should throw if variant already exists for workflow kind", () => {
45
+ const recipe1 = createTestRecipe({ id: "recipe1" });
46
+ const recipe2 = createTestRecipe({ id: "recipe2" });
47
+
48
+ registry.register(recipe1);
49
+
50
+ expect(() => registry.register(recipe2)).toThrow(
51
+ "Recipe variant 'default' for 'test.workflow' is already registered",
52
+ );
53
+ });
54
+ });
55
+
56
+ describe("getByKind", () => {
57
+ it("should return all recipes for a workflow kind", () => {
58
+ registry.register(
59
+ createTestRecipe({ id: "recipe1", variant: "default" }),
60
+ );
61
+ registry.register(createTestRecipe({ id: "recipe2", variant: "fast" }));
62
+ registry.register(
63
+ createTestRecipe({
64
+ id: "recipe3",
65
+ workflowKind: "other.workflow",
66
+ variant: "default",
67
+ }),
68
+ );
69
+
70
+ const recipes = registry.getByKind("test.workflow");
71
+ expect(recipes).toHaveLength(2);
72
+ expect(recipes.map((r) => r.id)).toContain("recipe1");
73
+ expect(recipes.map((r) => r.id)).toContain("recipe2");
74
+ });
75
+
76
+ it("should return empty array if no recipes found", () => {
77
+ expect(registry.getByKind("nonexistent")).toEqual([]);
78
+ });
79
+ });
80
+
81
+ describe("getVariant", () => {
82
+ it("should return specific variant", () => {
83
+ registry.register(
84
+ createTestRecipe({ id: "recipe1", variant: "default" }),
85
+ );
86
+ registry.register(createTestRecipe({ id: "recipe2", variant: "fast" }));
87
+
88
+ const recipe = registry.getVariant("test.workflow", "fast");
89
+ expect(recipe?.id).toBe("recipe2");
90
+ });
91
+
92
+ it("should return undefined if variant not found", () => {
93
+ registry.register(createTestRecipe());
94
+
95
+ expect(
96
+ registry.getVariant("test.workflow", "nonexistent"),
97
+ ).toBeUndefined();
98
+ });
99
+ });
100
+
101
+ describe("getDefault", () => {
102
+ it("should return default variant if exists", () => {
103
+ registry.register(
104
+ createTestRecipe({ id: "recipe1", variant: "default" }),
105
+ );
106
+ registry.register(createTestRecipe({ id: "recipe2", variant: "fast" }));
107
+
108
+ const recipe = registry.getDefault("test.workflow");
109
+ expect(recipe?.variant).toBe("default");
110
+ });
111
+
112
+ it("should return lowest priority recipe if no default variant", () => {
113
+ registry.register(
114
+ createTestRecipe({ id: "recipe1", variant: "fast", priority: 10 }),
115
+ );
116
+ registry.register(
117
+ createTestRecipe({ id: "recipe2", variant: "slow", priority: 5 }),
118
+ );
119
+
120
+ const recipe = registry.getDefault("test.workflow");
121
+ expect(recipe?.id).toBe("recipe2");
122
+ });
123
+ });
124
+
125
+ describe("listVariants", () => {
126
+ it("should list all variants for a workflow kind", () => {
127
+ registry.register(
128
+ createTestRecipe({ id: "recipe1", variant: "default" }),
129
+ );
130
+ registry.register(createTestRecipe({ id: "recipe2", variant: "fast" }));
131
+ registry.register(
132
+ createTestRecipe({ id: "recipe3", variant: "thorough" }),
133
+ );
134
+
135
+ const variants = registry.listVariants("test.workflow");
136
+ expect(variants).toHaveLength(3);
137
+ expect(variants).toContain("default");
138
+ expect(variants).toContain("fast");
139
+ expect(variants).toContain("thorough");
140
+ });
141
+ });
142
+
143
+ describe("query", () => {
144
+ it("should filter by workflow kind", () => {
145
+ registry.register(createTestRecipe({ id: "recipe1", variant: "v1" }));
146
+ registry.register(
147
+ createTestRecipe({
148
+ id: "recipe2",
149
+ workflowKind: "other",
150
+ variant: "v1",
151
+ }),
152
+ );
153
+
154
+ const results = registry.query({ workflowKind: "test.workflow" });
155
+ expect(results).toHaveLength(1);
156
+ expect(results[0].id).toBe("recipe1");
157
+ });
158
+
159
+ it("should filter by tags", () => {
160
+ registry.register(
161
+ createTestRecipe({
162
+ id: "recipe1",
163
+ variant: "v1",
164
+ tags: ["fast", "simple"],
165
+ }),
166
+ );
167
+ registry.register(
168
+ createTestRecipe({
169
+ id: "recipe2",
170
+ variant: "v2",
171
+ tags: ["slow", "complex"],
172
+ }),
173
+ );
174
+
175
+ const results = registry.query({ tags: ["fast"] });
176
+ expect(results).toHaveLength(1);
177
+ expect(results[0].id).toBe("recipe1");
178
+ });
179
+ });
180
+ });
181
+
182
+ describe("MemoryStepHandlerRegistry", () => {
183
+ let registry: MemoryStepHandlerRegistry;
184
+
185
+ beforeEach(() => {
186
+ registry = new MemoryStepHandlerRegistry();
187
+ });
188
+
189
+ it("should register and retrieve handlers", () => {
190
+ const handler = {
191
+ id: "test.handler",
192
+ description: "Test handler",
193
+ handler: async () => ({ result: "test" }),
194
+ };
195
+
196
+ registry.register(handler);
197
+
198
+ expect(registry.has("test.handler")).toBe(true);
199
+ expect(registry.get("test.handler")).toEqual(handler);
200
+ });
201
+
202
+ it("should throw if handler already registered", () => {
203
+ const handler = {
204
+ id: "test.handler",
205
+ handler: async () => ({}),
206
+ };
207
+
208
+ registry.register(handler);
209
+
210
+ expect(() => registry.register(handler)).toThrow(
211
+ "Step handler 'test.handler' is already registered",
212
+ );
213
+ });
214
+
215
+ it("should list handlers by tag", () => {
216
+ registry.register({
217
+ id: "handler1",
218
+ handler: async () => ({}),
219
+ tags: ["llm", "text"],
220
+ });
221
+ registry.register({
222
+ id: "handler2",
223
+ handler: async () => ({}),
224
+ tags: ["data", "text"],
225
+ });
226
+
227
+ const llmHandlers = registry.listByTag("llm");
228
+ expect(llmHandlers).toHaveLength(1);
229
+ expect(llmHandlers[0].id).toBe("handler1");
230
+
231
+ const textHandlers = registry.listByTag("text");
232
+ expect(textHandlers).toHaveLength(2);
233
+ });
234
+ });
235
+
236
+ describe("RuleBasedPlanner", () => {
237
+ let registry: MemoryRecipeRegistry;
238
+ let planner: RuleBasedPlanner;
239
+
240
+ beforeEach(() => {
241
+ registry = new MemoryRecipeRegistry();
242
+ planner = new RuleBasedPlanner({ recipeRegistry: registry });
243
+ });
244
+
245
+ const createTestRecipe = (overrides: Partial<Recipe> = {}): Recipe => ({
246
+ id: "test.recipe",
247
+ name: "Test Recipe",
248
+ workflowKind: "test.workflow",
249
+ variant: "default",
250
+ steps: [
251
+ { key: "step1", name: "Step 1", handlerRef: "handlers.step1" },
252
+ { key: "step2", name: "Step 2", handlerRef: "handlers.step2" },
253
+ ],
254
+ ...overrides,
255
+ });
256
+
257
+ describe("selectRecipe", () => {
258
+ it("should select recipe with matching conditions", async () => {
259
+ const conditions: RecipeCondition[] = [
260
+ { field: "priority", operator: "eq", value: "speed" },
261
+ ];
262
+
263
+ registry.register(
264
+ createTestRecipe({ id: "default", variant: "default" }),
265
+ );
266
+ registry.register(
267
+ createTestRecipe({
268
+ id: "fast",
269
+ variant: "fast",
270
+ conditions,
271
+ priority: 10,
272
+ }),
273
+ );
274
+
275
+ const result = await planner.selectRecipe("test.workflow", {
276
+ priority: "speed",
277
+ });
278
+
279
+ expect(result.recipe.id).toBe("fast");
280
+ expect(result.score).toBeGreaterThan(0);
281
+ });
282
+
283
+ it("should fall back to default if no conditions match", async () => {
284
+ registry.register(
285
+ createTestRecipe({ id: "default", variant: "default" }),
286
+ );
287
+ registry.register(
288
+ createTestRecipe({
289
+ id: "fast",
290
+ variant: "fast",
291
+ conditions: [{ field: "priority", operator: "eq", value: "speed" }],
292
+ }),
293
+ );
294
+
295
+ const result = await planner.selectRecipe("test.workflow", {
296
+ priority: "quality",
297
+ });
298
+
299
+ expect(result.recipe.id).toBe("default");
300
+ });
301
+
302
+ it("should use forced recipe from hints", async () => {
303
+ registry.register(
304
+ createTestRecipe({ id: "default", variant: "default" }),
305
+ );
306
+ registry.register(
307
+ createTestRecipe({ id: "specific", variant: "specific" }),
308
+ );
309
+
310
+ const context: PlanningContext = {
311
+ hints: { forceRecipeId: "specific" },
312
+ };
313
+
314
+ const result = await planner.selectRecipe("test.workflow", {}, context);
315
+
316
+ expect(result.recipe.id).toBe("specific");
317
+ expect(result.score).toBe(100);
318
+ });
319
+
320
+ it("should use preferred variant from hints", async () => {
321
+ registry.register(
322
+ createTestRecipe({ id: "default", variant: "default" }),
323
+ );
324
+ registry.register(createTestRecipe({ id: "fast", variant: "fast" }));
325
+
326
+ const context: PlanningContext = {
327
+ hints: { preferredVariant: "fast" },
328
+ };
329
+
330
+ const result = await planner.selectRecipe("test.workflow", {}, context);
331
+
332
+ expect(result.recipe.id).toBe("fast");
333
+ });
334
+
335
+ it("should throw if no recipes found", async () => {
336
+ await expect(planner.selectRecipe("nonexistent", {})).rejects.toThrow(
337
+ "No recipes found for workflow kind: nonexistent",
338
+ );
339
+ });
340
+ });
341
+
342
+ describe("generatePlan", () => {
343
+ it("should generate a plan from a recipe", async () => {
344
+ const recipe = createTestRecipe();
345
+
346
+ const plan = await planner.generatePlan(recipe, {});
347
+
348
+ expect(plan.recipeId).toBe("test.recipe");
349
+ expect(plan.variant).toBe("default");
350
+ expect(plan.steps).toHaveLength(2);
351
+ expect(plan.steps[0].key).toBe("step1");
352
+ expect(plan.steps[1].key).toBe("step2");
353
+ });
354
+
355
+ it("should apply skip hints", async () => {
356
+ const recipe = createTestRecipe();
357
+ const context: PlanningContext = {
358
+ hints: { skipSteps: ["step1"] },
359
+ };
360
+
361
+ const plan = await planner.generatePlan(recipe, {}, context);
362
+
363
+ expect(plan.steps).toHaveLength(1);
364
+ expect(plan.steps[0].key).toBe("step2");
365
+ expect(plan.modifications).toContainEqual(
366
+ expect.objectContaining({ type: "remove_step", stepKey: "step1" }),
367
+ );
368
+ });
369
+
370
+ it("should apply additional config from hints", async () => {
371
+ const recipe = createTestRecipe();
372
+ const context: PlanningContext = {
373
+ hints: { additionalConfig: { depth: "comprehensive" } },
374
+ };
375
+
376
+ const plan = await planner.generatePlan(recipe, {}, context);
377
+
378
+ expect(plan.steps[0].config).toEqual({ depth: "comprehensive" });
379
+ expect(plan.steps[1].config).toEqual({ depth: "comprehensive" });
380
+ });
381
+
382
+ it("should apply speed constraints", async () => {
383
+ const recipe = createTestRecipe({
384
+ steps: [
385
+ {
386
+ key: "step1",
387
+ name: "Step 1",
388
+ handlerRef: "h1",
389
+ timeout: 60000,
390
+ maxRetries: 5,
391
+ },
392
+ ],
393
+ });
394
+ const context: PlanningContext = {
395
+ constraints: { priority: "speed" },
396
+ };
397
+
398
+ const plan = await planner.generatePlan(recipe, {}, context);
399
+
400
+ expect(plan.steps[0].timeout).toBeLessThanOrEqual(30000);
401
+ expect(plan.steps[0].maxRetries).toBeLessThanOrEqual(1);
402
+ });
403
+
404
+ it("should apply duration constraints", async () => {
405
+ const recipe = createTestRecipe();
406
+ const context: PlanningContext = {
407
+ constraints: { maxDuration: 10000 },
408
+ };
409
+
410
+ const plan = await planner.generatePlan(recipe, {}, context);
411
+
412
+ // With 2 steps and 10s total, each step should get ~5s
413
+ expect(plan.steps[0].timeout).toBeLessThanOrEqual(5000);
414
+ expect(plan.steps[1].timeout).toBeLessThanOrEqual(5000);
415
+ });
416
+
417
+ it("should apply cost constraints (maxRetries=0)", async () => {
418
+ const recipe = createTestRecipe({
419
+ steps: [
420
+ { key: "step1", name: "Step 1", handlerRef: "h1", maxRetries: 5 },
421
+ { key: "step2", name: "Step 2", handlerRef: "h2", maxRetries: 3 },
422
+ ],
423
+ });
424
+ const context: PlanningContext = {
425
+ constraints: { priority: "cost" },
426
+ };
427
+
428
+ const plan = await planner.generatePlan(recipe, {}, context);
429
+
430
+ expect(plan.steps[0].maxRetries).toBe(0);
431
+ expect(plan.steps[1].maxRetries).toBe(0);
432
+ expect(plan.modifications).toContainEqual(
433
+ expect.objectContaining({
434
+ type: "set_default",
435
+ value: { priority: "cost" },
436
+ }),
437
+ );
438
+ });
439
+
440
+ it("should apply maxDuration to per-step timeouts evenly", async () => {
441
+ const recipe = createTestRecipe({
442
+ steps: [
443
+ { key: "step1", name: "Step 1", handlerRef: "h1" },
444
+ { key: "step2", name: "Step 2", handlerRef: "h2" },
445
+ { key: "step3", name: "Step 3", handlerRef: "h3" },
446
+ { key: "step4", name: "Step 4", handlerRef: "h4" },
447
+ ],
448
+ });
449
+ const context: PlanningContext = {
450
+ constraints: { maxDuration: 20000 },
451
+ };
452
+
453
+ const plan = await planner.generatePlan(recipe, {}, context);
454
+
455
+ // 20000ms / 4 steps = 5000ms each
456
+ for (const step of plan.steps) {
457
+ expect(step.timeout).toBe(5000);
458
+ }
459
+ expect(plan.modifications).toContainEqual(
460
+ expect.objectContaining({
461
+ type: "set_default",
462
+ value: { maxDuration: 20000 },
463
+ }),
464
+ );
465
+ });
466
+
467
+ it("should cap existing step timeout to per-step budget when maxDuration is set", async () => {
468
+ const recipe = createTestRecipe({
469
+ steps: [
470
+ { key: "step1", name: "Step 1", handlerRef: "h1", timeout: 30000 },
471
+ { key: "step2", name: "Step 2", handlerRef: "h2", timeout: 2000 },
472
+ ],
473
+ });
474
+ const context: PlanningContext = {
475
+ constraints: { maxDuration: 10000 },
476
+ };
477
+
478
+ const plan = await planner.generatePlan(recipe, {}, context);
479
+
480
+ // 10000ms / 2 steps = 5000ms budget each
481
+ expect(plan.steps[0].timeout).toBe(5000); // 30000 capped to 5000
482
+ expect(plan.steps[1].timeout).toBe(2000); // 2000 already under budget
483
+ });
484
+ });
485
+
486
+ describe("plan", () => {
487
+ it("should select recipe and generate plan in one call", async () => {
488
+ registry.register(createTestRecipe());
489
+
490
+ const plan = await planner.plan("test.workflow", {});
491
+
492
+ expect(plan.recipeId).toBe("test.recipe");
493
+ expect(plan.steps).toHaveLength(2);
494
+ expect(plan.reasoning).toContain("test.recipe");
495
+ });
496
+ });
497
+
498
+ describe("validatePlan", () => {
499
+ it("should validate a correct plan", async () => {
500
+ registry.register(createTestRecipe());
501
+ const plan = await planner.plan("test.workflow", {});
502
+
503
+ const result = planner.validatePlan(plan);
504
+
505
+ expect(result.valid).toBe(true);
506
+ expect(result.errors).toHaveLength(0);
507
+ });
508
+
509
+ it("should detect missing steps", () => {
510
+ const result = planner.validatePlan({
511
+ id: "plan-1",
512
+ recipeId: "test",
513
+ variant: "default",
514
+ modifications: [],
515
+ steps: [],
516
+ defaults: {},
517
+ createdAt: new Date(),
518
+ });
519
+
520
+ expect(result.valid).toBe(false);
521
+ expect(result.errors).toContain("Plan has no steps");
522
+ });
523
+
524
+ it("should detect duplicate step keys", () => {
525
+ const result = planner.validatePlan({
526
+ id: "plan-1",
527
+ recipeId: "test",
528
+ variant: "default",
529
+ modifications: [],
530
+ steps: [
531
+ { key: "step1", name: "Step 1", handlerRef: "h1", config: {} },
532
+ {
533
+ key: "step1",
534
+ name: "Step 1 Duplicate",
535
+ handlerRef: "h2",
536
+ config: {},
537
+ },
538
+ ],
539
+ defaults: {},
540
+ createdAt: new Date(),
541
+ });
542
+
543
+ expect(result.valid).toBe(false);
544
+ expect(result.errors).toContain("Duplicate step key: step1");
545
+ });
546
+ });
547
+
548
+ describe("estimateResources", () => {
549
+ it("should estimate resources for a plan", async () => {
550
+ registry.register(createTestRecipe());
551
+ const plan = await planner.plan("test.workflow", {});
552
+
553
+ const estimate = planner.estimateResources(plan);
554
+
555
+ expect(estimate.apiCalls).toBeGreaterThan(0);
556
+ expect(estimate.tokens).toBeGreaterThan(0);
557
+ expect(estimate.duration).toBeGreaterThan(0);
558
+ });
559
+ });
560
+ });
561
+
562
+ describe("createRegistry", () => {
563
+ it("should create a combined registry", () => {
564
+ const { recipes, handlers } = createRegistry();
565
+
566
+ expect(recipes).toBeInstanceOf(MemoryRecipeRegistry);
567
+ expect(handlers).toBeInstanceOf(MemoryStepHandlerRegistry);
568
+ });
569
+ });
570
+
571
+ describe("Condition Evaluation", () => {
572
+ let registry: MemoryRecipeRegistry;
573
+ let planner: RuleBasedPlanner;
574
+
575
+ beforeEach(() => {
576
+ registry = new MemoryRecipeRegistry();
577
+ planner = new RuleBasedPlanner({ recipeRegistry: registry });
578
+ });
579
+
580
+ const testConditions = async (
581
+ conditions: RecipeCondition[],
582
+ input: Record<string, unknown>,
583
+ shouldMatch: boolean,
584
+ ) => {
585
+ registry.clear();
586
+ registry.register({
587
+ id: "conditional",
588
+ name: "Conditional Recipe",
589
+ workflowKind: "test.workflow",
590
+ variant: "conditional",
591
+ steps: [{ key: "s1", name: "S1", handlerRef: "h1" }],
592
+ conditions,
593
+ priority: 10,
594
+ });
595
+ registry.register({
596
+ id: "default",
597
+ name: "Default Recipe",
598
+ workflowKind: "test.workflow",
599
+ variant: "default",
600
+ steps: [{ key: "s1", name: "S1", handlerRef: "h1" }],
601
+ });
602
+
603
+ const result = await planner.selectRecipe("test.workflow", input);
604
+ if (shouldMatch) {
605
+ expect(result.recipe.id).toBe("conditional");
606
+ } else {
607
+ expect(result.recipe.id).toBe("default");
608
+ }
609
+ };
610
+
611
+ it("should evaluate eq operator", async () => {
612
+ await testConditions(
613
+ [{ field: "type", operator: "eq", value: "fast" }],
614
+ { type: "fast" },
615
+ true,
616
+ );
617
+ await testConditions(
618
+ [{ field: "type", operator: "eq", value: "fast" }],
619
+ { type: "slow" },
620
+ false,
621
+ );
622
+ });
623
+
624
+ it("should evaluate neq operator", async () => {
625
+ await testConditions(
626
+ [{ field: "type", operator: "neq", value: "slow" }],
627
+ { type: "fast" },
628
+ true,
629
+ );
630
+ await testConditions(
631
+ [{ field: "type", operator: "neq", value: "slow" }],
632
+ { type: "slow" },
633
+ false,
634
+ );
635
+ });
636
+
637
+ it("should evaluate gt operator", async () => {
638
+ await testConditions(
639
+ [{ field: "count", operator: "gt", value: 10 }],
640
+ { count: 15 },
641
+ true,
642
+ );
643
+ await testConditions(
644
+ [{ field: "count", operator: "gt", value: 10 }],
645
+ { count: 5 },
646
+ false,
647
+ );
648
+ });
649
+
650
+ it("should evaluate gte operator", async () => {
651
+ await testConditions(
652
+ [{ field: "count", operator: "gte", value: 10 }],
653
+ { count: 10 },
654
+ true,
655
+ );
656
+ await testConditions(
657
+ [{ field: "count", operator: "gte", value: 10 }],
658
+ { count: 9 },
659
+ false,
660
+ );
661
+ });
662
+
663
+ it("should evaluate lt operator", async () => {
664
+ await testConditions(
665
+ [{ field: "count", operator: "lt", value: 10 }],
666
+ { count: 5 },
667
+ true,
668
+ );
669
+ await testConditions(
670
+ [{ field: "count", operator: "lt", value: 10 }],
671
+ { count: 15 },
672
+ false,
673
+ );
674
+ });
675
+
676
+ it("should evaluate lte operator", async () => {
677
+ await testConditions(
678
+ [{ field: "count", operator: "lte", value: 10 }],
679
+ { count: 10 },
680
+ true,
681
+ );
682
+ await testConditions(
683
+ [{ field: "count", operator: "lte", value: 10 }],
684
+ { count: 11 },
685
+ false,
686
+ );
687
+ });
688
+
689
+ it("should evaluate contains operator for strings", async () => {
690
+ await testConditions(
691
+ [{ field: "name", operator: "contains", value: "test" }],
692
+ { name: "my-test-file" },
693
+ true,
694
+ );
695
+ await testConditions(
696
+ [{ field: "name", operator: "contains", value: "test" }],
697
+ { name: "my-file" },
698
+ false,
699
+ );
700
+ });
701
+
702
+ it("should evaluate contains operator for arrays", async () => {
703
+ await testConditions(
704
+ [{ field: "tags", operator: "contains", value: "urgent" }],
705
+ { tags: ["urgent", "review"] },
706
+ true,
707
+ );
708
+ await testConditions(
709
+ [{ field: "tags", operator: "contains", value: "urgent" }],
710
+ { tags: ["normal"] },
711
+ false,
712
+ );
713
+ });
714
+
715
+ it("should evaluate matches operator", async () => {
716
+ await testConditions(
717
+ [{ field: "email", operator: "matches", value: "^test@" }],
718
+ { email: "test@example.com" },
719
+ true,
720
+ );
721
+ await testConditions(
722
+ [{ field: "email", operator: "matches", value: "^test@" }],
723
+ { email: "user@example.com" },
724
+ false,
725
+ );
726
+ });
727
+
728
+ it("should evaluate exists operator", async () => {
729
+ await testConditions(
730
+ [{ field: "optional", operator: "exists" }],
731
+ { optional: "value" },
732
+ true,
733
+ );
734
+ await testConditions(
735
+ [{ field: "optional", operator: "exists" }],
736
+ { other: "value" },
737
+ false,
738
+ );
739
+ await testConditions(
740
+ [{ field: "optional", operator: "exists" }],
741
+ { optional: "" },
742
+ false,
743
+ );
744
+ });
745
+
746
+ it("should evaluate notExists operator", async () => {
747
+ await testConditions(
748
+ [{ field: "optional", operator: "notExists" }],
749
+ { other: "value" },
750
+ true,
751
+ );
752
+ await testConditions(
753
+ [{ field: "optional", operator: "notExists" }],
754
+ { optional: "value" },
755
+ false,
756
+ );
757
+ });
758
+
759
+ it("should evaluate nested fields", async () => {
760
+ await testConditions(
761
+ [{ field: "config.settings.enabled", operator: "eq", value: true }],
762
+ { config: { settings: { enabled: true } } },
763
+ true,
764
+ );
765
+ await testConditions(
766
+ [{ field: "config.settings.enabled", operator: "eq", value: true }],
767
+ { config: { settings: { enabled: false } } },
768
+ false,
769
+ );
770
+ });
771
+
772
+ it("should require all conditions to match", async () => {
773
+ await testConditions(
774
+ [
775
+ { field: "type", operator: "eq", value: "fast" },
776
+ { field: "count", operator: "gt", value: 5 },
777
+ ],
778
+ { type: "fast", count: 10 },
779
+ true,
780
+ );
781
+ await testConditions(
782
+ [
783
+ { field: "type", operator: "eq", value: "fast" },
784
+ { field: "count", operator: "gt", value: 5 },
785
+ ],
786
+ { type: "fast", count: 3 },
787
+ false,
788
+ );
789
+ });
790
+ });