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'
|
|
|
|
|
import { transcodeService } from '../transcode'
|
2026-06-05 21:25:08 +08:00
|
|
|
import {
|
|
|
|
|
getMediaInfoActivity,
|
2026-06-07 19:25:47 +08:00
|
|
|
overlayAnnotationsActivity,
|
|
|
|
|
takeScreenshotsActivity,
|
2026-06-05 21:25:08 +08:00
|
|
|
transcodeVideoActivity,
|
|
|
|
|
updateAssetMediaActivity,
|
|
|
|
|
} 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-05-19 16:05:09 +08:00
|
|
|
},
|
|
|
|
|
}))
|
|
|
|
|
|
2026-06-01 23:29:11 +08:00
|
|
|
vi.mock('../transcode', () => ({
|
2026-05-19 16:05:09 +08:00
|
|
|
transcodeService: {
|
|
|
|
|
getVideoInfo: vi.fn(),
|
|
|
|
|
getImageInfo: vi.fn(),
|
|
|
|
|
transcodeVideo: vi.fn(),
|
|
|
|
|
createTempDir: vi.fn().mockReturnValue('/tmp'),
|
|
|
|
|
removeDir: vi.fn(),
|
2026-06-05 21:25:08 +08:00
|
|
|
takeScreenshots: vi.fn(),
|
|
|
|
|
overlayAnnotations: 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,
|
|
|
|
|
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',
|
|
|
|
|
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,
|
|
|
|
|
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',
|
|
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should set file_type to audio for audio files', async () => {
|
|
|
|
|
const asset = await prisma.asset.create({
|
|
|
|
|
data: { name: 'a.mp3', type: 'file', status: 'uploaded' },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await getMediaInfoActivity({
|
|
|
|
|
assetId: asset.id,
|
|
|
|
|
filePath: '/tmp/a.mp3',
|
|
|
|
|
mediaType: 'audio/mpeg',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(metadataService.updateAssetMetadata).toHaveBeenCalledWith(
|
|
|
|
|
asset.id,
|
|
|
|
|
expect.arrayContaining([{ key: 'file_type', value: 'audio' }]),
|
|
|
|
|
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',
|
|
|
|
|
mediaType: 'application/pdf',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(metadataService.updateAssetMetadata).toHaveBeenCalledWith(
|
|
|
|
|
asset.id,
|
|
|
|
|
expect.arrayContaining([{ key: 'file_type', value: 'document' }]),
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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-05-19 16:05:09 +08:00
|
|
|
})
|