2026-07-25 09:47:20 +08:00
|
|
|
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'
|
|
|
|
|
import { prisma, AssetStatus } from '@shumai/db'
|
|
|
|
|
import { setupTestDbHooks } from '@shumai/db/test'
|
|
|
|
|
import { workflowService, TaskQueueTranscode } from '@shumai/workflow-core'
|
|
|
|
|
import { initTranscodeWorkflows } from '@shumai/transcode'
|
|
|
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
|
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
|
import * as path from 'path'
|
|
|
|
|
import * as fs from 'fs'
|
|
|
|
|
|
|
|
|
|
const currentDir = path.dirname(fileURLToPath(import.meta.url))
|
|
|
|
|
const fixturesDir = path.resolve(currentDir, '../fixtures')
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
describe('Workflow E2E - transcodeImageWorkflow', () => {
|
|
|
|
|
setupTestDbHooks()
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
beforeAll(async () => {
|
|
|
|
|
process.env.S3_BUCKET = 'shumai-e2e-test-bucket-transcode'
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
initTranscodeWorkflows()
|
|
|
|
|
await workflowService.startWorkers(TaskQueueTranscode)
|
|
|
|
|
})
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
afterAll(async () => {
|
|
|
|
|
await workflowService.shutdownWorkers()
|
|
|
|
|
workflowService.close()
|
|
|
|
|
vi.restoreAllMocks()
|
|
|
|
|
try {
|
|
|
|
|
console.log('Cleaning up local E2E storage files...')
|
|
|
|
|
await s3Service.deletePrefix('shumai-e2e-test-bucket-transcode', '')
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Failed to clean up E2E storage folder:', err)
|
|
|
|
|
}
|
|
|
|
|
})
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
it('should run transcodeMedia workflow for an image asset successfully', async () => {
|
|
|
|
|
// 1. Seed Database
|
|
|
|
|
const team = await prisma.team.create({
|
|
|
|
|
data: { name: 'E2E Image Transcode Team' },
|
2026-07-25 09:47:20 +08:00
|
|
|
})
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const project = await prisma.project.create({
|
|
|
|
|
data: { name: 'E2E Image Transcode Project', teamId: team.id },
|
2026-07-25 09:47:20 +08:00
|
|
|
})
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const storageKey = await prisma.storageKey.create({
|
|
|
|
|
data: {
|
|
|
|
|
key: 'projects/e2e/image-trans.png',
|
|
|
|
|
},
|
|
|
|
|
})
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const asset = await prisma.asset.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'image-trans.png',
|
|
|
|
|
type: 'file',
|
|
|
|
|
status: 'uploaded',
|
|
|
|
|
mediaType: 'image/png',
|
|
|
|
|
projectId: project.id,
|
|
|
|
|
storageKeyId: storageKey.id,
|
|
|
|
|
},
|
|
|
|
|
})
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
// 2. Seed S3 Storage from Fixture
|
|
|
|
|
const pngPath = path.join(fixturesDir, 'small.png')
|
|
|
|
|
const pngBuffer = fs.readFileSync(pngPath)
|
|
|
|
|
await s3Service.putObject(
|
|
|
|
|
'shumai-e2e-test-bucket-transcode',
|
|
|
|
|
'projects/e2e/image-trans.png',
|
|
|
|
|
pngBuffer,
|
|
|
|
|
pngBuffer.length,
|
|
|
|
|
'image/png',
|
|
|
|
|
)
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
// 3. Create Workflow Task
|
|
|
|
|
const task = await prisma.workflowTask.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: 'transcode_image',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
assetId: asset.id,
|
|
|
|
|
projectId: project.id,
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
payload: {
|
2026-07-25 09:47:20 +08:00
|
|
|
projectId: project.id,
|
2026-10-19 15:35:08 +08:00
|
|
|
transcode: {
|
|
|
|
|
thumbnail: true,
|
2026-07-25 09:47:20 +08:00
|
|
|
},
|
|
|
|
|
},
|
2026-10-19 15:35:08 +08:00
|
|
|
},
|
|
|
|
|
})
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
// 4. Wait for workflow to complete
|
|
|
|
|
console.log(
|
|
|
|
|
`Submitted E2E Image Transcode Workflow Task. ID: ${task.id}. Awaiting completion...`,
|
|
|
|
|
)
|
|
|
|
|
const completedTask = await workflowService.executeWait(task, 45000)
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
// 5. Verification
|
|
|
|
|
expect(completedTask.status).toBe('completed')
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const updatedAsset = await prisma.asset.findUnique({
|
|
|
|
|
where: { id: asset.id },
|
|
|
|
|
})
|
|
|
|
|
expect(updatedAsset?.status).toBe(AssetStatus.processed)
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const mediaInfo = updatedAsset?.media as unknown as {
|
|
|
|
|
proxyType: string
|
|
|
|
|
imageTranscodes: unknown[]
|
|
|
|
|
thumbnail: unknown
|
|
|
|
|
}
|
|
|
|
|
expect(mediaInfo).toBeDefined()
|
|
|
|
|
expect(mediaInfo.proxyType).toBe('image')
|
|
|
|
|
expect(mediaInfo.imageTranscodes).toBeDefined()
|
|
|
|
|
expect(mediaInfo.imageTranscodes.length).toBeGreaterThan(0)
|
|
|
|
|
expect(mediaInfo.thumbnail).toBeDefined()
|
|
|
|
|
}, 50000)
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
it('should run transcodeMedia workflow for a PSD image asset successfully', async () => {
|
|
|
|
|
// 1. Seed Database
|
|
|
|
|
const team = await prisma.team.create({
|
|
|
|
|
data: { name: 'E2E PSD Transcode Team' },
|
|
|
|
|
})
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const project = await prisma.project.create({
|
|
|
|
|
data: { name: 'E2E PSD Transcode Project', teamId: team.id },
|
|
|
|
|
})
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const storageKey = await prisma.storageKey.create({
|
|
|
|
|
data: {
|
|
|
|
|
key: 'projects/e2e/test.psd',
|
|
|
|
|
},
|
|
|
|
|
})
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const asset = await prisma.asset.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'test.psd',
|
|
|
|
|
type: 'file',
|
|
|
|
|
status: 'uploaded',
|
|
|
|
|
mediaType: 'image/vnd.adobe.photoshop',
|
|
|
|
|
projectId: project.id,
|
|
|
|
|
storageKeyId: storageKey.id,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 2. Seed S3 Storage from fixture test.psd
|
|
|
|
|
const psdPath = path.join(fixturesDir, 'test.psd')
|
|
|
|
|
const psdBuffer = fs.readFileSync(psdPath)
|
|
|
|
|
await s3Service.putObject(
|
|
|
|
|
'shumai-e2e-test-bucket-transcode',
|
|
|
|
|
'projects/e2e/test.psd',
|
|
|
|
|
psdBuffer,
|
|
|
|
|
psdBuffer.length,
|
|
|
|
|
'image/vnd.adobe.photoshop',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 3. Create Workflow Task
|
|
|
|
|
const task = await prisma.workflowTask.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: 'transcode_image',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
assetId: asset.id,
|
|
|
|
|
projectId: project.id,
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
payload: {
|
2026-07-25 09:47:20 +08:00
|
|
|
projectId: project.id,
|
2026-10-19 15:35:08 +08:00
|
|
|
transcode: {
|
|
|
|
|
thumbnail: true,
|
2026-07-25 09:47:20 +08:00
|
|
|
},
|
|
|
|
|
},
|
2026-10-19 15:35:08 +08:00
|
|
|
},
|
|
|
|
|
})
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
// 4. Wait for workflow to complete
|
|
|
|
|
console.log(
|
|
|
|
|
`Submitted E2E PSD Image Transcode Workflow Task. ID: ${task.id}. Awaiting completion...`,
|
|
|
|
|
)
|
|
|
|
|
const completedTask = await workflowService.executeWait(task, 45000)
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
// 5. Verification
|
|
|
|
|
expect(completedTask.status).toBe('completed')
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const updatedAsset = await prisma.asset.findUnique({
|
|
|
|
|
where: { id: asset.id },
|
|
|
|
|
})
|
|
|
|
|
expect(updatedAsset?.status).toBe(AssetStatus.processed)
|
2026-07-25 09:47:20 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const mediaInfo = updatedAsset?.media as unknown as {
|
|
|
|
|
proxyType: string
|
|
|
|
|
imageTranscodes: unknown[]
|
|
|
|
|
thumbnail: unknown
|
|
|
|
|
}
|
|
|
|
|
expect(mediaInfo).toBeDefined()
|
|
|
|
|
expect(mediaInfo.proxyType).toBe('image')
|
|
|
|
|
expect(mediaInfo.imageTranscodes).toBeDefined()
|
|
|
|
|
expect(mediaInfo.imageTranscodes.length).toBeGreaterThan(0)
|
|
|
|
|
expect(mediaInfo.thumbnail).toBeDefined()
|
|
|
|
|
}, 50000)
|
2026-09-29 21:09:27 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
it('should run transcodeMedia workflow for a RAW image asset (.dng) successfully', async () => {
|
|
|
|
|
// 1. Seed Database
|
|
|
|
|
const team = await prisma.team.create({
|
|
|
|
|
data: { name: 'E2E RAW Transcode Team' },
|
|
|
|
|
})
|
2026-09-29 21:09:27 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const project = await prisma.project.create({
|
|
|
|
|
data: { name: 'E2E RAW Transcode Project', teamId: team.id },
|
|
|
|
|
})
|
2026-09-29 21:09:27 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const storageKey = await prisma.storageKey.create({
|
|
|
|
|
data: {
|
|
|
|
|
key: 'projects/e2e/test.dng',
|
|
|
|
|
},
|
|
|
|
|
})
|
2026-09-29 21:09:27 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const asset = await prisma.asset.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'test.dng',
|
|
|
|
|
type: 'file',
|
|
|
|
|
status: 'uploaded',
|
|
|
|
|
mediaType: 'image/x-adobe-dng',
|
|
|
|
|
projectId: project.id,
|
|
|
|
|
storageKeyId: storageKey.id,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 2. Seed S3 Storage from fixture test.dng
|
|
|
|
|
const dngPath = path.join(fixturesDir, 'test.dng')
|
|
|
|
|
const dngBuffer = fs.readFileSync(dngPath)
|
|
|
|
|
await s3Service.putObject(
|
|
|
|
|
'shumai-e2e-test-bucket-transcode',
|
|
|
|
|
'projects/e2e/test.dng',
|
|
|
|
|
dngBuffer,
|
|
|
|
|
dngBuffer.length,
|
|
|
|
|
'image/x-adobe-dng',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 3. Create Workflow Task
|
|
|
|
|
const task = await prisma.workflowTask.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: 'transcode_image',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
assetId: asset.id,
|
|
|
|
|
projectId: project.id,
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
payload: {
|
2026-09-29 21:09:27 +08:00
|
|
|
projectId: project.id,
|
2026-10-19 15:35:08 +08:00
|
|
|
transcode: {
|
|
|
|
|
thumbnail: true,
|
2026-09-29 21:09:27 +08:00
|
|
|
},
|
|
|
|
|
},
|
2026-10-19 15:35:08 +08:00
|
|
|
},
|
|
|
|
|
})
|
2026-09-29 21:09:27 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
// 4. Wait for workflow to complete
|
|
|
|
|
console.log(
|
|
|
|
|
`Submitted E2E RAW Image Transcode Workflow Task. ID: ${task.id}. Awaiting completion...`,
|
|
|
|
|
)
|
|
|
|
|
const completedTask = await workflowService.executeWait(task, 45000)
|
2026-09-29 21:09:27 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
// 5. Verification
|
|
|
|
|
expect(completedTask.status).toBe('completed')
|
2026-09-29 21:09:27 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const updatedAsset = await prisma.asset.findUnique({
|
|
|
|
|
where: { id: asset.id },
|
|
|
|
|
})
|
|
|
|
|
expect(updatedAsset?.status).toBe(AssetStatus.processed)
|
2026-09-29 21:09:27 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const mediaInfo = updatedAsset?.media as unknown as {
|
|
|
|
|
proxyType: string
|
|
|
|
|
imageTranscodes: unknown[]
|
|
|
|
|
thumbnail: unknown
|
|
|
|
|
}
|
|
|
|
|
expect(mediaInfo).toBeDefined()
|
|
|
|
|
expect(mediaInfo.proxyType).toBe('image')
|
|
|
|
|
expect(mediaInfo.imageTranscodes).toBeDefined()
|
|
|
|
|
expect(mediaInfo.imageTranscodes.length).toBeGreaterThan(0)
|
|
|
|
|
expect(mediaInfo.thumbnail).toBeDefined()
|
|
|
|
|
}, 50000)
|
2026-10-10 03:41:49 -04:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
it('should transcode a rotated camera photo upright', async () => {
|
|
|
|
|
// 1. Seed Database
|
|
|
|
|
const team = await prisma.team.create({
|
|
|
|
|
data: { name: 'E2E Rotated Transcode Team' },
|
|
|
|
|
})
|
2026-10-10 03:41:49 -04:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const project = await prisma.project.create({
|
|
|
|
|
data: { name: 'E2E Rotated Transcode Project', teamId: team.id },
|
|
|
|
|
})
|
2026-10-10 03:41:49 -04:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const storageKey = await prisma.storageKey.create({
|
|
|
|
|
data: {
|
|
|
|
|
key: 'projects/e2e/portrait-rotated.jpg',
|
|
|
|
|
},
|
|
|
|
|
})
|
2026-10-10 03:41:49 -04:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const asset = await prisma.asset.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'portrait-rotated.jpg',
|
|
|
|
|
type: 'file',
|
|
|
|
|
status: 'uploaded',
|
|
|
|
|
mediaType: 'image/jpeg',
|
|
|
|
|
projectId: project.id,
|
|
|
|
|
storageKeyId: storageKey.id,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 2. Seed S3 Storage from fixture portrait-rotated.jpg: stored 600x400 with EXIF
|
|
|
|
|
// orientation 6, the way cameras save portrait shots, so it is displayed 400x600.
|
|
|
|
|
const jpgPath = path.join(fixturesDir, 'portrait-rotated.jpg')
|
|
|
|
|
const jpgBuffer = fs.readFileSync(jpgPath)
|
|
|
|
|
await s3Service.putObject(
|
|
|
|
|
'shumai-e2e-test-bucket-transcode',
|
|
|
|
|
'projects/e2e/portrait-rotated.jpg',
|
|
|
|
|
jpgBuffer,
|
|
|
|
|
jpgBuffer.length,
|
|
|
|
|
'image/jpeg',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 3. Create Workflow Task
|
|
|
|
|
const task = await prisma.workflowTask.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: 'transcode_image',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
assetId: asset.id,
|
|
|
|
|
projectId: project.id,
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
payload: {
|
2026-10-10 03:41:49 -04:00
|
|
|
projectId: project.id,
|
2026-10-19 15:35:08 +08:00
|
|
|
transcode: {
|
|
|
|
|
thumbnail: true,
|
2026-10-10 03:41:49 -04:00
|
|
|
},
|
|
|
|
|
},
|
2026-10-19 15:35:08 +08:00
|
|
|
},
|
|
|
|
|
})
|
2026-10-10 03:41:49 -04:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
// 4. Wait for workflow to complete
|
|
|
|
|
console.log(
|
|
|
|
|
`Submitted E2E Rotated Image Transcode Workflow Task. ID: ${task.id}. Awaiting completion...`,
|
|
|
|
|
)
|
|
|
|
|
const completedTask = await workflowService.executeWait(task, 45000)
|
2026-10-10 03:41:49 -04:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
// 5. Verification: the asset and its transcode are portrait, not the stored landscape.
|
|
|
|
|
expect(completedTask.status).toBe('completed')
|
2026-10-10 03:41:49 -04:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const updatedAsset = await prisma.asset.findUnique({
|
|
|
|
|
where: { id: asset.id },
|
|
|
|
|
})
|
|
|
|
|
expect(updatedAsset?.status).toBe(AssetStatus.processed)
|
2026-10-10 03:41:49 -04:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const mediaInfo = updatedAsset?.media as unknown as {
|
|
|
|
|
imageTranscodes: Array<{ width: number; height: number }>
|
|
|
|
|
metadata: { originalWidth: number; originalHeight: number }
|
|
|
|
|
}
|
|
|
|
|
expect(mediaInfo.metadata.originalWidth).toBe(400)
|
|
|
|
|
expect(mediaInfo.metadata.originalHeight).toBe(600)
|
|
|
|
|
expect(mediaInfo.imageTranscodes[0].width).toBe(400)
|
|
|
|
|
expect(mediaInfo.imageTranscodes[0].height).toBe(600)
|
|
|
|
|
}, 50000)
|
|
|
|
|
})
|