|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { hasUpstreamFileSource, isFileDependentActionType } from '../../src/utils/flowValidation' |
| 3 | +import type { WorkflowEdge, WorkflowNode } from '../../src/types/workflow' |
| 4 | + |
| 5 | +// Only the File Event Trigger reliably supplies a file to the run's template-variable |
| 6 | +// context (backend/pkg/executor/executor.go only fills `vars["file.*"]` when a non-empty |
| 7 | +// resourcePath is passed to Executor.Run, and only the SSE event path always provides one: |
| 8 | +// - the scheduler always calls Run(..., "schedule", "") — resourcePath is hardcoded empty. |
| 9 | +// - the manual "Run now" panel sends whatever the user optionally typed into a free-text |
| 10 | +// field, so it isn't guaranteed either. |
| 11 | +// - the SSE event manager passes the actual file path from the triggering event. |
| 12 | +describe('hasUpstreamFileSource', () => { |
| 13 | + const trigger = (id: string, data: WorkflowNode['data']): WorkflowNode => ({ |
| 14 | + id, |
| 15 | + type: 'trigger', |
| 16 | + position: { x: 0, y: 0 }, |
| 17 | + data |
| 18 | + }) |
| 19 | + const llm = (id: string): WorkflowNode => ({ |
| 20 | + id, |
| 21 | + type: 'llm', |
| 22 | + position: { x: 0, y: 0 }, |
| 23 | + data: { prompt: 'summarize {{file.content}}' } |
| 24 | + }) |
| 25 | + const action = (id: string, actionType: 'move' | 'tag'): WorkflowNode => ({ |
| 26 | + id, |
| 27 | + type: 'action', |
| 28 | + position: { x: 0, y: 0 }, |
| 29 | + data: { actionType } |
| 30 | + }) |
| 31 | + const edge = (source: string, target: string): WorkflowEdge => ({ id: `${source}-${target}`, source, target }) |
| 32 | + |
| 33 | + it('flags a manual trigger chained directly into a move-file action', () => { |
| 34 | + const nodes = [trigger('trigger', { triggerType: 'manual' }), action('action-1', 'move')] |
| 35 | + const edges = [edge('trigger', 'action-1')] |
| 36 | + |
| 37 | + expect(hasUpstreamFileSource('action-1', nodes, edges)).toBe(false) |
| 38 | + }) |
| 39 | + |
| 40 | + it('does not flag an event trigger chained directly into a move-file action', () => { |
| 41 | + const nodes = [trigger('trigger', { triggerType: 'event', event: { type: 'upload' } }), action('action-1', 'move')] |
| 42 | + const edges = [edge('trigger', 'action-1')] |
| 43 | + |
| 44 | + expect(hasUpstreamFileSource('action-1', nodes, edges)).toBe(true) |
| 45 | + }) |
| 46 | + |
| 47 | + it('still flags manual trigger -> llm -> move-file (no file anywhere upstream)', () => { |
| 48 | + const nodes = [trigger('trigger', { triggerType: 'manual' }), llm('llm-1'), action('action-1', 'move')] |
| 49 | + const edges = [edge('trigger', 'llm-1'), edge('llm-1', 'action-1')] |
| 50 | + |
| 51 | + expect(hasUpstreamFileSource('action-1', nodes, edges)).toBe(false) |
| 52 | + }) |
| 53 | + |
| 54 | + it('does not flag event trigger -> llm -> move-file', () => { |
| 55 | + const nodes = [ |
| 56 | + trigger('trigger', { triggerType: 'event', event: { type: 'upload' } }), |
| 57 | + llm('llm-1'), |
| 58 | + action('action-1', 'move') |
| 59 | + ] |
| 60 | + const edges = [edge('trigger', 'llm-1'), edge('llm-1', 'action-1')] |
| 61 | + |
| 62 | + expect(hasUpstreamFileSource('action-1', nodes, edges)).toBe(true) |
| 63 | + }) |
| 64 | + |
| 65 | + it('flags a schedule trigger too, since the scheduler always runs with an empty resourcePath', () => { |
| 66 | + const nodes = [trigger('trigger', { triggerType: 'schedule', schedule: '0 * * * *' }), action('action-1', 'move')] |
| 67 | + const edges = [edge('trigger', 'action-1')] |
| 68 | + |
| 69 | + expect(hasUpstreamFileSource('action-1', nodes, edges)).toBe(false) |
| 70 | + }) |
| 71 | + |
| 72 | + it('returns true for a node with no upstream trigger at all (nothing to flag against)', () => { |
| 73 | + const nodes = [action('action-1', 'move')] |
| 74 | + expect(hasUpstreamFileSource('action-1', nodes, [])).toBe(true) |
| 75 | + }) |
| 76 | +}) |
| 77 | + |
| 78 | +describe('isFileDependentActionType', () => { |
| 79 | + it('flags actions that operate on a target file', () => { |
| 80 | + expect(isFileDependentActionType('tag')).toBe(true) |
| 81 | + expect(isFileDependentActionType('comment')).toBe(true) |
| 82 | + expect(isFileDependentActionType('move')).toBe(true) |
| 83 | + expect(isFileDependentActionType('copy')).toBe(true) |
| 84 | + expect(isFileDependentActionType('rename')).toBe(true) |
| 85 | + }) |
| 86 | + |
| 87 | + it('does not flag notify, which needs no target file', () => { |
| 88 | + expect(isFileDependentActionType('notify')).toBe(false) |
| 89 | + }) |
| 90 | +}) |
0 commit comments