Files
waoowaoo/tests/unit/worker/video-worker.test.ts

207 lines
5.9 KiB
TypeScript

import type { Job } from 'bullmq'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { TASK_TYPE, type TaskJobData } from '@/lib/task/types'
type WorkerProcessor = (job: Job<TaskJobData>) => Promise<unknown>
type PanelRow = {
id: string
videoUrl: string | null
imageUrl: string | null
videoPrompt: string | null
description: string | null
firstLastFramePrompt: string | null
}
const workerState = vi.hoisted(() => ({
processor: null as WorkerProcessor | null,
}))
const reportTaskProgressMock = vi.hoisted(() => vi.fn(async () => undefined))
const withTaskLifecycleMock = vi.hoisted(() =>
vi.fn(async (job: Job<TaskJobData>, handler: WorkerProcessor) => await handler(job)),
)
const utilsMock = vi.hoisted(() => ({
assertTaskActive: vi.fn(async () => undefined),
getProjectModels: vi.fn(async () => ({ videoRatio: '16:9' })),
resolveLipSyncVideoSource: vi.fn(async () => 'https://provider.example/lipsync.mp4'),
resolveVideoSourceFromGeneration: vi.fn(async () => 'https://provider.example/video.mp4'),
toSignedUrlIfCos: vi.fn((url: string | null) => (url ? `https://signed.example/${url}` : null)),
uploadVideoSourceToCos: vi.fn(async () => 'cos/lip-sync/video.mp4'),
}))
const prismaMock = vi.hoisted(() => ({
novelPromotionPanel: {
findUnique: vi.fn(),
findFirst: vi.fn(),
update: vi.fn(async () => undefined),
},
novelPromotionVoiceLine: {
findUnique: vi.fn(),
},
}))
vi.mock('bullmq', () => ({
Queue: class {
constructor(_name: string) {}
async add() {
return { id: 'job-1' }
}
async getJob() {
return null
}
},
Worker: class {
constructor(_name: string, processor: WorkerProcessor) {
workerState.processor = processor
}
},
}))
vi.mock('@/lib/redis', () => ({ queueRedis: {} }))
vi.mock('@/lib/workers/shared', () => ({
reportTaskProgress: reportTaskProgressMock,
withTaskLifecycle: withTaskLifecycleMock,
}))
vi.mock('@/lib/workers/utils', () => utilsMock)
vi.mock('@/lib/prisma', () => ({ prisma: prismaMock }))
vi.mock('@/lib/media/outbound-image', () => ({
normalizeToBase64ForGeneration: vi.fn(async (input: string) => input),
}))
vi.mock('@/lib/model-capabilities/lookup', () => ({
resolveBuiltinCapabilitiesByModelKey: vi.fn(() => ({ video: { firstlastframe: true } })),
}))
vi.mock('@/lib/model-config-contract', () => ({
parseModelKeyStrict: vi.fn(() => ({ provider: 'fal' })),
}))
vi.mock('@/lib/api-config', () => ({
getProviderConfig: vi.fn(async () => ({ apiKey: 'api-key' })),
}))
function buildPanel(overrides?: Partial<PanelRow>): PanelRow {
return {
id: 'panel-1',
videoUrl: 'cos/base-video.mp4',
imageUrl: 'cos/panel-image.png',
videoPrompt: 'panel prompt',
description: 'panel description',
firstLastFramePrompt: null,
...(overrides || {}),
}
}
function buildJob(params: {
type: TaskJobData['type']
payload?: Record<string, unknown>
targetType?: string
targetId?: string
}): Job<TaskJobData> {
return {
data: {
taskId: 'task-1',
type: params.type,
locale: 'zh',
projectId: 'project-1',
episodeId: 'episode-1',
targetType: params.targetType ?? 'NovelPromotionPanel',
targetId: params.targetId ?? 'panel-1',
payload: params.payload ?? {},
userId: 'user-1',
},
} as unknown as Job<TaskJobData>
}
describe('worker video processor behavior', () => {
beforeEach(async () => {
vi.clearAllMocks()
workerState.processor = null
prismaMock.novelPromotionPanel.findUnique.mockResolvedValue(buildPanel())
prismaMock.novelPromotionPanel.findFirst.mockResolvedValue(buildPanel())
prismaMock.novelPromotionVoiceLine.findUnique.mockResolvedValue({
id: 'line-1',
audioUrl: 'cos/line-1.mp3',
})
const mod = await import('@/lib/workers/video.worker')
mod.createVideoWorker()
})
it('VIDEO_PANEL: 缺少 payload.videoModel 时显式失败', async () => {
const processor = workerState.processor
expect(processor).toBeTruthy()
const job = buildJob({
type: TASK_TYPE.VIDEO_PANEL,
payload: {},
})
await expect(processor!(job)).rejects.toThrow('VIDEO_MODEL_REQUIRED: payload.videoModel is required')
})
it('LIP_SYNC: 缺少 panel 时显式失败', async () => {
const processor = workerState.processor
expect(processor).toBeTruthy()
prismaMock.novelPromotionPanel.findUnique.mockResolvedValueOnce(null)
const job = buildJob({
type: TASK_TYPE.LIP_SYNC,
payload: { voiceLineId: 'line-1' },
targetId: 'panel-missing',
})
await expect(processor!(job)).rejects.toThrow('Lip-sync panel not found')
})
it('LIP_SYNC: 正常路径写回 lipSyncVideoUrl 并清理 lipSyncTaskId', async () => {
const processor = workerState.processor
expect(processor).toBeTruthy()
const job = buildJob({
type: TASK_TYPE.LIP_SYNC,
payload: {
voiceLineId: 'line-1',
lipSyncModel: 'fal::lipsync-model',
},
targetId: 'panel-1',
})
const result = await processor!(job) as { panelId: string; voiceLineId: string; lipSyncVideoUrl: string }
expect(result).toEqual({
panelId: 'panel-1',
voiceLineId: 'line-1',
lipSyncVideoUrl: 'cos/lip-sync/video.mp4',
})
expect(utilsMock.resolveLipSyncVideoSource).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
userId: 'user-1',
modelKey: 'fal::lipsync-model',
}),
)
expect(prismaMock.novelPromotionPanel.update).toHaveBeenCalledWith({
where: { id: 'panel-1' },
data: {
lipSyncVideoUrl: 'cos/lip-sync/video.mp4',
lipSyncTaskId: null,
},
})
})
it('未知任务类型: 显式报错', async () => {
const processor = workerState.processor
expect(processor).toBeTruthy()
const unsupportedJob = buildJob({
type: TASK_TYPE.AI_CREATE_CHARACTER,
})
await expect(processor!(unsupportedJob)).rejects.toThrow('Unsupported video task type')
})
})