|
| 1 | +import { createPinia, setActivePinia } from 'pinia' |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | +import { ref } from 'vue' |
| 4 | + |
| 5 | +import type { NodeProgressState } from '@/schemas/apiSchema' |
| 6 | +import { useExecutionStore } from '@/stores/executionStore' |
| 7 | + |
| 8 | +const { handlers } = vi.hoisted(() => ({ |
| 9 | + handlers: {} as Record<string, (e: { detail: unknown }) => void> |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@/scripts/app', () => ({ app: { rootGraph: {} } })) |
| 13 | + |
| 14 | +vi.mock('@/scripts/api', () => ({ |
| 15 | + api: { |
| 16 | + addEventListener: (name: string, fn: (e: { detail: unknown }) => void) => { |
| 17 | + handlers[name] = fn |
| 18 | + }, |
| 19 | + removeEventListener: () => {} |
| 20 | + } |
| 21 | +})) |
| 22 | + |
| 23 | +vi.mock('@/platform/workflow/management/stores/workflowStore', () => ({ |
| 24 | + useWorkflowStore: () => ({ |
| 25 | + isOpen: () => false, |
| 26 | + openWorkflows: [], |
| 27 | + nodeLocatorIdToNodeExecutionId: () => null |
| 28 | + }) |
| 29 | +})) |
| 30 | + |
| 31 | +vi.mock('@/renderer/core/canvas/canvasStore', () => ({ |
| 32 | + useCanvasStore: () => ({ canvas: undefined }) |
| 33 | +})) |
| 34 | + |
| 35 | +vi.mock('@/stores/executionErrorStore', () => ({ |
| 36 | + useExecutionErrorStore: () => ({ |
| 37 | + clearExecutionStartErrors: () => {}, |
| 38 | + clearPromptError: () => {} |
| 39 | + }) |
| 40 | +})) |
| 41 | + |
| 42 | +vi.mock('@/stores/nodeOutputStore', () => ({ |
| 43 | + useNodeOutputStore: () => ({ revokePreviewsByExecutionId: () => {} }) |
| 44 | +})) |
| 45 | + |
| 46 | +vi.mock('@/composables/useAppMode', () => ({ |
| 47 | + useAppMode: () => ({ mode: ref('default'), isAppMode: ref(false) }) |
| 48 | +})) |
| 49 | + |
| 50 | +vi.mock('@/platform/telemetry', () => ({ useTelemetry: () => undefined })) |
| 51 | + |
| 52 | +vi.mock('@/utils/appMode', () => ({ |
| 53 | + getWorkflowMode: () => 'workflow', |
| 54 | + isAppModeValue: () => false |
| 55 | +})) |
| 56 | + |
| 57 | +function progressState( |
| 58 | + jobId: string, |
| 59 | + nodes: Record<string, Partial<NodeProgressState>> |
| 60 | +) { |
| 61 | + handlers['progress_state']?.({ detail: { prompt_id: jobId, nodes } }) |
| 62 | +} |
| 63 | + |
| 64 | +function setup() { |
| 65 | + const store = useExecutionStore() |
| 66 | + store.bindExecutionEvents() |
| 67 | + return store |
| 68 | +} |
| 69 | + |
| 70 | +beforeEach(() => { |
| 71 | + setActivePinia(createPinia()) |
| 72 | + for (const key of Object.keys(handlers)) delete handlers[key] |
| 73 | +}) |
| 74 | + |
| 75 | +describe('executionStore node progress', () => { |
| 76 | + it('is idle until an execution starts', () => { |
| 77 | + const store = setup() |
| 78 | + expect(store.isIdle).toBe(true) |
| 79 | + |
| 80 | + handlers['execution_start']?.({ detail: { prompt_id: 'job-1' } }) |
| 81 | + expect(store.isIdle).toBe(false) |
| 82 | + }) |
| 83 | + |
| 84 | + it('derives the running node ids from a progress_state event', () => { |
| 85 | + const store = setup() |
| 86 | + |
| 87 | + progressState('job-1', { |
| 88 | + n1: { state: 'running', value: 1, max: 4 }, |
| 89 | + n2: { state: 'finished' }, |
| 90 | + n3: { state: 'pending' } |
| 91 | + }) |
| 92 | + |
| 93 | + expect(store.executingNodeIds).toEqual(['n1']) |
| 94 | + expect(store.executingNodeId).toBe('n1') |
| 95 | + }) |
| 96 | + |
| 97 | + it('exposes fractional progress for the executing node', () => { |
| 98 | + const store = setup() |
| 99 | + |
| 100 | + progressState('job-1', { |
| 101 | + n1: { state: 'running', value: 1, max: 4 } |
| 102 | + }) |
| 103 | + |
| 104 | + expect(store.executingNodeProgress).toBe(0.25) |
| 105 | + }) |
| 106 | + |
| 107 | + it('reports no executing node when none are running', () => { |
| 108 | + const store = setup() |
| 109 | + |
| 110 | + progressState('job-1', { |
| 111 | + n1: { state: 'finished' }, |
| 112 | + n2: { state: 'pending' } |
| 113 | + }) |
| 114 | + |
| 115 | + expect(store.executingNodeIds).toEqual([]) |
| 116 | + expect(store.executingNodeId).toBeNull() |
| 117 | + }) |
| 118 | + |
| 119 | + it('replaces progress state on each progress_state event', () => { |
| 120 | + const store = setup() |
| 121 | + |
| 122 | + progressState('job-1', { n1: { state: 'running', value: 1, max: 4 } }) |
| 123 | + expect(store.executingNodeId).toBe('n1') |
| 124 | + |
| 125 | + progressState('job-1', { n2: { state: 'running', value: 2, max: 2 } }) |
| 126 | + expect(store.executingNodeIds).toEqual(['n2']) |
| 127 | + }) |
| 128 | +}) |
0 commit comments