Files
short-drama-agent-front/tests/features/workflows/operations.test.ts
T

65 lines
2.8 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { getOperation, runOperation } from '@/features/workflows/operations'
/**
* 根据真实校验值决定消息,不能只依据 Promise 成功。
* @param result 后端业务校验结果
* @returns 与通过状态相符的临时反馈
*/
function validationFeedback(result: { passed: boolean }) {
return {
type: result.passed ? ('success' as const) : ('warning' as const),
content: result.passed ? '身份图片校验通过' : '身份图片校验未通过'
}
}
describe('项目级长请求互斥', () => {
it('同一项目不同时执行两个 graph,错误不伪装成功', async () => {
let fail!: (reason: Error) => void
const action = vi.fn<() => Promise<unknown>>(
() =>
new Promise((_resolve, reject) => {
fail = reject
})
)
const first = runOperation('operation-test', '拆解', action)
expect(await runOperation('operation-test', '改写', action)).toBe(false)
expect(action).toHaveBeenCalledTimes(1)
fail(new Error('连接中断'))
expect(await first).toBe(false)
expect(getOperation('operation-test')).toMatchObject({ pending: false, error: '连接中断', notice: '' })
})
it('HTTP 成功但业务校验未通过时保留警告语义,再次通过时更新结果', async () => {
const projectId = 'operation-validation-test'
expect(await runOperation(projectId, '校验', async () => ({ passed: false }), validationFeedback)).toBe(true)
expect(getOperation(projectId)).toMatchObject({
noticeType: 'warning',
notice: '身份图片校验未通过',
pending: false
})
expect(await runOperation(projectId, '校验', async () => ({ passed: true }), validationFeedback)).toBe(true)
expect(getOperation(projectId)).toMatchObject({ noticeType: 'success', notice: '身份图片校验通过' })
expect(
await runOperation(
projectId,
'校验',
async () => {
throw new Error('服务不可用')
},
validationFeedback
)
).toBe(false)
expect(getOperation(projectId)).toMatchObject({ noticeType: null, notice: '', error: '服务不可用' })
})
it('异步任务提交只提示已提交,不提前提示生成完成', async () => {
const projectId = 'operation-submission-test'
await runOperation(projectId, '提交图片生成', async () => ({ status: 'pending' }), {
type: 'info',
content: '任务已提交生成'
})
expect(getOperation(projectId)).toMatchObject({ noticeType: 'info', notice: '任务已提交生成' })
})
})