|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { computeWorkflowDag } from './dag' |
| 3 | + |
| 4 | +function block(id: string, name: string, type = 'function') { |
| 5 | + return { id, type, name } |
| 6 | +} |
| 7 | + |
| 8 | +describe('computeWorkflowDag', () => { |
| 9 | + it('builds a linear adjacency with sinks as empty arrays', () => { |
| 10 | + const dag = computeWorkflowDag({ |
| 11 | + blocks: { a: block('a', 'A', 'starter'), b: block('b', 'B'), c: block('c', 'C') }, |
| 12 | + edges: [ |
| 13 | + { source: 'a', target: 'b' }, |
| 14 | + { source: 'b', target: 'c' }, |
| 15 | + ], |
| 16 | + } as any) |
| 17 | + |
| 18 | + expect(dag).toEqual({ A: ['B'], B: ['C'], C: [] }) |
| 19 | + }) |
| 20 | + |
| 21 | + it('captures condition branches (if/else) as downstream names', () => { |
| 22 | + const dag = computeWorkflowDag({ |
| 23 | + blocks: { |
| 24 | + cond: block('cond', 'Cond', 'condition'), |
| 25 | + x: block('x', 'X'), |
| 26 | + y: block('y', 'Y'), |
| 27 | + }, |
| 28 | + edges: [ |
| 29 | + { source: 'cond', sourceHandle: 'if', target: 'x' }, |
| 30 | + { source: 'cond', sourceHandle: 'else', target: 'y' }, |
| 31 | + ], |
| 32 | + } as any) |
| 33 | + |
| 34 | + expect(dag.Cond).toEqual(['X', 'Y']) |
| 35 | + expect(dag.X).toEqual([]) |
| 36 | + expect(dag.Y).toEqual([]) |
| 37 | + }) |
| 38 | + |
| 39 | + it('captures router routes (sorted)', () => { |
| 40 | + const dag = computeWorkflowDag({ |
| 41 | + blocks: { |
| 42 | + r: block('r', 'Router', 'router_v2'), |
| 43 | + s: block('s', 'Support'), |
| 44 | + sa: block('sa', 'Sales'), |
| 45 | + }, |
| 46 | + edges: [ |
| 47 | + { source: 'r', sourceHandle: 'route-0', target: 's' }, |
| 48 | + { source: 'r', sourceHandle: 'route-1', target: 'sa' }, |
| 49 | + ], |
| 50 | + } as any) |
| 51 | + |
| 52 | + expect(dag.Router).toEqual(['Sales', 'Support']) |
| 53 | + }) |
| 54 | + |
| 55 | + it('captures subflow container edges (loop-start-source / loop-end-source)', () => { |
| 56 | + const dag = computeWorkflowDag({ |
| 57 | + blocks: { |
| 58 | + loop: block('loop', 'Loop', 'loop'), |
| 59 | + child: block('child', 'Child'), |
| 60 | + after: block('after', 'After'), |
| 61 | + }, |
| 62 | + edges: [ |
| 63 | + { source: 'loop', sourceHandle: 'loop-start-source', target: 'child' }, |
| 64 | + { source: 'loop', sourceHandle: 'loop-end-source', target: 'after' }, |
| 65 | + ], |
| 66 | + } as any) |
| 67 | + |
| 68 | + expect(dag.Loop).toEqual(['After', 'Child']) |
| 69 | + expect(dag.Child).toEqual([]) |
| 70 | + expect(dag.After).toEqual([]) |
| 71 | + }) |
| 72 | + |
| 73 | + it('excludes note blocks, dedups parallel edges, and ignores edges to missing blocks', () => { |
| 74 | + const dag = computeWorkflowDag({ |
| 75 | + blocks: { |
| 76 | + a: block('a', 'A', 'starter'), |
| 77 | + x: block('x', 'X'), |
| 78 | + note: block('note', 'Note', 'note'), |
| 79 | + }, |
| 80 | + edges: [ |
| 81 | + { source: 'a', target: 'x' }, |
| 82 | + { source: 'a', sourceHandle: 'error', target: 'x' }, // duplicate target |
| 83 | + { source: 'a', target: 'note' }, // note is excluded |
| 84 | + { source: 'a', target: 'ghost' }, // missing block |
| 85 | + ], |
| 86 | + } as any) |
| 87 | + |
| 88 | + expect(dag.A).toEqual(['X']) |
| 89 | + expect(dag.X).toEqual([]) |
| 90 | + expect(dag.Note).toBeUndefined() |
| 91 | + }) |
| 92 | +}) |
0 commit comments