import { flushPromises, mount } from '@vue/test-utils' import { afterEach, describe, expect, it, vi } from 'vitest' import { effectScope, ref } from 'vue' import WorkflowExecutionPanel from '@/features/workflows/WorkflowExecutionPanel.vue' import { executionPercentage, executionStageLabel, useWorkflowExecution } from '@/features/workflows/execution' import type { WorkflowExecution } from '@/features/workflows/execution.types' const execution = (overrides: Partial = {}): WorkflowExecution => ({ id: 'execution-1', projectId: 'project-1', workflowName: 'video-quality', status: 'running', stage: 'validating', current: 1, total: 2, currentResourceId: 'shot-1', metadata: null, result: null, error: null, startedAt: '2026-09-23T00:00:00Z', completedAt: null, createdAt: '2026-09-23T00:00:00Z', updatedAt: '2026-09-23T00:01:00Z', ...overrides }) afterEach(() => { localStorage.clear() vi.unstubAllGlobals() }) describe('WorkflowExecution 前端契约', () => { it('保留未知阶段并限制真实进度范围', () => { expect(executionStageLabel('repairing')).toBe('自动修复') expect(executionStageLabel('new_backend_stage')).toBe('new_backend_stage') expect(executionPercentage(1, 2)).toBe(50) expect(executionPercentage(4, 2)).toBe(100) expect(executionPercentage(1, 0)).toBe(0) }) it('刷新页面后使用保存的 executionId 读取后端权威状态', async () => { localStorage.setItem('short-drama:workflow-execution:project-1:video-quality', 'execution-1') const fetcher = vi.fn().mockResolvedValue( new Response(JSON.stringify({ data: execution() })) ) vi.stubGlobal('fetch', fetcher) const scope = effectScope() const tracker = scope.run(() => useWorkflowExecution(ref('project-1'), 'video-quality'))! await flushPromises() expect(fetcher.mock.calls[0]?.[0]).toBe('/api/workflow-executions/execution-1') expect(tracker.query.data.value?.stage).toBe('validating') tracker.dismiss() expect(localStorage.getItem('short-drama:workflow-execution:project-1:video-quality')).toBeNull() scope.stop() }) it('完成面板展示质量汇总和下一步状态', () => { const wrapper = mount(WorkflowExecutionPanel, { props: { execution: execution({ status: 'completed', stage: 'completed', current: 2, result: { targetCount: 2, passed: 1, exhausted: 1, providerFailed: 0, submissionFailed: 0 } }), loading: false, error: '' } }) expect(wrapper.text()).toContain('本轮任务已结束') expect(wrapper.text()).toContain('通过 1') expect(wrapper.text()).toContain('修复耗尽 1') }) })