87 lines
2.6 KiB
TypeScript
87 lines
2.6 KiB
TypeScript
/** @jest-environment node */
|
|
|
|
import { NextRequest } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { POST } from '@/app/api/materials/[id]/retry-transcode/route'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { enqueueVideoTranscodeTask } from '@/lib/video-transcode-task'
|
|
|
|
jest.mock('next-auth', () => ({
|
|
getServerSession: jest.fn(),
|
|
}))
|
|
|
|
jest.mock('@/lib/auth', () => ({
|
|
authOptions: {},
|
|
}))
|
|
|
|
jest.mock('@/lib/prisma', () => ({
|
|
prisma: {
|
|
material: {
|
|
findUnique: jest.fn(),
|
|
update: jest.fn(),
|
|
},
|
|
user: {
|
|
findUnique: jest.fn(),
|
|
},
|
|
},
|
|
}))
|
|
|
|
jest.mock('@/lib/video-transcode-task', () => ({
|
|
enqueueVideoTranscodeTask: jest.fn(),
|
|
USER_TRANSCODE_FAILURE_MESSAGE: '预览还没做好,请稍后重试',
|
|
}))
|
|
|
|
describe('retry transcode route', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
;(getServerSession as jest.Mock).mockResolvedValue({
|
|
user: { id: 'owner-1', role: 'USER' },
|
|
})
|
|
;(prisma.material.findUnique as jest.Mock).mockResolvedValue({
|
|
id: 'material-1',
|
|
userId: 'owner-1',
|
|
videoProcessingStatus: 'failed',
|
|
videoSourceUrl: 'uploads/materials/source.mp4',
|
|
videoCoverUrl: null,
|
|
})
|
|
;(prisma.user.findUnique as jest.Mock).mockResolvedValue({ role: 'USER' })
|
|
;(prisma.material.update as jest.Mock).mockResolvedValue({})
|
|
;(enqueueVideoTranscodeTask as jest.Mock).mockResolvedValue(undefined)
|
|
})
|
|
|
|
function createRequest() {
|
|
return new NextRequest('http://localhost/api/materials/material-1/retry-transcode', {
|
|
method: 'POST',
|
|
})
|
|
}
|
|
|
|
it('重试时会真正将视频任务加入转码队列', async () => {
|
|
const response = await POST(createRequest(), { params: { id: 'material-1' } })
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(enqueueVideoTranscodeTask).toHaveBeenCalledWith(
|
|
'material-1',
|
|
'uploads/materials/source.mp4',
|
|
true
|
|
)
|
|
})
|
|
|
|
it('入队失败时回写失败状态而不是永久停留在 processing', async () => {
|
|
;(enqueueVideoTranscodeTask as jest.Mock).mockRejectedValue(new Error('queue unavailable'))
|
|
|
|
const response = await POST(createRequest(), { params: { id: 'material-1' } })
|
|
const body = await response.json()
|
|
|
|
expect(response.status).toBe(500)
|
|
expect(body.error).toBe('预览还没做好,请稍后重试')
|
|
expect(body.error).not.toContain('queue unavailable')
|
|
expect(prisma.material.update).toHaveBeenLastCalledWith({
|
|
where: { id: 'material-1' },
|
|
data: {
|
|
videoProcessingStatus: 'failed',
|
|
transcodeLastError: '预览还没做好,请稍后重试',
|
|
},
|
|
})
|
|
})
|
|
})
|