mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 10:56:52 +00:00
124 lines
3.7 KiB
TypeScript
124 lines
3.7 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { transcodeMedia } from './transcode'
|
|
import { WorkflowTask, WorkflowTaskStatus, WorkflowTaskType } from '@shumai/db'
|
|
import * as workflowUtils from '@shumai/workflow-core'
|
|
|
|
vi.mock('@shumai/workflow-core', async (importOriginal) => {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const actual = (await importOriginal()) as any
|
|
return {
|
|
...actual,
|
|
getActivities: vi.fn(),
|
|
executeActivity: vi.fn(),
|
|
sleep: vi.fn(),
|
|
}
|
|
})
|
|
|
|
describe('transcodeMedia Fallback Dispatcher', () => {
|
|
const mockActivities = {
|
|
updateTaskStatusActivity: Object.assign(vi.fn(), {
|
|
_activityName: 'updateTaskStatusActivity',
|
|
}),
|
|
updateAssetStatusActivity: Object.assign(vi.fn(), {
|
|
_activityName: 'updateAssetStatusActivity',
|
|
}),
|
|
getAssetActivity: Object.assign(vi.fn(), { _activityName: 'getAssetActivity' }),
|
|
getTranscodeWorkerQueueActivity: Object.assign(vi.fn(), {
|
|
_activityName: 'getTranscodeWorkerQueueActivity',
|
|
}),
|
|
renderPdfPagesActivity: Object.assign(vi.fn(), {
|
|
_activityName: 'renderPdfPagesActivity',
|
|
}),
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
;(workflowUtils.getActivities as any).mockReturnValue(mockActivities)
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
;(workflowUtils.executeActivity as any).mockImplementation(
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
(_queue: string, fn: any, ...args: any[]) => {
|
|
if (typeof fn !== 'function') {
|
|
throw new Error(`fn is not a function in executeActivity. Queue: ${_queue}`)
|
|
}
|
|
return fn(...args)
|
|
},
|
|
)
|
|
|
|
mockActivities.getTranscodeWorkerQueueActivity.mockResolvedValue('transcode_worker_queue')
|
|
})
|
|
|
|
it('should dispatch to pdfPages workflow when payload.pdfPages is present', async () => {
|
|
const task: WorkflowTask = {
|
|
id: 'task-pdf-pages',
|
|
assetId: 'asset-pdf',
|
|
type: WorkflowTaskType.transcode,
|
|
status: WorkflowTaskStatus.pending,
|
|
payload: {
|
|
projectId: 'proj-1',
|
|
pdfPages: {
|
|
start: 1,
|
|
end: 2,
|
|
},
|
|
},
|
|
output: null,
|
|
sessionId: null,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
heartbeat: null,
|
|
teamId: 'team-1',
|
|
projectId: 'proj-1',
|
|
uid: 'task-uid',
|
|
model: null,
|
|
inputTokens: 0,
|
|
outputTokens: 0,
|
|
}
|
|
|
|
mockActivities.getAssetActivity.mockResolvedValue({
|
|
id: 'asset-pdf',
|
|
storageKey: { key: 'doc.pdf' },
|
|
mediaType: 'application/pdf',
|
|
})
|
|
|
|
mockActivities.renderPdfPagesActivity.mockResolvedValue([])
|
|
|
|
await transcodeMedia(task)
|
|
|
|
expect(mockActivities.renderPdfPagesActivity).toHaveBeenCalled()
|
|
})
|
|
|
|
it('should handle failures and update task status with error', async () => {
|
|
const task: WorkflowTask = {
|
|
id: 'task-fail',
|
|
assetId: 'asset-1',
|
|
type: WorkflowTaskType.transcode,
|
|
status: WorkflowTaskStatus.pending,
|
|
sessionId: null,
|
|
output: null,
|
|
payload: {
|
|
projectId: 'proj-1',
|
|
},
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
heartbeat: null,
|
|
teamId: 'team-1',
|
|
projectId: 'proj-1',
|
|
uid: 'task-uid',
|
|
model: null,
|
|
inputTokens: 0,
|
|
outputTokens: 0,
|
|
}
|
|
|
|
mockActivities.getAssetActivity.mockRejectedValue(new Error('FFmpeg failed'))
|
|
|
|
await expect(transcodeMedia(task)).rejects.toThrow('FFmpeg failed')
|
|
|
|
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
|
taskId: 'task-fail',
|
|
status: WorkflowTaskStatus.failed,
|
|
output: { error: 'FFmpeg failed' },
|
|
})
|
|
})
|
|
})
|