2026-06-07 19:25:47 +08:00
|
|
|
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
|
|
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
2026-06-01 23:08:58 +08:00
|
|
|
import { prisma } from '@shumai/db'
|
2026-06-02 18:48:54 +08:00
|
|
|
import { setupTestDbHooks } from '@shumai/db/test'
|
2026-06-07 19:25:47 +08:00
|
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
2026-07-18 10:32:38 +08:00
|
|
|
import { transcodeService } from '@shumai/core/src/transcode/transcode'
|
2026-06-18 11:54:30 +08:00
|
|
|
import * as child_process from 'child_process'
|
|
|
|
|
|
|
|
|
|
vi.mock('child_process', () => ({
|
|
|
|
|
execFile: vi.fn(),
|
|
|
|
|
}))
|
2026-06-05 21:25:08 +08:00
|
|
|
import {
|
|
|
|
|
getMediaInfoActivity,
|
2026-06-07 19:25:47 +08:00
|
|
|
overlayAnnotationsActivity,
|
2026-07-19 09:45:14 +08:00
|
|
|
renderPdfPagesActivity,
|
2026-06-07 19:25:47 +08:00
|
|
|
takeScreenshotsActivity,
|
2026-06-05 21:25:08 +08:00
|
|
|
transcodeVideoActivity,
|
2026-07-05 15:11:19 +08:00
|
|
|
transcodeAudioActivity,
|
2026-06-05 21:25:08 +08:00
|
|
|
updateAssetMediaActivity,
|
2026-06-14 09:54:29 +08:00
|
|
|
downloadMediaToTmpActivity,
|
|
|
|
|
transcodeImageActivity,
|
|
|
|
|
generateSpriteActivity,
|
2026-07-19 21:19:43 +08:00
|
|
|
generatePdfProxyActivity,
|
2026-06-18 11:54:30 +08:00
|
|
|
transcodeVideoChunkActivity,
|
|
|
|
|
deleteS3ObjectActivity,
|
2026-06-05 21:25:08 +08:00
|
|
|
} from './transcode'
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-06-01 23:16:22 +08:00
|
|
|
vi.mock('@shumai/core/src/s3/s3', () => ({
|
2026-05-19 16:05:09 +08:00
|
|
|
s3Service: {
|
|
|
|
|
getObject: vi.fn(),
|
|
|
|
|
putObject: vi.fn(),
|
|
|
|
|
headObject: vi.fn(),
|
|
|
|
|
presign: vi.fn(),
|
2026-06-07 17:27:47 +08:00
|
|
|
downloadToFile: vi.fn(),
|
2026-06-18 11:54:30 +08:00
|
|
|
deleteObject: vi.fn(),
|
2026-05-19 16:05:09 +08:00
|
|
|
},
|
|
|
|
|
}))
|
|
|
|
|
|
2026-06-16 09:47:27 +08:00
|
|
|
vi.mock('@shumai/core/src/transcode/transcode', () => ({
|
2026-05-19 16:05:09 +08:00
|
|
|
transcodeService: {
|
|
|
|
|
getVideoInfo: vi.fn(),
|
2026-07-05 15:11:19 +08:00
|
|
|
getAudioInfo: vi.fn(),
|
2026-05-19 16:05:09 +08:00
|
|
|
getImageInfo: vi.fn(),
|
2026-07-18 10:32:38 +08:00
|
|
|
getPdfInfo: vi.fn().mockResolvedValue({
|
|
|
|
|
originalWidth: 800,
|
|
|
|
|
originalHeight: 1000,
|
|
|
|
|
duration: 0,
|
|
|
|
|
bitRate: 0,
|
|
|
|
|
frameRate: 0,
|
|
|
|
|
totalFrames: 10,
|
|
|
|
|
hasAudio: false,
|
|
|
|
|
mimeType: 'application/pdf',
|
|
|
|
|
}),
|
2026-05-19 16:05:09 +08:00
|
|
|
transcodeVideo: vi.fn(),
|
2026-07-05 15:11:19 +08:00
|
|
|
transcodeAudio: vi.fn(),
|
2026-06-14 09:54:29 +08:00
|
|
|
transcodeImage: vi.fn(),
|
|
|
|
|
generateSprite: vi.fn(),
|
2026-07-18 10:32:38 +08:00
|
|
|
generatePdfSprite: vi.fn().mockResolvedValue({
|
|
|
|
|
pageCount: 10,
|
|
|
|
|
originalWidth: 800,
|
|
|
|
|
originalHeight: 1000,
|
|
|
|
|
}),
|
2026-07-19 21:19:43 +08:00
|
|
|
generatePdfFromText: vi.fn(),
|
|
|
|
|
generatePdfFromCsv: vi.fn(),
|
2026-05-19 16:05:09 +08:00
|
|
|
createTempDir: vi.fn().mockReturnValue('/tmp'),
|
|
|
|
|
removeDir: vi.fn(),
|
2026-06-05 21:25:08 +08:00
|
|
|
takeScreenshots: vi.fn(),
|
|
|
|
|
overlayAnnotations: vi.fn(),
|
2026-07-19 09:45:14 +08:00
|
|
|
renderPdfPages: vi.fn(),
|
2026-05-19 16:05:09 +08:00
|
|
|
},
|
|
|
|
|
}))
|
|
|
|
|
|
2026-06-07 17:27:47 +08:00
|
|
|
vi.mock('@shumai/core/src/metadata/metadata', () => ({
|
|
|
|
|
metadataService: {
|
|
|
|
|
updateAssetMetadata: vi.fn(),
|
|
|
|
|
},
|
|
|
|
|
}))
|
|
|
|
|
|
2026-05-19 16:05:09 +08:00
|
|
|
vi.mock('fs', async (importOriginal) => {
|
|
|
|
|
const actual = await importOriginal<typeof import('fs')>()
|
|
|
|
|
return {
|
|
|
|
|
...actual,
|
|
|
|
|
readFileSync: vi.fn().mockReturnValue(Buffer.from('fake data')),
|
2026-06-07 17:27:47 +08:00
|
|
|
existsSync: vi.fn().mockReturnValue(true),
|
|
|
|
|
mkdirSync: vi.fn(),
|
|
|
|
|
unlinkSync: vi.fn(),
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('Transcode Activities', () => {
|
|
|
|
|
setupTestDbHooks()
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks()
|
|
|
|
|
})
|
|
|
|
|
|
2026-06-07 17:27:47 +08:00
|
|
|
it('should call getVideoInfo and set file_type to video', async () => {
|
2026-05-19 16:05:09 +08:00
|
|
|
const asset = await prisma.asset.create({
|
|
|
|
|
data: { name: 'v.mp4', type: 'file', status: 'uploaded' },
|
|
|
|
|
})
|
|
|
|
|
vi.mocked(transcodeService.getVideoInfo).mockResolvedValue({
|
|
|
|
|
originalWidth: 1920,
|
|
|
|
|
originalHeight: 1080,
|
|
|
|
|
duration: 10,
|
|
|
|
|
bitRate: 1000,
|
|
|
|
|
frameRate: 30,
|
2026-06-28 15:11:32 +08:00
|
|
|
totalFrames: 300,
|
2026-05-19 16:05:09 +08:00
|
|
|
hasAudio: true,
|
2026-05-26 15:41:12 +08:00
|
|
|
videoCodec: 'Advanced Video Coding',
|
|
|
|
|
audioCodec: 'MPEG-4 Audio',
|
2026-05-26 15:09:06 +08:00
|
|
|
audioChannels: 2,
|
|
|
|
|
audioSampleRate: 48000,
|
|
|
|
|
audioBitDepth: 16,
|
2026-05-19 16:05:09 +08:00
|
|
|
mimeType: 'video/mp4',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const result = await getMediaInfoActivity({
|
|
|
|
|
assetId: asset.id,
|
|
|
|
|
filePath: '/tmp/v.mp4',
|
2026-07-20 10:22:36 +08:00
|
|
|
proxyType: 'video',
|
2026-05-19 16:05:09 +08:00
|
|
|
mediaType: 'video/mp4',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(transcodeService.getVideoInfo).toHaveBeenCalledWith('/tmp/v.mp4')
|
|
|
|
|
expect(result.duration).toBe(10)
|
2026-06-07 19:25:47 +08:00
|
|
|
expect(result.metadata?.originalWidth).toBe(1920)
|
|
|
|
|
expect(result.metadata?.videoCodec).toBe('Advanced Video Coding')
|
|
|
|
|
expect(result.metadata?.audioCodec).toBe('MPEG-4 Audio')
|
|
|
|
|
expect(result.metadata?.audioChannels).toBe(2)
|
|
|
|
|
expect(result.metadata?.audioSampleRate).toBe(48000)
|
|
|
|
|
expect(result.metadata?.audioBitDepth).toBe(16)
|
2026-06-07 17:27:47 +08:00
|
|
|
expect(metadataService.updateAssetMetadata).toHaveBeenCalledWith(
|
|
|
|
|
asset.id,
|
|
|
|
|
expect.arrayContaining([{ key: 'file_type', value: 'video' }]),
|
|
|
|
|
true,
|
|
|
|
|
)
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
2026-06-07 17:27:47 +08:00
|
|
|
it('should call getImageInfo and set file_type to image', async () => {
|
2026-05-19 16:05:09 +08:00
|
|
|
const asset = await prisma.asset.create({
|
|
|
|
|
data: { name: 'i.png', type: 'file', status: 'uploaded' },
|
|
|
|
|
})
|
|
|
|
|
vi.mocked(transcodeService.getImageInfo).mockResolvedValue({
|
|
|
|
|
originalWidth: 800,
|
|
|
|
|
originalHeight: 600,
|
|
|
|
|
duration: 0,
|
|
|
|
|
bitRate: 0,
|
|
|
|
|
frameRate: 0,
|
2026-06-28 15:11:32 +08:00
|
|
|
totalFrames: 0,
|
2026-05-19 16:05:09 +08:00
|
|
|
hasAudio: false,
|
|
|
|
|
mimeType: 'image/png',
|
|
|
|
|
})
|
|
|
|
|
|
2026-06-07 19:25:47 +08:00
|
|
|
const result = await getMediaInfoActivity({
|
2026-05-19 16:05:09 +08:00
|
|
|
assetId: asset.id,
|
|
|
|
|
filePath: '/tmp/i.png',
|
2026-07-20 10:22:36 +08:00
|
|
|
proxyType: 'image',
|
2026-05-19 16:05:09 +08:00
|
|
|
mediaType: 'image/png',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(transcodeService.getImageInfo).toHaveBeenCalledWith('/tmp/i.png')
|
2026-06-07 19:25:47 +08:00
|
|
|
expect(result.metadata?.originalWidth).toBe(800)
|
|
|
|
|
expect(result.metadata?.duration).toBe(0)
|
2026-06-07 17:27:47 +08:00
|
|
|
expect(metadataService.updateAssetMetadata).toHaveBeenCalledWith(
|
|
|
|
|
asset.id,
|
|
|
|
|
expect.arrayContaining([{ key: 'file_type', value: 'image' }]),
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
2026-07-05 15:11:19 +08:00
|
|
|
it('should set file_type to audio for audio files and extract audio metadata', async () => {
|
2026-06-07 17:27:47 +08:00
|
|
|
const asset = await prisma.asset.create({
|
|
|
|
|
data: { name: 'a.mp3', type: 'file', status: 'uploaded' },
|
|
|
|
|
})
|
2026-07-05 15:11:19 +08:00
|
|
|
vi.mocked(transcodeService.getAudioInfo).mockResolvedValue({
|
|
|
|
|
originalWidth: 0,
|
|
|
|
|
originalHeight: 0,
|
|
|
|
|
duration: 45.5,
|
|
|
|
|
bitRate: 128000,
|
|
|
|
|
frameRate: 0,
|
|
|
|
|
totalFrames: 0,
|
|
|
|
|
hasAudio: true,
|
|
|
|
|
audioCodec: 'mp3',
|
|
|
|
|
audioChannels: 2,
|
|
|
|
|
audioSampleRate: 44100,
|
|
|
|
|
audioBitDepth: 16,
|
|
|
|
|
mimeType: 'audio/mpeg',
|
|
|
|
|
})
|
2026-06-07 17:27:47 +08:00
|
|
|
|
2026-07-05 15:11:19 +08:00
|
|
|
const result = await getMediaInfoActivity({
|
2026-06-07 17:27:47 +08:00
|
|
|
assetId: asset.id,
|
|
|
|
|
filePath: '/tmp/a.mp3',
|
2026-07-20 10:22:36 +08:00
|
|
|
proxyType: 'audio',
|
2026-06-07 17:27:47 +08:00
|
|
|
mediaType: 'audio/mpeg',
|
|
|
|
|
})
|
|
|
|
|
|
2026-07-05 15:11:19 +08:00
|
|
|
expect(transcodeService.getAudioInfo).toHaveBeenCalledWith('/tmp/a.mp3')
|
|
|
|
|
expect(result.duration).toBe(45.5)
|
|
|
|
|
expect(result.metadata?.duration).toBe(45.5)
|
|
|
|
|
expect(result.metadata?.audioCodec).toBe('mp3')
|
|
|
|
|
expect(result.metadata?.audioChannels).toBe(2)
|
|
|
|
|
expect(result.metadata?.audioSampleRate).toBe(44100)
|
|
|
|
|
expect(result.metadata?.audioBitDepth).toBe(16)
|
|
|
|
|
|
2026-06-07 17:27:47 +08:00
|
|
|
expect(metadataService.updateAssetMetadata).toHaveBeenCalledWith(
|
|
|
|
|
asset.id,
|
2026-07-05 15:11:19 +08:00
|
|
|
expect.arrayContaining([
|
|
|
|
|
{ key: 'file_type', value: 'audio' },
|
|
|
|
|
{ key: 'duration', value: 45.5 },
|
|
|
|
|
{ key: 'audio_codec', value: 'mp3' },
|
|
|
|
|
{ key: 'audio_channels', value: 2 },
|
|
|
|
|
{ key: 'audio_sample_rate', value: 44100 },
|
|
|
|
|
{ key: 'audio_bit_depth', value: 16 },
|
|
|
|
|
]),
|
2026-06-07 17:27:47 +08:00
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should set file_type to document for pdf/text files', async () => {
|
|
|
|
|
const asset = await prisma.asset.create({
|
|
|
|
|
data: { name: 'd.pdf', type: 'file', status: 'uploaded' },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await getMediaInfoActivity({
|
|
|
|
|
assetId: asset.id,
|
|
|
|
|
filePath: '/tmp/d.pdf',
|
2026-07-20 10:22:36 +08:00
|
|
|
proxyType: 'pdf',
|
2026-06-07 17:27:47 +08:00
|
|
|
mediaType: 'application/pdf',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(metadataService.updateAssetMetadata).toHaveBeenCalledWith(
|
|
|
|
|
asset.id,
|
2026-07-19 21:19:43 +08:00
|
|
|
expect.arrayContaining([
|
|
|
|
|
{ key: 'file_type', value: 'document' },
|
|
|
|
|
{ key: 'proxy_type', value: 'pdf' },
|
|
|
|
|
]),
|
2026-06-07 17:27:47 +08:00
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
2026-07-19 21:19:43 +08:00
|
|
|
it('should generate PDF proxy for text/csv files in generatePdfProxyActivity', async () => {
|
|
|
|
|
const asset = await prisma.asset.create({
|
|
|
|
|
data: { name: 'test.txt', type: 'file', status: 'uploaded' },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const generatePdfFromTextSpy = vi
|
|
|
|
|
.spyOn(transcodeService, 'generatePdfFromText')
|
|
|
|
|
.mockImplementation(async (_inPath, outPath) => {
|
|
|
|
|
const fs = await import('fs')
|
|
|
|
|
fs.writeFileSync(outPath, 'fake pdf content')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const res = await generatePdfProxyActivity({
|
|
|
|
|
assetId: asset.id,
|
|
|
|
|
assetKey: 'files/asset1/test.txt',
|
|
|
|
|
filePath: '/tmp/test.txt',
|
|
|
|
|
mediaType: 'text/plain',
|
|
|
|
|
filename: 'test.txt',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(res.pdfProxyKey).toBe(`files/${asset.id}/proxy.pdf`)
|
|
|
|
|
expect(generatePdfFromTextSpy).toHaveBeenCalled()
|
|
|
|
|
generatePdfFromTextSpy.mockRestore()
|
|
|
|
|
})
|
|
|
|
|
|
2026-06-07 17:27:47 +08:00
|
|
|
it('should set file_type to file for unknown types', async () => {
|
|
|
|
|
const asset = await prisma.asset.create({
|
|
|
|
|
data: { name: 'u.xyz', type: 'file', status: 'uploaded' },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await getMediaInfoActivity({
|
|
|
|
|
assetId: asset.id,
|
|
|
|
|
filePath: '/tmp/u.xyz',
|
|
|
|
|
mediaType: 'application/octet-stream',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(metadataService.updateAssetMetadata).toHaveBeenCalledWith(
|
|
|
|
|
asset.id,
|
|
|
|
|
expect.arrayContaining([{ key: 'file_type', value: 'file' }]),
|
|
|
|
|
true,
|
|
|
|
|
)
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should update asset media field', async () => {
|
|
|
|
|
const asset = await prisma.asset.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'v.mp4',
|
2026-05-25 00:52:45 +08:00
|
|
|
storageKey: { create: { key: 'v.mp4' } },
|
2026-05-19 16:05:09 +08:00
|
|
|
status: 'uploaded',
|
|
|
|
|
type: 'file',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
const mediaInfo: any = { duration: 123 }
|
|
|
|
|
await updateAssetMediaActivity({ assetId: asset.id, mediaInfo })
|
|
|
|
|
|
|
|
|
|
const updated = await prisma.asset.findUnique({ where: { id: asset.id } })
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
expect((updated?.media as any).duration).toBe(123)
|
|
|
|
|
expect(s3Service.putObject).toHaveBeenCalled()
|
|
|
|
|
})
|
|
|
|
|
it('should call transcodeService.transcodeVideo', async () => {
|
|
|
|
|
vi.mocked(s3Service.headObject).mockRejectedValue(new Error('Not found'))
|
|
|
|
|
|
|
|
|
|
await transcodeVideoActivity({
|
|
|
|
|
assetKey: 'v.mp4',
|
|
|
|
|
filePath: '/tmp/v.mp4',
|
|
|
|
|
videoSpec: { resolution: '720p', width: 1280, height: 720 },
|
|
|
|
|
duration: 10,
|
|
|
|
|
originalFps: 30,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(transcodeService.transcodeVideo).toHaveBeenCalled()
|
|
|
|
|
})
|
2026-06-05 21:25:08 +08:00
|
|
|
|
|
|
|
|
it('should call transcodeService.takeScreenshots', async () => {
|
|
|
|
|
vi.mocked(transcodeService.takeScreenshots).mockResolvedValue([
|
|
|
|
|
{ key: 'screenshots/shot1.webp', timestamp: 1.0 },
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const result = await takeScreenshotsActivity({
|
|
|
|
|
assetKey: 'v.mp4',
|
|
|
|
|
assetId: 'asset-1',
|
|
|
|
|
start: 0,
|
|
|
|
|
end: 10,
|
|
|
|
|
count: 1,
|
|
|
|
|
commentTimestamp: 1.0,
|
|
|
|
|
annotations: [],
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(transcodeService.takeScreenshots).toHaveBeenCalledWith({
|
|
|
|
|
assetKey: 'v.mp4',
|
|
|
|
|
assetId: 'asset-1',
|
|
|
|
|
start: 0,
|
|
|
|
|
end: 10,
|
|
|
|
|
count: 1,
|
|
|
|
|
commentTimestamp: 1.0,
|
|
|
|
|
annotations: [],
|
|
|
|
|
})
|
|
|
|
|
expect(result).toEqual([{ key: 'screenshots/shot1.webp', timestamp: 1.0 }])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should call transcodeService.overlayAnnotations', async () => {
|
|
|
|
|
vi.mocked(transcodeService.overlayAnnotations).mockResolvedValue('annotations/ann.webp')
|
|
|
|
|
|
|
|
|
|
const result = await overlayAnnotationsActivity({
|
|
|
|
|
assetKey: 'i.png',
|
|
|
|
|
assetId: 'asset-2',
|
|
|
|
|
annotations: [],
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(transcodeService.overlayAnnotations).toHaveBeenCalledWith({
|
|
|
|
|
assetKey: 'i.png',
|
|
|
|
|
assetId: 'asset-2',
|
|
|
|
|
annotations: [],
|
|
|
|
|
})
|
|
|
|
|
expect(result).toBe('annotations/ann.webp')
|
|
|
|
|
})
|
2026-06-14 09:54:29 +08:00
|
|
|
|
2026-07-19 09:45:14 +08:00
|
|
|
it('should call transcodeService.renderPdfPages', async () => {
|
|
|
|
|
vi.mocked(transcodeService.renderPdfPages).mockResolvedValue([
|
|
|
|
|
{ key: 'pdf_pages/doc-page-1.webp', page: 1 },
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const result = await renderPdfPagesActivity({
|
|
|
|
|
assetKey: 'doc.pdf',
|
|
|
|
|
assetId: 'asset-pdf',
|
|
|
|
|
start: 1,
|
|
|
|
|
end: 1,
|
|
|
|
|
commentTimestamp: 1,
|
|
|
|
|
annotations: [],
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(transcodeService.renderPdfPages).toHaveBeenCalledWith({
|
|
|
|
|
assetKey: 'doc.pdf',
|
|
|
|
|
assetId: 'asset-pdf',
|
|
|
|
|
start: 1,
|
|
|
|
|
end: 1,
|
|
|
|
|
commentTimestamp: 1,
|
|
|
|
|
annotations: [],
|
|
|
|
|
})
|
|
|
|
|
expect(result).toEqual([{ key: 'pdf_pages/doc-page-1.webp', page: 1 }])
|
|
|
|
|
})
|
|
|
|
|
|
2026-06-14 09:54:29 +08:00
|
|
|
describe('Non-retryable Error Handling', () => {
|
|
|
|
|
it('should throw non-retryable ApplicationFailure when downloadMediaToTmpActivity fails with ENOENT', async () => {
|
|
|
|
|
vi.mocked(s3Service.downloadToFile).mockRejectedValue({
|
|
|
|
|
code: 'ENOENT',
|
|
|
|
|
message: 'ENOENT: no such file or directory',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await expect(downloadMediaToTmpActivity({ assetKey: 'missing.mp4' })).rejects.toThrowError(
|
|
|
|
|
/Failed to download media to tmp/,
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should throw non-retryable ApplicationFailure when getMediaInfoActivity fails with ENOENT', async () => {
|
|
|
|
|
const asset = await prisma.asset.create({
|
|
|
|
|
data: { name: 'missing.mp4', type: 'file', status: 'uploaded' },
|
|
|
|
|
})
|
|
|
|
|
vi.mocked(transcodeService.getVideoInfo).mockRejectedValue({
|
|
|
|
|
code: 'ENOENT',
|
|
|
|
|
message: 'ENOENT: no such file or directory',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
getMediaInfoActivity({
|
|
|
|
|
assetId: asset.id,
|
|
|
|
|
filePath: '/tmp/missing.mp4',
|
|
|
|
|
mediaType: 'video/mp4',
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toThrowError(/Failed to get media info/)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should throw non-retryable ApplicationFailure when transcodeVideoActivity fails with ffmpeg error', async () => {
|
|
|
|
|
vi.mocked(s3Service.headObject).mockRejectedValue(new Error('Not found'))
|
|
|
|
|
vi.mocked(transcodeService.transcodeVideo).mockRejectedValue(new Error('FFmpeg failed'))
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
transcodeVideoActivity({
|
|
|
|
|
assetKey: 'v.mp4',
|
|
|
|
|
filePath: '/tmp/v.mp4',
|
|
|
|
|
videoSpec: { resolution: '720p', width: 1280, height: 720 },
|
|
|
|
|
duration: 10,
|
|
|
|
|
originalFps: 30,
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toThrowError(/Video transcoding failed/)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should throw non-retryable ApplicationFailure when transcodeImageActivity fails with sharp error', async () => {
|
|
|
|
|
vi.mocked(s3Service.headObject).mockRejectedValue(new Error('Not found'))
|
|
|
|
|
vi.mocked(transcodeService.transcodeImage).mockRejectedValue(new Error('sharp failed'))
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
transcodeImageActivity({
|
|
|
|
|
assetKey: 'i.png',
|
|
|
|
|
filePath: '/tmp/i.png',
|
|
|
|
|
imageSpec: { width: 800, height: 600, quality: 90, format: 'webp' },
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toThrowError(/Image transcoding failed/)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should throw non-retryable ApplicationFailure when generateSpriteActivity fails with error', async () => {
|
|
|
|
|
vi.mocked(s3Service.headObject).mockRejectedValue(new Error('Not found'))
|
|
|
|
|
vi.mocked(transcodeService.generateSprite).mockRejectedValue(new Error('FFmpeg failed'))
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
generateSpriteActivity({
|
|
|
|
|
assetKey: 'v.mp4',
|
|
|
|
|
filePath: '/tmp/v.mp4',
|
|
|
|
|
mediaInfo: { duration: 10 } as unknown as PrismaJson.MediaInfo,
|
|
|
|
|
spriteSpec: { key: 'sprite.webp', frames: 100, tileX: 10, tileY: 10 },
|
|
|
|
|
posterSpec: { key: 'poster.webp' },
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toThrowError(/Sprite\/Poster generation failed/)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should throw non-retryable ApplicationFailure when takeScreenshotsActivity fails with error', async () => {
|
|
|
|
|
vi.mocked(transcodeService.takeScreenshots).mockRejectedValue(new Error('FFmpeg failed'))
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
takeScreenshotsActivity({
|
|
|
|
|
assetKey: 'v.mp4',
|
|
|
|
|
assetId: 'asset-1',
|
|
|
|
|
start: 0,
|
|
|
|
|
end: 10,
|
|
|
|
|
count: 1,
|
|
|
|
|
commentTimestamp: 1.0,
|
|
|
|
|
annotations: [],
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toThrowError(/Screenshot extraction failed/)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should throw non-retryable ApplicationFailure when overlayAnnotationsActivity fails with error', async () => {
|
|
|
|
|
vi.mocked(transcodeService.overlayAnnotations).mockRejectedValue(new Error('sharp failed'))
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
overlayAnnotationsActivity({
|
|
|
|
|
assetKey: 'i.png',
|
|
|
|
|
assetId: 'asset-2',
|
|
|
|
|
annotations: [],
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toThrowError(/Overlay annotations failed/)
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-06-18 11:54:30 +08:00
|
|
|
|
|
|
|
|
describe('Video Chunk Activities', () => {
|
|
|
|
|
it('should transcode video chunk using ffmpeg and upload to S3', async () => {
|
|
|
|
|
// Mock child_process.execFile to simulate ffmpeg succeeding
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
;(child_process.execFile as any).mockImplementation(
|
2026-06-18 11:57:38 +08:00
|
|
|
(
|
|
|
|
|
file: string,
|
|
|
|
|
args: string[],
|
|
|
|
|
cb: (err: Error | null, result: { stdout: string; stderr: string }) => void,
|
|
|
|
|
) => {
|
2026-06-18 11:54:30 +08:00
|
|
|
cb(null, { stdout: '', stderr: '' })
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const res = await transcodeVideoChunkActivity({
|
|
|
|
|
assetId: 'asset-abc',
|
|
|
|
|
filePath: '/tmp/full-video.mp4',
|
|
|
|
|
startTime: 10,
|
|
|
|
|
endTime: 25,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Verify ffmpeg command args
|
|
|
|
|
expect(child_process.execFile).toHaveBeenCalledWith(
|
|
|
|
|
'ffmpeg',
|
|
|
|
|
expect.arrayContaining([
|
|
|
|
|
'-y',
|
|
|
|
|
'-i',
|
|
|
|
|
'/tmp/full-video.mp4',
|
|
|
|
|
'-ss',
|
|
|
|
|
'10',
|
|
|
|
|
'-t',
|
|
|
|
|
'15',
|
2026-06-18 19:53:19 +08:00
|
|
|
'-vf',
|
|
|
|
|
'fps=1',
|
|
|
|
|
'-c:v',
|
|
|
|
|
'libx264',
|
|
|
|
|
'-preset',
|
|
|
|
|
'ultrafast',
|
|
|
|
|
'-crf',
|
|
|
|
|
'28',
|
|
|
|
|
'-an',
|
2026-06-18 11:54:30 +08:00
|
|
|
]),
|
|
|
|
|
expect.any(Function),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Verify S3 upload
|
|
|
|
|
expect(s3Service.putObject).toHaveBeenCalledWith(
|
|
|
|
|
'shumai',
|
|
|
|
|
expect.stringMatching(/^files\/asset-abc\/tmp-embedding-chunks\/chunk-10-25-.*\.mp4$/),
|
|
|
|
|
expect.any(Buffer),
|
|
|
|
|
expect.any(Number),
|
|
|
|
|
'video/mp4',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(res.chunkKey).toMatch(/^files\/asset-abc\/tmp-embedding-chunks\/chunk-10-25-.*\.mp4$/)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should delete S3 object successfully', async () => {
|
|
|
|
|
await deleteS3ObjectActivity({ key: 'files/test-key.mp4' })
|
|
|
|
|
expect(s3Service.deleteObject).toHaveBeenCalledWith('shumai', 'files/test-key.mp4')
|
|
|
|
|
})
|
2026-07-05 15:11:19 +08:00
|
|
|
|
|
|
|
|
it('should transcode audio file and upload proxy to S3', async () => {
|
|
|
|
|
vi.mocked(s3Service.headObject).mockRejectedValue(new Error('Not found'))
|
|
|
|
|
vi.mocked(transcodeService.transcodeAudio).mockResolvedValue()
|
|
|
|
|
|
|
|
|
|
const res = await transcodeAudioActivity({
|
|
|
|
|
assetKey: 'files/proj-123/a.wav',
|
|
|
|
|
filePath: '/tmp/a.wav',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(transcodeService.transcodeAudio).toHaveBeenCalledWith({
|
|
|
|
|
inputFile: '/tmp/a.wav',
|
|
|
|
|
outputFile: expect.stringContaining('a-audio-proxy.mp4'),
|
|
|
|
|
bitrate: '128k',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(s3Service.putObject).toHaveBeenCalledWith(
|
|
|
|
|
'shumai',
|
|
|
|
|
'files/proj-123/a-audio-proxy.mp4',
|
|
|
|
|
expect.any(Buffer),
|
|
|
|
|
expect.any(Number),
|
|
|
|
|
'video/mp4',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(res).toEqual({
|
|
|
|
|
width: 0,
|
|
|
|
|
height: 0,
|
|
|
|
|
key: 'files/proj-123/a-audio-proxy.mp4',
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should return existing audio transcode if already exists in S3', async () => {
|
|
|
|
|
vi.mocked(s3Service.headObject).mockResolvedValue(
|
|
|
|
|
{} as Awaited<ReturnType<typeof s3Service.headObject>>,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const res = await transcodeAudioActivity({
|
|
|
|
|
assetKey: 'files/proj-123/a.wav',
|
|
|
|
|
filePath: '/tmp/a.wav',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(transcodeService.transcodeAudio).not.toHaveBeenCalled()
|
|
|
|
|
expect(res).toEqual({
|
|
|
|
|
width: 0,
|
|
|
|
|
height: 0,
|
|
|
|
|
key: 'files/proj-123/a-audio-proxy.mp4',
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-06-18 11:54:30 +08:00
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|