Files

204 lines
8.9 KiB
TypeScript

import { flushPromises, mount } from '@vue/test-utils'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { effectScope, ref } from 'vue'
import { NTag } from 'naive-ui'
import WorkflowExecutionPanel from '@/features/workflows/WorkflowExecutionPanel.vue'
import ContextNotice from '@/components/ui/ContextNotice.vue'
import { executionPercentage, executionStageLabel, useWorkflowExecution } from '@/features/workflows/execution'
import type { WorkflowExecution } from '@/features/workflows/execution.types'
const execution = (overrides: Partial<WorkflowExecution> = {}): 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.each(['failed', 'completed'] as const)('派生任务 %s 保留部分结果,不用绿色表示全部完成', status => {
const wrapper = mount(WorkflowExecutionPanel, {
props: {
loading: false,
error: '',
execution: execution({
workflowName: 'storyboard-visual-state',
status,
current: 2,
total: 2,
result: {
episodeCount: 2,
completedEpisodes: 1,
skippedEpisodes: 0,
failedEpisodes: 1,
manualReviewRequired: true,
episodes: [
{ episodeNo: 1, status: 'completed' },
{ episodeNo: 2, status: 'failed', error: '来源发生变化' }
]
}
})
}
})
expect(wrapper.text()).toContain('已保存 1 集')
expect(wrapper.text()).toContain('第 2 集 · 失败')
expect(wrapper.text()).toContain('来源发生变化')
expect(wrapper.findAllComponents(NTag).some(tag => tag.props('type') === 'success')).toBe(false)
expect(wrapper.findAllComponents(ContextNotice).some(notice => notice.props('type') === 'success')).toBe(false)
wrapper.unmount()
})
it.each([
{ projectStatus: 'need_review', type: 'warning', role: 'alert' },
{ projectStatus: 'completed', type: 'success', role: undefined },
{ projectStatus: undefined, type: 'info', role: undefined }
])(
'弹窗提示按实际结果突出注意事项,不将待审或未知结果显示为成功:$projectStatus',
({ projectStatus, type, role }) => {
const wrapper = mount(WorkflowExecutionPanel, {
props: {
execution: execution({
workflowName: 'create-drama',
status: 'completed',
result: { projectStatus }
}),
loading: false,
error: '',
embedded: true
}
})
const notice = wrapper.getComponent(ContextNotice)
expect(notice.props('type')).toBe(type)
expect(notice.attributes('role')).toBe(role)
expect(notice.find('strong').exists()).toBe(true)
expect(notice.find('svg').exists()).toBe(true)
wrapper.unmount()
}
)
it.each([
{ projectStatus: 'completed', color: 'success', label: '已完成' },
{ projectStatus: 'need_review', color: 'warning', label: '待修订' },
{ projectStatus: undefined, color: 'default', label: '结果待确认' }
])('创作任务结束按明确项目审核结果展示:$projectStatus', ({ projectStatus, color, label }) => {
const wrapper = mount(WorkflowExecutionPanel, {
props: {
execution: execution({
workflowName: 'create-drama',
status: 'completed',
stage: 'completed',
result: { projectStatus }
}),
loading: false,
error: ''
}
})
expect(wrapper.getComponent(NTag).props('type')).toBe(color)
expect(wrapper.getComponent(NTag).text()).toContain(label)
wrapper.unmount()
})
it('拆解进度区分抽取计数与工作流完成,校验失败不显示全成功', () => {
const wrapper = mount(WorkflowExecutionPanel, {
props: {
execution: execution({
workflowName: 'breakdown',
status: 'completed',
current: 3,
total: 3,
metadata: { progressUnit: 'extraction_tasks' },
result: { storyboardShotValidation: { valid: false, issues: [] } }
}),
loading: false,
error: ''
}
})
expect(wrapper.text().replace(/\s/g, '')).toContain('抽取任务3/3')
expect(wrapper.text()).toContain('校验未通过')
expect(wrapper.get('section').attributes('data-task-state')).toBe('error')
expect(wrapper.find('details').exists()).toBe(false)
expect(wrapper.text()).not.toContain('最新镜头资产状态')
wrapper.unmount()
})
it.each([
{ result: { storyboardShotValidation: { valid: true, issues: [] } }, state: 'success', label: '已完成' },
{ result: null, state: 'unknown', label: '结果待确认' },
{
result: { storyboardShotValidation: { valid: true, issues: [] }, storyboardPendingQualityEpisodeNos: [1] },
state: 'error',
label: '待修订'
}
])('拆解终态仅在明确通过校验且没有待修订剧集时显示成功底色:$state', ({ result, state, label }) => {
const wrapper = mount(WorkflowExecutionPanel, {
props: {
execution: execution({ workflowName: 'breakdown', status: 'completed', result }),
loading: false,
error: '',
showStatusHint: false
}
})
expect(wrapper.get('section').attributes('data-task-state')).toBe(state)
expect(wrapper.text()).toContain(label)
expect(wrapper.find('details').exists()).toBe(false)
expect(wrapper.text()).toContain('更新时间')
wrapper.unmount()
})
it('未知阶段以待确认展示且不影响真实进度范围', () => {
expect(executionStageLabel('repairing')).toBe('自动修复')
expect(executionStageLabel('new_backend_stage')).toBe('状态待确认')
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<typeof fetch>().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?projectId=project-1')
expect(tracker.query.data.value?.stage).toBe('validating')
tracker.dismiss()
expect(localStorage.getItem('short-drama:workflow-execution:project-1:video-quality')).toBe('execution-1')
expect(tracker.active.value).toBe(true)
fetcher.mockResolvedValue(new Response(JSON.stringify({ data: execution({ status: 'completed' }) })))
await tracker.query.refresh()
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')
})
})