import { afterEach, describe, expect, it, vi } from 'vitest' import { productionApi } from '@/features/production/api' import { validateProductionScope } from '@/features/production/useAdvancedProduction' import type { ProductionStartInput } from '@/features/production/types' afterEach(() => vi.unstubAllGlobals()) describe('生产后端真实请求契约', () => { it('计划使用 POST JSON 并将图片参数与启动保持同源', async () => { const fetchMock = vi .fn() .mockImplementation(async () => new Response(JSON.stringify({ data: { projectId: 'p' } }))) vi.stubGlobal('fetch', fetchMock) const input: ProductionStartInput = { imageProvider: 'qwen-image', imageGenerationOptions: { size: '1024*1024', seed: 3 }, videoProvider: 'wan', startFrom: 'subjectImages', stopAfter: 'videoPrompts', stableIdentityAnchorLimit: 1, subjectImageLimit: 2, keyframeLimit: 3, videoPromptLimit: 4 } const controller = new AbortController() await productionApi.plan('p', controller.signal, input) await productionApi.startPipeline('p', input) expect(fetchMock.mock.calls[0]?.[0]).toBe('/api/projects/p/production/plan') expect(fetchMock.mock.calls[1]?.[0]).toBe('/api/projects/p/production/start') for (const [, request] of fetchMock.mock.calls) { expect(request?.method).toBe('POST') expect(JSON.parse(String(request?.body))).toEqual(input) } expect(fetchMock.mock.calls[0]?.[1]?.signal).toBeInstanceOf(AbortSignal) }) it('409 携带现有 executionId 且封装不重试', async () => { const fetchMock = vi.fn().mockImplementation( async () => new Response(JSON.stringify({ message: '已有任务', details: { executionId: 'existing' } }), { status: 409 }) ) vi.stubGlobal('fetch', fetchMock) await expect(productionApi.startPipeline('p')).rejects.toMatchObject({ status: 409, details: { executionId: 'existing' } }) expect(fetchMock).toHaveBeenCalledTimes(1) }) it('拒绝反向阶段、零值和非整数,支持主体图片结束阶段', () => { expect(validateProductionScope({ startFrom: 'videoPrompts', stopAfter: 'subjectImages' })).toContain('结束阶段') expect(validateProductionScope({ subjectImageLimit: 0 })).toContain('整数') expect(validateProductionScope({ videoLimit: 0 })).toContain('整数') expect(validateProductionScope({ videoLimit: 1.5 })).toContain('整数') expect(validateProductionScope({ stableIdentityAnchorLimit: 1.5 })).toContain('整数') expect( validateProductionScope({ startFrom: 'subjectImages', stopAfter: 'subjectImages', videoPromptLimit: 1 }) ).toBe('') }) })