fix(short-drama-agent-front): 修复拆解进度展示与问题交互
This commit is contained in:
1 parent
3414cc8232
commit
db91a4b871
19 files changed
+870
-150
No files matched your search
@@ -0,0 +1,113 @@
|
||||
import { effectScope, ref } from 'vue'
|
||||
import { flushPromises } from '@vue/test-utils'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { useWorkflowExecution } from '@/features/workflows/execution'
|
||||
import { getOperation } from '@/features/workflows/operations'
|
||||
import type { WorkflowExecution } from '@/features/workflows/execution.types'
|
||||
|
||||
/** 后端自行启动的拆解任务,本地没有保存过句柄。 */
|
||||
function record(projectId: string, status: WorkflowExecution['status'] = 'running'): WorkflowExecution {
|
||||
return {
|
||||
id: `external-${projectId}`,
|
||||
projectId,
|
||||
workflowName: 'breakdown',
|
||||
status,
|
||||
stage: 'breakdown',
|
||||
current: 0,
|
||||
total: 0,
|
||||
currentResourceId: null,
|
||||
metadata: {},
|
||||
result: null,
|
||||
error: null,
|
||||
startedAt: '2026-10-17T00:00:00Z',
|
||||
completedAt: null,
|
||||
createdAt: '2026-10-17T00:00:00Z',
|
||||
updatedAt: '2026-10-17T00:00:00Z'
|
||||
}
|
||||
}
|
||||
afterEach(() => {
|
||||
localStorage.clear()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
describe('服务端任务发现', () => {
|
||||
it('页面保持打开时通过事件接上新任务,终态不被迟到运行消息重新激活', async () => {
|
||||
const task = record('discovery-stream')
|
||||
/** 仅模拟浏览器事件连接,所有任务仍来自测试响应。 */
|
||||
class TestEventSource extends EventTarget {
|
||||
static last: TestEventSource
|
||||
close = vi.fn<() => void>()
|
||||
constructor() {
|
||||
super()
|
||||
TestEventSource.last = this
|
||||
}
|
||||
}
|
||||
vi.stubGlobal('EventSource', TestEventSource)
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi
|
||||
.fn<typeof fetch>()
|
||||
.mockImplementation(
|
||||
async url => new Response(JSON.stringify({ data: String(url).includes('/latest?') ? null : task }))
|
||||
)
|
||||
)
|
||||
const scope = effectScope()
|
||||
const tracker = scope.run(() => useWorkflowExecution(ref(task.projectId), 'breakdown', { discover: true }))!
|
||||
await flushPromises()
|
||||
expect(tracker.executionId.value).toBe('')
|
||||
TestEventSource.last.dispatchEvent(new MessageEvent('message', { data: JSON.stringify(task) }))
|
||||
await flushPromises()
|
||||
expect(tracker.active.value).toBe(true)
|
||||
task.status = 'completed'
|
||||
TestEventSource.last.dispatchEvent(new MessageEvent('message', { data: JSON.stringify(task) }))
|
||||
await flushPromises()
|
||||
TestEventSource.last.dispatchEvent(
|
||||
new MessageEvent('message', { data: JSON.stringify({ ...task, status: 'running' }) })
|
||||
)
|
||||
await flushPromises()
|
||||
expect(tracker.active.value).toBe(false)
|
||||
expect(tracker.query.data.value?.status).toBe('completed')
|
||||
scope.stop()
|
||||
expect(TestEventSource.last.close).toHaveBeenCalledOnce()
|
||||
})
|
||||
it('没有本地句柄时发现后台任务并立即锁住项目操作', async () => {
|
||||
const id = ref('discovery-active')
|
||||
const task = record(id.value)
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn<typeof fetch>().mockImplementation(async () => new Response(JSON.stringify({ data: task })))
|
||||
)
|
||||
const scope = effectScope()
|
||||
const tracker = scope.run(() => useWorkflowExecution(id, 'breakdown', { discover: true }))!
|
||||
await flushPromises()
|
||||
expect(tracker.executionId.value).toBe(task.id)
|
||||
expect(tracker.active.value).toBe(true)
|
||||
expect(getOperation(id.value).pending).toBe(true)
|
||||
scope.stop()
|
||||
})
|
||||
|
||||
it('切换项目后忽略旧项目迟到的发现响应', async () => {
|
||||
let resolveOld!: (value: Response) => void
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn<typeof fetch>().mockImplementation(async url => {
|
||||
if (String(url).includes('/discovery-old/'))
|
||||
return new Promise(resolve => {
|
||||
resolveOld = resolve
|
||||
})
|
||||
return new Response(JSON.stringify({ data: null }))
|
||||
})
|
||||
)
|
||||
const id = ref('discovery-old')
|
||||
const scope = effectScope()
|
||||
const tracker = scope.run(() => useWorkflowExecution(id, 'breakdown', { discover: true }))!
|
||||
await flushPromises()
|
||||
id.value = 'discovery-new'
|
||||
await flushPromises()
|
||||
resolveOld(new Response(JSON.stringify({ data: record('discovery-old') })))
|
||||
await flushPromises()
|
||||
expect(tracker.executionId.value).toBe('')
|
||||
expect(getOperation('discovery-new').pending).toBe(false)
|
||||
scope.stop()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user