|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 4 | +import { AutomationEngine } from '../engine.js'; |
| 5 | +import type { NodeExecutor } from '../engine.js'; |
| 6 | +import { registerTryCatchNode } from './try-catch-node.js'; |
| 7 | + |
| 8 | +function silentLogger() { |
| 9 | + return { info() {}, warn() {}, error() {}, debug() {}, child() { return silentLogger(); } } as any; |
| 10 | +} |
| 11 | +function ctx() { |
| 12 | + return { logger: silentLogger(), getService() { throw new Error('none'); } } as any; |
| 13 | +} |
| 14 | + |
| 15 | +describe('try/catch/retry executor (ADR-0031)', () => { |
| 16 | + let engine: AutomationEngine; |
| 17 | + let ran: string[]; |
| 18 | + let attempts: number; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + engine = new AutomationEngine(silentLogger()); |
| 22 | + ran = []; |
| 23 | + attempts = 0; |
| 24 | + registerTryCatchNode(engine, ctx()); |
| 25 | + |
| 26 | + engine.registerNodeExecutor({ |
| 27 | + type: 'ok', |
| 28 | + async execute(node) { ran.push((node.config as any)?.tag ?? 'ok'); return { success: true }; }, |
| 29 | + } as NodeExecutor); |
| 30 | + |
| 31 | + // Always fails. |
| 32 | + engine.registerNodeExecutor({ |
| 33 | + type: 'boom', |
| 34 | + async execute() { ran.push('boom'); return { success: false, error: 'kaboom' }; }, |
| 35 | + } as NodeExecutor); |
| 36 | + |
| 37 | + // Fails the first N times (config.failTimes), then succeeds — for retry tests. |
| 38 | + engine.registerNodeExecutor({ |
| 39 | + type: 'flaky', |
| 40 | + async execute(node) { |
| 41 | + attempts++; |
| 42 | + const failTimes = Number((node.config as any)?.failTimes ?? 0); |
| 43 | + ran.push(`flaky#${attempts}`); |
| 44 | + if (attempts <= failTimes) return { success: false, error: `transient ${attempts}` }; |
| 45 | + return { success: true }; |
| 46 | + }, |
| 47 | + } as NodeExecutor); |
| 48 | + |
| 49 | + // Reads the caught error variable. |
| 50 | + engine.registerNodeExecutor({ |
| 51 | + type: 'handler', |
| 52 | + async execute(node, variables) { |
| 53 | + const v = (node.config as any)?.errVar ?? '$error'; |
| 54 | + ran.push(`handler:${JSON.stringify(variables.get(v))}`); |
| 55 | + return { success: true }; |
| 56 | + }, |
| 57 | + } as NodeExecutor); |
| 58 | + }); |
| 59 | + |
| 60 | + const tcFlow = (tcConfig: Record<string, unknown>) => ({ |
| 61 | + name: 'tc_flow', |
| 62 | + label: 'TryCatch Flow', |
| 63 | + type: 'autolaunched' as const, |
| 64 | + nodes: [ |
| 65 | + { id: 'start', type: 'start', label: 'Start' }, |
| 66 | + { id: 'tc', type: 'try_catch', label: 'Guarded', config: tcConfig }, |
| 67 | + { id: 'after', type: 'ok', label: 'After', config: { tag: 'after' } }, |
| 68 | + { id: 'end', type: 'end', label: 'End' }, |
| 69 | + ], |
| 70 | + edges: [ |
| 71 | + { id: 'e1', source: 'start', target: 'tc' }, |
| 72 | + { id: 'e2', source: 'tc', target: 'after' }, |
| 73 | + { id: 'e3', source: 'after', target: 'end' }, |
| 74 | + ], |
| 75 | + }); |
| 76 | + |
| 77 | + it('runs the try region and continues when it succeeds (no catch invoked)', async () => { |
| 78 | + engine.registerFlow('tc_flow', tcFlow({ |
| 79 | + try: { nodes: [{ id: 't', type: 'ok', label: 'T', config: { tag: 'try' } }], edges: [] }, |
| 80 | + catch: { nodes: [{ id: 'c', type: 'ok', label: 'C', config: { tag: 'catch' } }], edges: [] }, |
| 81 | + })); |
| 82 | + |
| 83 | + const result = await engine.execute('tc_flow'); |
| 84 | + expect(result.success).toBe(true); |
| 85 | + expect(ran).toEqual(['try', 'after']); // catch not run, downstream continued |
| 86 | + }); |
| 87 | + |
| 88 | + it('runs the catch region when the try region fails, binding the error', async () => { |
| 89 | + engine.registerFlow('tc_flow', tcFlow({ |
| 90 | + try: { nodes: [{ id: 't', type: 'boom', label: 'T' }], edges: [] }, |
| 91 | + catch: { nodes: [{ id: 'c', type: 'handler', label: 'C' }], edges: [] }, |
| 92 | + })); |
| 93 | + |
| 94 | + const result = await engine.execute('tc_flow'); |
| 95 | + expect(result.success).toBe(true); // error handled by catch |
| 96 | + expect(ran[0]).toBe('boom'); |
| 97 | + expect(ran.some(r => r.startsWith('handler:') && r.includes('kaboom'))).toBe(true); |
| 98 | + expect(ran[ran.length - 1]).toBe('after'); // downstream continued after catch |
| 99 | + }); |
| 100 | + |
| 101 | + it('retries the try region with backoff and succeeds without running catch', async () => { |
| 102 | + engine.registerFlow('tc_flow', tcFlow({ |
| 103 | + try: { nodes: [{ id: 't', type: 'flaky', label: 'T', config: { failTimes: 2 } }], edges: [] }, |
| 104 | + catch: { nodes: [{ id: 'c', type: 'boom', label: 'C' }], edges: [] }, |
| 105 | + retry: { maxRetries: 3, retryDelayMs: 0 }, |
| 106 | + })); |
| 107 | + |
| 108 | + const result = await engine.execute('tc_flow'); |
| 109 | + expect(result.success).toBe(true); |
| 110 | + expect(attempts).toBe(3); // failed twice, succeeded on the third |
| 111 | + expect(ran).not.toContain('boom'); // catch never ran |
| 112 | + expect(ran[ran.length - 1]).toBe('after'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('falls through to catch after exhausting retries', async () => { |
| 116 | + engine.registerFlow('tc_flow', tcFlow({ |
| 117 | + try: { nodes: [{ id: 't', type: 'flaky', label: 'T', config: { failTimes: 99 } }], edges: [] }, |
| 118 | + catch: { nodes: [{ id: 'c', type: 'ok', label: 'C', config: { tag: 'catch' } }], edges: [] }, |
| 119 | + retry: { maxRetries: 2, retryDelayMs: 0 }, |
| 120 | + })); |
| 121 | + |
| 122 | + const result = await engine.execute('tc_flow'); |
| 123 | + expect(result.success).toBe(true); |
| 124 | + expect(attempts).toBe(3); // initial + 2 retries |
| 125 | + expect(ran).toContain('catch'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('fails the node when the try region fails and there is no catch', async () => { |
| 129 | + engine.registerFlow('tc_flow', tcFlow({ |
| 130 | + try: { nodes: [{ id: 't', type: 'boom', label: 'T' }], edges: [] }, |
| 131 | + })); |
| 132 | + |
| 133 | + const result = await engine.execute('tc_flow'); |
| 134 | + expect(result.success).toBe(false); |
| 135 | + expect(result.error).toMatch(/try region failed.*kaboom/); |
| 136 | + expect(ran).not.toContain('after'); // downstream did not run |
| 137 | + }); |
| 138 | + |
| 139 | + it('fails the node when the catch region itself fails', async () => { |
| 140 | + engine.registerFlow('tc_flow', tcFlow({ |
| 141 | + try: { nodes: [{ id: 't', type: 'boom', label: 'T' }], edges: [] }, |
| 142 | + catch: { nodes: [{ id: 'c', type: 'boom', label: 'C' }], edges: [] }, |
| 143 | + })); |
| 144 | + |
| 145 | + const result = await engine.execute('tc_flow'); |
| 146 | + expect(result.success).toBe(false); |
| 147 | + expect(result.error).toMatch(/catch region failed/); |
| 148 | + }); |
| 149 | + |
| 150 | + it('rejects a malformed try region at registerFlow', () => { |
| 151 | + expect(() => |
| 152 | + engine.registerFlow('bad_tc', tcFlow({ |
| 153 | + // two entry/exit nodes, no edges → not single-entry/single-exit |
| 154 | + try: { nodes: [{ id: 'a', type: 'ok', label: 'A' }, { id: 'b', type: 'ok', label: 'B' }], edges: [] }, |
| 155 | + })), |
| 156 | + ).toThrow(/try_catch 'tc' try/); |
| 157 | + }); |
| 158 | + |
| 159 | + it('runs a multi-node try region in order', async () => { |
| 160 | + engine.registerFlow('tc_flow', tcFlow({ |
| 161 | + try: { |
| 162 | + nodes: [ |
| 163 | + { id: 't1', type: 'ok', label: 'T1', config: { tag: 't1' } }, |
| 164 | + { id: 't2', type: 'ok', label: 'T2', config: { tag: 't2' } }, |
| 165 | + ], |
| 166 | + edges: [{ id: 'te', source: 't1', target: 't2' }], |
| 167 | + }, |
| 168 | + })); |
| 169 | + |
| 170 | + const result = await engine.execute('tc_flow'); |
| 171 | + expect(result.success).toBe(true); |
| 172 | + expect(ran).toEqual(['t1', 't2', 'after']); |
| 173 | + }); |
| 174 | +}); |
0 commit comments