import { afterEach, describe, expect, it, vi } from 'vitest' import { flushPromises, mount, type VueWrapper } from '@vue/test-utils' import { NCheckbox } from 'naive-ui' import KeyframeEditDialog from '@/features/production/components/KeyframeEditDialog.vue' import { keyframeEditApi } from '@/features/production/keyframe-edit-api' import { generationConfigApi } from '@/features/generation-config/api' import { spatialApi } from '@/features/storyboard/spatial-api' import { productionApi } from '@/features/production/api' import { workflowExecutionApi } from '@/features/workflows/execution-api' import { getOperation } from '@/features/workflows/operations' import { keyframeFixture } from '@/features/production/testing/fixtures' import { imageCatalog, generationProfile } from '../../fixtures/generation' /** 编辑回归只模拟 API,验证确认、过期预览和费用范围,不调用真实模型。 */ let wrapper: VueWrapper | undefined let sequence = 0 afterEach(() => { wrapper?.unmount() wrapper = undefined document.body.innerHTML = '' localStorage.clear() vi.restoreAllMocks() }) /** * 通过实际按钮操作弹窗,保留表单的公开事件与禁用守卫。 * @param text 当前按钮文案 * @returns 按钮元素 */ function button(text: string) { const element = [...document.querySelectorAll('button')].find( item => item.textContent?.trim() === text ) if (!element) throw new Error(`找不到按钮:${text}`) return element } /** 创建独立项目避免后台句柄影响其他测试。 */ async function openEditor() { const projectId = `edit-regression-${sequence++}` const source = keyframeFixture({ isPrimary: false }) vi.spyOn(generationConfigApi, 'catalog').mockResolvedValue(imageCatalog) vi.spyOn(generationConfigApi, 'profile').mockResolvedValue(generationProfile(projectId)) vi.spyOn(spatialApi, 'bindings').mockResolvedValue([]) const preview = vi.spyOn(keyframeEditApi, 'preview').mockImplementation(async (shotId, sourceId, input) => ({ shotId, sourceKeyframeId: sourceId, provider: 'qwen-image', model: 'test', editInstruction: input.editInstruction, editMode: input.editMode, sourceImageSlots: 1, maxInputImages: 4, selected: [], omitted: [] })) const start = vi.spyOn(keyframeEditApi, 'start').mockResolvedValue({ executionId: `exec-${projectId}`, projectId, shotId: source.shotId, sourceKeyframeId: source.id, status: 'running' }) vi.spyOn(productionApi, 'listKeyframes').mockResolvedValue([source]) vi.spyOn(workflowExecutionApi, 'get').mockResolvedValue({ id: `exec-${projectId}`, projectId, workflowName: 'keyframe-quality', status: 'completed', stage: 'editKeyframe', current: 1, total: 1, currentResourceId: source.shotId, metadata: { action: 'edit-keyframe' }, result: { validationSkipped: true, passed: false, primaryChanged: false }, error: null, startedAt: null, completedAt: null, createdAt: '', updatedAt: '' }) wrapper = mount(KeyframeEditDialog, { attachTo: document.body, props: { projectId, shotId: source.shotId, source, disabled: false, open: true } }) await flushPromises() const textarea = document.querySelector('textarea')! textarea.value = '人物上半身自然转向右侧树桩,双眼保持协调' textarea.dispatchEvent(new Event('input', { bubbles: true })) await flushPromises() return { projectId, source, preview, start, textarea } } describe('首帧编辑的费用与来源隔离', () => { it('只预览不启动模型;修改指令会撤销旧预览和确认', async () => { const { start, textarea } = await openEditor() button('预览编辑输入').click() await flushPromises() expect(start).not.toHaveBeenCalled() wrapper!.getComponent(NCheckbox).vm.$emit('update:checked', true) await flushPromises() expect(button('确认编辑').disabled).toBe(false) textarea.value = '修改后的要求' textarea.dispatchEvent(new Event('input', { bubbles: true })) await flushPromises() expect(button('确认编辑').disabled).toBe(true) expect(wrapper!.getComponent(NCheckbox).props('checked')).toBe(false) expect(start).not.toHaveBeenCalled() }) it('源图变为主图时阻止以旧候选费用范围提交', async () => { const { source, start, projectId } = await openEditor() button('预览编辑输入').click() await flushPromises() wrapper!.getComponent(NCheckbox).vm.$emit('update:checked', true) await flushPromises() vi.mocked(productionApi.listKeyframes).mockResolvedValue([{ ...source, isPrimary: true }]) button('确认编辑').click() await flushPromises() expect(start).not.toHaveBeenCalled() expect(getOperation(projectId).error).toContain('主首帧状态已变化') }) it('候选编辑提交可序列化参数,202 句柄落库观察后释放真实终态锁', async () => { const { source, start, projectId } = await openEditor() button('预览编辑输入').click() await flushPromises() wrapper!.getComponent(NCheckbox).vm.$emit('update:checked', true) await flushPromises() button('确认编辑').click() await flushPromises() expect(start).toHaveBeenCalledTimes(1) expect(start.mock.calls[0]?.slice(0, 2)).toEqual([source.shotId, source.id]) expect(start.mock.calls[0]?.[2].editInstruction).toContain('上半身') expect(localStorage.getItem(`short-drama:workflow-execution:${projectId}:keyframe-quality`)).toBe( `exec-${projectId}` ) expect(getOperation(projectId).pending).toBe(false) expect(wrapper!.emitted('started')?.[0]).toEqual([`exec-${projectId}`]) }) })