import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { flushPromises, mount, DOMWrapper, type VueWrapper } from '@vue/test-utils' import StoryboardDiagnosis from '@/features/breakdown/components/StoryboardDiagnosis.vue' import DiagnosisShotDetails from '@/features/breakdown/components/DiagnosisShotDetails.vue' import { preparationApi } from '@/features/storyboard-preparation/api' import { breakdownApi } from '@/features/breakdown/api' import { workflowExecutionApi } from '@/features/workflows/execution-api' import { diagnosisCanStart } from '@/features/breakdown/diagnosis' import type { DiagnosisPreflight } from '@/features/breakdown/diagnosis.types' /** 当前正式镜头的免费来源检查。 */ const checked = (): DiagnosisPreflight => ({ projectId: 'diag-p', shotId: 'db-shot', episodeNo: 1, beatNo: 1, shotNo: 1, policyRevision: 3, policyFingerprint: 'a'.repeat(64), existingDiagnosisId: null, existingValid: false, eligibleForFurtherValidation: true, errors: [], requiresExecutionValidation: true, shouldDiagnose: true, willCallModel: false, willModifyAssets: false }) let wrapper: VueWrapper | undefined /** 最近范围的真实形状,不包含可用于编辑的完整动作字段。 */ const preview = () => ({ projectId: 'diag-p', episodeNo: 1, candidateCount: 1, needsEvidenceCount: 1, invalidSnapshotCount: 0, candidates: [{ shotId: 'db-shot', episodeNo: 1, beatNo: 1, shotNo: 1, title: '当前镜头', description: '原始描述', durationSeconds: 4, requiredActionCount: 2, hasDurationPlanning: true, hasHandOccupancy: true, status: 'has_evidence' as const }], willCallModel: false as const, willModifyAssets: false as const }) /** 只读接口全部隔离,任何未授权模型请求均由测试捕获。 */ function mocks() { vi.spyOn(workflowExecutionApi, 'latest').mockResolvedValue(null) vi.spyOn(breakdownApi, 'legacyActionDiagnosisResults').mockResolvedValue({ projectId: 'diag-p', episodeNo: 1, counts: { total: 0, conflict: 0, clear: 0, insufficient: 0, invalid: 0, stale: 0, pending: 0 }, shots: [] }) vi.spyOn(breakdownApi, 'handOccupancyDiagnostics').mockResolvedValue({ projectId: 'diag-p', shotCount: 0, diagnostics: [] } as never) vi.spyOn(breakdownApi, 'legacyActionDiagnosisPreview').mockImplementation(async () => preview()) return vi.spyOn(breakdownApi, 'startLegacyActionDiagnosis') } beforeEach(() => { vi.spyOn(preparationApi, 'revisions').mockResolvedValue({ projectId: 'diag-p', shotId: 'db-shot', count: 0, revisions: [] }) }) afterEach(() => { wrapper?.unmount(); wrapper = undefined; vi.restoreAllMocks(); vi.unstubAllGlobals(); document.body.innerHTML = '' }) describe('单镜头诊断收费前来源检查', () => { it('规则缺失、跨项目、已可复用和不完整检查都不能进入新诊断', () => { expect(diagnosisCanStart(checked(), 'diag-p', 'db-shot')).toBe(true) for (const invalid of [{ projectId: 'other' }, { shotId: 'other' }, { existingValid: true }, { shouldDiagnose: false }, { policyRevision: null }, { policyFingerprint: null }, { errors: ['QUALITY_POLICY_SNAPSHOT_UNKNOWN'] }, { eligibleForFurtherValidation: false }]) expect(diagnosisCanStart({ ...checked(), ...invalid }, 'diag-p', 'db-shot')).toBe(false) }) it('正式单镜头入口先预检查,缺少规则快照显示中文原因且不发收费请求', async () => { const start = mocks() const read = vi.spyOn(breakdownApi, 'diagnosisPreflight').mockResolvedValue({ ...checked(), shouldDiagnose: false, eligibleForFurtherValidation: false, errors: ['QUALITY_POLICY_SNAPSHOT_UNKNOWN'] }) wrapper = mount(StoryboardDiagnosis, { attachTo: document.body, props: { projectId: 'diag-p', episodeNo: 1, episodeNos: [1] } }) wrapper.getComponent(StoryboardDiagnosis).vm.open({ episodeNo: 1, beatNo: 1, shotNo: 1 }); await flushPromises() expect(read).toHaveBeenCalledWith('diag-p', 'db-shot') const drawer = new DOMWrapper(document.querySelector('[aria-label="动作诊断"]')!) expect(drawer.text()).toContain('缺少可信的质量规则记录') expect(drawer.get('[aria-label="启动 AI 诊断"]').attributes('disabled')).toBeDefined() expect(start).not.toHaveBeenCalled() }) it('费用确认后再次检查,规则变化时保留只读结果并撤销启动', async () => { const start = mocks() vi.spyOn(breakdownApi, 'diagnosisPreflight').mockResolvedValueOnce(checked()).mockResolvedValue({ ...checked(), policyRevision: 4, policyFingerprint: 'b'.repeat(64) }) wrapper = mount(StoryboardDiagnosis, { attachTo: document.body, props: { projectId: 'diag-p', episodeNo: 1, episodeNos: [1] } }) wrapper.getComponent(StoryboardDiagnosis).vm.open({ episodeNo: 1, beatNo: 1, shotNo: 1 }); await flushPromises() await new DOMWrapper(document.querySelector('[aria-label="动作诊断"]')!).get('[aria-label="启动 AI 诊断"]').trigger('click') await flushPromises() const confirmation = new DOMWrapper(document.querySelector('.app-dialog')!) await confirmation.findAll('button').find(button => button.text() === '确认启动 AI 诊断')!.trigger('click') await flushPromises() expect(start).not.toHaveBeenCalled() expect(new DOMWrapper(document.querySelector('[aria-label="动作诊断"]')!).get('[aria-label="启动 AI 诊断"]').attributes('disabled')).toBeDefined() }) it('镜头描述在费用确认期间变化时必须重新预览,不能直接诊断新内容', async () => { const start = mocks() vi.spyOn(breakdownApi, 'diagnosisPreflight').mockResolvedValue(checked()) vi.mocked(breakdownApi.legacyActionDiagnosisPreview).mockResolvedValueOnce(preview()).mockResolvedValue({ ...preview(), candidates: [{ ...preview().candidates[0]!, description: '已更改的新镜头' }] }) wrapper = mount(StoryboardDiagnosis, { attachTo: document.body, props: { projectId: 'diag-p', episodeNo: 1, episodeNos: [1] } }) wrapper.getComponent(StoryboardDiagnosis).vm.open({ episodeNo: 1, beatNo: 1, shotNo: 1 }); await flushPromises() await new DOMWrapper(document.querySelector('[aria-label="动作诊断"]')!).get('[aria-label="启动 AI 诊断"]').trigger('click'); await flushPromises() await new DOMWrapper(document.querySelector('.app-dialog')!).findAll('button').find(button => button.text() === '确认启动 AI 诊断')!.trigger('click'); await flushPromises() expect(start).not.toHaveBeenCalled() }) it('修订条件与历史按需读取,跨项目响应不显示为可修订', async () => { const history = vi.spyOn(breakdownApi, 'diagnosisHistory').mockResolvedValue({ projectId: 'other', shotId: 'db-shot', count: 0, revisions: [] }) vi.spyOn(breakdownApi, 'diagnosisRepairReadiness').mockResolvedValue({ projectId: 'other', shotId: 'db-shot', diagnosisId: 'd', eligible: true, repairCandidates: [], blockingReasons: [], requiresManualReview: true, canApplyDirectly: false, willCallModel: false, willModifyAssets: false }) wrapper = mount(DiagnosisShotDetails, { props: { projectId: 'diag-p', shot: { shotId: 'db-shot', episodeNo: 1, beatNo: 1, shotNo: 1, title: '镜头', diagnosisId: 'd', status: 'conflict_found', issues: [], createdAt: null } } }) expect(history).not.toHaveBeenCalled() await wrapper.get('button').trigger('click'); await flushPromises() expect(wrapper.text()).toContain('记录与当前镜头不一致') expect(wrapper.text()).not.toContain('已满足人工修订') }) it('旧诊断即使只读资格返回 eligible,也不能显示成当前可修订', async () => { vi.spyOn(breakdownApi, 'diagnosisHistory').mockResolvedValue({ projectId: 'diag-p', shotId: 'db-shot', count: 0, revisions: [] }) vi.spyOn(breakdownApi, 'diagnosisRepairReadiness').mockResolvedValue({ projectId: 'diag-p', shotId: 'db-shot', diagnosisId: 'd', eligible: true, repairCandidates: [], blockingReasons: [], requiresManualReview: true, canApplyDirectly: false, willCallModel: false, willModifyAssets: false }) wrapper = mount(DiagnosisShotDetails, { props: { projectId: 'diag-p', shot: { shotId: 'db-shot', episodeNo: 1, beatNo: 1, shotNo: 1, title: '镜头', diagnosisId: 'd', status: 'stale', policyStatus: 'stale', shotStale: false, issues: [], createdAt: null } } }) await wrapper.get('button').trigger('click'); await flushPromises() expect(wrapper.text()).not.toContain('已满足人工修订') }) })