import { afterEach, describe, expect, it, vi } from 'vitest' import { defineComponent } from 'vue' import { flushPromises, mount, type VueWrapper } from '@vue/test-utils' import { ApiError } from '@/lib/http' import { useVideoAudit } from '@/features/production/useVideoAudit' import { videoAuditApi } from '@/features/production/video-audit-api' import { auditEvidenceIssues, auditReleaseIssues } from '@/features/production/video-audit' import { workflowExecutionApi } from '@/features/workflows/execution-api' import { videoAuditFixture, videoEvidenceFixture } from '../../fixtures/billing' /** 每次释放实际组合逻辑与请求替身。 */ let wrapper: VueWrapper | undefined afterEach(() => { wrapper?.unmount() vi.restoreAllMocks() }) /** * 挂载原视频审计逻辑,仅模拟同步 HTTP 边界。 * @returns 原证据、表单、接口及刷新通知 */ async function setup() { const { audit, original } = videoAuditFixture() const history = vi .spyOn(videoAuditApi, 'history') .mockResolvedValue({ history: [original], blockingTasks: [original] }) vi.spyOn(workflowExecutionApi, 'latest').mockResolvedValue(null) const get = vi.spyOn(videoAuditApi, 'get').mockImplementation(async () => structuredClone(audit)) const task = vi.spyOn(videoAuditApi, 'task').mockImplementation(async () => structuredClone(original)) const register = vi .spyOn(videoAuditApi, 'register') .mockResolvedValue({ evidenceId: 'event', billingBlocked: true, reused: false }) const review = vi .spyOn(videoAuditApi, 'review') .mockResolvedValue({ reviewId: 'review', status: 'released', billingBlocked: false, reused: false }) const changed = vi.fn<() => void>() let form!: ReturnType wrapper = mount( defineComponent({ setup() { form = useVideoAudit( { projectId: audit.projectId, shotId: audit.shotId, videoId: original.videoId! }, changed ) return () => null } }) ) await flushPromises() return { audit, original, form, get, task, register, review, changed, history } } /** * 填写隔离材料,未知仍可登记,不能据此自动核清。 * @param form 原表单 */ async function fillEvidence(form: ReturnType) { const evidence = videoEvidenceFixture() form.operator.value = '隔离登记人' form.providerRequestId.value = evidence.providerRequestId! form.correlation.value = evidence.correlation form.artifacts.value = evidence.artifacts form.outcome.value = evidence.execution.outcome form.finding.value = evidence.billing.finding form.amount.value = evidence.billing.amount form.executionIds.value = evidence.execution.evidenceIds form.billingIds.value = evidence.billing.evidenceIds await flushPromises() form.confirmed.value = true } describe('视频外部费用审计独立登记与人工复核', () => { it('登记原身份、原任务版本、未知金额,不解除或修改旧质量', async () => { const { audit, original, form, register, review, changed } = await setup() const before = structuredClone(original) await fillEvidence(form) form.amount.value = null form.finding.value = 'unknown' await flushPromises() form.confirmed.value = true await Promise.all([form.submit(), form.submit()]) expect(register).toHaveBeenCalledOnce() expect(register.mock.calls[0]![2]).toMatchObject({ expectedTaskFingerprint: audit.taskFingerprint, evidence: { identity: audit.steps[0]!.identity, billing: { amount: null, finding: 'unknown', currency: 'USD' } } }) expect(review).not.toHaveBeenCalled() expect(original).toEqual(before) expect(changed).toHaveBeenCalledOnce() expect(form.confirmed.value).toBe(false) }) it.each(['VIDEO_AUDIT_STALE', 'VIDEO_AUDIT_REVIEW_CONFLICT', 'VIDEO_AUDIT_IDENTITY_MISMATCH'])( '409 %s 刷新版本并保留表单,不自动重新提交', async code => { const { form, register, get, audit, changed } = await setup() await fillEvidence(form) register.mockImplementationOnce(async () => { audit.steps[0]!.reviewFingerprint = '9'.repeat(64) throw new ApiError('版本已变化', 409, undefined, code) }) await form.submit() expect(get).toHaveBeenCalledTimes(2) expect(form.operator.value).toBe('隔离登记人') expect(form.providerRequestId.value).toBe('mock-provider-request') expect(form.artifacts.value).toHaveLength(2) expect(form.confirmed.value).toBe(false) await form.submit() expect(register).toHaveBeenCalledOnce() expect(changed).not.toHaveBeenCalled() } ) it('核清只提交三个服务端版本并刷新原资源,费用成功不改变过期/待审质量', async () => { const { audit, original, form, review, register, get, task, changed } = await setup() const step = audit.steps[0]! step.evidence.push({ id: 'evidence', taskFingerprint: audit.taskFingerprint, stepName: step.name, createdAt: '2026-11-13T00:00:00Z', digest: '1'.repeat(64), body: { operator: '隔离登记人', stepName: step.name, expectedTaskFingerprint: audit.taskFingerprint, evidence: videoEvidenceFixture() } }) await form.refresh() form.action.value = 'review' form.evidenceId.value = 'evidence' form.reviewer.value = '隔离复核人' form.reason.value = '核对隔离原件' form.confirmationSha256.value = '3'.repeat(64) await flushPromises() expect(form.releaseIssues.value).toEqual([]) form.decision.value = 'release_future_charges' await flushPromises() form.confirmed.value = true const before = structuredClone(original) review.mockImplementationOnce(async () => { audit.billingBlocked = false step.status = 'released' return { reviewId: 'review', status: 'released', billingBlocked: false, reused: false } }) await form.submit() expect(review).toHaveBeenCalledExactlyOnceWith( audit.shotId, audit.taskId, expect.objectContaining({ expectedTaskFingerprint: audit.taskFingerprint, expectedEvidenceSetFingerprint: step.evidenceSetFingerprint, expectedReviewFingerprint: step.reviewFingerprint, decision: 'release_future_charges' }) ) expect(register).not.toHaveBeenCalled() expect(original).toEqual(before) expect(form.query.data.value!.original.current).toBe(false) expect(form.query.data.value!.original.needsReview).toBe(true) expect(form.query.data.value!.audit.billingBlocked).toBe(false) expect(get.mock.calls.length).toBeGreaterThan(1) expect(task.mock.calls.length).toBeGreaterThan(1) expect(changed).toHaveBeenCalledOnce() }) it('真实原件缺失、歧义、未知费用、币种、原 SDK 事实与本地编号均禁止核清', () => { const { audit } = videoAuditFixture(), step = audit.steps[0]! for (const kind of [ 'missing', 'ambiguous', 'unknown', 'currency', 'sdk', 'local', 'observability', 'not-received' ] as const) { const evidence = videoEvidenceFixture(), currentStep = structuredClone(step) if (kind === 'missing') evidence.artifacts = [] if (kind === 'ambiguous') evidence.correlation = 'ambiguous' if (kind === 'unknown') evidence.billing.amount = null if (kind === 'currency') evidence.billing.currency = 'CNY' if (kind === 'sdk') currentStep.hasOriginalReceipt = true if (kind === 'local') evidence.providerRequestId = evidence.identity.requestId if (kind === 'observability') evidence.artifacts.forEach(item => (item.kind = 'langsmith')) if (kind === 'not-received') evidence.execution.outcome = 'not-received' expect(auditReleaseIssues(evidence, currentStep, audit).length).toBeGreaterThan(0) } }) it('校验重复/缺失原件引用,允许未知声明但不会推断零费用', () => { const evidence = videoEvidenceFixture() evidence.artifacts[1]!.id = 'execution' expect(auditEvidenceIssues(evidence).length).toBeGreaterThan(0) const unknown = videoEvidenceFixture() unknown.correlation = 'unverified' unknown.execution.outcome = 'unknown' unknown.billing.finding = 'unknown' unknown.billing.amount = null expect(auditEvidenceIssues(unknown)).toEqual([]) expect(auditReleaseIssues(unknown, videoAuditFixture().audit.steps[0]!, videoAuditFixture().audit)).not.toEqual( [] ) }) it('原候选过期预检失败后仍允许按原任务号回读,归属错误禁止写入', async () => { const { form, history, get, register, review } = await setup() history.mockRejectedValueOnce(new ApiError('旧候选已过期', 409)) await form.discovery.refresh() expect(form.discovery.data.value!.notice).toContain('原任务编号') get.mockImplementationOnce(async () => ({ ...videoAuditFixture().audit, projectId: 'other' })) await form.refresh() form.confirmed.value = true await form.submit() expect(register).not.toHaveBeenCalled() expect(review).not.toHaveBeenCalled() expect(form.query.error.value).toContain('归属') }) })