|
1 | 1 | import { describe, it, expect } from 'vitest'; |
2 | 2 | import type { IAutomationService, AutomationResult } from './automation-service'; |
| 3 | +import type { FlowParsed } from '../automation/flow.zod'; |
| 4 | +import type { ExecutionLog } from '../automation/execution.zod'; |
3 | 5 |
|
4 | 6 | describe('Automation Service Contract', () => { |
5 | 7 | it('should allow a minimal IAutomationService implementation with required methods', () => { |
@@ -79,4 +81,90 @@ describe('Automation Service Contract', () => { |
79 | 81 | expect(flows).toHaveLength(3); |
80 | 82 | expect(flows).toContain('approval_flow'); |
81 | 83 | }); |
| 84 | + |
| 85 | + it('should return typed FlowParsed from getFlow', async () => { |
| 86 | + const mockFlow: FlowParsed = { |
| 87 | + name: 'approval_flow', |
| 88 | + label: 'Approval Flow', |
| 89 | + type: 'autolaunched', |
| 90 | + status: 'draft', |
| 91 | + version: 1, |
| 92 | + enabled: true, |
| 93 | + nodes: [ |
| 94 | + { id: 'start', type: 'start', label: 'Start' }, |
| 95 | + { id: 'end', type: 'end', label: 'End' }, |
| 96 | + ], |
| 97 | + edges: [{ id: 'e1', source: 'start', target: 'end' }], |
| 98 | + }; |
| 99 | + |
| 100 | + const service: IAutomationService = { |
| 101 | + execute: async () => ({ success: true }), |
| 102 | + listFlows: async () => ['approval_flow'], |
| 103 | + getFlow: async (name) => name === 'approval_flow' ? mockFlow : null, |
| 104 | + }; |
| 105 | + |
| 106 | + const flow = await service.getFlow!('approval_flow'); |
| 107 | + expect(flow).not.toBeNull(); |
| 108 | + expect(flow!.name).toBe('approval_flow'); |
| 109 | + expect(flow!.nodes).toHaveLength(2); |
| 110 | + |
| 111 | + const missing = await service.getFlow!('nonexistent'); |
| 112 | + expect(missing).toBeNull(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should return typed ExecutionLog from listRuns and getRun', async () => { |
| 116 | + const mockRun: ExecutionLog = { |
| 117 | + id: 'exec_001', |
| 118 | + flowName: 'approval_flow', |
| 119 | + status: 'completed', |
| 120 | + trigger: { type: 'api' }, |
| 121 | + steps: [{ |
| 122 | + nodeId: 'start', |
| 123 | + nodeType: 'start', |
| 124 | + status: 'success', |
| 125 | + startedAt: '2026-02-01T10:00:00Z', |
| 126 | + durationMs: 1, |
| 127 | + }], |
| 128 | + startedAt: '2026-02-01T10:00:00Z', |
| 129 | + completedAt: '2026-02-01T10:00:01Z', |
| 130 | + durationMs: 1000, |
| 131 | + }; |
| 132 | + |
| 133 | + const service: IAutomationService = { |
| 134 | + execute: async () => ({ success: true }), |
| 135 | + listFlows: async () => ['approval_flow'], |
| 136 | + listRuns: async (_flowName, _options?) => [mockRun], |
| 137 | + getRun: async (runId) => runId === 'exec_001' ? mockRun : null, |
| 138 | + }; |
| 139 | + |
| 140 | + const runs = await service.listRuns!('approval_flow'); |
| 141 | + expect(runs).toHaveLength(1); |
| 142 | + expect(runs[0].id).toBe('exec_001'); |
| 143 | + expect(runs[0].status).toBe('completed'); |
| 144 | + expect(runs[0].steps).toHaveLength(1); |
| 145 | + |
| 146 | + const run = await service.getRun!('exec_001'); |
| 147 | + expect(run).not.toBeNull(); |
| 148 | + expect(run!.flowName).toBe('approval_flow'); |
| 149 | + expect(run!.durationMs).toBe(1000); |
| 150 | + |
| 151 | + const missingRun = await service.getRun!('nonexistent'); |
| 152 | + expect(missingRun).toBeNull(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should support toggleFlow to enable/disable flows', async () => { |
| 156 | + let flowEnabled = true; |
| 157 | + |
| 158 | + const service: IAutomationService = { |
| 159 | + execute: async () => ({ success: true }), |
| 160 | + listFlows: async () => ['test_flow'], |
| 161 | + toggleFlow: async (_name, enabled) => { flowEnabled = enabled; }, |
| 162 | + }; |
| 163 | + |
| 164 | + await service.toggleFlow!('test_flow', false); |
| 165 | + expect(flowEnabled).toBe(false); |
| 166 | + |
| 167 | + await service.toggleFlow!('test_flow', true); |
| 168 | + expect(flowEnabled).toBe(true); |
| 169 | + }); |
82 | 170 | }); |
0 commit comments