Files

86 lines
4.4 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import { productionApi } from '@/features/production/api'
import { productionResumeIssues, validateProductionScope } from '@/features/production/useAdvancedProduction'
import type { ProductionStartInput } from '@/features/production/types'
afterEach(() => vi.unstubAllGlobals())
describe('生产后端真实请求契约', () => {
it('计划保持 POST 且只发送支持的模型参数,启动另外携带执行范围和视频参数', async () => {
const fetchMock = vi
.fn<typeof fetch>()
.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',
videoGenerationOptions: { duration: -1, resolution: '720p' },
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')
expect(fetchMock.mock.calls.every(([, request]) => request?.method === 'POST')).toBe(true)
expect(JSON.parse(String(fetchMock.mock.calls[0]?.[1]?.body))).toEqual({
imageProvider: input.imageProvider,
imageGenerationOptions: input.imageGenerationOptions,
videoProvider: input.videoProvider
})
expect(JSON.parse(String(fetchMock.mock.calls[1]?.[1]?.body))).toEqual(input)
expect(fetchMock.mock.calls[0]?.[1]?.signal).toBeInstanceOf(AbortSignal)
})
it('409 携带现有 executionId 且封装不重试', async () => {
const fetchMock = vi.fn<typeof fetch>().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('')
})
it('指定续跑阶段时要求已有有效资产,不能把可生成误判为已完成', () => {
const stage = { total: 1, planned: 1, skipped: 0, upstreamResolvable: 0, manualBlocked: 0 }
const plan = { subjectImages: stage, keyframes: stage }
expect(productionResumeIssues(plan, {})).toEqual([])
expect(productionResumeIssues(plan, { startFrom: 'subjectImages' }).join()).toContain('角色身份')
expect(productionResumeIssues(plan, { startFrom: 'keyframes' }).join()).toContain('主体参考图')
expect(productionResumeIssues(plan, { startFrom: 'videoPrompts' }).join()).toContain('有效首帧')
expect(
productionResumeIssues(
{
characterIdentity: { total: 1, ready: 1, pending: 0, autoResolvable: 0, manualBlocked: 0 },
subjectImages: { ...stage, skipped: 1, planned: 0 },
keyframes: { ...stage, skipped: 1, planned: 0 }
},
{ startFrom: 'videoPrompts' }
)
).toEqual([])
})
})