import { describe, expect, it, vi, afterEach } from 'vitest' import { motionEvidenceCurrent, type MotionRevision } from '@/features/storyboard/motion-revision' import { motionApi } from '@/features/storyboard/motion-api' import { keyframeFixture } from '@/features/production/testing/fixtures' import type { WorkflowExecution } from '@/features/workflows/execution.types' /** 完整的不可变修订,供证据绑定验证。 */ const revision: MotionRevision = { revisionId: 'r1', shotId: 'shot-1', source: 'manual', parentRevisionId: null, reason: '调整动作', fingerprint: 'fp1', createdAt: '2026-10-14T00:00:00Z', isActive: false, durationSeconds: 4, motionPlan: { beats: [ { startSecond: 0, endSecond: 4, action: '人物缓慢行走', cameraMotion: '保持静止', physicalContinuity: '动作连贯自然', subjectRefs: ['@CH0001'], closingState: '停在画面中央' } ], globalMotionConstraints: ['保持动作连续'] } } /** 真正完成并通过的后台证据,指针为空表示历史 AI 生效状态。 */ const primary = keyframeFixture() const execution: WorkflowExecution = { id: 'e1', projectId: 'p1', workflowName: 'motion-plan-start-state-validation', status: 'completed', stage: 'completed', current: 1, total: 1, currentResourceId: null, metadata: { shotId: 'shot-1' }, error: null, startedAt: null, completedAt: null, createdAt: '', updatedAt: '', result: { shotId: 'shot-1', revisionId: 'r1', fingerprint: 'fp1', primaryKeyframeId: primary.id, primaryKeyframeImageUrl: primary.imageUrl, expectedRevisionId: null, passed: true, stale: false, validation: { passed: true } } } afterEach(() => vi.unstubAllGlobals()) describe('动作修订激活证据', () => { it('仅接受完成且通过的对应修订与首帧', () => expect(motionEvidenceCurrent(execution, revision, primary, null)).toBe(true)) it.each([ { passed: false }, { stale: true }, { revisionId: 'r2' }, { fingerprint: 'other' }, { shotId: 'shot-2' }, { validation: { passed: false } } ])('拒绝失败、失效或不属于当前目标的证据 %j', patch => { expect( motionEvidenceCurrent({ ...execution, result: { ...execution.result, ...patch } }, revision, primary, null) ).toBe(false) }) it('任务结束不等于通过,活动任务也不能激活', () => { expect(motionEvidenceCurrent({ ...execution, status: 'running' }, revision, primary, null)).toBe(false) expect(motionEvidenceCurrent({ ...execution, status: 'failed' }, revision, primary, null)).toBe(false) }) it('同 ID 首帧换图和生效指针变化都撤销证据', () => { expect(motionEvidenceCurrent(execution, revision, { ...primary, imageUrl: '/new.png' }, null)).toBe(false) expect(motionEvidenceCurrent(execution, revision, primary, 'r2')).toBe(false) }) it('保存显式发送空父版本,视觉校验发送当前生效指针,激活发送真实任务 ID', async () => { const fetcher = vi.fn().mockImplementation(async () => new Response('{"data":{}}')) vi.stubGlobal('fetch', fetcher) await motionApi.previewRevision('s/1', revision.motionPlan) await motionApi.saveRevision('s/1', { motionPlan: revision.motionPlan, parentRevisionId: null, reason: null }) await motionApi.validateRevision('s/1', 'r/1', { primaryKeyframeId: primary.id, expectedRevisionId: null, expectedPlanFingerprint: 'a'.repeat(64) }) await motionApi.activateRevision('s/1', 'r/1', { expectedRevisionId: null, validationExecutionId: 'e1' }) const calls = fetcher.mock.calls expect(calls.map(([url]) => url)).toEqual([ '/api/storyboard-shots/s%2F1/motion-plan/revisions/preview', '/api/storyboard-shots/s%2F1/motion-plan/revisions', '/api/storyboard-shots/s%2F1/motion-plan/revisions/r%2F1/start-state-validation/start', '/api/storyboard-shots/s%2F1/motion-plan/revisions/r%2F1/active' ]) expect(JSON.parse(String(calls[1]?.[1]?.body))).toEqual({ motionPlan: revision.motionPlan, parentRevisionId: null, reason: null }) expect(JSON.parse(String(calls[2]?.[1]?.body))).toEqual({ primaryKeyframeId: primary.id, expectedRevisionId: null, expectedPlanFingerprint: 'a'.repeat(64) }) expect(JSON.parse(String(calls[3]?.[1]?.body))).toEqual({ expectedRevisionId: null, validationExecutionId: 'e1' }) }) })