118 lines
4.6 KiB
TypeScript
118 lines
4.6 KiB
TypeScript
import { effectScope, ref } from 'vue'
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { useWorkflowExecution } from '@/features/workflows/execution'
|
|
import { workflowExecutionApi } from '@/features/workflows/execution-api'
|
|
|
|
/** 每个测试恢复句柄和网络依赖,避免项目任务泄漏到下一次验证。 */
|
|
afterEach(() => {
|
|
localStorage.clear()
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
describe('异步任务启动后的项目归属', () => {
|
|
it('原项目的启动回执到达时,只保存原项目句柄,不切换当前项目的观察目标', async () => {
|
|
const loader = vi.spyOn(workflowExecutionApi, 'get')
|
|
const scope = effectScope()
|
|
const projectId = ref('project-b')
|
|
const execution = scope.run(() => useWorkflowExecution(projectId, 'production'))!
|
|
await execution.track('execution-a', 'project-a')
|
|
expect(localStorage.getItem('short-drama:workflow-execution:project-a:production')).toBe('execution-a')
|
|
expect(localStorage.getItem('short-drama:workflow-execution:project-b:production')).toBeNull()
|
|
expect(execution.executionId.value).toBe('')
|
|
expect(loader).not.toHaveBeenCalled()
|
|
scope.stop()
|
|
})
|
|
})
|
|
|
|
it('同项目首帧后台任务在各观察器间同步,并阻止其它付费操作直到真实终态', async () => {
|
|
const { getOperation } = await import('@/features/workflows/operations')
|
|
const id = ref('shared-quality-project')
|
|
const record = {
|
|
id: 'quality-exec',
|
|
projectId: id.value,
|
|
workflowName: 'keyframe-quality',
|
|
status: 'running' as 'running' | 'completed',
|
|
stage: 'keyframe_quality',
|
|
current: 0,
|
|
total: 1,
|
|
currentResourceId: null,
|
|
metadata: {},
|
|
result: null,
|
|
error: null,
|
|
startedAt: null,
|
|
completedAt: null,
|
|
createdAt: '2026-10-08',
|
|
updatedAt: '2026-10-08'
|
|
}
|
|
vi.spyOn(workflowExecutionApi, 'get').mockImplementation(async () => ({ ...record }))
|
|
const scope = effectScope()
|
|
const [first, second] = scope.run(() => [
|
|
useWorkflowExecution(id, 'keyframe-quality'),
|
|
useWorkflowExecution(id, 'keyframe-quality')
|
|
])!
|
|
await first!.track(record.id, id.value)
|
|
expect(getOperation(id.value).pending).toBe(true)
|
|
expect(second!.executionId.value).toBe(record.id)
|
|
expect(second!.active.value).toBe(true)
|
|
record.status = 'completed'
|
|
await first!.query.refresh()
|
|
expect(getOperation(id.value).pending).toBe(false)
|
|
scope.stop()
|
|
})
|
|
|
|
it('已卸载观察器的迟到 running 记录不能重新锁住其它观察器已确认结束的任务', async () => {
|
|
const { getOperation } = await import('@/features/workflows/operations')
|
|
const id = ref('terminal-monotonic-project')
|
|
const record = {
|
|
id: 'terminal-exec',
|
|
projectId: id.value,
|
|
workflowName: 'keyframe-quality',
|
|
status: 'running' as 'running' | 'completed',
|
|
stage: 'keyframe_quality',
|
|
current: 0,
|
|
total: 1,
|
|
currentResourceId: null,
|
|
metadata: {},
|
|
result: null,
|
|
error: null,
|
|
startedAt: null,
|
|
completedAt: null,
|
|
createdAt: '2026-10-08',
|
|
updatedAt: '2026-10-08'
|
|
}
|
|
let delayed = false
|
|
let finish!: (value: typeof record) => void
|
|
vi.spyOn(workflowExecutionApi, 'get').mockImplementation(async () => {
|
|
if (delayed) {
|
|
delayed = false
|
|
return new Promise(resolve => {
|
|
finish = resolve
|
|
})
|
|
}
|
|
return { ...record }
|
|
})
|
|
const firstScope = effectScope()
|
|
const secondScope = effectScope()
|
|
const first = firstScope.run(() => useWorkflowExecution(id, 'keyframe-quality'))!
|
|
const second = secondScope.run(() => useWorkflowExecution(id, 'keyframe-quality'))!
|
|
await first.track(record.id, id.value)
|
|
delayed = true
|
|
const lateResponse = first.query.refresh()
|
|
firstScope.stop()
|
|
record.status = 'completed'
|
|
await second.query.refresh()
|
|
expect(getOperation(id.value).pending).toBe(false)
|
|
finish({ ...record, status: 'running' })
|
|
await lateResponse
|
|
expect(getOperation(id.value).pending).toBe(false)
|
|
secondScope.stop()
|
|
})
|
|
|
|
it('后台启动未返回编号时不静默当作已跟踪,也不保存虚假句柄', async () => {
|
|
const scope = effectScope()
|
|
const execution = scope.run(() => useWorkflowExecution(ref('missing-execution-id-project'), 'production'))!
|
|
await expect(execution.track('', 'missing-execution-id-project')).rejects.toThrow('任务编号')
|
|
expect(localStorage.getItem('short-drama:workflow-execution:missing-execution-id-project:production')).toBeNull()
|
|
scope.stop()
|
|
})
|