Files

114 lines
4.7 KiB
TypeScript

import { DOMWrapper, flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import { afterEach, describe, expect, it, vi } from 'vitest'
import DramaRewriteAction from '@/features/create-drama/review/DramaRewriteAction.vue'
import { dramaReviewApi } from '@/features/create-drama/review/api'
import { workflowExecutionApi } from '@/features/workflows/execution-api'
import { rewritePreview } from './fixtures'
/** 当前挂载的改写入口。 */
let wrapper: VueWrapper | undefined
/**
* 从实际入口打开改写预检。
* @param id 当前测试项目
* @returns 当前确认弹窗
*/
async function openRewrite(id: string) {
wrapper = mount(DramaRewriteAction, { attachTo: document.body, props: { projectId: id } })
await wrapper.get('button').trigger('click')
await flushPromises()
return new DOMWrapper(document.querySelector<HTMLElement>('.app-dialog[aria-label="恢复改写"]')!)
}
afterEach(() => {
wrapper?.unmount()
wrapper = undefined
document.body.innerHTML = ''
localStorage.clear()
})
describe('恢复改写前的范围与费用确认', () => {
it('打开和关闭只做预检,明确确认后才提交 confirmed 并跟踪后台任务', async () => {
const id = 'rewrite-confirmed'
vi.spyOn(dramaReviewApi, 'rewritePreview').mockResolvedValue(rewritePreview(id))
vi.spyOn(workflowExecutionApi, 'get').mockResolvedValue({
id: 'rewrite-task',
projectId: id,
workflowName: 'create-drama',
status: 'running',
stage: 'resumeRewrite',
current: 0,
total: 0,
currentResourceId: null,
metadata: null,
result: null,
error: null,
startedAt: null,
completedAt: null,
createdAt: '2026-10-28T00:00:00Z',
updatedAt: '2026-10-28T00:00:00Z'
})
const fetcher = vi.fn<typeof fetch>().mockImplementation(
async () =>
new Response(JSON.stringify({ data: { executionId: 'rewrite-task', status: 'running' } }), {
status: 202
})
)
vi.stubGlobal('fetch', fetcher)
const dialog = await openRewrite(id)
expect(dialog.text()).toContain('第 1、2 集')
expect(fetcher).not.toHaveBeenCalled()
const confirm = dialog.findAll('button').find(button => button.text() === '确认恢复改写')!
expect(confirm.attributes('disabled')).toBeDefined()
await dialog.get('[role="checkbox"]').trigger('click')
await confirm.trigger('click')
await flushPromises()
expect(fetcher).toHaveBeenCalledTimes(1)
expect(fetcher.mock.calls[0]![0]).toBe(`/api/projects/${id}/resume-rewrite`)
expect(JSON.parse(String(fetcher.mock.calls[0]![1]!.body))).toEqual({ confirmed: true })
expect(localStorage.getItem(`short-drama:workflow-execution:${id}:create-drama`)).toBe('rewrite-task')
})
it('二次预检发现保存记录或集数变化时撤销旧确认,不发送改写请求', async () => {
const id = 'rewrite-changed'
vi.spyOn(dramaReviewApi, 'rewritePreview')
.mockResolvedValueOnce(rewritePreview(id))
.mockResolvedValue({ ...rewritePreview(id), checkpointId: 'cp-new', episodeNumbers: [2] })
const fetcher = vi.fn<typeof fetch>()
vi.stubGlobal('fetch', fetcher)
const dialog = await openRewrite(id)
await dialog.get('[role="checkbox"]').trigger('click')
await dialog
.findAll('button')
.find(button => button.text() === '确认恢复改写')!
.trigger('click')
await flushPromises()
expect(fetcher).not.toHaveBeenCalled()
expect(dialog.text()).toContain('已变化')
expect(dialog.get('[role="checkbox"]').attributes('aria-checked')).toBe('false')
})
it('服务端未就绪时可关闭和重新读取,但不能启动改写', async () => {
const id = 'rewrite-blocked'
vi.spyOn(dramaReviewApi, 'rewritePreview').mockResolvedValue({
...rewritePreview(id),
ready: false,
status: 'needs_review'
})
const dialog = await openRewrite(id)
expect(dialog.text()).toContain('没有可执行的改写目标')
expect(
dialog
.findAll('button')
.find(button => button.text() === '确认恢复改写')!
.attributes('disabled')
).toBeDefined()
expect(
dialog
.findAll('button')
.find(button => button.text() === '取消')!
.attributes('disabled')
).toBeUndefined()
})
})