|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import { AutomationEngine } from './engine.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * The start node's trigger-condition gate must hold on EVERY attempt, not |
| 8 | + * only the first. |
| 9 | + * |
| 10 | + * `execute()` evaluates the start condition; a condition that cannot be |
| 11 | + * evaluated THROWS (ADR-0032 §1c), and the throw lands in `execute()`'s catch, |
| 12 | + * which hands a flow declaring `errorHandling.strategy: 'retry'` to |
| 13 | + * `retryExecution`. Every retry attempt runs through `executeWithoutRetry`. |
| 14 | + * Before the repair that method went from the start-node lookup straight to |
| 15 | + * the nodes and never evaluated the condition, so a condition that faulted on |
| 16 | + * attempt 1 let attempts 2..N run the flow's nodes ungated: the author's gate |
| 17 | + * on whether the flow runs at all, opened by a fault. |
| 18 | + * |
| 19 | + * The rule pinned here: both attempt paths call one evaluator |
| 20 | + * (`startConditionHolds`), so a fault stays a fault (retried, never a pass), |
| 21 | + * a condition that does not hold skips the run exactly as on attempt 1, and a |
| 22 | + * condition that holds runs the nodes once. |
| 23 | + * |
| 24 | + * Written against the OBSERVABLE side effect, an executor counting real node |
| 25 | + * executions, never the returned envelope alone: the defect's harm is a |
| 26 | + * side-effecting node running when it should not. |
| 27 | + */ |
| 28 | + |
| 29 | +function createTestLogger(): any { |
| 30 | + return { info: () => {}, warn: () => {}, error: () => {}, debug: () => {}, child: () => createTestLogger() }; |
| 31 | +} |
| 32 | + |
| 33 | +/** An engine holding one start-gated flow whose single work node counts every REAL execution. */ |
| 34 | +function gatedFlowEngine(opts: { strategy?: 'retry' | 'fail' } = {}) { |
| 35 | + const engine = new AutomationEngine(createTestLogger()); |
| 36 | + const runs = { count: 0 }; |
| 37 | + |
| 38 | + engine.registerNodeExecutor({ |
| 39 | + type: 'probe_step', |
| 40 | + async execute() { |
| 41 | + runs.count++; |
| 42 | + return { success: true }; |
| 43 | + }, |
| 44 | + } as any); |
| 45 | + |
| 46 | + engine.registerFlow('gated', { |
| 47 | + name: 'gated', |
| 48 | + label: 'Gated', |
| 49 | + type: 'autolaunched', |
| 50 | + ...(opts.strategy === 'retry' |
| 51 | + ? { errorHandling: { strategy: 'retry', maxRetries: 2, backoffMs: 0 } } |
| 52 | + : {}), |
| 53 | + nodes: [ |
| 54 | + { id: 'start', type: 'start', label: 'Start', config: { condition: 'record.amount > 10' } }, |
| 55 | + { id: 'work', type: 'probe_step', label: 'Work' }, |
| 56 | + { id: 'end', type: 'end', label: 'End' }, |
| 57 | + ], |
| 58 | + edges: [ |
| 59 | + { id: 'e0', source: 'start', target: 'work' }, |
| 60 | + { id: 'e1', source: 'work', target: 'end' }, |
| 61 | + ], |
| 62 | + } as any); |
| 63 | + |
| 64 | + return { engine, runs }; |
| 65 | +} |
| 66 | + |
| 67 | +/** `'abc' > 10` has no CEL overload, so the real evaluator faults on this record. */ |
| 68 | +const FAULTING_RECORD = { id: 'r1', amount: 'abc' }; |
| 69 | + |
| 70 | +describe("start condition under errorHandling.strategy: 'retry' — evaluated on every attempt", () => { |
| 71 | + it('a condition that faults on EVERY attempt runs no node, and the run ends failed', async () => { |
| 72 | + const { engine, runs } = gatedFlowEngine({ strategy: 'retry' }); |
| 73 | + const evaluate = vi.spyOn(engine, 'evaluateCondition'); |
| 74 | + |
| 75 | + const result = await engine.execute('gated', { record: { ...FAULTING_RECORD } } as any); |
| 76 | + |
| 77 | + // The point: the side-effecting node never ran. Before the repair it |
| 78 | + // ran on attempt 2, and that attempt answered success. |
| 79 | + expect(runs.count).toBe(0); |
| 80 | + expect(result.success).toBe(false); |
| 81 | + expect(result.status).toBe('failed'); |
| 82 | + expect(result.error).toContain('condition'); |
| 83 | + |
| 84 | + // A fault is retried, never skipped: the gate was evaluated on attempt |
| 85 | + // 1 and on each of the `maxRetries: 2` retry attempts, and each attempt |
| 86 | + // left one failed row. |
| 87 | + expect(evaluate).toHaveBeenCalledTimes(3); |
| 88 | + const failedRows = await engine.listRuns('gated', { status: 'failed' }); |
| 89 | + expect(failedRows).toHaveLength(3); |
| 90 | + }); |
| 91 | + |
| 92 | + it('a condition that faults on attempt 1 and holds on attempt 2 runs the nodes once', async () => { |
| 93 | + const { engine, runs } = gatedFlowEngine({ strategy: 'retry' }); |
| 94 | + // Attempt 1 faults; every later call is the real evaluator, which |
| 95 | + // judges `record.amount > 10` against a record where it holds. |
| 96 | + const evaluate = vi.spyOn(engine, 'evaluateCondition').mockImplementationOnce(() => { |
| 97 | + throw new Error('condition evaluation error: transient fault — source: `record.amount > 10`'); |
| 98 | + }); |
| 99 | + |
| 100 | + const result = await engine.execute('gated', { record: { id: 'r1', amount: 20 } } as any); |
| 101 | + |
| 102 | + expect(result.success).toBe(true); |
| 103 | + expect((result.output as { skipped?: unknown } | undefined)?.skipped).toBeUndefined(); |
| 104 | + expect(runs.count).toBe(1); |
| 105 | + expect(evaluate).toHaveBeenCalledTimes(2); |
| 106 | + }); |
| 107 | + |
| 108 | + it('a condition that faults on attempt 1 and does not hold on attempt 2 skips, running no node', async () => { |
| 109 | + const { engine, runs } = gatedFlowEngine({ strategy: 'retry' }); |
| 110 | + const evaluate = vi.spyOn(engine, 'evaluateCondition').mockImplementationOnce(() => { |
| 111 | + throw new Error('condition evaluation error: transient fault — source: `record.amount > 10`'); |
| 112 | + }); |
| 113 | + |
| 114 | + const result = await engine.execute('gated', { record: { id: 'r1', amount: 5 } } as any); |
| 115 | + |
| 116 | + // The same skip envelope attempt 1 answers when its condition does not hold. |
| 117 | + expect(result).toMatchObject({ |
| 118 | + success: true, |
| 119 | + output: { skipped: true, reason: 'condition_not_met' }, |
| 120 | + flowLabel: 'Gated', |
| 121 | + }); |
| 122 | + expect(runs.count).toBe(0); |
| 123 | + expect(evaluate).toHaveBeenCalledTimes(2); |
| 124 | + }); |
| 125 | + |
| 126 | + it('CONTROL — the default strategy answers failed on the first fault and never retries', async () => { |
| 127 | + const { engine, runs } = gatedFlowEngine(); |
| 128 | + const evaluate = vi.spyOn(engine, 'evaluateCondition'); |
| 129 | + |
| 130 | + const result = await engine.execute('gated', { record: { ...FAULTING_RECORD } } as any); |
| 131 | + |
| 132 | + expect(runs.count).toBe(0); |
| 133 | + expect(result.success).toBe(false); |
| 134 | + expect(result.status).toBe('failed'); |
| 135 | + expect(result.error).toContain('condition'); |
| 136 | + expect(evaluate).toHaveBeenCalledTimes(1); |
| 137 | + const failedRows = await engine.listRuns('gated', { status: 'failed' }); |
| 138 | + expect(failedRows).toHaveLength(1); |
| 139 | + }); |
| 140 | + |
| 141 | + it('CONTROL — under retry, a condition that holds or does not hold on attempt 1 is answered there', async () => { |
| 142 | + const held = gatedFlowEngine({ strategy: 'retry' }); |
| 143 | + const ran = await held.engine.execute('gated', { record: { id: 'r1', amount: 20 } } as any); |
| 144 | + expect(ran.success).toBe(true); |
| 145 | + expect(held.runs.count).toBe(1); |
| 146 | + |
| 147 | + const notHeld = gatedFlowEngine({ strategy: 'retry' }); |
| 148 | + const skipped = await notHeld.engine.execute('gated', { record: { id: 'r1', amount: 5 } } as any); |
| 149 | + expect(skipped).toMatchObject({ success: true, output: { skipped: true, reason: 'condition_not_met' } }); |
| 150 | + expect(notHeld.runs.count).toBe(0); |
| 151 | + }); |
| 152 | +}); |
0 commit comments