import { describe, expect, it, vi, afterEach } from 'vitest' import { qualityApi } from '@/features/production/quality-api' import { qualityTargets, parseKeyframeValidationOutput, isQualityBatchResult, qualityKey } from '@/features/production/quality' import type { KeyframeReadiness } from '@/features/production/types' /** 契约测试仅使用本地模拟数据,不调用生成服务。 */ afterEach(() => vi.unstubAllGlobals()) describe('首帧质量正式后端契约', () => { it('202 只表示后台开始且保留单镜头模型参数', async () => { const fetcher = vi .fn() .mockResolvedValue( new Response( JSON.stringify({ data: { executionId: 'exec', projectId: 'p', status: 'running', total: 1 } }), { status: 202 } ) ) vi.stubGlobal('fetch', fetcher) const result = await qualityApi.generate('p', { concurrency: 1, maxRepairAttempts: 2, force: true, episodeNo: 2, limit: 1, shotId: 's', provider: 'image-provider', generationOptions: { quality: 'high' } }) expect(result.executionId).toBe('exec') expect(isQualityBatchResult(result)).toBe(false) expect(JSON.parse(String(fetcher.mock.calls[0]![1]?.body))).toMatchObject({ shotId: 's', limit: 1, provider: 'image-provider', generationOptions: { quality: 'high' } }) }) it('就绪项按集和镜头筛选,不因其他集过期漏掉缺失项', () => { const readiness = { stalePrimaryKeyframe: 1, items: [ { shotId: 's', episodeNo: 2, status: 'ready' }, { shotId: 'other', episodeNo: 1, status: 'ready', primaryKeyframeStale: true } ] } as KeyframeReadiness expect( qualityTargets(readiness, { concurrency: 1, maxRepairAttempts: 1, force: false, episodeNo: 2, limit: 1, shotId: 's' }).map(item => item.shotId) ).toEqual(['s']) expect(qualityKey('p', { kind: 'batch', episodeNo: 2, shotId: 's', title: '' })).not.toBe( qualityKey('p', { kind: 'batch', episodeNo: 2, title: '' }) ) }) it('历史输出 JSON 必须具有真实结论,原始数据不冒充通过', () => { const validation = { passed: false, summary: '缺少主体', subjects: [], subjectCountConsistent: false, unauthorizedText: { detected: false, texts: [] }, issues: [] } expect(parseKeyframeValidationOutput(JSON.stringify(validation))).toEqual(validation) expect(parseKeyframeValidationOutput('{"raw":"PASS"}')).toBeNull() expect(parseKeyframeValidationOutput('invalid')).toBeNull() }) it('历史接口转义目标并传递取消信号', async () => { const fetcher = vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: [] }))) vi.stubGlobal('fetch', fetcher) await qualityApi.keyframeHistory('s/a', 'k/b', new AbortController().signal) expect(fetcher.mock.calls[0]![0]).toBe('/api/storyboard-shots/s%2Fa/keyframes/k%2Fb/validations') expect(fetcher.mock.calls[0]![1]?.signal).toBeInstanceOf(AbortSignal) }) }) // 同步复用回执也必须依据真实质量状态,不能只看 HTTP 200 或 passed 字段。 describe('同步首帧质量展示门禁', () => { it('来源失效、待核查或存在硬错误时不显示通过,原回执保持不变', async () => { const { keyframeValidationPassed } = await import('@/features/production/quality') const result = { passed: true, summary: '历史通过', subjects: [], subjectCountConsistent: true, unauthorizedText: { detected: false, texts: [] }, issues: [] } expect(keyframeValidationPassed(result)).toBe(true) expect(keyframeValidationPassed({ ...result, qualityDecision: 'stale' })).toBe(false) expect(keyframeValidationPassed({ ...result, recoveryRequired: true })).toBe(false) expect( keyframeValidationPassed({ ...result, issues: [{ type: 'physical', severity: 'error', description: '结构冲突' }] }) ).toBe(false) expect(result.passed).toBe(true) }) })