import { afterEach, describe, expect, it, vi } from 'vitest' import { effectScope, ref } from 'vue' import { flushPromises } from '@vue/test-utils' import { assertCandidateReview, breakdownQuality, normalizeBreakdownState, selectBreakdownSource } from '@/features/breakdown/quality' import { useBreakdownQuality } from '@/features/breakdown/useBreakdownQuality' import { breakdownApi } from '@/features/breakdown/api' import { candidateCheckpoint, candidateReview, candidateState } from '../../fixtures/breakdown-candidate' import type { CandidateRevalidation } from '@/features/breakdown/candidate.types' /** 测试后释放自动版本观察,避免跨用例请求。 */ let scope: ReturnType | undefined afterEach(() => { scope?.stop(); scope = undefined; vi.restoreAllMocks() }) describe('拆解当前质量与来源隔离', () => { it('新版候选缺少顶层成果时不借用完整的旧嵌套成果充数', () => { const previous = candidateState() const current = normalizeBreakdownState({ candidateOnly: true, breakdownResult: previous }) expect(current.subjectCandidates).toBeUndefined() expect(current.storyboardEpisodeShots).toBeUndefined() expect(current.breakdownResult?.storyboardShotValidation).toBeUndefined() }) it('主体补齐后使用镜头快照中的最新 Beat 引用,不重新显示旧规划中的缺失道具', () => { const state = candidateState() state.storyboardPlans = structuredClone(state.storyboardPlans) state.storyboardPlans![0]!.beats[0]!.subjectRefs = ['@CH0001'] const result = normalizeBreakdownState(state) expect(result.storyboardPlans![0]!.beats[0]!.subjectRefs).toContain('@PR0001') expect(state.storyboardPlans![0]!.beats[0]!.subjectRefs).not.toContain('@PR0001') }) it('最新候选通过时不显示历史五个错误,也不把候选当作正式结果', () => { const old = Array.from({ length: 5 }, (_, index) => ({ episodeNo: 1, message: `历史错误 ${index}` })) const record = candidateCheckpoint(candidateState({ breakdownResult: { storyboardShotValidation: { valid: false, issues: old } } })) const source = selectBreakdownSource([record]) const quality = breakdownQuality(source, candidateReview({ previousIssues: old })) expect(source.kind).toBe('candidate') expect(source.state?.storyboardShotValidation?.valid).toBe(true) expect(quality.issueCount).toBe(0) expect(quality.historyIssues).toHaveLength(5) expect(quality.committed).toBe(false) expect(quality.canRequestCommitPreview).toBe(true) }) it('真实结构问题与动作阻断分别统计,正常道具检查不会变成错误', () => { const block = { episodeNo: 1, beatNo: 1, shotNo: 2, status: 'blocked' as const, blockingCodes: ['state_used_as_action_phase'], warningCodes: [], isPendingEpisode: true } const quality = breakdownQuality(selectBreakdownSource([candidateCheckpoint()]), candidateReview({ currentIssues: [{ episodeNo: 1, beatNo: 1, shotNo: 1, message: '主体引用缺失' }], actionQualityBlocks: [block], pendingQualityEpisodeNos: [1], candidateValidation: { valid: false, issues: ['Storyboard 质量审核未通过', '仍存在待处理的分镜质量问题'] }, propReferenceDiagnostics: [{ episodeNo: 1, beatNo: 1, shotNo: 1, subjectRef: '@PR0001', shotTitle: '', shotDescription: '', beatAllowsSubject: true, shotHasSubject: true, shotHasBinding: true, subjectName: '铜铃', availableForms: [] }] })) expect(quality.structuralIssues).toHaveLength(1) expect(quality.actionBlocks).toHaveLength(1) expect(quality.issueCount).toBe(2) expect(quality.canRequestCommitPreview).toBe(false) }) it('审核读取中、失败和版本过期均不回退到历史质量', () => { const source = selectBreakdownSource([candidateCheckpoint()]) expect(breakdownQuality(source, candidateReview(), true).structureValid).toBeNull() expect(breakdownQuality(source, null, false, '读取失败').canRequestCommitPreview).toBe(false) expect(breakdownQuality(source, candidateReview({ checkpointId: 'old' })).needsRefresh).toBe(true) expect(() => assertCandidateReview(candidateReview({ checkpointId: 'old' }), 'candidate-project', 'candidate-current')).toThrow('过期') }) it('完整性审核中的 Form 绑定异常计入结构问题,不能标记结构通过', () => { const quality = breakdownQuality(selectBreakdownSource([candidateCheckpoint()]), candidateReview({ candidateValidation: { valid: false, issues: ['剧集 1 Shot 2 主体 @CH0001 形态绑定无效'] } })) expect(quality.structuralCount).toBe(1) expect(quality.structureValid).toBe(false) expect(quality.problems[0]?.type).toBe('binding') expect(quality.canRequestCommitPreview).toBe(false) }) it('正式提交的持久化标记优先,不继承嵌套旧错误;新候选不覆盖已有正式结果', () => { const formal = candidateCheckpoint(candidateState({ storyboardPersisted: true, candidateCommitted: true, breakdownResult: { storyboardShotValidation: { valid: false, issues: [{ episodeNo: 1, message: '旧错误' }] } } }), 'formal') const source = selectBreakdownSource([formal]) expect(breakdownQuality(source, null).source).toBe('formal') expect(breakdownQuality(source, null).issueCount).toBe(0) expect(selectBreakdownSource([formal, candidateCheckpoint()]).formal?.checkpointId).toBe('formal') }) it('只有错误的最新记录可以回看成果,但不能继承候选提交资格或旧错误', () => { const source = selectBreakdownSource([candidateCheckpoint(), { ...candidateCheckpoint({}, 'failed'), createdAt: '2026-10-17T10:00:00Z' }]) const quality = breakdownQuality(source, null) expect(source.kind).toBe('history') expect(quality.isCandidate).toBe(false) expect(quality.structureValid).toBeNull() expect(quality.canRequestCommitPreview).toBe(false) }) it('相同版本并发审核只请求一次,版本变化取消旧请求并拒绝晚到响应', async () => { let resolveOld!: (value: CandidateRevalidation) => void const signals: AbortSignal[] = [] const read = vi.spyOn(breakdownApi, 'revalidateCandidate').mockImplementation(async (_id, signal) => { signals.push(signal!) if (signals.length === 1) return new Promise(resolve => { resolveOld = resolve }) return candidateReview({ checkpointId: 'new' }) }) const records = ref([candidateCheckpoint()]) scope = effectScope() const state = scope.run(() => useBreakdownQuality(ref('candidate-project'), records))! void state.refresh() void state.refresh() expect(read).toHaveBeenCalledTimes(1) records.value = [candidateCheckpoint(candidateState(), 'new')] await flushPromises() expect(signals[0]?.aborted).toBe(true) expect(state.review.value?.checkpointId).toBe('new') resolveOld(candidateReview()) await flushPromises() expect(state.review.value?.checkpointId).toBe('new') expect(read).toHaveBeenCalledTimes(2) }) it.each(['failure', 'stale'] as const)('候选审核 %s 保持未知并禁止进入提交安全检查', async mode => { vi.spyOn(breakdownApi, 'revalidateCandidate').mockImplementation(async () => { if (mode === 'failure') throw new Error('服务不可用') return candidateReview({ checkpointId: 'old' }) }) scope = effectScope() const state = scope.run(() => useBreakdownQuality(ref('candidate-project'), ref([candidateCheckpoint()])))! await flushPromises() expect(state.error.value).not.toBe('') expect(state.quality.value.structureValid).toBeNull() expect(state.quality.value.canRequestCommitPreview).toBe(false) }) it('正式提交后刷新或重新进入,不重新请求候选接口', async () => { const read = vi.spyOn(breakdownApi, 'revalidateCandidate') scope = effectScope() const state = scope.run(() => useBreakdownQuality(ref('candidate-project'), ref([candidateCheckpoint(candidateState({ storyboardPersisted: true, candidateCommitted: true }))])))! await state.refresh() expect(read).not.toHaveBeenCalled() expect(state.quality.value.committed).toBe(true) }) })