import { afterEach, describe, expect, it, vi } from 'vitest' import { identityImageFixture } from '@/features/subject-identity/testing/fixtures' import { subjectIdentityApi } from '@/features/subject-identity/api' /** 每个契约用例完成后清理网络替身。 */ afterEach(() => vi.unstubAllGlobals()) /** 验证正式身份路由和付费请求没有自动重试。 */ describe('身份正式接口契约', () => { it('普通生图保留任务编号和嵌套图片,选角仍使用独立图片回执', async () => { const image = identityImageFixture({ status: 'generating', imageUrl: null, isAnchor: false }) const started = { executionId: 'execution-1', image } const fetcher = vi .fn() .mockResolvedValueOnce(new Response(JSON.stringify({ data: started }), { status: 202 })) .mockResolvedValueOnce(new Response(JSON.stringify({ data: image }), { status: 202 })) vi.stubGlobal('fetch', fetcher) const result = await subjectIdentityApi.generateImage('subject-1', { viewType: 'primary' }) expect(result.executionId).toBe('execution-1') expect(result.image.identityId).toBe(image.identityId) expect(result.image.status).toBe('generating') expect(await subjectIdentityApi.generateCastingCandidate('subject-1', {})).toEqual(image) expect(fetcher).toHaveBeenCalledTimes(2) }) it('预览与实际生成传递相同参数,并支持取消预览', async () => { const fetcher = vi .fn() .mockImplementation(async () => new Response(JSON.stringify({ data: {} }), { status: 200 })) vi.stubGlobal('fetch', fetcher) const input = { viewType: 'primary' as const, provider: 'qwen-image', generationOptions: { size: '2048*2048' }, referenceImageId: 'image-1', prompt: '真实完整提示词' } await subjectIdentityApi.previewImage('subject-1', input, new AbortController().signal) await subjectIdentityApi.generateImage('subject-1', input) expect(fetcher.mock.calls[0]?.[0]).toContain('/subjects/subject-1/identity/images/preview') expect(fetcher.mock.calls[0]?.[1]?.body).toBe(JSON.stringify(input)) expect(fetcher.mock.calls[1]?.[1]?.body).toBe(fetcher.mock.calls[0]?.[1]?.body) }) it('校验指定正式图片,质量生成保留有限修复次数和模型参数', async () => { const fetcher = vi .fn() .mockImplementation(async () => new Response(JSON.stringify({ data: {} }), { status: 200 })) vi.stubGlobal('fetch', fetcher) await subjectIdentityApi.validateImage('subject-1', 'image-1') const input = { maxRepairAttempts: 1, provider: 'qwen-image', generationOptions: { size: '2048*2048' } } await subjectIdentityApi.generateStableAnchorQuality('subject-1', input) expect(fetcher.mock.calls[0]?.[0]).toContain('/subjects/subject-1/identity/images/image-1/validate') expect(fetcher.mock.calls[1]?.[0]).toContain('/subjects/subject-1/identity/stable-anchor/generate-quality') expect(fetcher.mock.calls[1]?.[1]?.body).toBe(JSON.stringify(input)) expect(fetcher).toHaveBeenCalledTimes(2) }) })