|
1 | 1 | /** |
| 2 | + * @vitest-environment node |
| 3 | + * |
2 | 4 | * Tests for the per-workflow execution store. |
3 | 5 | * |
4 | 6 | * These tests cover: |
|
7 | 9 | * - Execution lifecycle (start/stop clears run path) |
8 | 10 | * - Block and edge run status tracking |
9 | 11 | * - Active block management |
10 | | - * - Debug state management |
| 12 | + * - The {@link ExecutionStatus} enum and its derived `isExecuting` / |
| 13 | + * `isDebugging` booleans (exhaustive status → flag mapping + transitions) |
11 | 14 | * - Execution snapshot management |
12 | 15 | * - Store reset |
13 | 16 | * - Immutability guarantees |
14 | 17 | * |
15 | 18 | * @remarks |
| 19 | + * The store under test transitively imports the workflow registry store, |
| 20 | + * which drags in the block registry and emcn icon CSS. To keep this a true |
| 21 | + * unit test that loads under the node environment, the registry store is |
| 22 | + * mocked to a minimal stub (the store actions never touch it — only the |
| 23 | + * convenience hooks do, which are not exercised here). |
| 24 | + * |
16 | 25 | * Most tests use `it.concurrent` with unique workflow IDs per test. |
17 | 26 | * Because the store isolates state by workflow ID, concurrent tests |
18 | 27 | * do not interfere with each other. The `reset` and `immutability` |
|
21 | 30 |
|
22 | 31 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
23 | 32 |
|
| 33 | +vi.mock('@/stores/workflows/registry/store', () => ({ |
| 34 | + useWorkflowRegistry: Object.assign( |
| 35 | + vi.fn(() => null), |
| 36 | + { getState: vi.fn(() => ({ activeWorkflowId: null })) } |
| 37 | + ), |
| 38 | +})) |
| 39 | + |
24 | 40 | vi.unmock('@/stores/execution/store') |
25 | 41 | vi.unmock('@/stores/execution/types') |
26 | 42 |
|
27 | 43 | import { useExecutionStore } from '@/stores/execution/store' |
28 | | -import { defaultWorkflowExecutionState, initialState } from '@/stores/execution/types' |
| 44 | +import { |
| 45 | + defaultWorkflowExecutionState, |
| 46 | + deriveExecutionFlags, |
| 47 | + type ExecutionStatus, |
| 48 | + initialState, |
| 49 | +} from '@/stores/execution/types' |
29 | 50 |
|
30 | 51 | describe('useExecutionStore', () => { |
31 | 52 | describe('getWorkflowExecution', () => { |
32 | 53 | it.concurrent('should return default state for an unknown workflow', () => { |
33 | 54 | const state = useExecutionStore.getState().getWorkflowExecution('wf-get-default') |
34 | 55 |
|
| 56 | + expect(state.status).toBe('idle') |
35 | 57 | expect(state.isExecuting).toBe(false) |
36 | 58 | expect(state.isDebugging).toBe(false) |
37 | 59 | expect(state.activeBlockIds.size).toBe(0) |
@@ -63,22 +85,35 @@ describe('useExecutionStore', () => { |
63 | 85 | }) |
64 | 86 | }) |
65 | 87 |
|
| 88 | + describe('deriveExecutionFlags', () => { |
| 89 | + it.concurrent('maps every status to the documented legacy booleans', () => { |
| 90 | + const cases: Array<[ExecutionStatus, boolean, boolean]> = [ |
| 91 | + ['idle', false, false], |
| 92 | + ['running', true, false], |
| 93 | + ['debugging', true, true], |
| 94 | + ] |
| 95 | + for (const [status, isExecuting, isDebugging] of cases) { |
| 96 | + expect(deriveExecutionFlags(status)).toEqual({ isExecuting, isDebugging }) |
| 97 | + } |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
66 | 101 | describe('setIsExecuting', () => { |
67 | | - it.concurrent('should set isExecuting to true', () => { |
| 102 | + it.concurrent('should set isExecuting to true (status running)', () => { |
68 | 103 | useExecutionStore.getState().setIsExecuting('wf-exec-true', true) |
69 | 104 |
|
70 | | - expect(useExecutionStore.getState().getWorkflowExecution('wf-exec-true').isExecuting).toBe( |
71 | | - true |
72 | | - ) |
| 105 | + const state = useExecutionStore.getState().getWorkflowExecution('wf-exec-true') |
| 106 | + expect(state.isExecuting).toBe(true) |
| 107 | + expect(state.status).toBe('running') |
73 | 108 | }) |
74 | 109 |
|
75 | | - it.concurrent('should set isExecuting to false', () => { |
| 110 | + it.concurrent('should set isExecuting to false (status idle)', () => { |
76 | 111 | useExecutionStore.getState().setIsExecuting('wf-exec-false', true) |
77 | 112 | useExecutionStore.getState().setIsExecuting('wf-exec-false', false) |
78 | 113 |
|
79 | | - expect(useExecutionStore.getState().getWorkflowExecution('wf-exec-false').isExecuting).toBe( |
80 | | - false |
81 | | - ) |
| 114 | + const state = useExecutionStore.getState().getWorkflowExecution('wf-exec-false') |
| 115 | + expect(state.isExecuting).toBe(false) |
| 116 | + expect(state.status).toBe('idle') |
82 | 117 | }) |
83 | 118 |
|
84 | 119 | it.concurrent('should clear lastRunPath and lastRunEdges when starting execution', () => { |
@@ -107,6 +142,131 @@ describe('useExecutionStore', () => { |
107 | 142 | expect(state.isExecuting).toBe(false) |
108 | 143 | expect(state.lastRunPath.get('block-1')).toBe('success') |
109 | 144 | }) |
| 145 | + |
| 146 | + it.concurrent('starting a debug run then setIsExecuting(true) clears the run path', () => { |
| 147 | + const wf = 'wf-exec-debug-start-clears' |
| 148 | + useExecutionStore.getState().setIsExecuting(wf, true) |
| 149 | + useExecutionStore.getState().setIsDebugging(wf, true) |
| 150 | + useExecutionStore.getState().setBlockRunStatus(wf, 'block-1', 'success') |
| 151 | + |
| 152 | + useExecutionStore.getState().setIsExecuting(wf, true) |
| 153 | + |
| 154 | + const state = useExecutionStore.getState().getWorkflowExecution(wf) |
| 155 | + expect(state.status).toBe('debugging') |
| 156 | + expect(state.isExecuting).toBe(true) |
| 157 | + expect(state.isDebugging).toBe(true) |
| 158 | + expect(state.lastRunPath.size).toBe(0) |
| 159 | + expect(state.lastRunEdges.size).toBe(0) |
| 160 | + }) |
| 161 | + }) |
| 162 | + |
| 163 | + describe('setIsDebugging', () => { |
| 164 | + it.concurrent('should toggle debug mode', () => { |
| 165 | + const wf = 'wf-debug-toggle' |
| 166 | + useExecutionStore.getState().setIsDebugging(wf, true) |
| 167 | + |
| 168 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).isDebugging).toBe(true) |
| 169 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).isExecuting).toBe(true) |
| 170 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).status).toBe('debugging') |
| 171 | + |
| 172 | + useExecutionStore.getState().setIsDebugging(wf, false) |
| 173 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).isDebugging).toBe(false) |
| 174 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).isExecuting).toBe(true) |
| 175 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).status).toBe('running') |
| 176 | + }) |
| 177 | + |
| 178 | + it.concurrent('setIsDebugging(false) while idle is a no-op (stays idle)', () => { |
| 179 | + const wf = 'wf-debug-false-idle' |
| 180 | + useExecutionStore.getState().setIsDebugging(wf, false) |
| 181 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).status).toBe('idle') |
| 182 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).isExecuting).toBe(false) |
| 183 | + }) |
| 184 | + |
| 185 | + it.concurrent('setIsDebugging(false) while running keeps running', () => { |
| 186 | + const wf = 'wf-debug-false-running' |
| 187 | + useExecutionStore.getState().setIsExecuting(wf, true) |
| 188 | + useExecutionStore.getState().setIsDebugging(wf, false) |
| 189 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).status).toBe('running') |
| 190 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).isExecuting).toBe(true) |
| 191 | + }) |
| 192 | + |
| 193 | + it.concurrent('does not clear the run path when entering debug mode', () => { |
| 194 | + const wf = 'wf-debug-keeps-path' |
| 195 | + useExecutionStore.getState().setBlockRunStatus(wf, 'block-1', 'success') |
| 196 | + useExecutionStore.getState().setIsDebugging(wf, true) |
| 197 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).lastRunPath.get('block-1')).toBe( |
| 198 | + 'success' |
| 199 | + ) |
| 200 | + }) |
| 201 | + }) |
| 202 | + |
| 203 | + describe('status enum', () => { |
| 204 | + it.concurrent('idle derives both flags false', () => { |
| 205 | + const wf = 'wf-status-idle' |
| 206 | + const state = useExecutionStore.getState().getWorkflowExecution(wf) |
| 207 | + expect(state.status).toBe('idle') |
| 208 | + expect(state.isExecuting).toBe(false) |
| 209 | + expect(state.isDebugging).toBe(false) |
| 210 | + }) |
| 211 | + |
| 212 | + it.concurrent('running derives isExecuting only', () => { |
| 213 | + const wf = 'wf-status-running' |
| 214 | + useExecutionStore.getState().setStatus(wf, 'running') |
| 215 | + const state = useExecutionStore.getState().getWorkflowExecution(wf) |
| 216 | + expect(state.status).toBe('running') |
| 217 | + expect(state.isExecuting).toBe(true) |
| 218 | + expect(state.isDebugging).toBe(false) |
| 219 | + }) |
| 220 | + |
| 221 | + it.concurrent('debugging derives both flags true', () => { |
| 222 | + const wf = 'wf-status-debugging' |
| 223 | + useExecutionStore.getState().setStatus(wf, 'debugging') |
| 224 | + const state = useExecutionStore.getState().getWorkflowExecution(wf) |
| 225 | + expect(state.status).toBe('debugging') |
| 226 | + expect(state.isExecuting).toBe(true) |
| 227 | + expect(state.isDebugging).toBe(true) |
| 228 | + }) |
| 229 | + |
| 230 | + it.concurrent('setStatus preserves the run path unless clearRunPath is passed', () => { |
| 231 | + const wf = 'wf-status-path-rules' |
| 232 | + useExecutionStore.getState().setStatus(wf, 'debugging') |
| 233 | + useExecutionStore.getState().setBlockRunStatus(wf, 'block-1', 'success') |
| 234 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).lastRunPath.size).toBe(1) |
| 235 | + |
| 236 | + useExecutionStore.getState().setStatus(wf, 'running') |
| 237 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).lastRunPath.size).toBe(1) |
| 238 | + |
| 239 | + useExecutionStore.getState().setStatus(wf, 'running', { clearRunPath: true }) |
| 240 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).lastRunPath.size).toBe(0) |
| 241 | + }) |
| 242 | + |
| 243 | + it.concurrent('the derived booleans always agree with the stored status', () => { |
| 244 | + const wf = 'wf-status-no-drift' |
| 245 | + for (const status of ['idle', 'running', 'debugging', 'idle'] as const) { |
| 246 | + useExecutionStore.getState().setStatus(wf, status) |
| 247 | + const state = useExecutionStore.getState().getWorkflowExecution(wf) |
| 248 | + expect({ isExecuting: state.isExecuting, isDebugging: state.isDebugging }).toEqual( |
| 249 | + deriveExecutionFlags(status) |
| 250 | + ) |
| 251 | + } |
| 252 | + }) |
| 253 | + |
| 254 | + it.concurrent('setIsExecuting(true) preserves an active debug session', () => { |
| 255 | + const wf = 'wf-status-debug-preserve' |
| 256 | + useExecutionStore.getState().setStatus(wf, 'debugging') |
| 257 | + useExecutionStore.getState().setIsExecuting(wf, true) |
| 258 | + expect(useExecutionStore.getState().getWorkflowExecution(wf).status).toBe('debugging') |
| 259 | + }) |
| 260 | + |
| 261 | + it.concurrent('setIsExecuting(false) returns to idle from any mode', () => { |
| 262 | + const wf = 'wf-status-stop' |
| 263 | + useExecutionStore.getState().setStatus(wf, 'debugging') |
| 264 | + useExecutionStore.getState().setIsExecuting(wf, false) |
| 265 | + const state = useExecutionStore.getState().getWorkflowExecution(wf) |
| 266 | + expect(state.status).toBe('idle') |
| 267 | + expect(state.isExecuting).toBe(false) |
| 268 | + expect(state.isDebugging).toBe(false) |
| 269 | + }) |
110 | 270 | }) |
111 | 271 |
|
112 | 272 | describe('setActiveBlocks', () => { |
@@ -151,18 +311,6 @@ describe('useExecutionStore', () => { |
151 | 311 | }) |
152 | 312 | }) |
153 | 313 |
|
154 | | - describe('setIsDebugging', () => { |
155 | | - it.concurrent('should toggle debug mode', () => { |
156 | | - const wf = 'wf-debug-toggle' |
157 | | - useExecutionStore.getState().setIsDebugging(wf, true) |
158 | | - |
159 | | - expect(useExecutionStore.getState().getWorkflowExecution(wf).isDebugging).toBe(true) |
160 | | - |
161 | | - useExecutionStore.getState().setIsDebugging(wf, false) |
162 | | - expect(useExecutionStore.getState().getWorkflowExecution(wf).isDebugging).toBe(false) |
163 | | - }) |
164 | | - }) |
165 | | - |
166 | 314 | describe('setExecutor', () => { |
167 | 315 | it.concurrent('should store and clear executor', () => { |
168 | 316 | const wf = 'wf-executor' |
|
0 commit comments