import { afterEach, describe, expect, it, vi } from 'vitest' import { visionState, submitKeyframeVision, physicalRetryTarget } from '@/features/production/keyframe-vision' import { workflowExecutionApi } from '@/features/workflows/execution-api' import type { KeyframeValidationHistoryItem } from '@/features/production/quality.types' /** 创建隔离的历史任务,不调用任何模型。 */ const record = (extra: Partial = {}): KeyframeValidationHistoryItem => ({ id: 'original-task', status: 'running', output: null, error: null, startedAt: null, finishedAt: null, createdAt: '2026-11-07', ...extra }) afterEach(() => { sessionStorage.clear() vi.unstubAllGlobals() }) describe('历史终止和独立单步授权', () => { it('终止记录保持原状态,但不再作为活动任务,不允许普通重新审核', async () => { const terminated = record({ legacyTermination: { auditId: 'audit', terminatedAt: '2026-11-07', billingFinding: 'unresolved', requiresNewAuthorization: true } }) expect(visionState([terminated]).active).toBeUndefined() expect(visionState([terminated]).terminated?.id).toBe('original-task') await expect(submitKeyframeVision('s', 'k', [], [terminated], 'p')).rejects.toThrow('授权') }) it('独立授权只发送授权编号,不混入文字或继续参数', async () => { const fetcher = vi.fn().mockResolvedValue( new Response( JSON.stringify({ data: { shotId: 's', keyframeId: 'k', validationTaskId: 'new-task', executionId: 'new-run', status: 'pending' } }), { status: 202 } ) ) vi.stubGlobal('fetch', fetcher) const terminated = record({ legacyTermination: { auditId: 'audit', terminatedAt: '2026-11-07', billingFinding: 'unresolved', requiresNewAuthorization: true } }) await expect( submitKeyframeVision('s', 'k', { legacyVisionAuthorizationId: 'grant' }, [terminated], 'p') ).resolves.toBeNull() expect(JSON.parse(String(fetcher.mock.calls[0]![1]?.body))).toEqual({ legacyVisionAuthorizationId: 'grant' }) expect(String(fetcher.mock.calls[0]![0])).toContain('projectId=p') }) it('仅 General 已完成且 Physical 失败并有计量的原任务可进入单步授权', () => { const failed = record({ status: 'failed', requestFingerprint: 'a'.repeat(64), recoveryRequired: true, steps: [ { name: 'general', status: 'completed', issues: [] }, { name: 'physical', status: 'failed', usage: { totalTokens: 40 } } ] }) expect(physicalRetryTarget([failed])?.id).toBe('original-task') expect(physicalRetryTarget([{ ...failed, steps: [{ name: 'general', status: 'failed' }] }])).toBeUndefined() }) it('物理专项三项同时发送,可处理费用已记录的失败任务', async () => { const fetcher = vi.fn().mockResolvedValue( new Response( JSON.stringify({ data: { shotId: 's2', keyframeId: 'k2', validationTaskId: 'original-task', executionId: 'physical-run', status: 'pending' } }), { status: 202 } ) ) vi.stubGlobal('fetch', fetcher) const failed = record({ status: 'failed', requestFingerprint: 'a'.repeat(64), recoveryRequired: true, steps: [ { name: 'general', status: 'completed', issues: [] }, { name: 'physical', status: 'failed', usage: { totalTokens: 40 } } ] }) const input = { resumeValidationTaskId: failed.id, expectedRequestFingerprint: failed.requestFingerprint!, physicalRetryAuthorizationId: 'physical-grant' } await expect(submitKeyframeVision('s2', 'k2', input, [failed], 'p')).resolves.toBeNull() expect(JSON.parse(String(fetcher.mock.calls[0]![1]?.body))).toEqual(input) }) it('GET 和 SSE 传递相同项目范围,旧调用省略范围仍兼容', async () => { const fetcher = vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: {} }))) vi.stubGlobal('fetch', fetcher) await workflowExecutionApi.get('run', undefined, 'project') expect(String(fetcher.mock.calls[0]![0])).toContain('projectId=project') expect(workflowExecutionApi.executionEventsUrl('run', 'project')).toContain('projectId=project') expect(workflowExecutionApi.executionEventsUrl('run')).not.toContain('?') }) })