import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { DOMWrapper, flushPromises, mount, type VueWrapper } from '@vue/test-utils' import BreakdownCandidatePanel from '@/features/breakdown/components/BreakdownCandidatePanel.vue' import { projectsApi } from '@/features/projects/api' import { breakdownApi } from '@/features/breakdown/api' import { breakdownQuality, selectBreakdownSource } from '@/features/breakdown/quality' import { getOperation } from '@/features/workflows/operations' import { candidateCheckpoint, candidateReview, candidateState, commitPreview } from '../../fixtures/breakdown-candidate' import type { CandidateCommitPreview } from '@/features/breakdown/candidate.types' /** 真实确认弹窗挂载于 body,验证完成后清理。 */ let wrapper: VueWrapper | undefined beforeEach(() => { vi.spyOn(projectsApi, 'checkpoints').mockResolvedValue([candidateCheckpoint()]) }) afterEach(() => { wrapper?.unmount(); wrapper = undefined; document.body.innerHTML = ''; vi.restoreAllMocks() }) /** 创建质量已通过但尚未经过安全预检查的面板。 */ function openPanel() { wrapper = mount(BreakdownCandidatePanel, { attachTo: document.body, props: { projectId: 'candidate-project', checkpointId: 'candidate-current', state: candidateState(), quality: breakdownQuality(selectBreakdownSource([candidateCheckpoint()]), candidateReview()), disabled: false } }) return wrapper } /** 点击只读预检查入口,不直接提交。 */ async function checkCandidate() { await wrapper!.findAll('button').find(button => button.text() === '提交候选为正式结果')!.trigger('click') await flushPromises() } /** 用户勾选并点击最终确认按钮。 */ async function confirmCandidate() { const dialog = new DOMWrapper(document.querySelector('.app-dialog')) await dialog.get('[role="checkbox"]').trigger('click') await dialog.findAll('button').find(button => button.text() === '确认提交候选为正式结果')!.trigger('click') await flushPromises() } describe('候选预检查与最终提交门禁', () => { it('审核通过不会直接提交;预检查及最终确认均通过后只提交一次并刷新', async () => { const preview = vi.spyOn(breakdownApi, 'candidateCommitPreview').mockResolvedValue(commitPreview()) const commit = vi.spyOn(breakdownApi, 'commitCandidate').mockResolvedValue({ projectId: 'candidate-project', subjectCount: 2, subjectFormCount: 2, episodeCount: 1, beatCount: 1, shotCount: 2, shotSubjectCount: 2 }) openPanel() expect(preview).not.toHaveBeenCalled() expect(commit).not.toHaveBeenCalled() await checkCandidate() expect(commit).not.toHaveBeenCalled() expect(document.querySelector('.app-dialog')?.textContent).toContain('确认提交候选为正式结果') await confirmCandidate() expect(preview).toHaveBeenCalledTimes(2) expect(commit).toHaveBeenCalledExactlyOnceWith('candidate-project') expect(wrapper!.emitted('refresh')).toHaveLength(1) expect(wrapper!.text()).toContain('候选已提交') await wrapper!.setProps({ state: candidateState({ storyboardPersisted: true, candidateCommitted: true }), quality: breakdownQuality(selectBreakdownSource([candidateCheckpoint(candidateState({ storyboardPersisted: true, candidateCommitted: true }))]), null) }) expect(wrapper!.findAll('button').some(button => button.text() === '提交候选为正式结果')).toBe(false) }) it('ready=false 显示未通过项且不打开最终确认', async () => { const result = commitPreview() result.ready = false result.checks.noProtectedShots = false vi.spyOn(breakdownApi, 'candidateCommitPreview').mockResolvedValue(result) const commit = vi.spyOn(breakdownApi, 'commitCandidate') openPanel() await checkCandidate() expect(wrapper!.text()).toContain('没有需要保护的镜头资产') expect(document.querySelector('.app-dialog')).toBeNull() expect(commit).not.toHaveBeenCalled() }) it('即使 ready=true,缺少一项安全门禁也禁止确认', async () => { const result = commitPreview() result.checks.noSubjectImages = false vi.spyOn(breakdownApi, 'candidateCommitPreview').mockResolvedValue(result) openPanel() await checkCandidate() expect(document.querySelector('.app-dialog')).toBeNull() expect(wrapper!.text()).toContain('主体图片') }) it('最终确认重新发现版本变化时禁止替换并要求刷新', async () => { vi.mocked(projectsApi.checkpoints).mockResolvedValue([candidateCheckpoint(candidateState(), 'new')]) vi.spyOn(breakdownApi, 'candidateCommitPreview').mockResolvedValue(commitPreview()) const commit = vi.spyOn(breakdownApi, 'commitCandidate') openPanel() await checkCandidate() await confirmCandidate() expect(commit).not.toHaveBeenCalled() expect(getOperation('candidate-project').error).toContain('版本已变化') expect(wrapper!.emitted('refresh')).toHaveLength(1) }) it('最终确认时安全条件改变,原先通过的预检查不能继续使用', async () => { vi.spyOn(breakdownApi, 'candidateCommitPreview').mockResolvedValueOnce(commitPreview()).mockResolvedValueOnce(commitPreview({ ready: false })) const commit = vi.spyOn(breakdownApi, 'commitCandidate') openPanel() await checkCandidate() await confirmCandidate() expect(commit).not.toHaveBeenCalled() expect(getOperation('candidate-project').error).toContain('安全条件已变化') }) it('版本变化后晚到的预检查响应不会重新打开确认窗口', async () => { let resolve!: (value: CandidateCommitPreview) => void const preview = vi.spyOn(breakdownApi, 'candidateCommitPreview').mockImplementation(() => new Promise(done => { resolve = done })) openPanel() await checkCandidate() await wrapper!.findAll('button').find(button => button.text() === '提交候选为正式结果')!.trigger('click') expect(preview).toHaveBeenCalledTimes(1) await wrapper!.setProps({ checkpointId: 'new' }) resolve(commitPreview()) await flushPromises() expect(document.querySelector('.app-dialog')).toBeNull() }) it('新快照没有候选标记时不会复用旧候选入口', async () => { openPanel() await wrapper!.setProps({ checkpointId: 'new', state: {} }) expect(wrapper!.text()).not.toContain('提交候选为正式结果') }) })