181 lines
7.3 KiB
TypeScript
181 lines
7.3 KiB
TypeScript
import { effectScope, ref } from 'vue'
|
|
import { flushPromises } from '@vue/test-utils'
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { useKeyframeVision, visionState } from '@/features/production/keyframe-vision'
|
|
import type { KeyframeValidationHistoryItem } from '@/features/production/quality.types'
|
|
|
|
/** 仅模拟公开历史,不连接真实执行和 Provider。 */
|
|
function task(executionId = 'shared-run', status = 'running'): KeyframeValidationHistoryItem {
|
|
return {
|
|
id: 'same-task',
|
|
executionId,
|
|
status,
|
|
output: null,
|
|
error: null,
|
|
startedAt: null,
|
|
finishedAt: null,
|
|
createdAt: '2026-11-07'
|
|
}
|
|
}
|
|
/** 带项目归属的真实形状执行片段。 */
|
|
function execution(projectId: string, id = 'shared-run', status = 'running') {
|
|
return {
|
|
id,
|
|
projectId,
|
|
workflowName: 'shot-keyframe-validation',
|
|
status,
|
|
stage: 'validating',
|
|
current: 0,
|
|
total: 2,
|
|
updatedAt: '2026-11-07T00:00:00Z'
|
|
}
|
|
}
|
|
/** 记录所有事件连接,以模拟迟到事件及断线。 */
|
|
class Source extends EventTarget {
|
|
/** 测试创建的连接。 */ static instances: Source[] = []
|
|
/** 断线不提交新审核。 */ close = vi.fn<() => void>()
|
|
/**
|
|
* 保存实际订阅地址。
|
|
* @param url 同项目执行事件路径
|
|
*/
|
|
constructor(public url: string) {
|
|
super()
|
|
Source.instances.push(this)
|
|
}
|
|
}
|
|
let scope = effectScope()
|
|
afterEach(() => {
|
|
scope.stop()
|
|
scope = effectScope()
|
|
Source.instances = []
|
|
localStorage.clear()
|
|
sessionStorage.clear()
|
|
vi.useRealTimers()
|
|
vi.unstubAllGlobals()
|
|
})
|
|
describe('审核 GET 与 SSE 项目隔离', () => {
|
|
it('同执行编号切项目也关闭旧 SSE,旧 404 和迟到事件不污染新项目', async () => {
|
|
let resolveOld!: (response: Response) => void
|
|
const fetcher = vi.fn<typeof fetch>(async input => {
|
|
const url = new URL(String(input), 'http://localhost')
|
|
const p = url.searchParams.get('projectId')!
|
|
if (url.pathname.endsWith('/validations')) return new Response(JSON.stringify({ data: [task()] }))
|
|
if (p === 'scope-old')
|
|
return new Promise<Response>(resolve => {
|
|
resolveOld = resolve
|
|
})
|
|
return new Response(JSON.stringify({ data: execution(p) }))
|
|
})
|
|
vi.stubGlobal('fetch', fetcher)
|
|
vi.stubGlobal('EventSource', Source)
|
|
const key = ref(JSON.stringify(['s', 'k', 'scope-old']))
|
|
const vision = scope.run(() => useKeyframeVision(key))!
|
|
await flushPromises()
|
|
const old = Source.instances[0]!
|
|
expect(old.url).toContain('projectId=scope-old')
|
|
key.value = JSON.stringify(['s', 'k', 'scope-new'])
|
|
await flushPromises()
|
|
expect(old.close).toHaveBeenCalled()
|
|
expect(Source.instances.at(-1)!.url).toContain('projectId=scope-new')
|
|
resolveOld(new Response(JSON.stringify({ message: '项目不匹配' }), { status: 404 }))
|
|
old.dispatchEvent(
|
|
new MessageEvent('message', { data: JSON.stringify(execution('scope-old', 'shared-run', 'failed')) })
|
|
)
|
|
await flushPromises()
|
|
expect(vision.execution.data.value?.projectId).toBe('scope-new')
|
|
expect(vision.execution.error.value).toBe('')
|
|
})
|
|
it('SSE 断开后只读轮询恢复,绝不自动 POST', async () => {
|
|
vi.useFakeTimers()
|
|
const fetcher = vi.fn<typeof fetch>(
|
|
async input =>
|
|
new Response(
|
|
JSON.stringify({
|
|
data: String(input).includes('/validations') ? [task()] : execution('poll-project')
|
|
})
|
|
)
|
|
)
|
|
vi.stubGlobal('fetch', fetcher)
|
|
vi.stubGlobal('EventSource', Source)
|
|
scope.run(() => useKeyframeVision(ref(JSON.stringify(['s', 'k', 'poll-project']))))!
|
|
await flushPromises()
|
|
const source = Source.instances[0]!
|
|
source.dispatchEvent(new Event('error'))
|
|
await vi.advanceTimersByTimeAsync(2600)
|
|
await flushPromises()
|
|
expect(source.close).toHaveBeenCalled()
|
|
expect(
|
|
fetcher.mock.calls.filter(([url]) => String(url).includes('/workflow-executions/')).length
|
|
).toBeGreaterThan(1)
|
|
expect(fetcher.mock.calls.every(([, init]) => init?.method === 'GET')).toBe(true)
|
|
})
|
|
it('刷新时服务已有更新执行,不保留旧提交的未知费用锁', async () => {
|
|
const key = JSON.stringify(['s', 'k', 'external-new-run'])
|
|
sessionStorage.setItem(
|
|
'keyframe-vision:' + key,
|
|
JSON.stringify({
|
|
before: 'same-task',
|
|
beforeExecutionId: 'before-run',
|
|
taskId: 'same-task',
|
|
executionId: 'previous-run'
|
|
})
|
|
)
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn<typeof fetch>(
|
|
async input =>
|
|
new Response(
|
|
JSON.stringify({
|
|
data: String(input).includes('/validations')
|
|
? [task('current-run', 'completed')]
|
|
: execution('external-new-run', 'current-run', 'completed')
|
|
})
|
|
)
|
|
)
|
|
)
|
|
const vision = scope.run(() => useKeyframeVision(ref(key)))!
|
|
await flushPromises()
|
|
expect(vision.uncertain.value).toBe(false)
|
|
expect(vision.execution.data.value?.id).toBe('current-run')
|
|
})
|
|
it('同任务出现新执行时切换观察编号,旧执行失败不覆盖新执行', async () => {
|
|
let records = [task('old-execution', 'failed')]
|
|
const fetcher = vi.fn<typeof fetch>(
|
|
async input =>
|
|
new Response(
|
|
JSON.stringify({
|
|
data: String(input).includes('/validations')
|
|
? records
|
|
: execution(
|
|
'multi',
|
|
String(input).includes('new-execution') ? 'new-execution' : 'old-execution',
|
|
String(input).includes('new-execution') ? 'running' : 'failed'
|
|
)
|
|
})
|
|
)
|
|
)
|
|
vi.stubGlobal('fetch', fetcher)
|
|
vi.stubGlobal('EventSource', Source)
|
|
const vision = scope.run(() => useKeyframeVision(ref(JSON.stringify(['s', 'k', 'multi']))))!
|
|
await flushPromises()
|
|
const old = Source.instances[0]!
|
|
records = [task('new-execution', 'running')]
|
|
await vision.history.refresh()
|
|
await flushPromises()
|
|
old.dispatchEvent(
|
|
new MessageEvent('message', { data: JSON.stringify(execution('multi', 'old-execution', 'failed')) })
|
|
)
|
|
expect(vision.execution.data.value?.id).toBe('new-execution')
|
|
expect(vision.state.value.active?.id).toBe('same-task')
|
|
expect(vision.observedExecutions.value.map(item => item.executionId)).toEqual([
|
|
'old-execution',
|
|
'new-execution'
|
|
])
|
|
const state = visionState([
|
|
task('new-execution', 'completed'),
|
|
{ ...task('old-execution', 'failed'), id: 'older-task', recoveryRequired: true }
|
|
])
|
|
expect(state.unknown).toBeUndefined()
|
|
})
|
|
})
|