import { afterEach, describe, expect, it, vi } from 'vitest' import { flushPromises, mount, type VueWrapper } from '@vue/test-utils' import { NCheckbox, NSelect } from 'naive-ui' import CandidateRepairs from '@/features/breakdown/components/CandidateRepairs.vue' import ConfirmAction from '@/features/workflows/ConfirmAction.vue' import { breakdownApi } from '@/features/breakdown/api' import { candidateReview, candidateState } from '../../fixtures/breakdown-candidate' import type { CandidateRevalidation } from '@/features/breakdown/candidate.types' /** 候选修复组件实例,禁止真实写入。 */ let wrapper: VueWrapper | undefined afterEach(() => { wrapper?.unmount(); wrapper = undefined; vi.restoreAllMocks(); document.body.innerHTML = '' }) /** * 只将真实阻断镜头的原始动作放入审核响应。 * @returns 两个动作阻断与原始阶段规划 */ function blockedReview(): CandidateRevalidation { const state = candidateState() const blocks = [1, 2].map(shotNo => ({ episodeNo: 1, beatNo: 1, shotNo, status: 'blocked' as const, blockingCodes: ['state_used_as_action_phase'], warningCodes: [], isPendingEpisode: true })) return candidateReview({ actionQualityBlocks: blocks, actionRepairTargets: blocks.map(block => ({ ...block, shot: { ...state.storyboardEpisodeShots![0]!.beatShots[0]!.shots[block.shotNo - 1]!, requiredActions: state.storyboardEpisodeShots![0]!.beatShots[0]!.shots[block.shotNo - 1]!.requiredActions!, durationPlanning: state.storyboardEpisodeShots![0]!.beatShots[0]!.shots[block.shotNo - 1]!.durationPlanning! } })), pendingQualityEpisodeNos: [1] }) } describe('人工候选修复', () => { it('主体和形态只能来自当前候选,错误归属的形态不能提交;成功请求刷新新版本', async () => { const state = candidateState() const review = candidateReview({ missingSubjectRefs: [{ episodeNo: 1, beatNo: 1, shotNo: 1, message: '缺少铜铃 @PR0001', shot: state.storyboardEpisodeShots![0]!.beatShots[0]!.shots[0]!, beatSubjectRefs: ['@PR0001'], subject: state.subjectCandidates![1]!, availableForms: [state.subjectForms![1]!] }] }) const repair = vi.spyOn(breakdownApi, 'repairCandidateSubject').mockResolvedValue({ checkpointId: 'repaired', storyboardShotValidation: { valid: true, issues: [] }, candidateValidation: { valid: true, issues: [] } }) wrapper = mount(CandidateRepairs, { props: { projectId: 'candidate-project', checkpointId: 'candidate-current', state, review, disabled: false } }) wrapper.findAllComponents(NSelect)[0]!.vm.$emit('update:value', '1/1/1') await flushPromises() wrapper.findAllComponents(NSelect)[1]!.vm.$emit('update:value', '@PR0001') await flushPromises() expect(wrapper.findAllComponents(NSelect)[2]!.props('options')).toEqual([{ label: '铜铃形态', value: 'prop-form' }]) wrapper.findAllComponents(NSelect)[2]!.vm.$emit('update:value', 'person-form') await flushPromises() wrapper.getComponent(ConfirmAction).vm.$emit('confirm') await flushPromises() expect(repair).not.toHaveBeenCalled() wrapper.findAllComponents(NSelect)[2]!.vm.$emit('update:value', 'prop-form') await flushPromises() wrapper.getComponent(ConfirmAction).vm.$emit('confirm') await flushPromises() expect(repair).toHaveBeenCalledExactlyOnceWith('candidate-project', { expectedCheckpointId: 'candidate-current', episodeNo: 1, beatNo: 1, shotNo: 1, subjectRef: '@PR0001', formId: 'prop-form' }) expect(wrapper.emitted('refresh')).toHaveLength(1) }) it('多个动作必须经过选择和描述编辑,批量失败保留全部草稿且不发送 kind', async () => { const repair = vi.spyOn(breakdownApi, 'repairCandidateActions').mockRejectedValue(new Error('目标动作处于并行阶段,不能自动覆盖其他动作描述')) wrapper = mount(CandidateRepairs, { props: { projectId: 'candidate-project', checkpointId: 'candidate-current', state: candidateState(), review: blockedReview(), disabled: false } }) const confirm = () => wrapper!.getComponent(ConfirmAction) confirm().vm.$emit('confirm') await flushPromises() expect(repair).not.toHaveBeenCalled() for (const [index, checkbox] of wrapper.findAllComponents(NCheckbox).entries()) { checkbox.vm.$emit('update:checked', true) await flushPromises() await wrapper.findAll('textarea')[index]!.setValue(`人物伸手握住并拿起铜铃 ${index + 1}`) } expect(wrapper.get('[aria-label="批量修复摘要"]').text()).toContain('共 2 个动作') confirm().vm.$emit('confirm') await flushPromises() expect(repair).toHaveBeenCalledTimes(1) const input = repair.mock.calls[0]![1] expect(input.expectedCheckpointId).toBe('candidate-current') expect(input.repairs).toHaveLength(2) expect(input.repairs.every(item => !('kind' in item))).toBe(true) expect(wrapper.findAll('textarea').map(field => field.element.value)).toEqual(['人物伸手握住并拿起铜铃 1', '人物伸手握住并拿起铜铃 2']) expect(wrapper.text()).toContain('并行阶段') expect(wrapper.emitted('refresh')).toBeUndefined() }) it('版本过期后旧编辑内容不能提交,正式模式也不能写候选', async () => { const repair = vi.spyOn(breakdownApi, 'repairCandidateActions') wrapper = mount(CandidateRepairs, { props: { projectId: 'candidate-project', checkpointId: 'new-version', state: candidateState(), review: blockedReview(), disabled: false } }) expect(wrapper.findAll('textarea')).toHaveLength(0) expect(repair).not.toHaveBeenCalled() await wrapper.setProps({ checkpointId: 'candidate-current', state: candidateState({ storyboardPersisted: true }) }) await flushPromises() expect(wrapper.findAllComponents(NCheckbox).every(item => item.props('disabled'))).toBe(true) }) })