mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-27 19:34:38 +00:00
* feat(transcode): add HLS live streaming support * fix(hls): resolve version stacks for public HLS and add workflow E2E test
5363 lines
166 KiB
TypeScript
5363 lines
166 KiB
TypeScript
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
|
|
import { prisma } from '@shumai/db'
|
|
import { setupTestDbHooks } from '@shumai/db/test'
|
|
import { HTTPException } from 'hono/http-exception'
|
|
|
|
import { AssetType, AssetStatus, Prisma, WorkflowTaskType, WorkflowTaskStatus } from '@shumai/db'
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
|
import { uploadService } from '@shumai/core/src/upload/upload'
|
|
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
|
import { AssetService } from './asset'
|
|
|
|
import { ensureJpegInStorage } from '@shumai/core/src/s3/preview-converter'
|
|
import { workflowService } from '@shumai/workflow-core'
|
|
|
|
vi.mock('@shumai/core/src/s3/s3', () => ({
|
|
s3Service: {
|
|
presign: vi.fn().mockResolvedValue('http://mock-s3-url'),
|
|
putObject: vi.fn().mockResolvedValue(undefined),
|
|
getObject: vi.fn().mockResolvedValue({ buffer: Buffer.from('') }),
|
|
deleteObject: vi.fn().mockResolvedValue(1),
|
|
deletePrefix: vi.fn().mockResolvedValue(1),
|
|
copyObject: vi.fn().mockResolvedValue(undefined),
|
|
},
|
|
}))
|
|
|
|
vi.mock('@shumai/core/src/s3/preview-converter', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('@shumai/core/src/s3/preview-converter')>()
|
|
return {
|
|
...actual,
|
|
ensureJpegInStorage: vi.fn().mockResolvedValue(undefined),
|
|
}
|
|
})
|
|
|
|
describe('AssetService', () => {
|
|
setupTestDbHooks()
|
|
|
|
let assetService: AssetService
|
|
|
|
beforeEach(async () => {
|
|
assetService = new AssetService()
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
const setupBasicAssets = async () => {
|
|
const user = await prisma.user.create({
|
|
data: {
|
|
name: 'TestUser_Asset_' + Date.now(),
|
|
type: 'human',
|
|
email: `test-${Date.now()}@example.com`,
|
|
},
|
|
})
|
|
const team = await prisma.team.create({
|
|
data: { name: 'TestTeam_Asset_' + Date.now() },
|
|
})
|
|
await prisma.teamMember.create({
|
|
data: {
|
|
teamId: team.id,
|
|
userId: user.id,
|
|
role: 'owner',
|
|
},
|
|
})
|
|
const project = await prisma.project.create({
|
|
data: { name: 'TestProject_Asset', teamId: team.id },
|
|
})
|
|
const team2 = await prisma.team.create({
|
|
data: { name: 'TestTeam2_Asset_' + Date.now() },
|
|
})
|
|
const project2 = await prisma.project.create({
|
|
data: { name: 'TestProject2_Asset', teamId: team2.id },
|
|
})
|
|
const project3 = await prisma.project.create({
|
|
data: { name: 'TestProject3_Asset', teamId: team.id },
|
|
})
|
|
|
|
const root = await prisma.asset.create({
|
|
data: {
|
|
name: 'root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
fileCount: 3,
|
|
sizeByte: 960,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const folderA = await prisma.asset.create({
|
|
data: {
|
|
name: 'folderA',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
parentId: root.id,
|
|
creatorId: user.id,
|
|
fileCount: 2,
|
|
sizeByte: 300,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const fileA1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileA1',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: folderA.id,
|
|
creatorId: user.id,
|
|
sizeByte: 100,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const fileA2 = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileA2',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: folderA.id,
|
|
creatorId: user.id,
|
|
sizeByte: 200,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const folderB = await prisma.asset.create({
|
|
data: {
|
|
name: 'folderB',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
parentId: root.id,
|
|
creatorId: user.id,
|
|
fileCount: 2,
|
|
sizeByte: 410,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const stackB = await prisma.asset.create({
|
|
data: {
|
|
name: 'stackB',
|
|
type: AssetType.version_stack,
|
|
projectId: project.id,
|
|
parentId: folderB.id,
|
|
creatorId: user.id,
|
|
fileCount: 2,
|
|
sizeByte: 110,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const fileB1v1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileB1v1',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: stackB.id,
|
|
creatorId: user.id,
|
|
sizeByte: 50,
|
|
sortIndex: 'a0',
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const fileB1v2 = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileB1v2',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: stackB.id,
|
|
creatorId: user.id,
|
|
sizeByte: 60,
|
|
sortIndex: 'a1',
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const fileB2 = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileB2',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: folderB.id,
|
|
creatorId: user.id,
|
|
sizeByte: 300,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const fileRoot1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileRoot1',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: root.id,
|
|
creatorId: user.id,
|
|
sizeByte: 250,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const root2 = await prisma.asset.create({
|
|
data: {
|
|
name: 'root2',
|
|
type: AssetType.folder,
|
|
projectId: project2.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const root3 = await prisma.asset.create({
|
|
data: {
|
|
name: 'root3',
|
|
type: AssetType.folder,
|
|
projectId: project3.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
return {
|
|
user,
|
|
project,
|
|
assets: {
|
|
root,
|
|
folderA,
|
|
fileA1,
|
|
fileA2,
|
|
folderB,
|
|
stackB,
|
|
fileB1v1,
|
|
fileB1v2,
|
|
fileB2,
|
|
fileRoot1,
|
|
root2,
|
|
root3,
|
|
},
|
|
}
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const verifyAsset = async (id: string, expected: any) => {
|
|
const a = await prisma.asset.findUnique({ where: { id } })
|
|
expect(a).toBeDefined()
|
|
if (!a) return
|
|
|
|
if (expected.name !== undefined) expect(a.name).toBe(expected.name)
|
|
expect(a.type).toBe(expected.type)
|
|
expect(a.fileCount).toBe(expected.fileCount)
|
|
expect(Number(a.sizeByte)).toBe(expected.size)
|
|
}
|
|
|
|
it('can create a folder and list children', async () => {
|
|
const { project } = await setupBasicAssets()
|
|
|
|
const rootFolder = await assetService.createAsset({
|
|
name: 'Root Folder',
|
|
type: 'folder',
|
|
projectId: project.id,
|
|
})
|
|
|
|
expect(rootFolder.id).toBeDefined()
|
|
expect(rootFolder.name).toBe('Root Folder')
|
|
|
|
const childFile = await assetService.createAsset({
|
|
name: 'Child File.txt',
|
|
type: 'file',
|
|
parentId: rootFolder.id,
|
|
contentType: 'text/plain',
|
|
sizeByte: 1024,
|
|
})
|
|
|
|
const children = await assetService.listChildren({
|
|
assetId: rootFolder.id,
|
|
assetType: 'file',
|
|
first: 10,
|
|
})
|
|
|
|
expect(children.data.length).toBe(1)
|
|
expect(children.data[0].id).toBe(childFile.id)
|
|
})
|
|
|
|
it('handles reparenting - Case 1: move into sibling folder', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
await assetService.reparentAssets({
|
|
assetIds: [assets.fileRoot1.id],
|
|
newParentId: assets.folderA.id,
|
|
creatorId: user.id,
|
|
})
|
|
await verifyAsset(assets.root.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 2,
|
|
size: 960,
|
|
})
|
|
await verifyAsset(assets.folderA.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 3,
|
|
size: 550,
|
|
})
|
|
})
|
|
|
|
it('handles reparenting - Case 2: move out to immediate parent', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
await assetService.reparentAssets({
|
|
assetIds: [assets.fileA1.id],
|
|
newParentId: assets.root.id,
|
|
creatorId: user.id,
|
|
})
|
|
await verifyAsset(assets.root.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 4,
|
|
size: 960,
|
|
})
|
|
await verifyAsset(assets.folderA.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 1,
|
|
size: 200,
|
|
})
|
|
})
|
|
|
|
it('handles reparenting - Case 3: generic move', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
await assetService.reparentAssets({
|
|
assetIds: [assets.fileA1.id],
|
|
newParentId: assets.folderB.id,
|
|
creatorId: user.id,
|
|
})
|
|
await verifyAsset(assets.root.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 3,
|
|
size: 960,
|
|
})
|
|
await verifyAsset(assets.folderA.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 1,
|
|
size: 200,
|
|
})
|
|
await verifyAsset(assets.folderB.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 3,
|
|
size: 510,
|
|
})
|
|
})
|
|
|
|
it('handles reparenting - Error: move across teams', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
await expect(
|
|
assetService.reparentAssets({
|
|
assetIds: [assets.fileA1.id],
|
|
newParentId: assets.root2.id,
|
|
creatorId: user.id,
|
|
}),
|
|
).rejects.toThrow('Cannot move asset')
|
|
})
|
|
|
|
it('handles reparenting - Success: move across projects in same team', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
await assetService.reparentAssets({
|
|
assetIds: [assets.fileA1.id],
|
|
newParentId: assets.root3.id,
|
|
creatorId: user.id,
|
|
})
|
|
await verifyAsset(assets.root.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 3,
|
|
size: 860,
|
|
})
|
|
await verifyAsset(assets.folderA.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 1,
|
|
size: 200,
|
|
})
|
|
await verifyAsset(assets.root3.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 1,
|
|
size: 100,
|
|
})
|
|
})
|
|
|
|
it('handles copying assets recursively', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
|
|
await assetService.copyAssets({
|
|
assetIds: [assets.folderA.id],
|
|
newParentId: assets.root3.id,
|
|
creatorId: user.id,
|
|
withComments: false,
|
|
})
|
|
|
|
// Verify root3 now has 1 more folder
|
|
await verifyAsset(assets.root3.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 1,
|
|
size: 300,
|
|
})
|
|
|
|
// Verify children of folderA were copied
|
|
const copiedFolders = await prisma.asset.findMany({
|
|
where: { parentId: assets.root3.id, name: 'folderA' },
|
|
})
|
|
expect(copiedFolders.length).toBe(1)
|
|
const newFolderA = copiedFolders[0]
|
|
|
|
const children = await prisma.asset.findMany({
|
|
where: { parentId: newFolderA.id },
|
|
})
|
|
expect(children.length).toBe(2)
|
|
expect(children.map((c) => c.name)).toContain('fileA1')
|
|
expect(children.map((c) => c.name)).toContain('fileA2')
|
|
})
|
|
|
|
it('handles copying assets with comments and attachments', async () => {
|
|
const { user, assets, project } = await setupBasicAssets()
|
|
|
|
// Create a comment with attachment on fileA1
|
|
const attachmentAsset = await prisma.asset.create({
|
|
data: {
|
|
name: 'attach.png',
|
|
type: AssetType.attachment,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
sizeByte: 50,
|
|
},
|
|
})
|
|
|
|
const comment = await prisma.assetComment.create({
|
|
data: {
|
|
assetId: assets.fileA1.id,
|
|
creatorId: user.id,
|
|
message: 'Original Comment',
|
|
},
|
|
})
|
|
|
|
await prisma.assetCommentAttachment.create({
|
|
data: {
|
|
commentId: comment.id,
|
|
assetId: attachmentAsset.id,
|
|
},
|
|
})
|
|
|
|
await assetService.copyAssets({
|
|
assetIds: [assets.fileA1.id],
|
|
newParentId: assets.root3.id,
|
|
creatorId: user.id,
|
|
withComments: true,
|
|
})
|
|
|
|
// Find the copied file
|
|
const copiedFiles = await prisma.asset.findMany({
|
|
where: { parentId: assets.root3.id, name: 'fileA1' },
|
|
})
|
|
expect(copiedFiles.length).toBe(1)
|
|
const newFileA1 = copiedFiles[0]
|
|
|
|
// Verify comments were copied
|
|
const newComments = await prisma.assetComment.findMany({
|
|
where: { assetId: newFileA1.id },
|
|
include: { attachments: { include: { asset: true } } },
|
|
})
|
|
expect(newComments.length).toBe(1)
|
|
expect(newComments[0].message).toBe('Original Comment')
|
|
expect(newComments[0].attachments.length).toBe(1)
|
|
|
|
// Verify attachment asset was deep copied
|
|
const newAttachmentAsset = newComments[0].attachments[0].asset
|
|
expect(newAttachmentAsset.id).not.toBe(attachmentAsset.id)
|
|
expect(newAttachmentAsset.name).toBe('attach.png')
|
|
expect(newAttachmentAsset.type).toBe(AssetType.attachment)
|
|
})
|
|
|
|
it('rejects copying a folder into its own descendant', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
|
|
// Create a subfolder inside folderA
|
|
const subfolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'subfolder',
|
|
type: AssetType.folder,
|
|
projectId: assets.folderA.projectId,
|
|
parentId: assets.folderA.id,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
await expect(
|
|
assetService.copyAssets({
|
|
assetIds: [assets.folderA.id],
|
|
newParentId: subfolder.id,
|
|
creatorId: user.id,
|
|
withComments: false,
|
|
}),
|
|
).rejects.toThrow('Cannot copy a folder into its own descendant')
|
|
})
|
|
|
|
it('uses destination project when copying comment attachments', async () => {
|
|
const { user, assets, project } = await setupBasicAssets()
|
|
|
|
// Set up project 2
|
|
const project2 = await prisma.project.create({
|
|
data: { name: 'Project 2', teamId: project.teamId },
|
|
})
|
|
const root2 = await prisma.asset.create({
|
|
data: { name: 'root2', type: AssetType.folder, projectId: project2.id, status: 'uploaded' },
|
|
})
|
|
|
|
// Create a comment with attachment on fileA1
|
|
const attachmentAsset = await prisma.asset.create({
|
|
data: {
|
|
name: 'attach.png',
|
|
type: AssetType.attachment,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
sizeByte: 50,
|
|
},
|
|
})
|
|
|
|
const comment = await prisma.assetComment.create({
|
|
data: {
|
|
assetId: assets.fileA1.id,
|
|
creatorId: user.id,
|
|
message: 'Original Comment',
|
|
},
|
|
})
|
|
|
|
await prisma.assetCommentAttachment.create({
|
|
data: {
|
|
commentId: comment.id,
|
|
assetId: attachmentAsset.id,
|
|
},
|
|
})
|
|
|
|
// Copy to project 2
|
|
await assetService.copyAssets({
|
|
assetIds: [assets.fileA1.id],
|
|
newParentId: root2.id,
|
|
creatorId: user.id,
|
|
withComments: true,
|
|
})
|
|
|
|
// Find the copied file in project 2
|
|
const newFileA1 = await prisma.asset.findFirstOrThrow({
|
|
where: { parentId: root2.id, name: 'fileA1' },
|
|
})
|
|
|
|
// Verify comments were copied
|
|
const newComments = await prisma.assetComment.findMany({
|
|
where: { assetId: newFileA1.id },
|
|
include: { attachments: { include: { asset: true } } },
|
|
})
|
|
expect(newComments.length).toBe(1)
|
|
|
|
// Verify attachment asset belongs to PROJECT 2
|
|
const newAttachmentAsset = newComments[0].attachments[0].asset
|
|
expect(newAttachmentAsset.projectId).toBe(project2.id)
|
|
})
|
|
|
|
it('handles reparenting - Version Stack: move into stack', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
await assetService.reparentAssets({
|
|
assetIds: [assets.fileB2.id],
|
|
newParentId: assets.stackB.id,
|
|
creatorId: user.id,
|
|
})
|
|
await verifyAsset(assets.root.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 3,
|
|
size: 960,
|
|
})
|
|
await verifyAsset(assets.folderB.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 1,
|
|
size: 410,
|
|
})
|
|
await verifyAsset(assets.stackB.id, {
|
|
type: AssetType.version_stack,
|
|
fileCount: 3,
|
|
size: 410,
|
|
})
|
|
})
|
|
|
|
it('rejects reparenting a version_stack onto a file', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
await expect(
|
|
assetService.reparentAssets({
|
|
assetIds: [assets.stackB.id],
|
|
newParentId: assets.fileA1.id,
|
|
creatorId: user.id,
|
|
}),
|
|
).rejects.toThrow('Source asset must be a file to create a version stack')
|
|
})
|
|
|
|
it('rejects reparenting a version_stack onto another version_stack', async () => {
|
|
const { user, assets, project } = await setupBasicAssets()
|
|
const stack2 = await prisma.asset.create({
|
|
data: {
|
|
name: 'stack2',
|
|
type: AssetType.version_stack,
|
|
projectId: project.id,
|
|
parentId: assets.folderA.id,
|
|
creatorId: user.id,
|
|
fileCount: 2,
|
|
sizeByte: 100,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
await expect(
|
|
assetService.reparentAssets({
|
|
assetIds: [assets.stackB.id],
|
|
newParentId: stack2.id,
|
|
creatorId: user.id,
|
|
}),
|
|
).rejects.toThrow('Cannot move version_stack asset into a version stack')
|
|
})
|
|
|
|
it('rejects reparenting a folder onto a version_stack', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
await expect(
|
|
assetService.reparentAssets({
|
|
assetIds: [assets.folderA.id],
|
|
newParentId: assets.stackB.id,
|
|
creatorId: user.id,
|
|
}),
|
|
).rejects.toThrow('Cannot move folder asset into a version stack')
|
|
})
|
|
|
|
it('rejects reparenting multiple files onto a version_stack at once', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
await expect(
|
|
assetService.reparentAssets({
|
|
assetIds: [assets.fileA1.id, assets.fileA2.id],
|
|
newParentId: assets.stackB.id,
|
|
creatorId: user.id,
|
|
}),
|
|
).rejects.toThrow('Can only move one file at a time into a version stack')
|
|
})
|
|
|
|
it('rejects reparenting a folder into itself', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
await expect(
|
|
assetService.reparentAssets({
|
|
assetIds: [assets.folderA.id],
|
|
newParentId: assets.folderA.id,
|
|
creatorId: user.id,
|
|
}),
|
|
).rejects.toThrow('Cannot move an asset into itself or its own descendant')
|
|
})
|
|
|
|
it('rejects reparenting a folder into its own descendant', async () => {
|
|
const { user, assets, project } = await setupBasicAssets()
|
|
const subfolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'subfolderA',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
parentId: assets.folderA.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
await expect(
|
|
assetService.reparentAssets({
|
|
assetIds: [assets.folderA.id],
|
|
newParentId: subfolder.id,
|
|
creatorId: user.id,
|
|
}),
|
|
).rejects.toThrow('Cannot move an asset into itself or its own descendant')
|
|
})
|
|
|
|
describe('fileCount verification', () => {
|
|
it('createAsset should increment parent fileCount', async () => {
|
|
const { user, assets, project } = await setupBasicAssets()
|
|
|
|
const parent = assets.folderA
|
|
const initialCount = parent.fileCount // should be 2 from setup (fileA1, fileA2)
|
|
|
|
// Create a new folder inside folderA
|
|
await assetService.createAsset({
|
|
name: 'newFolder',
|
|
type: 'folder',
|
|
parentId: parent.id,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
})
|
|
|
|
const updatedParent = await prisma.asset.findUnique({ where: { id: parent.id } })
|
|
expect(updatedParent?.fileCount).toBe(initialCount + 1)
|
|
|
|
// Create a new file inside folderA
|
|
await assetService.createAsset({
|
|
name: 'newFile',
|
|
type: 'file',
|
|
parentId: parent.id,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
})
|
|
|
|
const updatedParent2 = await prisma.asset.findUnique({ where: { id: parent.id } })
|
|
expect(updatedParent2?.fileCount).toBe(initialCount + 2)
|
|
})
|
|
|
|
it('reparentAssets should update fileCount when moving folders', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
|
|
const sourceParent = assets.root
|
|
const targetParent = assets.folderB
|
|
const folderToMove = assets.folderA
|
|
|
|
// folderA is a child of root.
|
|
// root children: folderA, folderB, fileRoot1. count=3.
|
|
// folderB children: stackB, fileB2. count=2.
|
|
|
|
await assetService.reparentAssets({
|
|
assetIds: [folderToMove.id],
|
|
newParentId: targetParent.id,
|
|
creatorId: user.id,
|
|
})
|
|
|
|
const updatedSourceParent = await prisma.asset.findUnique({ where: { id: sourceParent.id } })
|
|
expect(updatedSourceParent?.fileCount).toBe(2)
|
|
|
|
const updatedTargetParent = await prisma.asset.findUnique({ where: { id: targetParent.id } })
|
|
expect(updatedTargetParent?.fileCount).toBe(3)
|
|
})
|
|
})
|
|
|
|
it('handles reparenting - Version Stack: dissolve with 1 remaining', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
await assetService.reparentAssets({
|
|
assetIds: [assets.fileB1v1.id],
|
|
newParentId: assets.folderB.id,
|
|
creatorId: user.id,
|
|
})
|
|
|
|
const stack = await prisma.asset.findUnique({
|
|
where: { id: assets.stackB.id },
|
|
})
|
|
expect(stack).toBeNull() // Stack should be dissolved
|
|
|
|
await verifyAsset(assets.root.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 3,
|
|
size: 960,
|
|
})
|
|
await verifyAsset(assets.folderB.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 3,
|
|
size: 410,
|
|
})
|
|
})
|
|
|
|
it('handles soft deleting and restoring assets recursively', async () => {
|
|
const { assets } = await setupBasicAssets()
|
|
|
|
// Initially folderA has 2 files and size 300
|
|
// root has 3 files (folderA, folderB, fileRoot1) and size 960
|
|
await assetService.deleteAssets([assets.folderA.id])
|
|
|
|
const folderA = await prisma.asset.findUnique({
|
|
where: { id: assets.folderA.id },
|
|
})
|
|
expect(folderA?.isDeleted).toBe(true)
|
|
|
|
const fileA1 = await prisma.asset.findUnique({
|
|
where: { id: assets.fileA1.id },
|
|
})
|
|
expect(fileA1?.isDeleted).toBe(true)
|
|
|
|
// Verify root size decreased by folderA's size
|
|
await verifyAsset(assets.root.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 2, // folderA removed from child count
|
|
size: 660, // 960 - 300
|
|
})
|
|
|
|
// Restore folderA
|
|
await assetService.restoreAssets([assets.folderA.id])
|
|
|
|
const folderRestoredA = await prisma.asset.findUnique({
|
|
where: { id: assets.folderA.id },
|
|
})
|
|
expect(folderRestoredA?.isDeleted).toBe(false)
|
|
|
|
expect(folderRestoredA?.deletedAt).toBeNull()
|
|
|
|
const fileA1Restored = await prisma.asset.findUnique({
|
|
where: { id: assets.fileA1.id },
|
|
})
|
|
expect(fileA1Restored?.isDeleted).toBe(false)
|
|
|
|
// Verify root restored properly
|
|
await verifyAsset(assets.root.id, {
|
|
type: AssetType.folder,
|
|
fileCount: 3,
|
|
size: 960,
|
|
})
|
|
})
|
|
|
|
it('can create and list comments', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
|
|
const c1 = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'Hello World',
|
|
attachmentIds: [],
|
|
})
|
|
|
|
expect(c1.id).toBeDefined()
|
|
expect(c1.message).toBe('Hello World')
|
|
|
|
const c2 = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'Reply',
|
|
replyToId: c1.id,
|
|
attachmentIds: [],
|
|
})
|
|
|
|
const commentInfo = await assetService.getComment(c1.id)
|
|
expect(commentInfo.replies.length).toBe(1)
|
|
expect(commentInfo.replies[0].id).toBe(c2.id)
|
|
|
|
const list = await assetService.listComments(assets.fileA1.id, {
|
|
first: 10,
|
|
})
|
|
expect(list.data.length).toBe(1) // Only parent comments
|
|
expect(list.data[0].id).toBe(c1.id)
|
|
expect(list.data[0].replies.length).toBe(1)
|
|
})
|
|
|
|
it('deleteComment validation and execution', async () => {
|
|
const { user, project, assets } = await setupBasicAssets()
|
|
|
|
// 1. Create a comment
|
|
const comment = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'Self Comment',
|
|
attachmentIds: [],
|
|
})
|
|
|
|
// Create another user
|
|
const otherUser = await prisma.user.create({
|
|
data: {
|
|
name: 'OtherUser',
|
|
type: 'human',
|
|
email: `other-${Date.now()}@example.com`,
|
|
},
|
|
})
|
|
|
|
// Add other user to the team as editor
|
|
await prisma.teamMember.create({
|
|
data: {
|
|
teamId: project.teamId,
|
|
userId: otherUser.id,
|
|
role: 'editor',
|
|
},
|
|
})
|
|
|
|
// Create comment by other user
|
|
const otherComment = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: otherUser.id,
|
|
message: 'Other Comment',
|
|
attachmentIds: [],
|
|
})
|
|
|
|
// 3. User (owner) should be able to delete their own comment
|
|
await expect(
|
|
assetService.deleteComment({ commentId: comment.id, userId: user.id }),
|
|
).resolves.not.toThrow()
|
|
// Verify it is deleted from DB
|
|
await expect(prisma.assetComment.findUnique({ where: { id: comment.id } })).resolves.toBeNull()
|
|
|
|
// 4. User (owner) should be able to delete other user's comment
|
|
await expect(
|
|
assetService.deleteComment({ commentId: otherComment.id, userId: user.id }),
|
|
).resolves.not.toThrow()
|
|
// Verify it is deleted from DB
|
|
await expect(
|
|
prisma.assetComment.findUnique({ where: { id: otherComment.id } }),
|
|
).resolves.toBeNull()
|
|
|
|
// Create a new comment by user
|
|
const comment2 = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'Self Comment 2',
|
|
attachmentIds: [],
|
|
})
|
|
|
|
// 5. Other user (editor, non-creator) should NOT be able to delete user's comment
|
|
await expect(
|
|
assetService.deleteComment({ commentId: comment2.id, userId: otherUser.id }),
|
|
).rejects.toThrow(HTTPException)
|
|
|
|
// 6. Other user (editor, creator) should be able to delete their own comment
|
|
const otherComment2 = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: otherUser.id,
|
|
message: 'Other Comment 2',
|
|
attachmentIds: [],
|
|
})
|
|
await expect(
|
|
assetService.deleteComment({ commentId: otherComment2.id, userId: otherUser.id }),
|
|
).resolves.not.toThrow()
|
|
})
|
|
|
|
it('deleteComment cleans up S3 attachment files and database records', async () => {
|
|
const { user, project, assets } = await setupBasicAssets()
|
|
|
|
// 1. Create StorageKey and Asset for attachment
|
|
const storageKey = await prisma.storageKey.create({
|
|
data: {
|
|
key: 'attachments/test-attachment-file.png',
|
|
status: 'active',
|
|
},
|
|
})
|
|
|
|
const attachmentAsset = await prisma.asset.create({
|
|
data: {
|
|
name: 'test-attachment-file.png',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: assets.root.id,
|
|
creatorId: user.id,
|
|
storageKeyId: storageKey.id,
|
|
sizeByte: 100,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
// 3. Create a comment with this attachment
|
|
const comment = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'Comment with attachment',
|
|
attachmentIds: [attachmentAsset.id],
|
|
})
|
|
|
|
// Verify it is created in DB
|
|
const dbComment = await prisma.assetComment.findUnique({
|
|
where: { id: comment.id },
|
|
include: { attachments: true },
|
|
})
|
|
expect(dbComment?.attachments.length).toBe(1)
|
|
|
|
// 4. Delete the comment
|
|
await expect(
|
|
assetService.deleteComment({ commentId: comment.id, userId: user.id }),
|
|
).resolves.not.toThrow()
|
|
|
|
// 5. Verify the comment is deleted from DB
|
|
await expect(prisma.assetComment.findUnique({ where: { id: comment.id } })).resolves.toBeNull()
|
|
|
|
// 6. Verify that the attachment Asset and StorageKey are deleted from DB
|
|
await expect(prisma.asset.findUnique({ where: { id: attachmentAsset.id } })).resolves.toBeNull()
|
|
await expect(prisma.storageKey.findUnique({ where: { id: storageKey.id } })).resolves.toBeNull()
|
|
|
|
// 7. Verify S3 deletion was called
|
|
const { s3Service } = await import('@shumai/core/src/s3/s3')
|
|
expect(s3Service.deleteObject).toHaveBeenCalledWith(
|
|
'shumai',
|
|
'attachments/test-attachment-file.png',
|
|
)
|
|
})
|
|
|
|
it('can create and retrieve comments with video timestamp', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
|
|
const comment = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'Interesting point at 5.5s',
|
|
second: 5.5,
|
|
attachmentIds: [],
|
|
})
|
|
|
|
expect(comment.second).toBe(5.5)
|
|
|
|
const retrieved = await assetService.getComment(comment.id)
|
|
expect(retrieved.second).toBe(5.5)
|
|
|
|
const list = await assetService.listComments(assets.fileA1.id, {
|
|
first: 10,
|
|
})
|
|
expect(list.data[0].second).toBe(5.5)
|
|
})
|
|
|
|
it('correctly handles AI comments with agent identity', async () => {
|
|
const { assets, project } = await setupBasicAssets()
|
|
|
|
// Create an agent user
|
|
const agentUser = await prisma.user.create({
|
|
data: {
|
|
id: 'agent-id',
|
|
name: 'Smart Agent',
|
|
email: 'agent@test.com',
|
|
type: 'agent',
|
|
},
|
|
})
|
|
|
|
// Create the agent record
|
|
await prisma.agent.create({
|
|
data: {
|
|
id: agentUser.id,
|
|
teamId: project.teamId,
|
|
type: 'autofill',
|
|
config: {
|
|
provider: 'test',
|
|
model: 'test',
|
|
},
|
|
},
|
|
})
|
|
|
|
// Create a real agent session
|
|
const session = await prisma.agentSession.create({
|
|
data: {
|
|
id: 'session-id-123',
|
|
agentId: agentUser.id,
|
|
cwd: process.cwd(),
|
|
},
|
|
})
|
|
|
|
// Create an AI comment manually using the activity-like logic
|
|
await prisma.assetComment.create({
|
|
data: {
|
|
assetId: assets.fileA1.id,
|
|
message: 'I am an AI',
|
|
sessionId: session.id,
|
|
creatorId: agentUser.id,
|
|
},
|
|
})
|
|
|
|
const list = await assetService.listComments(assets.fileA1.id, {
|
|
first: 10,
|
|
})
|
|
|
|
const aiComment = list.data.find((c) => !!c.sessionId)
|
|
expect(aiComment).toBeDefined()
|
|
expect(aiComment?.creator.id).toBe(agentUser.id)
|
|
expect(aiComment?.creator.name).toBe('Smart Agent')
|
|
})
|
|
|
|
it('orders comments by id asc', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
|
|
const c1 = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'First',
|
|
attachmentIds: [],
|
|
})
|
|
|
|
const c2 = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'Second',
|
|
attachmentIds: [],
|
|
})
|
|
|
|
const list = await assetService.listComments(assets.fileA1.id, {
|
|
first: 10,
|
|
})
|
|
|
|
expect(list.data.length).toBe(2)
|
|
expect(list.data[0].id).toBe(c1.id)
|
|
expect(list.data[1].id).toBe(c2.id)
|
|
expect(list.data[0].message).toBe('First')
|
|
expect(list.data[1].message).toBe('Second')
|
|
|
|
// Test replies order
|
|
const r1 = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'Reply 1',
|
|
replyToId: c1.id,
|
|
attachmentIds: [],
|
|
})
|
|
|
|
const r2 = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'Reply 2',
|
|
replyToId: c1.id,
|
|
attachmentIds: [],
|
|
})
|
|
|
|
const c1WithReplies = await assetService.getComment(c1.id)
|
|
expect(c1WithReplies.replies.length).toBe(2)
|
|
expect(c1WithReplies.replies[0].id).toBe(r1.id)
|
|
expect(c1WithReplies.replies[1].id).toBe(r2.id)
|
|
|
|
const listWithReplies = await assetService.listComments(assets.fileA1.id, {
|
|
first: 10,
|
|
})
|
|
expect(listWithReplies.data[0].replies.length).toBe(2)
|
|
expect(listWithReplies.data[0].replies[0].id).toBe(r1.id)
|
|
expect(listWithReplies.data[0].replies[1].id).toBe(r2.id)
|
|
})
|
|
|
|
it('can mark comments as complete and incomplete', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
|
|
const comment = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'To be completed',
|
|
attachmentIds: [],
|
|
})
|
|
|
|
expect(comment.isCompleted).toBe(false)
|
|
expect(comment.completionLastChangedBy).toBeNull()
|
|
|
|
// Mark as complete
|
|
const completed = await assetService.completeComment(comment.id, true, user.id)
|
|
expect(completed.isCompleted).toBe(true)
|
|
expect(completed.completionLastChangedBy).toBeDefined()
|
|
expect(completed.completionLastChangedBy?.id).toBe(user.id)
|
|
|
|
// Mark as incomplete
|
|
const incompleted = await assetService.completeComment(comment.id, false, user.id)
|
|
expect(incompleted.isCompleted).toBe(false)
|
|
expect(incompleted.completionLastChangedBy?.id).toBe(user.id)
|
|
})
|
|
|
|
describe('Comment Reactions', () => {
|
|
it('adds, removes, and lists reactions chronologically with creatorNames', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
|
|
const user2 = await prisma.user.create({
|
|
data: {
|
|
name: 'Bob',
|
|
type: 'human',
|
|
email: `bob-${Date.now()}@example.com`,
|
|
},
|
|
})
|
|
|
|
const user3 = await prisma.user.create({
|
|
data: {
|
|
name: 'Charlie',
|
|
type: 'human',
|
|
email: `charlie-${Date.now()}@example.com`,
|
|
},
|
|
})
|
|
|
|
const comment = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'A wonderful comment',
|
|
attachmentIds: [],
|
|
})
|
|
|
|
// Add reaction 1: Bob adds 👍
|
|
await assetService.addCommentReaction(comment.id, user2.id, '👍')
|
|
// Idempotency: Bob adds 👍 again
|
|
await assetService.addCommentReaction(comment.id, user2.id, '👍')
|
|
|
|
// Add reaction 2: Charlie adds ❤️
|
|
await assetService.addCommentReaction(comment.id, user3.id, '❤️')
|
|
|
|
// Add reaction 3: Alice (user) adds 👍
|
|
await assetService.addCommentReaction(comment.id, user.id, '👍')
|
|
|
|
// Fetch as user (Alice)
|
|
const commentAsAlice = await assetService.getComment(comment.id, user.id)
|
|
expect(commentAsAlice.reactionCounts).toHaveLength(2)
|
|
|
|
// First reaction added was 👍, then ❤️
|
|
expect(commentAsAlice.reactionCounts[0].code).toBe('👍')
|
|
expect(commentAsAlice.reactionCounts[0].count).toBe(2)
|
|
expect(commentAsAlice.reactionCounts[0].requestingUserReacted).toBe(true)
|
|
expect(commentAsAlice.reactionCounts[0].creatorNames).toEqual(['Bob'])
|
|
|
|
expect(commentAsAlice.reactionCounts[1].code).toBe('❤️')
|
|
expect(commentAsAlice.reactionCounts[1].count).toBe(1)
|
|
expect(commentAsAlice.reactionCounts[1].requestingUserReacted).toBe(false)
|
|
expect(commentAsAlice.reactionCounts[1].creatorNames).toEqual(['Charlie'])
|
|
|
|
// Fetch as Bob
|
|
const commentAsBob = await assetService.getComment(comment.id, user2.id)
|
|
expect(commentAsBob.reactionCounts[0].code).toBe('👍')
|
|
expect(commentAsBob.reactionCounts[0].requestingUserReacted).toBe(true)
|
|
expect(commentAsBob.reactionCounts[0].creatorNames).toEqual([user.name])
|
|
|
|
// Remove Alice's 👍
|
|
await assetService.removeCommentReaction(comment.id, user.id, '👍')
|
|
// Idempotency
|
|
await assetService.removeCommentReaction(comment.id, user.id, '👍')
|
|
|
|
const updatedAlice = await assetService.getComment(comment.id, user.id)
|
|
expect(updatedAlice.reactionCounts[0].code).toBe('👍')
|
|
expect(updatedAlice.reactionCounts[0].count).toBe(1)
|
|
expect(updatedAlice.reactionCounts[0].requestingUserReacted).toBe(false)
|
|
expect(updatedAlice.reactionCounts[0].creatorNames).toEqual(['Bob'])
|
|
|
|
// Verify listComments returns reactions for comments and replies
|
|
const reply = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user2.id,
|
|
message: 'A nice reply',
|
|
replyToId: comment.id,
|
|
attachmentIds: [],
|
|
})
|
|
await assetService.addCommentReaction(reply.id, user.id, '🎉')
|
|
|
|
const listRes = await assetService.listComments(assets.fileA1.id, { first: 10 }, user.id)
|
|
const listedComment = listRes.data.find((c) => c.id === comment.id)
|
|
expect(listedComment).toBeDefined()
|
|
expect(listedComment?.reactionCounts[0].code).toBe('👍')
|
|
expect(listedComment?.replies).toHaveLength(1)
|
|
expect(listedComment?.replies[0].reactionCounts).toHaveLength(1)
|
|
expect(listedComment?.replies[0].reactionCounts[0].code).toBe('🎉')
|
|
expect(listedComment?.replies[0].reactionCounts[0].requestingUserReacted).toBe(true)
|
|
})
|
|
|
|
it('throws error when adding reaction to nonexistent comment', async () => {
|
|
const { user } = await setupBasicAssets()
|
|
await expect(
|
|
assetService.addCommentReaction('nonexistent-id', user.id, '👍'),
|
|
).rejects.toThrow('Comment not found')
|
|
})
|
|
})
|
|
|
|
it('can export comments in all supported NLE formats', async () => {
|
|
const { user, assets } = await setupBasicAssets()
|
|
|
|
// Update asset media metadata with video frameRate and duration
|
|
await prisma.asset.update({
|
|
where: { id: assets.fileA1.id },
|
|
data: {
|
|
media: {
|
|
duration: 10,
|
|
filesize: 1024,
|
|
frames: 240,
|
|
finishedAt: new Date().toISOString(),
|
|
imageTranscodes: [],
|
|
videoTranscodes: [],
|
|
metadata: {
|
|
frameRate: 24,
|
|
duration: 10,
|
|
totalFrames: 240,
|
|
originalWidth: 1920,
|
|
originalHeight: 1080,
|
|
hasAudio: true,
|
|
bitRate: 5000,
|
|
startTimecode: '00:00:00:00',
|
|
format: 'mp4',
|
|
},
|
|
original: null,
|
|
},
|
|
},
|
|
})
|
|
|
|
const rootComment = await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'First scene looks great',
|
|
second: 2.5,
|
|
attachmentIds: [],
|
|
})
|
|
|
|
await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'Agreed!',
|
|
replyToId: rootComment.id,
|
|
attachmentIds: [],
|
|
})
|
|
|
|
await assetService.createComment({
|
|
assetId: assets.fileA1.id,
|
|
userId: user.id,
|
|
message: 'General feedback without timestamp',
|
|
attachmentIds: [],
|
|
})
|
|
|
|
// Test resolve-edl
|
|
const edl = await assetService.exportComments(assets.fileA1.id, 'resolve-edl')
|
|
expect(edl.filename.endsWith('_resolve.edl')).toBe(true)
|
|
expect(edl.mimeType).toBe('text/plain; charset=utf-8')
|
|
expect(edl.content).toContain('TITLE:')
|
|
expect(edl.content).toContain('First scene looks great')
|
|
expect(edl.content).toContain('^ Agreed!')
|
|
expect(edl.content).toContain('General feedback without timestamp')
|
|
|
|
// Test premiere-xml
|
|
const ppro = await assetService.exportComments(assets.fileA1.id, 'premiere-xml')
|
|
expect(ppro.filename.endsWith('_premiere.xml')).toBe(true)
|
|
expect(ppro.mimeType).toBe('application/xml; charset=utf-8')
|
|
expect(ppro.content).toContain('<xmeml version="4">')
|
|
expect(ppro.content).toContain('<comment>First scene looks great</comment>')
|
|
expect(ppro.content).toContain('<comment>Agreed!</comment>')
|
|
|
|
// Test media-composer-xml
|
|
const mc = await assetService.exportComments(assets.fileA1.id, 'media-composer-xml')
|
|
expect(mc.filename.endsWith('_media-composer.xml')).toBe(true)
|
|
expect(mc.mimeType).toBe('application/xml; charset=utf-8')
|
|
expect(mc.content).toContain('<Avid:StreamItems')
|
|
expect(mc.content).toContain('First scene looks great')
|
|
|
|
// Test fcp-fiojson
|
|
const fcp = await assetService.exportComments(assets.fileA1.id, 'fcp-fiojson')
|
|
expect(fcp.filename.endsWith('.fiojson')).toBe(true)
|
|
expect(fcp.mimeType).toBe('application/json; charset=utf-8')
|
|
const parsedFcp = JSON.parse(fcp.content) as { comments: unknown[] }
|
|
expect(parsedFcp.comments.length).toBe(3)
|
|
})
|
|
|
|
it('triggers AI workflow on bot mention', async () => {
|
|
const { user, project } = await setupBasicAssets()
|
|
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'test-file',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
// Create a bot agent
|
|
const botUser = await prisma.user.create({
|
|
data: {
|
|
name: 'Bot Agent',
|
|
type: 'agent',
|
|
email: 'bot-agent@shumai.ai',
|
|
},
|
|
})
|
|
const agent = await prisma.agent.create({
|
|
data: {
|
|
id: botUser.id,
|
|
teamId: project.teamId,
|
|
type: 'chat',
|
|
config: {
|
|
provider: 'google',
|
|
model: 'gemini-pro',
|
|
},
|
|
},
|
|
})
|
|
await prisma.teamMember.create({
|
|
data: {
|
|
teamId: project.teamId,
|
|
userId: botUser.id,
|
|
role: 'reviewer',
|
|
},
|
|
})
|
|
|
|
await assetService.createComment({
|
|
assetId: file.id,
|
|
userId: user.id,
|
|
message: `Hello <@${agent.id}> and <@default>`,
|
|
attachmentIds: [],
|
|
})
|
|
|
|
// Check if AI placeholder comments were NOT created here (moved to workflow)
|
|
const comments = await prisma.assetComment.findMany({
|
|
where: { assetId: file.id, sessionId: { not: null } },
|
|
})
|
|
expect(comments.length).toBe(0)
|
|
|
|
// Check if workflow tasks were created
|
|
const tasks = await prisma.workflowTask.findMany({
|
|
where: { assetId: file.id, type: 'chat' },
|
|
})
|
|
expect(tasks.length).toBe(2)
|
|
expect(tasks.some((t) => t.payload?.agent?.agentId === agent.id)).toBe(true)
|
|
expect(tasks.some((t) => t.payload?.agent?.agentId === 'default')).toBe(true)
|
|
})
|
|
|
|
it('implements agent comment interaction rules (Rule 1, 2, 3)', async () => {
|
|
const { user, project } = await setupBasicAssets()
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'rules-file',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const botUser = await prisma.user.create({
|
|
data: {
|
|
name: 'Bot Agent',
|
|
type: 'agent',
|
|
email: 'rule-bot@shumai.ai',
|
|
},
|
|
})
|
|
const agent = await prisma.agent.create({
|
|
data: {
|
|
id: botUser.id,
|
|
teamId: project.teamId,
|
|
type: 'chat',
|
|
config: {
|
|
provider: 'google',
|
|
model: 'gemini-pro',
|
|
},
|
|
},
|
|
})
|
|
|
|
// Rule 1: user mentions agent in chat, and it's not in a reply
|
|
const c1 = await assetService.createComment({
|
|
assetId: file.id,
|
|
userId: user.id,
|
|
message: `Rule 1 test: Hello <@${agent.id}>`,
|
|
attachmentIds: [],
|
|
})
|
|
|
|
const tasksRule1 = await prisma.workflowTask.findMany({
|
|
where: { assetId: file.id, payload: { path: ['agent', 'userCommentId'], equals: c1.id } },
|
|
})
|
|
expect(tasksRule1.length).toBe(1)
|
|
const p1 = tasksRule1[0].payload
|
|
expect(p1?.agent?.agentId).toBe(agent.id)
|
|
expect(p1?.agent?.sessionId).toBeUndefined()
|
|
expect(p1?.agent?.userId).toBe(user.id)
|
|
|
|
// Rule 2: user mentions agent in reply, and root is not an agent comment
|
|
const reply1 = await assetService.createComment({
|
|
assetId: file.id,
|
|
userId: user.id,
|
|
message: `Rule 2 reply: <@${agent.id}>`,
|
|
replyToId: c1.id,
|
|
attachmentIds: [],
|
|
})
|
|
|
|
const tasksRule2 = await prisma.workflowTask.findMany({
|
|
where: { assetId: file.id, payload: { path: ['agent', 'userCommentId'], equals: reply1.id } },
|
|
})
|
|
expect(tasksRule2.length).toBe(1)
|
|
const p2 = tasksRule2[0].payload
|
|
expect(p2?.agent?.agentId).toBe(agent.id)
|
|
expect(p2?.agent?.sessionId).toBeUndefined()
|
|
expect(p2?.agent?.userId).toBe(user.id)
|
|
|
|
// Create a root user comment
|
|
const userRoot = await prisma.assetComment.create({
|
|
data: {
|
|
assetId: file.id,
|
|
creatorId: user.id,
|
|
message: 'User root comment',
|
|
},
|
|
})
|
|
|
|
// Create an agent session first for the foreign key constraint
|
|
await prisma.agentSession.create({
|
|
data: {
|
|
id: 'test-session-rule3',
|
|
agentId: botUser.id,
|
|
cwd: process.cwd(),
|
|
},
|
|
})
|
|
|
|
// Create an agent comment as a reply to userRoot with sessionId
|
|
const agentReply = await prisma.assetComment.create({
|
|
data: {
|
|
assetId: file.id,
|
|
creatorId: botUser.id,
|
|
message: 'Agent reply comment',
|
|
replyToId: userRoot.id,
|
|
sessionId: 'test-session-rule3',
|
|
},
|
|
})
|
|
|
|
// Rule 3a: any user creates a reply directly to the agent comment, no explicit mention -> NO task created
|
|
const reply3a = await assetService.createComment({
|
|
assetId: file.id,
|
|
userId: user.id,
|
|
message: 'Rule 3a: reply without mention',
|
|
replyToId: agentReply.id,
|
|
attachmentIds: [],
|
|
})
|
|
|
|
// Verify replyToId of reply3a was normalized to the root comment userRoot.id
|
|
const dbReply3a = await prisma.assetComment.findUnique({
|
|
where: { id: reply3a.id },
|
|
})
|
|
expect(dbReply3a?.replyToId).toBe(userRoot.id)
|
|
|
|
const tasksRule3a = await prisma.workflowTask.findMany({
|
|
where: {
|
|
assetId: file.id,
|
|
payload: { path: ['agent', 'userCommentId'], equals: reply3a.id },
|
|
},
|
|
})
|
|
expect(tasksRule3a.length).toBe(0)
|
|
|
|
// Rule 3b: any user creates a reply directly to the agent comment, explicitly mentions agent
|
|
const reply3b = await assetService.createComment({
|
|
assetId: file.id,
|
|
userId: user.id,
|
|
message: `Rule 3b: reply with mention <@${botUser.id}>`,
|
|
replyToId: agentReply.id,
|
|
attachmentIds: [],
|
|
})
|
|
|
|
// Verify replyToId of reply3b was normalized to the root comment userRoot.id
|
|
const dbReply3b = await prisma.assetComment.findUnique({
|
|
where: { id: reply3b.id },
|
|
})
|
|
expect(dbReply3b?.replyToId).toBe(userRoot.id)
|
|
|
|
const tasksRule3b = await prisma.workflowTask.findMany({
|
|
where: {
|
|
assetId: file.id,
|
|
payload: { path: ['agent', 'userCommentId'], equals: reply3b.id },
|
|
},
|
|
})
|
|
expect(tasksRule3b.length).toBe(1)
|
|
const p3b = tasksRule3b[0].payload
|
|
expect(p3b?.agent?.agentId).toBe(botUser.id)
|
|
expect(p3b?.agent?.sessionId).toBe('test-session-rule3')
|
|
expect(p3b?.agent?.userId).toBe(user.id)
|
|
})
|
|
|
|
it('silently skips triggering agent workflow when comment creator lacks permission', async () => {
|
|
const { project } = await setupBasicAssets()
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'perm-file',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
// Create owner-only bot agent
|
|
const ownerBotUser = await prisma.user.create({
|
|
data: {
|
|
name: 'Owner Bot Agent',
|
|
type: 'agent',
|
|
email: `owner-bot-${Date.now()}@shumai.ai`,
|
|
},
|
|
})
|
|
const ownerAgent = await prisma.agent.create({
|
|
data: {
|
|
id: ownerBotUser.id,
|
|
teamId: project.teamId,
|
|
type: 'chat',
|
|
permission: 'owner',
|
|
config: { provider: 'p', model: 'm' },
|
|
},
|
|
})
|
|
|
|
// Create a reviewer user
|
|
const reviewerUser = await prisma.user.create({
|
|
data: {
|
|
name: 'Reviewer Member',
|
|
email: `rev-comment-${Date.now()}@shumai.ai`,
|
|
password: 'pw',
|
|
},
|
|
})
|
|
await prisma.teamMember.create({
|
|
data: { teamId: project.teamId, userId: reviewerUser.id, role: 'reviewer' },
|
|
})
|
|
|
|
// Reviewer mentions owner-only agent
|
|
const comment = await assetService.createComment({
|
|
assetId: file.id,
|
|
userId: reviewerUser.id,
|
|
message: `Hello <@${ownerAgent.id}>`,
|
|
attachmentIds: [],
|
|
})
|
|
|
|
// Comment was saved successfully
|
|
expect(comment.id).toBeDefined()
|
|
|
|
// But no workflow task was created for ownerAgent
|
|
const tasks = await prisma.workflowTask.findMany({
|
|
where: {
|
|
assetId: file.id,
|
|
payload: { path: ['agent', 'userCommentId'], equals: comment.id },
|
|
},
|
|
})
|
|
expect(tasks.length).toBe(0)
|
|
})
|
|
|
|
it('uses the effective project role for bot mention permission checks', async () => {
|
|
const { project } = await setupBasicAssets()
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'perm-file-2',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
// Editor-permission chat agent
|
|
const editorBotUser = await prisma.user.create({
|
|
data: {
|
|
name: 'Editor Bot Agent',
|
|
type: 'agent',
|
|
email: `editor-bot-${Date.now()}@shumai.ai`,
|
|
},
|
|
})
|
|
const editorAgent = await prisma.agent.create({
|
|
data: {
|
|
id: editorBotUser.id,
|
|
teamId: project.teamId,
|
|
type: 'chat',
|
|
permission: 'editor',
|
|
config: { provider: 'p', model: 'm' },
|
|
},
|
|
})
|
|
|
|
// Team editor, demoted to project reviewer in this project
|
|
const demoted = await prisma.user.create({
|
|
data: { name: 'Demoted Member', email: `dem-${Date.now()}@shumai.ai`, password: 'pw' },
|
|
})
|
|
const demotedTm = await prisma.teamMember.create({
|
|
data: { teamId: project.teamId, userId: demoted.id, role: 'editor', scope: 'team' },
|
|
})
|
|
await prisma.projectMember.create({
|
|
data: { projectId: project.id, teamMemberId: demotedTm.id, role: 'reviewer' },
|
|
})
|
|
|
|
// Project-scoped member invited as editor
|
|
const projEditor = await prisma.user.create({
|
|
data: { name: 'Proj Editor', email: `pe-${Date.now()}@shumai.ai`, password: 'pw' },
|
|
})
|
|
const projEditorTm = await prisma.teamMember.create({
|
|
data: { teamId: project.teamId, userId: projEditor.id, role: 'reviewer', scope: 'project' },
|
|
})
|
|
await prisma.projectMember.create({
|
|
data: { projectId: project.id, teamMemberId: projEditorTm.id, role: 'editor' },
|
|
})
|
|
|
|
// Demoted team editor (project reviewer) mentioning the editor agent: no workflow
|
|
const demotedComment = await assetService.createComment({
|
|
assetId: file.id,
|
|
userId: demoted.id,
|
|
message: `Hello <@${editorAgent.id}>`,
|
|
attachmentIds: [],
|
|
})
|
|
const demotedTasks = await prisma.workflowTask.findMany({
|
|
where: {
|
|
assetId: file.id,
|
|
payload: { path: ['agent', 'userCommentId'], equals: demotedComment.id },
|
|
},
|
|
})
|
|
expect(demotedTasks.length).toBe(0)
|
|
|
|
// Project-scoped editor mentioning the editor agent: workflow created
|
|
const peComment = await assetService.createComment({
|
|
assetId: file.id,
|
|
userId: projEditor.id,
|
|
message: `Hello <@${editorAgent.id}>`,
|
|
attachmentIds: [],
|
|
})
|
|
const peTasks = await prisma.workflowTask.findMany({
|
|
where: {
|
|
assetId: file.id,
|
|
payload: { path: ['agent', 'userCommentId'], equals: peComment.id },
|
|
},
|
|
})
|
|
expect(peTasks.length).toBe(1)
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
expect((peTasks[0].payload as any)?.agent?.userId).toBe(projEditor.id)
|
|
})
|
|
|
|
it('updates asset order correctly', async () => {
|
|
const { user, project } = await setupBasicAssets()
|
|
|
|
await prisma.asset.create({
|
|
data: {
|
|
name: 'A',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sortIndex: 'a0',
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const assetB = await prisma.asset.create({
|
|
data: {
|
|
name: 'B',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sortIndex: 'a1',
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const assetC = await prisma.asset.create({
|
|
data: {
|
|
name: 'C',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sortIndex: 'a2',
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
// Move C before B
|
|
await assetService.updateAssetOrder(assetC.id, {
|
|
beforeIndex: assetB.sortIndex!,
|
|
})
|
|
|
|
const updatedC = await prisma.asset.findUnique({ where: { id: assetC.id } })
|
|
expect(updatedC?.sortIndex && updatedC.sortIndex > 'a0').toBe(true)
|
|
expect(updatedC?.sortIndex && updatedC.sortIndex < 'a1').toBe(true)
|
|
})
|
|
|
|
it('updates asset order at the end', async () => {
|
|
const { user, project } = await setupBasicAssets()
|
|
|
|
await prisma.asset.create({
|
|
data: {
|
|
name: 'A',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sortIndex: 'a0',
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const assetB = await prisma.asset.create({
|
|
data: {
|
|
name: 'B',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sortIndex: 'a1',
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
// Move A after B
|
|
const assetA = await prisma.asset.findFirst({ where: { name: 'A' } })
|
|
await assetService.updateAssetOrder(assetA!.id, {
|
|
afterIndex: assetB.sortIndex!,
|
|
})
|
|
|
|
const updatedA = await prisma.asset.findUnique({ where: { id: assetA!.id } })
|
|
expect(updatedA?.sortIndex && updatedA.sortIndex > 'a1').toBe(true)
|
|
})
|
|
|
|
it('verifies that sortIndex uses C collation to prevent fractional indexing mismatch', async () => {
|
|
const { user, project } = await setupBasicAssets()
|
|
|
|
// Create a parent folder to contain our assets
|
|
const folder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Parent Folder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
// Create Asset A: In ASCII, "YjnsD0vZ" > "YjNuyGpV" because "n" (110) > "N" (78).
|
|
// In default Postgres locale collation, "YjnsD0vZ" < "YjNuyGpV".
|
|
const assetA = await prisma.asset.create({
|
|
data: {
|
|
name: 'Asset A',
|
|
type: AssetType.file,
|
|
parentId: folder.id,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sortIndex: 'YjnsD0vZ',
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
// Create Asset B
|
|
const assetB = await prisma.asset.create({
|
|
data: {
|
|
name: 'Asset B',
|
|
type: AssetType.file,
|
|
parentId: folder.id,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sortIndex: 'YjNuyGpV',
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
// Create Asset C
|
|
const assetC = await prisma.asset.create({
|
|
data: {
|
|
name: 'Asset C',
|
|
type: AssetType.file,
|
|
parentId: folder.id,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sortIndex: 'Yjzaaa',
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
// Verify ASCII ordering in JavaScript: assetA.sortIndex > assetB.sortIndex
|
|
expect(assetA.sortIndex! > assetB.sortIndex!).toBe(true)
|
|
|
|
// Verify Postgres sorting behavior:
|
|
// With C collation, it must follow ASCII ordering. Therefore, searching for
|
|
// sortIndex < assetB.sortIndex must NOT find assetA.
|
|
const prevAsset = await prisma.asset.findFirst({
|
|
where: {
|
|
parentId: folder.id,
|
|
sortIndex: { lt: assetB.sortIndex! },
|
|
id: { not: assetC.id },
|
|
},
|
|
orderBy: { sortIndex: 'desc' },
|
|
})
|
|
|
|
// Assert that the database collation is correctly "C" (returns null for prevAsset)
|
|
expect(prevAsset).toBeNull()
|
|
|
|
// Assert that updateAssetOrder succeeds without throwing a collation error
|
|
const updatedC = await assetService.updateAssetOrder(assetC.id, {
|
|
beforeIndex: assetB.sortIndex!,
|
|
})
|
|
expect(updatedC.sortIndex).toBeDefined()
|
|
expect(updatedC.sortIndex! < assetB.sortIndex!).toBe(true)
|
|
})
|
|
|
|
it('correctly bounds the new sortIndex when moving an asset after another asset in the middle of the list', async () => {
|
|
const { user, project } = await setupBasicAssets()
|
|
|
|
// Create three assets: A, B, C with specific sort indexes
|
|
const assetA = await prisma.asset.create({
|
|
data: {
|
|
name: 'Asset A',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sortIndex: 'a0',
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const assetB = await prisma.asset.create({
|
|
data: {
|
|
name: 'Asset B',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sortIndex: 'a1',
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const assetC = await prisma.asset.create({
|
|
data: {
|
|
name: 'Asset C',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sortIndex: 'a11',
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
// Move Asset A to be after Asset B (it should be placed between B ("a1") and C ("a11"))
|
|
const updatedA = await assetService.updateAssetOrder(assetA.id, {
|
|
afterIndex: assetB.sortIndex!,
|
|
})
|
|
|
|
// Verify it is placed after B
|
|
expect(updatedA.sortIndex! > assetB.sortIndex!).toBe(true)
|
|
// Verify it is placed before C (this fails under the buggy code because C's bound is ignored, generating a key greater than "a11")
|
|
expect(updatedA.sortIndex! < assetC.sortIndex!).toBe(true)
|
|
})
|
|
|
|
describe('Preview', () => {
|
|
it('toPreviewInfo includes video duration from media metadata', async () => {
|
|
const { project, user } = await setupBasicAssets()
|
|
|
|
const video = await prisma.asset.create({
|
|
data: {
|
|
name: 'clip.mp4',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sizeByte: 123456,
|
|
mediaType: 'video/mp4',
|
|
status: 'processed',
|
|
media: {
|
|
proxyType: 'video',
|
|
poster: { key: 'poster-key' },
|
|
metadata: { duration: 262 },
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
} as any,
|
|
},
|
|
})
|
|
|
|
const info = await assetService.getAsset({ assetId: video.id })
|
|
|
|
expect(info.preview).toBeDefined()
|
|
expect(info.preview?.proxyType).toBe('video')
|
|
expect(info.preview?.duration).toBe(262)
|
|
})
|
|
|
|
it('toPreviewInfo omits duration for images', async () => {
|
|
const { project, user } = await setupBasicAssets()
|
|
|
|
const image = await prisma.asset.create({
|
|
data: {
|
|
name: 'pic.png',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sizeByte: 5000,
|
|
mediaType: 'image/png',
|
|
status: 'processed',
|
|
media: {
|
|
proxyType: 'image',
|
|
thumbnail: { key: 'thumb-key' },
|
|
metadata: { originalWidth: 100, originalHeight: 80 },
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
} as any,
|
|
},
|
|
})
|
|
|
|
const info = await assetService.getAsset({ assetId: image.id })
|
|
|
|
expect(info.preview).toBeDefined()
|
|
expect(info.preview?.duration).toBeUndefined()
|
|
})
|
|
|
|
it('converts webp thumbnail to jpeg on demand when hasJpegPreview is false', async () => {
|
|
vi.mocked(ensureJpegInStorage).mockClear()
|
|
const { project, user } = await setupBasicAssets()
|
|
|
|
const image = await prisma.asset.create({
|
|
data: {
|
|
name: 'photo.webp',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sizeByte: 2000,
|
|
mediaType: 'image/webp',
|
|
status: 'processed',
|
|
hasJpegPreview: false,
|
|
media: {
|
|
proxyType: 'image',
|
|
thumbnail: { key: 'shumai/files/1/thumb.webp' },
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
} as any,
|
|
},
|
|
})
|
|
|
|
const preview = await assetService.toPreviewInfo(image, 'jpeg')
|
|
|
|
expect(preview).toBeDefined()
|
|
expect(ensureJpegInStorage).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
'shumai/files/1/thumb.webp',
|
|
'shumai/files/1/thumb.jpeg',
|
|
)
|
|
expect(s3Service.presign).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
'shumai/files/1/thumb.jpeg',
|
|
'GET',
|
|
)
|
|
|
|
const updated = await prisma.asset.findUnique({ where: { id: image.id } })
|
|
expect(updated?.hasJpegPreview).toBe(true)
|
|
})
|
|
|
|
it('skips conversion when hasJpegPreview is already true', async () => {
|
|
vi.mocked(ensureJpegInStorage).mockClear()
|
|
const { project, user } = await setupBasicAssets()
|
|
|
|
const image = await prisma.asset.create({
|
|
data: {
|
|
name: 'photo-converted.webp',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sizeByte: 2000,
|
|
mediaType: 'image/webp',
|
|
status: 'processed',
|
|
hasJpegPreview: true,
|
|
media: {
|
|
proxyType: 'image',
|
|
thumbnail: { key: 'shumai/files/2/thumb.webp' },
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
} as any,
|
|
},
|
|
})
|
|
|
|
const preview = await assetService.toPreviewInfo(image, 'jpeg')
|
|
|
|
expect(preview).toBeDefined()
|
|
expect(ensureJpegInStorage).not.toHaveBeenCalled()
|
|
expect(s3Service.presign).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
'shumai/files/2/thumb.jpeg',
|
|
'GET',
|
|
)
|
|
})
|
|
|
|
it('gracefully falls back to webp when conversion fails', async () => {
|
|
vi.mocked(ensureJpegInStorage).mockRejectedValueOnce(new Error('Sharp conversion failed'))
|
|
const { project, user } = await setupBasicAssets()
|
|
|
|
const image = await prisma.asset.create({
|
|
data: {
|
|
name: 'failing.webp',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sizeByte: 2000,
|
|
mediaType: 'image/webp',
|
|
status: 'processed',
|
|
hasJpegPreview: false,
|
|
media: {
|
|
proxyType: 'image',
|
|
thumbnail: { key: 'shumai/files/3/thumb.webp' },
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
} as any,
|
|
},
|
|
})
|
|
|
|
const preview = await assetService.toPreviewInfo(image, 'jpeg')
|
|
|
|
expect(preview).toBeDefined()
|
|
expect(s3Service.presign).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
'shumai/files/3/thumb.webp',
|
|
'GET',
|
|
)
|
|
|
|
const updated = await prisma.asset.findUnique({ where: { id: image.id } })
|
|
expect(updated?.hasJpegPreview).toBe(false)
|
|
})
|
|
|
|
it('listAssetsByIds forwards previewFormat and converts preview', async () => {
|
|
vi.mocked(ensureJpegInStorage).mockClear()
|
|
const { project, user } = await setupBasicAssets()
|
|
|
|
const image = await prisma.asset.create({
|
|
data: {
|
|
name: 'list-photo.webp',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sizeByte: 2000,
|
|
mediaType: 'image/webp',
|
|
status: 'processed',
|
|
hasJpegPreview: false,
|
|
media: {
|
|
proxyType: 'image',
|
|
thumbnail: { key: 'shumai/files/4/thumb.webp' },
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
} as any,
|
|
},
|
|
})
|
|
|
|
const list = await assetService.listAssetsByIds([image.id], 'jpeg')
|
|
|
|
expect(list).toHaveLength(1)
|
|
expect(ensureJpegInStorage).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
'shumai/files/4/thumb.webp',
|
|
'shumai/files/4/thumb.jpeg',
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('Symlinks', () => {
|
|
it('toAssetInfo resolves symlink target metadata', async () => {
|
|
const { project, user } = await setupBasicAssets()
|
|
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'original.png',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
sizeByte: 5000,
|
|
mediaType: 'image/png',
|
|
status: 'processed',
|
|
media: {
|
|
proxyType: 'image',
|
|
thumbnail: { key: 'thumb-key' },
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
} as any,
|
|
},
|
|
})
|
|
|
|
const symlink = await prisma.asset.create({
|
|
data: {
|
|
name: 'link.png',
|
|
type: AssetType.symlink,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
targetId: file.id,
|
|
status: 'processed',
|
|
},
|
|
})
|
|
|
|
const info = await assetService.getAsset({ assetId: symlink.id })
|
|
|
|
expect(info.id).toBe(symlink.id)
|
|
expect(info.name).toBe('link.png')
|
|
expect(info.type).toBe(AssetType.symlink)
|
|
// Resolved from target
|
|
expect(info.sizeByte).toBe(5000)
|
|
expect(info.proxyType).toBe('image')
|
|
expect(info.media).toBeDefined()
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
expect((info.media as any).thumbnail?.key).toBe('thumb-key')
|
|
})
|
|
|
|
it('listChildren includes symlinks by target type', async () => {
|
|
const { project } = await setupBasicAssets()
|
|
|
|
const folder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Shared Folder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: 'processed',
|
|
},
|
|
})
|
|
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'target.txt',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
status: 'processed',
|
|
},
|
|
})
|
|
|
|
await prisma.asset.create({
|
|
data: {
|
|
name: 'symlink.txt',
|
|
type: AssetType.symlink,
|
|
projectId: project.id,
|
|
parentId: folder.id,
|
|
targetId: file.id,
|
|
status: 'processed',
|
|
},
|
|
})
|
|
|
|
const res = await assetService.listChildren({
|
|
assetId: folder.id,
|
|
assetType: 'file',
|
|
})
|
|
|
|
expect(res.data).toHaveLength(1)
|
|
expect(res.data[0].type).toBe(AssetType.symlink)
|
|
expect(res.data[0].name).toBe('symlink.txt')
|
|
})
|
|
|
|
it('populates versionStack and latestVersion correctly when files are reparented', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
|
})
|
|
|
|
const parentFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Parent Folder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const fileA = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileA.txt',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: parentFolder.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
sizeByte: 100,
|
|
sortIndex: 'abc',
|
|
},
|
|
})
|
|
|
|
const fileB = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileB.txt',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: parentFolder.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
sizeByte: 200,
|
|
},
|
|
})
|
|
|
|
// Reparent fileB onto fileA to create a version stack
|
|
await assetService.reparentAssets({
|
|
creatorId: user.id,
|
|
assetIds: [fileB.id],
|
|
newParentId: fileA.id,
|
|
})
|
|
|
|
// Now list children of the parentFolder
|
|
const res = await assetService.listChildren({
|
|
assetId: parentFolder.id,
|
|
assetType: 'file',
|
|
})
|
|
|
|
expect(res.data).toHaveLength(1)
|
|
const stackAsset = res.data[0]
|
|
expect(stackAsset.type).toBe(AssetType.version_stack)
|
|
expect(stackAsset.sortIndex).toBe('abc')
|
|
expect(stackAsset.versionStack).toBeDefined()
|
|
expect(stackAsset.versionStack?.versions).toHaveLength(2)
|
|
const versions = stackAsset.versionStack!.versions
|
|
// Index 0 must be fileB (latest version) since it has lower sortIndex
|
|
expect(versions[0].id).toBe(fileB.id)
|
|
expect(versions[0].version).toBe(2)
|
|
expect(versions[0].current).toBe(true)
|
|
|
|
// Index 1 must be fileA (older version) since it has higher sortIndex
|
|
expect(versions[1].id).toBe(fileA.id)
|
|
expect(versions[1].version).toBe(1)
|
|
expect(versions[1].current).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('getAsset', () => {
|
|
it('does not include version_stack in ancestorFolders', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
|
})
|
|
|
|
const parentFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Parent Folder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const fileA = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileA.txt',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: parentFolder.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
sizeByte: 100,
|
|
},
|
|
})
|
|
|
|
const fileB = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileB.txt',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: parentFolder.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
sizeByte: 200,
|
|
},
|
|
})
|
|
|
|
// Reparent fileB onto fileA to create a version stack
|
|
await assetService.reparentAssets({
|
|
creatorId: user.id,
|
|
assetIds: [fileB.id],
|
|
newParentId: fileA.id,
|
|
})
|
|
|
|
// Now get stack info
|
|
const stack = await prisma.asset.findFirst({
|
|
where: { parentId: parentFolder.id, type: AssetType.version_stack },
|
|
})
|
|
expect(stack).toBeDefined()
|
|
|
|
// Fetch details of version fileB
|
|
const info = await assetService.getAsset({ assetId: fileB.id })
|
|
|
|
// Verify that ancestorFolders contains the parent folder but NOT the version stack
|
|
expect(info.ancestorFolders).toBeDefined()
|
|
const containsStack = info.ancestorFolders?.some((f) => f.id === stack!.id)
|
|
expect(containsStack).toBe(false)
|
|
expect(info.ancestorFolders?.find((f) => f.id === parentFolder.id)).toBeDefined()
|
|
})
|
|
})
|
|
|
|
describe('getStackVersions', () => {
|
|
it('returns all versions in a stack ordered by sortIndex asc', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
|
})
|
|
|
|
const stack = await prisma.asset.create({
|
|
data: {
|
|
name: 'stack',
|
|
type: AssetType.version_stack,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
|
|
const file1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'version1.txt',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: stack.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
sortIndex: 'a1', // older version has higher sortIndex
|
|
},
|
|
})
|
|
|
|
const file2 = await prisma.asset.create({
|
|
data: {
|
|
name: 'version2.txt',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: stack.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
sortIndex: 'a0', // newer/latest version has lower sortIndex
|
|
},
|
|
})
|
|
|
|
const versions = await assetService.getStackVersions(stack.id)
|
|
|
|
expect(versions).toHaveLength(2)
|
|
expect(versions[0].id).toBe(file2.id) // newer version must be first
|
|
expect(versions[0].version).toBe(2)
|
|
expect(versions[0].name).toBe('version2.txt')
|
|
expect(versions[0].creator?.id).toBe(user.id)
|
|
expect(versions[0].creator?.name).toBe('Test User')
|
|
|
|
expect(versions[1].id).toBe(file1.id) // older version must be second
|
|
expect(versions[1].version).toBe(1)
|
|
expect(versions[1].name).toBe('version1.txt')
|
|
})
|
|
})
|
|
|
|
describe('Asset Cascade Bug (Reproduction)', () => {
|
|
it('should NOT cascade delete children from DB before their S3 files are deleted', async () => {
|
|
const { project } = await setupBasicAssets()
|
|
const { s3Service } = await import('../s3/s3')
|
|
|
|
// 1. Create Folder A
|
|
const folderA = await prisma.asset.create({
|
|
data: {
|
|
name: 'Folder A',
|
|
type: AssetType.folder,
|
|
status: 'pending_purge',
|
|
isDeleted: true,
|
|
projectId: project.id,
|
|
},
|
|
})
|
|
|
|
// 2. Create 101 children files for Folder A
|
|
// Each has an S3 key.
|
|
const childCount = 101
|
|
const childData = Array.from({ length: childCount }).map((_, i) => ({
|
|
name: `File ${i}`,
|
|
type: AssetType.file,
|
|
status: AssetStatus.pending_purge,
|
|
isDeleted: true,
|
|
project: { connect: { id: project.id } },
|
|
parent: { connect: { id: folderA.id } },
|
|
storageKey: {
|
|
create: { key: `key-${i}` },
|
|
},
|
|
}))
|
|
|
|
for (const data of childData) {
|
|
const asset = await prisma.asset.create({
|
|
data: data as unknown as Prisma.AssetCreateInput,
|
|
include: { storageKey: true },
|
|
})
|
|
await prisma.storageKey.update({
|
|
where: { id: asset.storageKeyId! },
|
|
data: { createdAt: new Date(Date.now() - 25 * 60 * 60 * 1000) },
|
|
})
|
|
}
|
|
|
|
// 3. Trigger the purge job
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
await (assetService as any).purgePendingAssets()
|
|
|
|
// 6. Trigger GC job to physically delete files
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
await (assetService as any).purgeUnreferencedStorageKeys()
|
|
// Call it again to process the remaining items (batch size is 100)
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
await (assetService as any).purgeUnreferencedStorageKeys()
|
|
|
|
// 6. Verify S3 deletion was called for EVERY child
|
|
for (let i = 0; i < childCount; i++) {
|
|
expect(s3Service.deleteObject).toHaveBeenCalledWith(expect.any(String), `key-${i}`)
|
|
}
|
|
|
|
// Verify folder and all children are gone from DB
|
|
expect(await prisma.asset.findUnique({ where: { id: folderA.id } })).toBeNull()
|
|
const remainingChildren = await prisma.asset.count({ where: { parentId: folderA.id } })
|
|
expect(remainingChildren).toBe(0)
|
|
}, 20000)
|
|
|
|
it('should delete the entire directory prefix for assets with complex keys (e.g., files/ULID/raw)', async () => {
|
|
const { project } = await setupBasicAssets()
|
|
const { s3Service } = await import('../s3/s3')
|
|
|
|
// Create an asset with a complex key
|
|
const complexFile = await prisma.asset.create({
|
|
data: {
|
|
name: 'Complex File',
|
|
type: AssetType.file,
|
|
status: AssetStatus.pending_purge,
|
|
isDeleted: true,
|
|
project: { connect: { id: project.id } },
|
|
storageKey: {
|
|
create: {
|
|
key: 'files/01KSBRJVY3DPK111S2MECFKDQ4/raw',
|
|
createdAt: new Date(Date.now() - 25 * 60 * 60 * 1000),
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
// 1. Purge the asset record
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
await (assetService as any).purgePendingAssets()
|
|
|
|
// 2. Trigger GC job
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
await (assetService as any).purgeUnreferencedStorageKeys()
|
|
|
|
// Verify deletePrefix was called with the correct prefix instead of just deleteObject
|
|
expect(s3Service.deletePrefix).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
'files/01KSBRJVY3DPK111S2MECFKDQ4/',
|
|
)
|
|
|
|
// Verify record is gone
|
|
expect(await prisma.asset.findUnique({ where: { id: complexFile.id } })).toBeNull()
|
|
})
|
|
|
|
it('should set AgentSession.assetId to null when asset is purged, preserving session and session entries', async () => {
|
|
const { project } = await setupBasicAssets()
|
|
|
|
const agentUser = await prisma.user.create({
|
|
data: {
|
|
name: 'Test Agent',
|
|
email: `test-agent-${Date.now()}@example.com`,
|
|
type: 'agent',
|
|
},
|
|
})
|
|
const agent = await prisma.agent.create({
|
|
data: {
|
|
id: agentUser.id,
|
|
teamId: project.teamId,
|
|
type: 'chat',
|
|
config: { provider: 'test', model: 'test' },
|
|
},
|
|
})
|
|
|
|
const asset = await prisma.asset.create({
|
|
data: {
|
|
name: 'Asset With Session',
|
|
type: AssetType.file,
|
|
status: AssetStatus.pending_purge,
|
|
isDeleted: true,
|
|
project: { connect: { id: project.id } },
|
|
},
|
|
})
|
|
|
|
const session = await prisma.agentSession.create({
|
|
data: {
|
|
agentId: agent.id,
|
|
cwd: '/',
|
|
assetId: asset.id,
|
|
type: 'comment',
|
|
},
|
|
})
|
|
|
|
const entry = await prisma.agentSessionEntry.create({
|
|
data: {
|
|
id: `entry-${Date.now()}`,
|
|
sessionId: session.id,
|
|
assetId: asset.id,
|
|
type: 'message',
|
|
data: { content: 'test message' },
|
|
},
|
|
})
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
await (assetService as any).purgePendingAssets()
|
|
|
|
expect(await prisma.asset.findUnique({ where: { id: asset.id } })).toBeNull()
|
|
|
|
const updatedSession = await prisma.agentSession.findUnique({
|
|
where: { id: session.id },
|
|
})
|
|
expect(updatedSession).not.toBeNull()
|
|
expect(updatedSession?.assetId).toBeNull()
|
|
|
|
const updatedEntry = await prisma.agentSessionEntry.findUnique({
|
|
where: { id: entry.id },
|
|
})
|
|
expect(updatedEntry).not.toBeNull()
|
|
expect(updatedEntry?.sessionId).toBe(session.id)
|
|
})
|
|
})
|
|
|
|
describe('listChildren with showDeleted', () => {
|
|
const createImageFile = async (params: {
|
|
name: string
|
|
projectId: string
|
|
parentId: string
|
|
creatorId: string
|
|
thumbnailKey: string
|
|
duration?: number
|
|
sortIndex?: string
|
|
}) => {
|
|
return prisma.asset.create({
|
|
data: {
|
|
name: params.name,
|
|
type: AssetType.file,
|
|
status: AssetStatus.processed,
|
|
isDeleted: true,
|
|
projectId: params.projectId,
|
|
parentId: params.parentId,
|
|
creatorId: params.creatorId,
|
|
sizeByte: 100,
|
|
sortIndex: params.sortIndex,
|
|
mediaType: 'image/png',
|
|
media: {
|
|
duration: 0,
|
|
filesize: 0,
|
|
frames: 0,
|
|
imageTranscodes: [],
|
|
videoTranscodes: [],
|
|
finishedAt: new Date(0).toISOString(),
|
|
metadata:
|
|
params.duration === undefined
|
|
? null
|
|
: {
|
|
originalHeight: 0,
|
|
originalWidth: 0,
|
|
hasAudio: false,
|
|
duration: params.duration,
|
|
bitRate: 0,
|
|
frameRate: 0,
|
|
totalFrames: 0,
|
|
startTimecode: '00:00:00:00',
|
|
format: null,
|
|
},
|
|
original: null,
|
|
proxyType: 'image',
|
|
thumbnail: {
|
|
key: params.thumbnailKey,
|
|
width: 0,
|
|
height: 0,
|
|
quality: 0,
|
|
format: 'png',
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
it('returns previews for children of a folder trashed via deleteAssets', async () => {
|
|
const { project, user, assets } = await setupBasicAssets()
|
|
|
|
const folder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Trashed Folder',
|
|
type: AssetType.folder,
|
|
status: AssetStatus.uploaded,
|
|
projectId: project.id,
|
|
parentId: assets.root.id,
|
|
creatorId: user.id,
|
|
},
|
|
})
|
|
|
|
await createImageFile({
|
|
name: 'inside-1.png',
|
|
projectId: project.id,
|
|
parentId: folder.id,
|
|
creatorId: user.id,
|
|
thumbnailKey: 'thumb-1',
|
|
})
|
|
await createImageFile({
|
|
name: 'inside-2.png',
|
|
projectId: project.id,
|
|
parentId: folder.id,
|
|
creatorId: user.id,
|
|
thumbnailKey: 'thumb-2',
|
|
})
|
|
|
|
await assetService.deleteAssets([folder.id])
|
|
|
|
const result = await assetService.listChildren({
|
|
projectId: project.id,
|
|
showDeleted: true,
|
|
assetType: 'folder',
|
|
})
|
|
|
|
expect(result.data).toHaveLength(1)
|
|
expect(result.data[0].id).toBe(folder.id)
|
|
expect(result.data[0].deletedAt).not.toBeNull()
|
|
expect(result.data[0].latestChildren).toHaveLength(2)
|
|
expect(result.data[0].latestChildren?.every((c) => c.preview?.thumbnailUrl)).toBe(true)
|
|
})
|
|
|
|
it('resolves the latest deleted version for a trashed version stack child', async () => {
|
|
const { project, user, assets } = await setupBasicAssets()
|
|
|
|
const folder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Trashed Folder With Stack',
|
|
type: AssetType.folder,
|
|
status: AssetStatus.uploaded,
|
|
projectId: project.id,
|
|
parentId: assets.root.id,
|
|
creatorId: user.id,
|
|
},
|
|
})
|
|
|
|
const stack = await prisma.asset.create({
|
|
data: {
|
|
name: 'clip',
|
|
type: AssetType.version_stack,
|
|
status: AssetStatus.processed,
|
|
projectId: project.id,
|
|
parentId: folder.id,
|
|
creatorId: user.id,
|
|
},
|
|
})
|
|
|
|
await createImageFile({
|
|
name: 'clip-v1.png',
|
|
projectId: project.id,
|
|
parentId: stack.id,
|
|
creatorId: user.id,
|
|
thumbnailKey: 'v1-thumb',
|
|
duration: 11,
|
|
sortIndex: 'a1',
|
|
})
|
|
await createImageFile({
|
|
name: 'clip-v2.png',
|
|
projectId: project.id,
|
|
parentId: stack.id,
|
|
creatorId: user.id,
|
|
thumbnailKey: 'v2-thumb',
|
|
duration: 42,
|
|
sortIndex: 'a0',
|
|
})
|
|
|
|
await assetService.deleteAssets([folder.id])
|
|
|
|
const result = await assetService.listChildren({
|
|
projectId: project.id,
|
|
showDeleted: true,
|
|
assetType: 'folder',
|
|
})
|
|
|
|
const previews = result.data[0].latestChildren ?? []
|
|
expect(previews).toHaveLength(1)
|
|
// The latest version (lowest sortIndex) must be the one used for the preview
|
|
expect(previews[0].preview?.duration).toBe(42)
|
|
expect(previews[0].preview?.thumbnailUrl).toBeDefined()
|
|
})
|
|
|
|
it('still hides soft-deleted children when browsing a folder normally', async () => {
|
|
const { project, user, assets } = await setupBasicAssets()
|
|
|
|
const folder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Active Folder',
|
|
type: AssetType.folder,
|
|
status: AssetStatus.uploaded,
|
|
projectId: project.id,
|
|
parentId: assets.root.id,
|
|
creatorId: user.id,
|
|
},
|
|
})
|
|
|
|
await prisma.asset.create({
|
|
data: {
|
|
name: 'active.png',
|
|
type: AssetType.file,
|
|
status: AssetStatus.processed,
|
|
isDeleted: false,
|
|
projectId: project.id,
|
|
parentId: folder.id,
|
|
creatorId: user.id,
|
|
},
|
|
})
|
|
await prisma.asset.create({
|
|
data: {
|
|
name: 'deleted.png',
|
|
type: AssetType.file,
|
|
status: AssetStatus.processed,
|
|
isDeleted: true,
|
|
deletedAt: new Date(),
|
|
projectId: project.id,
|
|
parentId: folder.id,
|
|
creatorId: user.id,
|
|
},
|
|
})
|
|
|
|
const folderInfo = await assetService.getAsset({ assetId: folder.id })
|
|
expect(folderInfo.latestChildren).toHaveLength(1)
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('AssetService — natural sort by name', () => {
|
|
setupTestDbHooks()
|
|
|
|
let assetService: AssetService
|
|
|
|
beforeEach(() => {
|
|
assetService = new AssetService()
|
|
})
|
|
|
|
const setupNaturalSortAssets = async (names: string[]) => {
|
|
const user = await prisma.user.create({
|
|
data: {
|
|
name: 'natural-sort-user',
|
|
email: `natural-sort-${Date.now()}@example.com`,
|
|
type: 'human',
|
|
},
|
|
})
|
|
const team = await prisma.team.create({ data: { name: 'natural-sort-team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'natural-sort-proj', teamId: team.id },
|
|
})
|
|
const root = await prisma.asset.create({
|
|
data: {
|
|
name: 'root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
},
|
|
})
|
|
// Create files in a shuffled order so we are not relying on insertion order
|
|
for (const name of names) {
|
|
await prisma.asset.create({
|
|
data: {
|
|
name,
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: root.id,
|
|
creatorId: user.id,
|
|
status: 'uploaded',
|
|
sizeByte: 0,
|
|
},
|
|
})
|
|
}
|
|
return { root }
|
|
}
|
|
|
|
it('sorts files with numeric suffixes in natural order (asc)', async () => {
|
|
// Insert in non-natural lexicographic order to prove the collation does the work
|
|
const { root } = await setupNaturalSortAssets(['file10', 'file2', 'file20', 'file1'])
|
|
|
|
const result = await assetService.listChildren({
|
|
assetId: root.id,
|
|
assetType: AssetType.file,
|
|
sort: 'name',
|
|
order: 'asc',
|
|
first: 20,
|
|
})
|
|
|
|
expect(result.data.map((a) => a.name)).toEqual(['file1', 'file2', 'file10', 'file20'])
|
|
})
|
|
|
|
it('sorts files with numeric suffixes in natural order (desc)', async () => {
|
|
const { root } = await setupNaturalSortAssets(['file10', 'file2', 'file20', 'file1'])
|
|
|
|
const result = await assetService.listChildren({
|
|
assetId: root.id,
|
|
assetType: AssetType.file,
|
|
sort: 'name',
|
|
order: 'desc',
|
|
first: 20,
|
|
})
|
|
|
|
expect(result.data.map((a) => a.name)).toEqual(['file20', 'file10', 'file2', 'file1'])
|
|
})
|
|
|
|
it('sorts mixed alpha-numeric names correctly', async () => {
|
|
const { root } = await setupNaturalSortAssets(['asset 100', 'asset 9', 'asset 10', 'asset 2'])
|
|
|
|
const result = await assetService.listChildren({
|
|
assetId: root.id,
|
|
assetType: AssetType.file,
|
|
sort: 'name',
|
|
order: 'asc',
|
|
first: 20,
|
|
})
|
|
|
|
expect(result.data.map((a) => a.name)).toEqual(['asset 2', 'asset 9', 'asset 10', 'asset 100'])
|
|
})
|
|
|
|
it('does not regress lexicographic ordering for purely alphabetical names', async () => {
|
|
const { root } = await setupNaturalSortAssets(['banana', 'apple', 'cherry', 'apricot'])
|
|
|
|
const result = await assetService.listChildren({
|
|
assetId: root.id,
|
|
assetType: AssetType.file,
|
|
sort: 'name',
|
|
order: 'asc',
|
|
first: 20,
|
|
})
|
|
|
|
expect(result.data.map((a) => a.name)).toEqual(['apple', 'apricot', 'banana', 'cherry'])
|
|
})
|
|
|
|
describe('getDownloadLinks', () => {
|
|
it('resolves folders, version stacks, and symlinks to correct latest files and urls', async () => {
|
|
const user = await prisma.user.create({
|
|
data: {
|
|
name: 'DownloadUser',
|
|
email: `download-${Date.now()}@example.com`,
|
|
},
|
|
})
|
|
const team = await prisma.team.create({ data: { name: 'DownloadTeam' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'DownloadProject', teamId: team.id },
|
|
})
|
|
|
|
// Create folder structure:
|
|
// - rootFolder (folder)
|
|
// - file1.txt (file)
|
|
// - subFolder (folder)
|
|
// - file2.txt (file)
|
|
// - stack (version_stack)
|
|
// - version1.txt (file, sortIndex = 'a1')
|
|
// - version2.txt (file, sortIndex = 'a0') -> latest
|
|
// - symlinkToFile (symlink -> file1.txt)
|
|
// - symlinkToStack (symlink -> stack)
|
|
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'rootFolder',
|
|
type: AssetType.folder,
|
|
project: { connect: { id: project.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const file1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'file1.txt',
|
|
type: AssetType.file,
|
|
project: { connect: { id: project.id } },
|
|
parent: { connect: { id: rootFolder.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
storageKey: { create: { key: 'keys/file1' } },
|
|
},
|
|
})
|
|
|
|
const subFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'subFolder',
|
|
type: AssetType.folder,
|
|
project: { connect: { id: project.id } },
|
|
parent: { connect: { id: rootFolder.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
await prisma.asset.create({
|
|
data: {
|
|
name: 'file2.txt',
|
|
type: AssetType.file,
|
|
project: { connect: { id: project.id } },
|
|
parent: { connect: { id: subFolder.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
storageKey: { create: { key: 'keys/file2' } },
|
|
},
|
|
})
|
|
|
|
const stack = await prisma.asset.create({
|
|
data: {
|
|
name: 'stack',
|
|
type: AssetType.version_stack,
|
|
project: { connect: { id: project.id } },
|
|
parent: { connect: { id: subFolder.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
await prisma.asset.create({
|
|
data: {
|
|
name: 'version1.txt',
|
|
type: AssetType.file,
|
|
project: { connect: { id: project.id } },
|
|
parent: { connect: { id: stack.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
sortIndex: 'a1',
|
|
storageKey: { create: { key: 'keys/v1' } },
|
|
},
|
|
})
|
|
|
|
await prisma.asset.create({
|
|
data: {
|
|
name: 'version2.txt',
|
|
type: AssetType.file,
|
|
project: { connect: { id: project.id } },
|
|
parent: { connect: { id: stack.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
sortIndex: 'a0', // smaller sortIndex means latest
|
|
storageKey: { create: { key: 'keys/v2' } },
|
|
},
|
|
})
|
|
|
|
await prisma.asset.create({
|
|
data: {
|
|
name: 'symlinkToFile',
|
|
type: AssetType.symlink,
|
|
project: { connect: { id: project.id } },
|
|
parent: { connect: { id: rootFolder.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
target: { connect: { id: file1.id } },
|
|
},
|
|
})
|
|
|
|
await prisma.asset.create({
|
|
data: {
|
|
name: 'symlinkToStack',
|
|
type: AssetType.symlink,
|
|
project: { connect: { id: project.id } },
|
|
parent: { connect: { id: rootFolder.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
target: { connect: { id: stack.id } },
|
|
},
|
|
})
|
|
|
|
// Run download links request on rootFolder
|
|
const result = await assetService.getDownloadLinks([rootFolder.id])
|
|
|
|
// We expect:
|
|
// 1. file1.txt
|
|
// 2. file2.txt
|
|
// 3. version2.txt (latest version of the stack)
|
|
// 4. file1.txt (from symlinkToFile target resolution, deduplicated)
|
|
// 5. version2.txt (from symlinkToStack target resolution, deduplicated)
|
|
|
|
const fileNames = result.map((r) => r.name)
|
|
expect(fileNames).toHaveLength(3) // 3 unique files
|
|
expect(fileNames).toContain('file1.txt')
|
|
expect(fileNames).toContain('file2.txt')
|
|
expect(fileNames).toContain('version2.txt')
|
|
expect(fileNames).not.toContain('version1.txt') // older version should not be downloaded
|
|
expect(fileNames).not.toContain('rootFolder')
|
|
expect(fileNames).not.toContain('subFolder')
|
|
expect(fileNames).not.toContain('stack')
|
|
|
|
// Check urls are returned correctly (mock s3 presign resolves to 'http://mock-s3-url')
|
|
for (const item of result) {
|
|
expect(item.url).toBe('http://mock-s3-url')
|
|
}
|
|
|
|
// Renamed file names must be passed to presign so the download
|
|
// Content-Disposition header uses the asset name, not the storage key
|
|
for (const name of ['file1.txt', 'file2.txt', 'version2.txt']) {
|
|
expect(s3Service.presign).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
expect.any(String),
|
|
'GET',
|
|
true,
|
|
name,
|
|
)
|
|
}
|
|
})
|
|
|
|
it('resolves top-level symlinks to their targets when passed as direct starting IDs', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
|
})
|
|
|
|
const file1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'file1.txt',
|
|
type: AssetType.file,
|
|
project: { connect: { id: project.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
storageKey: { create: { key: 'keys/file1' } },
|
|
},
|
|
})
|
|
|
|
const symlink = await prisma.asset.create({
|
|
data: {
|
|
name: 'symlinkToFile',
|
|
type: AssetType.symlink,
|
|
project: { connect: { id: project.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
target: { connect: { id: file1.id } },
|
|
},
|
|
})
|
|
|
|
const result = await assetService.getDownloadLinks([symlink.id])
|
|
expect(result).toHaveLength(1)
|
|
expect(result[0].name).toBe('file1.txt')
|
|
expect(result[0].id).toBe(file1.id)
|
|
})
|
|
})
|
|
|
|
describe('getProjectIds', () => {
|
|
it('returns empty lists for empty inputs', async () => {
|
|
const result = await assetService.getProjectIds([])
|
|
expect(result).toEqual([])
|
|
})
|
|
|
|
it('resolves project IDs, including symlinks', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
|
})
|
|
|
|
const file1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'file1.txt',
|
|
type: AssetType.file,
|
|
project: { connect: { id: project.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const symlink = await prisma.asset.create({
|
|
data: {
|
|
name: 'symlink',
|
|
type: AssetType.symlink,
|
|
project: { connect: { id: project.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
target: { connect: { id: file1.id } },
|
|
},
|
|
})
|
|
|
|
const result = await assetService.getProjectIds([file1.id, symlink.id])
|
|
expect(result).toContain(project.id)
|
|
expect(result).toHaveLength(1)
|
|
})
|
|
})
|
|
|
|
describe('emptyTrash', () => {
|
|
it('should permanently delete trashed assets of the target project only', async () => {
|
|
const { s3Service } = await import('../s3/s3')
|
|
|
|
const team = await prisma.team.create({ data: { name: 'Empty Trash Team' } })
|
|
const project1 = await prisma.project.create({
|
|
data: { name: 'Project 1', teamId: team.id },
|
|
})
|
|
const project2 = await prisma.project.create({
|
|
data: { name: 'Project 2', teamId: team.id },
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
|
})
|
|
|
|
// 1. Create a trashed folder with a child file in Project 1
|
|
const folderA = await prisma.asset.create({
|
|
data: {
|
|
name: 'Folder A',
|
|
type: AssetType.folder,
|
|
status: 'trashed',
|
|
isDeleted: true,
|
|
project: { connect: { id: project1.id } },
|
|
creator: { connect: { id: user.id } },
|
|
deletedAt: new Date(),
|
|
},
|
|
})
|
|
|
|
const fileA1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'File A1',
|
|
type: AssetType.file,
|
|
status: 'uploaded',
|
|
isDeleted: true,
|
|
project: { connect: { id: project1.id } },
|
|
parent: { connect: { id: folderA.id } },
|
|
creator: { connect: { id: user.id } },
|
|
storageKey: {
|
|
create: {
|
|
key: 'files/project1-trashed/raw',
|
|
createdAt: new Date(Date.now() - 25 * 60 * 60 * 1000),
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
// 2. Create an active file in Project 1
|
|
const fileActive = await prisma.asset.create({
|
|
data: {
|
|
name: 'File Active',
|
|
type: AssetType.file,
|
|
status: 'uploaded',
|
|
isDeleted: false,
|
|
project: { connect: { id: project1.id } },
|
|
creator: { connect: { id: user.id } },
|
|
storageKey: {
|
|
create: {
|
|
key: 'files/project1-active/raw',
|
|
createdAt: new Date(Date.now() - 25 * 60 * 60 * 1000),
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
// 3. Create a trashed file in Project 2
|
|
const fileOtherTrashed = await prisma.asset.create({
|
|
data: {
|
|
name: 'File Other Trashed',
|
|
type: AssetType.file,
|
|
status: 'trashed',
|
|
isDeleted: true,
|
|
project: { connect: { id: project2.id } },
|
|
creator: { connect: { id: user.id } },
|
|
deletedAt: new Date(),
|
|
storageKey: {
|
|
create: {
|
|
key: 'files/project2-trashed/raw',
|
|
createdAt: new Date(Date.now() - 25 * 60 * 60 * 1000),
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
// Act
|
|
await assetService.emptyTrash(project1.id)
|
|
|
|
// Assert: Project 1 trashed assets are deleted from DB
|
|
expect(await prisma.asset.findUnique({ where: { id: folderA.id } })).toBeNull()
|
|
expect(await prisma.asset.findUnique({ where: { id: fileA1.id } })).toBeNull()
|
|
|
|
// Assert: Project 1 active assets are NOT deleted
|
|
expect(await prisma.asset.findUnique({ where: { id: fileActive.id } })).not.toBeNull()
|
|
|
|
// Assert: Project 2 trashed assets are NOT deleted
|
|
expect(await prisma.asset.findUnique({ where: { id: fileOtherTrashed.id } })).not.toBeNull()
|
|
|
|
// Assert: S3 deletion was called for Project 1's trashed file
|
|
expect(s3Service.deletePrefix).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
'files/project1-trashed/',
|
|
)
|
|
|
|
// Assert: S3 deletion was NOT called for Project 1's active file or Project 2's trashed file
|
|
expect(s3Service.deletePrefix).not.toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
'files/project1-active/',
|
|
)
|
|
expect(s3Service.deletePrefix).not.toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
'files/project2-trashed/',
|
|
)
|
|
})
|
|
|
|
it('should do nothing and not throw when there are no trashed assets', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Empty Test Team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Empty Test Project', teamId: team.id },
|
|
})
|
|
|
|
await expect(assetService.emptyTrash(project.id)).resolves.not.toThrow()
|
|
})
|
|
|
|
it('should purge trashed assets whose status was processed, cancel their in-flight tasks, and delete WorkflowTasks', async () => {
|
|
const cancelSpy = vi.spyOn(workflowService, 'cancel').mockResolvedValue(undefined)
|
|
|
|
const team = await prisma.team.create({ data: { name: 'Purge Task Team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Purge Task Project', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User Purge', email: `test-purge-${Date.now()}@example.com` },
|
|
})
|
|
|
|
// Asset was deleted, but status was set to 'processed' by background activity
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'skewed-status.mp4',
|
|
type: AssetType.file,
|
|
status: AssetStatus.processed,
|
|
isDeleted: true,
|
|
deletedAt: new Date(),
|
|
project: { connect: { id: project.id } },
|
|
parent: { connect: { id: rootFolder.id } },
|
|
creator: { connect: { id: user.id } },
|
|
storageKey: { create: { key: 'files/skewed-status/raw.mp4' } },
|
|
},
|
|
})
|
|
|
|
// In-flight task for this asset
|
|
const task = await prisma.workflowTask.create({
|
|
data: {
|
|
assetId: file.id,
|
|
type: WorkflowTaskType.transcode_video,
|
|
status: WorkflowTaskStatus.processing,
|
|
},
|
|
})
|
|
|
|
await assetService.emptyTrash(project.id)
|
|
|
|
// Asset should be deleted from DB
|
|
const assetInDb = await prisma.asset.findUnique({ where: { id: file.id } })
|
|
expect(assetInDb).toBeNull()
|
|
|
|
// Task should be cancelled
|
|
expect(cancelSpy).toHaveBeenCalledWith(task.id)
|
|
|
|
// WorkflowTask should be deleted from DB
|
|
const taskInDb = await prisma.workflowTask.findUnique({ where: { id: task.id } })
|
|
expect(taskInDb).toBeNull()
|
|
|
|
cancelSpy.mockRestore()
|
|
})
|
|
})
|
|
|
|
describe('expireTrashedAssets', () => {
|
|
it('should only purge root assets that have been in the trash for > 30 days and their descendants', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'GC Team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'GC Project', teamId: team.id },
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
|
})
|
|
|
|
const fortyDaysAgo = new Date(Date.now() - 40 * 24 * 60 * 60 * 1000)
|
|
const twentyDaysAgo = new Date(Date.now() - 20 * 24 * 60 * 60 * 1000)
|
|
|
|
// 1. Root expired folder (> 30 days)
|
|
const folderExpired = await prisma.asset.create({
|
|
data: {
|
|
name: 'Expired Folder',
|
|
type: AssetType.folder,
|
|
status: 'trashed',
|
|
isDeleted: true,
|
|
project: { connect: { id: project.id } },
|
|
creator: { connect: { id: user.id } },
|
|
deletedAt: fortyDaysAgo,
|
|
},
|
|
})
|
|
|
|
// Child of expired folder
|
|
const childOfExpired = await prisma.asset.create({
|
|
data: {
|
|
name: 'Child of Expired',
|
|
type: AssetType.file,
|
|
status: 'uploaded',
|
|
isDeleted: true,
|
|
project: { connect: { id: project.id } },
|
|
parent: { connect: { id: folderExpired.id } },
|
|
creator: { connect: { id: user.id } },
|
|
},
|
|
})
|
|
|
|
// 2. Root active folder (<= 30 days)
|
|
const folderActive = await prisma.asset.create({
|
|
data: {
|
|
name: 'Active Folder',
|
|
type: AssetType.folder,
|
|
status: 'trashed',
|
|
isDeleted: true,
|
|
project: { connect: { id: project.id } },
|
|
creator: { connect: { id: user.id } },
|
|
deletedAt: twentyDaysAgo,
|
|
},
|
|
})
|
|
|
|
// Child of active folder
|
|
const childOfActive = await prisma.asset.create({
|
|
data: {
|
|
name: 'Child of Active',
|
|
type: AssetType.file,
|
|
status: 'uploaded',
|
|
isDeleted: true,
|
|
project: { connect: { id: project.id } },
|
|
parent: { connect: { id: folderActive.id } },
|
|
creator: { connect: { id: user.id } },
|
|
},
|
|
})
|
|
|
|
// Act
|
|
await (
|
|
assetService as unknown as { expireTrashedAssets: () => Promise<void> }
|
|
).expireTrashedAssets()
|
|
|
|
// Assert
|
|
const expiredDb = await prisma.asset.findUnique({ where: { id: folderExpired.id } })
|
|
const childExpiredDb = await prisma.asset.findUnique({ where: { id: childOfExpired.id } })
|
|
const activeDb = await prisma.asset.findUnique({ where: { id: folderActive.id } })
|
|
const childActiveDb = await prisma.asset.findUnique({ where: { id: childOfActive.id } })
|
|
|
|
// The expired folder and its child should be marked as pending_purge
|
|
expect(expiredDb?.status).toBe('pending_purge')
|
|
expect(childExpiredDb?.status).toBe('pending_purge')
|
|
|
|
// The active folder and its child should remain 'trashed' and 'uploaded' respectively
|
|
expect(activeDb?.status).toBe('trashed')
|
|
expect(childActiveDb?.status).toBe('uploaded')
|
|
})
|
|
})
|
|
|
|
describe('getDownloadUrl', () => {
|
|
it('should return a presigned download URL for a key in the same directory', async () => {
|
|
const asset = await prisma.asset.create({
|
|
data: {
|
|
name: 'test.mp4',
|
|
type: AssetType.file,
|
|
status: AssetStatus.uploaded,
|
|
storageKey: {
|
|
create: { key: 'files/01TESTULID000000000000000/test.mp4' },
|
|
},
|
|
},
|
|
})
|
|
|
|
const url = await assetService.getDownloadUrl(
|
|
asset.id,
|
|
'files/01TESTULID000000000000000/test-720p.mp4',
|
|
)
|
|
|
|
expect(url).toBe('http://mock-s3-url')
|
|
expect(s3Service.presign).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
'files/01TESTULID000000000000000/test-720p.mp4',
|
|
'GET',
|
|
true,
|
|
'test-720p.mp4',
|
|
)
|
|
})
|
|
|
|
it('downloading video proxy for test.mov should use proxy filename with mp4 extension instead of test.mov', async () => {
|
|
const asset = await prisma.asset.create({
|
|
data: {
|
|
name: 'test.mov',
|
|
type: AssetType.file,
|
|
status: AssetStatus.uploaded,
|
|
storageKey: {
|
|
create: { key: 'files/01TESTULID000000000000000/test.mov' },
|
|
},
|
|
},
|
|
})
|
|
|
|
await assetService.getDownloadUrl(asset.id, 'files/01TESTULID000000000000000/test-1080p.mp4')
|
|
|
|
expect(s3Service.presign).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
'files/01TESTULID000000000000000/test-1080p.mp4',
|
|
'GET',
|
|
true,
|
|
'test-1080p.mp4',
|
|
)
|
|
})
|
|
|
|
it('downloading proxy for a renamed asset should preserve new stem with proxy resolution and extension', async () => {
|
|
const asset = await prisma.asset.create({
|
|
data: {
|
|
name: 'awesome.mov',
|
|
type: AssetType.file,
|
|
status: AssetStatus.uploaded,
|
|
storageKey: {
|
|
create: { key: 'files/01TESTULID000000000000000/test.mov' },
|
|
},
|
|
},
|
|
})
|
|
|
|
await assetService.getDownloadUrl(asset.id, 'files/01TESTULID000000000000000/test-1080p.mp4')
|
|
|
|
expect(s3Service.presign).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
'files/01TESTULID000000000000000/test-1080p.mp4',
|
|
'GET',
|
|
true,
|
|
'awesome-1080p.mp4',
|
|
)
|
|
})
|
|
|
|
it('should use the renamed asset name as the download filename', async () => {
|
|
const asset = await prisma.asset.create({
|
|
data: {
|
|
name: 'foo.png',
|
|
type: AssetType.file,
|
|
status: AssetStatus.uploaded,
|
|
storageKey: {
|
|
create: { key: 'files/01RENAMEDULID0000000000000/test.png' },
|
|
},
|
|
},
|
|
})
|
|
|
|
const url = await assetService.getDownloadUrl(
|
|
asset.id,
|
|
'files/01RENAMEDULID0000000000000/test.png',
|
|
)
|
|
|
|
expect(url).toBe('http://mock-s3-url')
|
|
expect(s3Service.presign).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
'files/01RENAMEDULID0000000000000/test.png',
|
|
'GET',
|
|
true,
|
|
'foo.png',
|
|
)
|
|
})
|
|
|
|
it('should return a presigned download URL for the original key', async () => {
|
|
const asset = await prisma.asset.create({
|
|
data: {
|
|
name: 'test.mp4',
|
|
type: AssetType.file,
|
|
status: AssetStatus.uploaded,
|
|
storageKey: {
|
|
create: { key: 'files/01TESTULID000000000000000/test.mp4' },
|
|
},
|
|
},
|
|
})
|
|
|
|
const url = await assetService.getDownloadUrl(
|
|
asset.id,
|
|
'files/01TESTULID000000000000000/test.mp4',
|
|
)
|
|
|
|
expect(url).toBe('http://mock-s3-url')
|
|
})
|
|
|
|
it('should throw if the key does not share the same parent directory', async () => {
|
|
const asset = await prisma.asset.create({
|
|
data: {
|
|
name: 'test.mp4',
|
|
type: AssetType.file,
|
|
status: AssetStatus.uploaded,
|
|
storageKey: {
|
|
create: { key: 'files/01TESTULID000000000000000/test.mp4' },
|
|
},
|
|
},
|
|
})
|
|
|
|
await expect(
|
|
assetService.getDownloadUrl(asset.id, 'files/01OTHERULIDXXXXXXXXXX/hack.mp4'),
|
|
).rejects.toThrow('Key does not belong to this asset')
|
|
})
|
|
|
|
it('should throw if the key contains path traversal segments', async () => {
|
|
const asset = await prisma.asset.create({
|
|
data: {
|
|
name: 'test.mp4',
|
|
type: AssetType.file,
|
|
status: AssetStatus.uploaded,
|
|
storageKey: {
|
|
create: { key: 'files/01TESTULID000000000000000/test.mp4' },
|
|
},
|
|
},
|
|
})
|
|
|
|
await expect(
|
|
assetService.getDownloadUrl(
|
|
asset.id,
|
|
'files/01TESTULID000000000000000/../01OTHERULIDXXXXXXXXXX/secret.mp4',
|
|
),
|
|
).rejects.toThrow('Key does not belong to this asset')
|
|
})
|
|
|
|
it('should throw if the asset does not exist', async () => {
|
|
await expect(
|
|
assetService.getDownloadUrl('nonexistent-id', 'files/01TESTULID000000000000000/test.mp4'),
|
|
).rejects.toThrow('Asset not found or has no storage key')
|
|
})
|
|
|
|
it('should throw if the asset has no storage key', async () => {
|
|
const asset = await prisma.asset.create({
|
|
data: {
|
|
name: 'folder',
|
|
type: AssetType.folder,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
await expect(
|
|
assetService.getDownloadUrl(asset.id, 'files/01TESTULID000000000000000/test.mp4'),
|
|
).rejects.toThrow('Asset not found or has no storage key')
|
|
})
|
|
|
|
it('should return a presigned download URL when given a symlink asset ID', async () => {
|
|
const realAsset = await prisma.asset.create({
|
|
data: {
|
|
name: 'fly.png',
|
|
type: AssetType.file,
|
|
status: AssetStatus.uploaded,
|
|
storageKey: {
|
|
create: { key: 'files/01KZ8VH3QHKQCSQDC5JPXCYT0R/fly.png' },
|
|
},
|
|
},
|
|
})
|
|
|
|
const symlinkAsset = await prisma.asset.create({
|
|
data: {
|
|
name: 'fly.png',
|
|
type: AssetType.symlink,
|
|
status: AssetStatus.uploaded,
|
|
targetId: realAsset.id,
|
|
},
|
|
})
|
|
|
|
const url = await assetService.getDownloadUrl(
|
|
symlinkAsset.id,
|
|
'files/01KZ8VH3QHKQCSQDC5JPXCYT0R/fly.png',
|
|
)
|
|
|
|
expect(url).toBe('http://mock-s3-url')
|
|
})
|
|
})
|
|
|
|
describe('resolveLatestVersionId', () => {
|
|
it('resolves the latest version ID of a version stack correctly (the one with the smallest sort index)', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
|
})
|
|
|
|
const stack = await prisma.asset.create({
|
|
data: {
|
|
name: 'stack',
|
|
type: AssetType.version_stack,
|
|
project: { connect: { id: project.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
// Oldest version (A): larger sort index
|
|
const fileA = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileA.mp4',
|
|
type: AssetType.file,
|
|
project: { connect: { id: project.id } },
|
|
parent: { connect: { id: stack.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
sortIndex: 'y',
|
|
},
|
|
})
|
|
|
|
// Latest version (B): smaller sort index
|
|
const fileB = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileB.mp4',
|
|
type: AssetType.file,
|
|
project: { connect: { id: project.id } },
|
|
parent: { connect: { id: stack.id } },
|
|
creator: { connect: { id: user.id } },
|
|
status: AssetStatus.uploaded,
|
|
sortIndex: 'x',
|
|
},
|
|
})
|
|
|
|
// Act: resolve latest version of the stack
|
|
const resolvedId = await assetService.resolveLatestVersionId(stack.id)
|
|
|
|
// Assert: should resolve to the latest version (fileB)
|
|
expect(resolvedId).toBe(fileB.id)
|
|
expect(resolvedId).not.toBe(fileA.id)
|
|
})
|
|
|
|
it('supports file and folder sizes > 2.14 GB (BigInt int8)', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team BigInt' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project BigInt', teamId: team.id },
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User BigInt', email: `test-bigint-${Date.now()}@example.com` },
|
|
})
|
|
|
|
// 3.5 GB = 3,500,000,000 bytes (exceeds int4 max 2,147,483,647)
|
|
const largeSize = 3_500_000_000
|
|
|
|
const largeFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'LargeStorageFolder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 0,
|
|
},
|
|
})
|
|
|
|
const largeFile = await assetService.createAsset({
|
|
name: 'huge_video_4k.mp4',
|
|
type: 'file',
|
|
projectId: project.id,
|
|
parentId: largeFolder.id,
|
|
sizeByte: largeSize,
|
|
creatorId: user.id,
|
|
})
|
|
|
|
expect(Number(largeFile.sizeByte)).toBe(largeSize)
|
|
|
|
// Test updating ancestor size with > 2.14 GB
|
|
await assetService.updateAncestorsSize(prisma, largeFolder.id, largeSize)
|
|
|
|
const updatedFolder = await prisma.asset.findUnique({
|
|
where: { id: largeFolder.id },
|
|
})
|
|
|
|
expect(Number(updatedFolder?.sizeByte)).toBe(largeSize)
|
|
|
|
// Test getAsset returns number format correctly
|
|
const info = await assetService.getAsset({ assetId: largeFile.id })
|
|
expect(info.sizeByte).toBe(largeSize)
|
|
})
|
|
})
|
|
|
|
describe('getAssetContext', () => {
|
|
it('returns context for project asset', async () => {
|
|
const user = await prisma.user.create({
|
|
data: { name: 'User_Ctx_' + Date.now(), email: `test-ctx-${Date.now()}@example.com` },
|
|
})
|
|
const team = await prisma.team.create({
|
|
data: { name: 'Team_Ctx_' + Date.now() },
|
|
})
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Project_Ctx', teamId: team.id },
|
|
})
|
|
const asset = await prisma.asset.create({
|
|
data: {
|
|
name: 'Asset_Ctx',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
creatorId: user.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const context = await assetService.getAssetContext(asset.id)
|
|
expect(context.projectId).toBe(project.id)
|
|
expect(context.teamId).toBe(team.id)
|
|
})
|
|
|
|
it('throws error when asset is not found', async () => {
|
|
await expect(assetService.getAssetContext('non-existent-id')).rejects.toThrow(
|
|
'Asset not found: non-existent-id',
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('AGENTS.md methods', () => {
|
|
it('getAgentsMd returns null when no agentmd is set', async () => {
|
|
const team = await prisma.team.create({
|
|
data: { name: 'Team_AgentsMd_' + Date.now() },
|
|
})
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Project_AgentsMd', teamId: team.id },
|
|
})
|
|
const folder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Folder_No_Md',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const content = await assetService.getAgentsMd(folder.id)
|
|
expect(content).toBeNull()
|
|
})
|
|
|
|
it('updateAgentsMd updates and getAgentsMd retrieves agentmd, and empty deletes', async () => {
|
|
const team = await prisma.team.create({
|
|
data: { name: 'Team_AgentsMd2_' + Date.now() },
|
|
})
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Project_AgentsMd2', teamId: team.id },
|
|
})
|
|
const folder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Folder_With_Md',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const updateResult = await assetService.updateAgentsMd(folder.id, '# Custom Guidelines')
|
|
expect(updateResult).toEqual({ content: '# Custom Guidelines' })
|
|
|
|
const content = await assetService.getAgentsMd(folder.id)
|
|
expect(content).toBe('# Custom Guidelines')
|
|
|
|
let folderInfo = await assetService.getAsset({ assetId: folder.id })
|
|
expect(folderInfo?.hasAgentsMd).toBe(true)
|
|
|
|
// Empty string deletes the record
|
|
const clearResult = await assetService.updateAgentsMd(folder.id, ' ')
|
|
expect(clearResult).toEqual({ content: '' })
|
|
|
|
const emptyContent = await assetService.getAgentsMd(folder.id)
|
|
expect(emptyContent).toBeNull()
|
|
|
|
folderInfo = await assetService.getAsset({ assetId: folder.id })
|
|
expect(folderInfo?.hasAgentsMd).toBe(false)
|
|
})
|
|
|
|
it('getNestedAgentsMd traverses hierarchy from root to leaf and formats virtual paths', async () => {
|
|
const team = await prisma.team.create({
|
|
data: { name: 'Team_NestedMd_' + Date.now() },
|
|
})
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Project_NestedMd', teamId: team.id },
|
|
})
|
|
|
|
// 1. Root folder
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Root Folder',
|
|
type: AssetType.root,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
await prisma.assetAgentMd.create({
|
|
data: {
|
|
assetId: rootFolder.id,
|
|
content: '# Root Instructions\nFollow global rules.',
|
|
},
|
|
})
|
|
|
|
// 2. Subfolder 1: marketing (has agentmd)
|
|
const marketingFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'marketing',
|
|
type: AssetType.folder,
|
|
parentId: rootFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
await prisma.assetAgentMd.create({
|
|
data: {
|
|
assetId: marketingFolder.id,
|
|
content: '# Marketing Instructions\nTone must be upbeat.',
|
|
},
|
|
})
|
|
|
|
// 3. Subfolder 2: campaigns (no agentmd)
|
|
const campaignsFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'campaigns',
|
|
type: AssetType.folder,
|
|
parentId: marketingFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
// 4. Subfolder 3: video-ads (has agentmd)
|
|
const videoAdsFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'video-ads',
|
|
type: AssetType.folder,
|
|
parentId: campaignsFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
await prisma.assetAgentMd.create({
|
|
data: {
|
|
assetId: videoAdsFolder.id,
|
|
content: '# Video Ads Instructions\nInclude resolution tags.',
|
|
},
|
|
})
|
|
|
|
// 5. File inside video-ads
|
|
const fileAsset = await prisma.asset.create({
|
|
data: {
|
|
name: 'ad_clip.mp4',
|
|
type: AssetType.file,
|
|
parentId: videoAdsFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const nestedFromLeaf = await assetService.getNestedAgentsMd(fileAsset.id)
|
|
expect(nestedFromLeaf).toEqual([
|
|
{
|
|
path: '/AGENTS.md',
|
|
content: '# Root Instructions\nFollow global rules.',
|
|
},
|
|
{
|
|
path: '/marketing/AGENTS.md',
|
|
content: '# Marketing Instructions\nTone must be upbeat.',
|
|
},
|
|
{
|
|
path: '/marketing/campaigns/video-ads/AGENTS.md',
|
|
content: '# Video Ads Instructions\nInclude resolution tags.',
|
|
},
|
|
])
|
|
|
|
const nestedFromMarketing = await assetService.getNestedAgentsMd(marketingFolder.id)
|
|
expect(nestedFromMarketing).toEqual([
|
|
{
|
|
path: '/AGENTS.md',
|
|
content: '# Root Instructions\nFollow global rules.',
|
|
},
|
|
{
|
|
path: '/marketing/AGENTS.md',
|
|
content: '# Marketing Instructions\nTone must be upbeat.',
|
|
},
|
|
])
|
|
})
|
|
|
|
it('rejects get/update agentsmd on non-folder assets', async () => {
|
|
const team = await prisma.team.create({
|
|
data: { name: 'Team_AgentsMd_Type_' + Date.now() },
|
|
})
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Project_AgentsMd_Type', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Root',
|
|
type: AssetType.root,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const fileAsset = await prisma.asset.create({
|
|
data: {
|
|
name: 'doc.pdf',
|
|
type: AssetType.file,
|
|
parentId: rootFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
await expect(assetService.getAgentsMd(fileAsset.id)).rejects.toThrow(
|
|
'AGENTS.md can only be stored on folders',
|
|
)
|
|
await expect(assetService.updateAgentsMd(fileAsset.id, '# Nope')).rejects.toThrow(
|
|
'AGENTS.md can only be stored on folders',
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('reparentAssets into version_stack with sharelink symlinks', () => {
|
|
it('deduplicates symlinks when both Stack A and File B are in the sharelink and File B is moved into Stack A', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const shareRoot = await prisma.asset.create({
|
|
data: {
|
|
name: 'ShareRoot',
|
|
type: AssetType.share_root,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
fileCount: 2,
|
|
},
|
|
})
|
|
|
|
const fileA1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileA1.txt',
|
|
type: AssetType.file,
|
|
parentId: rootFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 100,
|
|
sortIndex: 'a0',
|
|
},
|
|
})
|
|
const fileA2 = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileA2.txt',
|
|
type: AssetType.file,
|
|
parentId: rootFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 100,
|
|
sortIndex: 'a1',
|
|
},
|
|
})
|
|
const fileB = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileB.txt',
|
|
type: AssetType.file,
|
|
parentId: rootFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 200,
|
|
sortIndex: 'a2',
|
|
},
|
|
})
|
|
|
|
// Create Stack A from fileA1 and fileA2
|
|
const stackA = await prisma.asset.create({
|
|
data: {
|
|
name: '',
|
|
type: AssetType.version_stack,
|
|
parentId: rootFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
fileCount: 2,
|
|
sizeByte: 200,
|
|
},
|
|
})
|
|
await prisma.asset.update({ where: { id: fileA1.id }, data: { parentId: stackA.id } })
|
|
await prisma.asset.update({ where: { id: fileA2.id }, data: { parentId: stackA.id } })
|
|
|
|
// Create symlinks in shareRoot for both Stack A and File B
|
|
const symlinkStackA = await prisma.asset.create({
|
|
data: {
|
|
name: '',
|
|
type: AssetType.symlink,
|
|
parentId: shareRoot.id,
|
|
targetId: stackA.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const symlinkFileB = await prisma.asset.create({
|
|
data: {
|
|
name: fileB.name,
|
|
type: AssetType.symlink,
|
|
parentId: shareRoot.id,
|
|
targetId: fileB.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
// Move File B into Stack A
|
|
await assetService.reparentAssets({
|
|
newParentId: stackA.id,
|
|
assetIds: [fileB.id],
|
|
creatorId: user.id,
|
|
})
|
|
|
|
// Verify: File B's symlink was deleted, only Stack A's symlink remains
|
|
expect(await prisma.asset.findUnique({ where: { id: symlinkFileB.id } })).toBeNull()
|
|
const remainingSymlinks = await prisma.asset.findMany({
|
|
where: { parentId: shareRoot.id, type: AssetType.symlink },
|
|
})
|
|
expect(remainingSymlinks).toHaveLength(1)
|
|
expect(remainingSymlinks[0].id).toBe(symlinkStackA.id)
|
|
expect(remainingSymlinks[0].targetId).toBe(stackA.id)
|
|
|
|
// ShareRoot fileCount was decremented from 2 to 1
|
|
const updatedShareRoot = await prisma.asset.findUnique({ where: { id: shareRoot.id } })
|
|
expect(updatedShareRoot?.fileCount).toBe(1)
|
|
})
|
|
|
|
it('repoints symlink to Stack A when only File B is in sharelink and File B is moved into Stack A', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const shareRoot = await prisma.asset.create({
|
|
data: {
|
|
name: 'ShareRoot',
|
|
type: AssetType.share_root,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
fileCount: 1,
|
|
},
|
|
})
|
|
|
|
const fileA1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileA1.txt',
|
|
type: AssetType.file,
|
|
parentId: rootFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 100,
|
|
sortIndex: 'a0',
|
|
},
|
|
})
|
|
const stackA = await prisma.asset.create({
|
|
data: {
|
|
name: '',
|
|
type: AssetType.version_stack,
|
|
parentId: rootFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
fileCount: 1,
|
|
sizeByte: 100,
|
|
},
|
|
})
|
|
await prisma.asset.update({ where: { id: fileA1.id }, data: { parentId: stackA.id } })
|
|
|
|
const fileB = await prisma.asset.create({
|
|
data: {
|
|
name: 'fileB.txt',
|
|
type: AssetType.file,
|
|
parentId: rootFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 200,
|
|
sortIndex: 'a1',
|
|
},
|
|
})
|
|
|
|
// Create symlink for File B only
|
|
const symlinkFileB = await prisma.asset.create({
|
|
data: {
|
|
name: fileB.name,
|
|
type: AssetType.symlink,
|
|
parentId: shareRoot.id,
|
|
targetId: fileB.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
// Move File B into Stack A
|
|
await assetService.reparentAssets({
|
|
newParentId: stackA.id,
|
|
assetIds: [fileB.id],
|
|
creatorId: user.id,
|
|
})
|
|
|
|
// Verify: symlinkFileB now points to stackA.id
|
|
const updatedSymlink = await prisma.asset.findUnique({ where: { id: symlinkFileB.id } })
|
|
expect(updatedSymlink?.targetId).toBe(stackA.id)
|
|
|
|
// Listing assets for shareRoot returns Stack A with latest version name
|
|
const result = await assetService.listChildren({
|
|
assetId: shareRoot.id,
|
|
assetType: AssetType.file,
|
|
})
|
|
expect(result.data).toHaveLength(1)
|
|
expect(result.data[0].name).toBe(fileB.name) // fileB is the latest version
|
|
})
|
|
})
|
|
|
|
describe('updateAssetName', () => {
|
|
it('renames the latest version child file and returns updated AssetInfo when given a version stack ID', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const stack = await prisma.asset.create({
|
|
data: {
|
|
name: '',
|
|
type: AssetType.version_stack,
|
|
parentId: rootFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
fileCount: 2,
|
|
sizeByte: 300,
|
|
},
|
|
})
|
|
|
|
const fileV1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'v1.mp4',
|
|
type: AssetType.file,
|
|
parentId: stack.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 100,
|
|
sortIndex: 'b0',
|
|
},
|
|
})
|
|
|
|
const fileV2 = await prisma.asset.create({
|
|
data: {
|
|
name: 'v2.mp4',
|
|
type: AssetType.file,
|
|
parentId: stack.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 200,
|
|
sortIndex: 'a0', // latest version
|
|
},
|
|
})
|
|
|
|
const updated = await assetService.updateAssetName({
|
|
id: stack.id,
|
|
name: 'v2-renamed.mp4',
|
|
})
|
|
|
|
expect(updated.id).toBe(stack.id)
|
|
expect(updated.name).toBe('v2-renamed.mp4')
|
|
expect(updated.versionStack?.versions[0]?.name).toBe('v2-renamed.mp4')
|
|
|
|
const dbFileV2 = await prisma.asset.findUnique({ where: { id: fileV2.id } })
|
|
expect(dbFileV2?.name).toBe('v2-renamed.mp4')
|
|
|
|
const dbFileV1 = await prisma.asset.findUnique({ where: { id: fileV1.id } })
|
|
expect(dbFileV1?.name).toBe('v1.mp4')
|
|
|
|
const dbStack = await prisma.asset.findUnique({ where: { id: stack.id } })
|
|
expect(dbStack?.name).toBe('')
|
|
})
|
|
|
|
it('preserves the renamed filename when a version stack dissolves', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const stack = await prisma.asset.create({
|
|
data: {
|
|
name: '',
|
|
type: AssetType.version_stack,
|
|
parentId: rootFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
fileCount: 2,
|
|
sizeByte: 300,
|
|
sortIndex: 's0',
|
|
},
|
|
})
|
|
|
|
const fileV1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'v1.mp4',
|
|
type: AssetType.file,
|
|
parentId: stack.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 100,
|
|
sortIndex: 'b0',
|
|
},
|
|
})
|
|
|
|
const fileV2 = await prisma.asset.create({
|
|
data: {
|
|
name: 'v2.mp4',
|
|
type: AssetType.file,
|
|
parentId: stack.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 200,
|
|
sortIndex: 'a0',
|
|
},
|
|
})
|
|
|
|
await assetService.updateAssetName({
|
|
id: stack.id,
|
|
name: 'v2-renamed.mp4',
|
|
})
|
|
|
|
// Remove v1 from stack -> stack dissolves and v2 becomes standalone file
|
|
const { versionStackService } = await import('../versionStack/versionStack')
|
|
await versionStackService.removeVersionFromStack({
|
|
stackId: stack.id,
|
|
fileId: fileV1.id,
|
|
})
|
|
|
|
const remainingFile = await prisma.asset.findUnique({ where: { id: fileV2.id } })
|
|
expect(remainingFile?.parentId).toBe(rootFolder.id)
|
|
expect(remainingFile?.name).toBe('v2-renamed.mp4')
|
|
})
|
|
|
|
it('throws an error if version stack has no active versions to rename', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const emptyStack = await prisma.asset.create({
|
|
data: {
|
|
name: '',
|
|
type: AssetType.version_stack,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
await expect(
|
|
assetService.updateAssetName({
|
|
id: emptyStack.id,
|
|
name: 'new-name.mp4',
|
|
}),
|
|
).rejects.toThrow('No active version found in stack to rename')
|
|
})
|
|
|
|
it('renames a regular file and folder correctly', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const folder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Original Folder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'original.png',
|
|
type: AssetType.file,
|
|
parentId: folder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const updatedFolder = await assetService.updateAssetName({
|
|
id: folder.id,
|
|
name: 'Renamed Folder',
|
|
})
|
|
expect(updatedFolder.name).toBe('Renamed Folder')
|
|
|
|
const updatedFile = await assetService.updateAssetName({
|
|
id: file.id,
|
|
name: 'renamed.png',
|
|
})
|
|
expect(updatedFile.name).toBe('renamed.png')
|
|
})
|
|
|
|
it('cascades soft-delete and restoration to symlinks and updates parent fileCount', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const shareRoot = await prisma.asset.create({
|
|
data: {
|
|
name: 'Share Root',
|
|
type: AssetType.share_root,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
fileCount: 0,
|
|
},
|
|
})
|
|
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'test-file.txt',
|
|
type: AssetType.file,
|
|
parentId: rootFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
await prisma.asset.update({
|
|
where: { id: rootFolder.id },
|
|
data: { fileCount: 1 },
|
|
})
|
|
|
|
const symlink = await prisma.asset.create({
|
|
data: {
|
|
name: file.name,
|
|
type: AssetType.symlink,
|
|
parentId: shareRoot.id,
|
|
targetId: file.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
await prisma.asset.update({
|
|
where: { id: shareRoot.id },
|
|
data: { fileCount: 1 },
|
|
})
|
|
|
|
// Soft delete the file
|
|
await assetService.deleteAssets([file.id])
|
|
|
|
const deletedSymlink = await prisma.asset.findUnique({ where: { id: symlink.id } })
|
|
expect(deletedSymlink?.isDeleted).toBe(true)
|
|
expect(deletedSymlink?.status).toBe(AssetStatus.trashed)
|
|
|
|
const updatedShareRoot = await prisma.asset.findUnique({ where: { id: shareRoot.id } })
|
|
expect(updatedShareRoot?.fileCount).toBe(0)
|
|
|
|
// Restore the file
|
|
await assetService.restoreAssets([file.id])
|
|
|
|
const restoredSymlink = await prisma.asset.findUnique({ where: { id: symlink.id } })
|
|
expect(restoredSymlink?.isDeleted).toBe(false)
|
|
expect(restoredSymlink?.status).toBe(AssetStatus.processed)
|
|
|
|
const restoredShareRoot = await prisma.asset.findUnique({ where: { id: shareRoot.id } })
|
|
expect(restoredShareRoot?.fileCount).toBe(1)
|
|
})
|
|
|
|
it('cascades permanent purge to symlinks when emptying trash', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'Root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const shareRoot = await prisma.asset.create({
|
|
data: {
|
|
name: 'Share Root',
|
|
type: AssetType.share_root,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
fileCount: 0,
|
|
},
|
|
})
|
|
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'file-to-purge.txt',
|
|
type: AssetType.file,
|
|
parentId: rootFolder.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const symlink = await prisma.asset.create({
|
|
data: {
|
|
name: file.name,
|
|
type: AssetType.symlink,
|
|
parentId: shareRoot.id,
|
|
targetId: file.id,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
// Soft delete then empty trash
|
|
await assetService.deleteAssets([file.id])
|
|
await assetService.emptyTrash(project.id)
|
|
|
|
const purgedFile = await prisma.asset.findUnique({ where: { id: file.id } })
|
|
expect(purgedFile).toBeNull()
|
|
|
|
const purgedSymlink = await prisma.asset.findUnique({ where: { id: symlink.id } })
|
|
expect(purgedSymlink).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('createFile', () => {
|
|
it('creates a file, updates ancestor sizes, persists metadata, and triggers workflows', async () => {
|
|
const user = await prisma.user.create({
|
|
data: { name: 'FileUser', email: `fileuser-${Date.now()}@example.com` },
|
|
})
|
|
const team = await prisma.team.create({ data: { name: 'FileTeam' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'FileProject', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'RootFolder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const promptField = await metadataService.createProjectField(project.id, {
|
|
description: 'Prompt field',
|
|
config: {
|
|
name: 'Prompt',
|
|
type: 'text',
|
|
autofillSource: 'CREATION_CONTEXT',
|
|
},
|
|
})
|
|
|
|
const triggerSpy = vi
|
|
.spyOn(uploadService, 'triggerPostUploadWorkflows')
|
|
.mockResolvedValue(undefined)
|
|
|
|
const fileInfo = await assetService.createFile({
|
|
parentId: rootFolder.id,
|
|
name: 'document.txt',
|
|
key: 'files/test/doc.txt',
|
|
sizeByte: 1024,
|
|
contentType: 'text/plain',
|
|
creatorId: user.id,
|
|
metadata: { [promptField.key]: 'Project overview document' },
|
|
})
|
|
|
|
expect(fileInfo.id).toBeDefined()
|
|
expect(fileInfo.name).toBe('document.txt')
|
|
expect(fileInfo.type).toBe(AssetType.file)
|
|
expect(fileInfo.sizeByte).toBe(1024)
|
|
|
|
const folderAfter = await prisma.asset.findUnique({ where: { id: rootFolder.id } })
|
|
expect(folderAfter?.sizeByte).toBe(1024n)
|
|
expect(folderAfter?.fileCount).toBe(1)
|
|
|
|
const metaVal = await prisma.assetMetadataValue.findFirst({
|
|
where: { assetId: fileInfo.id, fieldKey: promptField.key },
|
|
})
|
|
expect(metaVal?.stringValue).toBe('Project overview document')
|
|
|
|
expect(triggerSpy).toHaveBeenCalledWith(expect.anything(), fileInfo.id, team.id, project.id)
|
|
})
|
|
|
|
it('creates a file with agentId and returns agent info in toAssetInfos', async () => {
|
|
const user = await prisma.user.create({
|
|
data: { name: 'HumanUser', email: `human-${Date.now()}@example.com` },
|
|
})
|
|
const team = await prisma.team.create({ data: { name: 'AgentFileTeam' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'AgentFileProject', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'RootFolder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const agentUser = await prisma.user.create({
|
|
data: {
|
|
name: 'AssetBot',
|
|
email: `bot-${Date.now()}@example.com`,
|
|
type: 'agent',
|
|
},
|
|
})
|
|
const agent = await prisma.agent.create({
|
|
data: {
|
|
id: agentUser.id,
|
|
teamId: team.id,
|
|
type: 'chat',
|
|
config: { provider: 'test', model: 'test' },
|
|
},
|
|
})
|
|
|
|
vi.spyOn(uploadService, 'triggerPostUploadWorkflows').mockResolvedValue(undefined)
|
|
|
|
const fileInfo = await assetService.createFile({
|
|
parentId: rootFolder.id,
|
|
name: 'generated.png',
|
|
key: 'files/test/generated.png',
|
|
sizeByte: 2048,
|
|
contentType: 'image/png',
|
|
creatorId: user.id,
|
|
agentId: agent.id,
|
|
})
|
|
|
|
expect(fileInfo.id).toBeDefined()
|
|
expect(fileInfo.agentId).toBe(agent.id)
|
|
expect(fileInfo.agent).toEqual({
|
|
id: agent.id,
|
|
name: 'AssetBot',
|
|
})
|
|
|
|
const fetched = await assetService.getAsset({ assetId: fileInfo.id })
|
|
expect(fetched.agentId).toBe(agent.id)
|
|
expect(fetched.agent).toEqual({
|
|
id: agent.id,
|
|
name: 'AssetBot',
|
|
})
|
|
})
|
|
|
|
it('rejects if metadata contains invalid keys', async () => {
|
|
const user = await prisma.user.create({
|
|
data: { name: 'FileUser2', email: `fileuser2-${Date.now()}@example.com` },
|
|
})
|
|
const team = await prisma.team.create({ data: { name: 'FileTeam2' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'FileProject2', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'RootFolder2',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
await expect(
|
|
assetService.createFile({
|
|
parentId: rootFolder.id,
|
|
name: 'invalid.txt',
|
|
key: 'files/test/inv.txt',
|
|
sizeByte: 100,
|
|
contentType: 'text/plain',
|
|
creatorId: user.id,
|
|
metadata: { unknownField: 'val' },
|
|
}),
|
|
).rejects.toThrow(/metadata contains keys that are not valid in this project/)
|
|
})
|
|
|
|
it('rolls back completely if post-upload workflow fails in transaction', async () => {
|
|
const user = await prisma.user.create({
|
|
data: { name: 'FileRollbackUser', email: `filerollback-${Date.now()}@example.com` },
|
|
})
|
|
const team = await prisma.team.create({ data: { name: 'FileRollbackTeam' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'FileRollbackProject', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'RootFolder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 0n,
|
|
fileCount: 0,
|
|
},
|
|
})
|
|
|
|
vi.spyOn(uploadService, 'triggerPostUploadWorkflows').mockRejectedValueOnce(
|
|
new Error('Simulated workflow failure'),
|
|
)
|
|
|
|
await expect(
|
|
assetService.createFile({
|
|
parentId: rootFolder.id,
|
|
name: 'should_rollback.txt',
|
|
key: 'files/test/should_rollback.txt',
|
|
sizeByte: 1024,
|
|
contentType: 'text/plain',
|
|
creatorId: user.id,
|
|
}),
|
|
).rejects.toThrow('Simulated workflow failure')
|
|
|
|
// Assert no asset was created
|
|
const created = await prisma.asset.findFirst({
|
|
where: { name: 'should_rollback.txt', projectId: project.id },
|
|
})
|
|
expect(created).toBeNull()
|
|
|
|
// Assert parent folder counters remained untouched
|
|
const folderAfter = await prisma.asset.findUnique({ where: { id: rootFolder.id } })
|
|
expect(folderAfter?.fileCount).toBe(0)
|
|
expect(folderAfter?.sizeByte).toBe(0n)
|
|
})
|
|
})
|
|
|
|
describe('createVersion', () => {
|
|
it('creates a version stack when target is a regular file', async () => {
|
|
const user = await prisma.user.create({
|
|
data: { name: 'VersionUser', email: `veruser-${Date.now()}@example.com` },
|
|
})
|
|
const team = await prisma.team.create({ data: { name: 'VersionTeam' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'VersionProject', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'RootFolder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const v1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'image.png',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 2000n,
|
|
},
|
|
})
|
|
|
|
const triggerSpy = vi
|
|
.spyOn(uploadService, 'triggerPostUploadWorkflows')
|
|
.mockResolvedValue(undefined)
|
|
|
|
const v2Info = await assetService.createVersion({
|
|
parentId: v1.id,
|
|
name: 'image.png',
|
|
key: 'files/test/v2.png',
|
|
sizeByte: 3000,
|
|
contentType: 'image/png',
|
|
creatorId: user.id,
|
|
})
|
|
|
|
expect(v2Info.id).toBeDefined()
|
|
|
|
const v2Record = await prisma.asset.findUnique({
|
|
where: { id: v2Info.id },
|
|
include: { parent: true },
|
|
})
|
|
expect(v2Record?.parent?.type).toBe(AssetType.version_stack)
|
|
expect(v2Record?.parent?.parentId).toBe(rootFolder.id)
|
|
|
|
const v1Record = await prisma.asset.findUnique({ where: { id: v1.id } })
|
|
expect(v1Record?.parentId).toBe(v2Record?.parentId)
|
|
|
|
const stackRecord = await prisma.asset.findUnique({ where: { id: v2Record!.parentId! } })
|
|
expect(stackRecord?.fileCount).toBe(2)
|
|
expect(stackRecord?.sizeByte).toBe(5000n)
|
|
|
|
expect(triggerSpy).toHaveBeenCalledWith(expect.anything(), v2Info.id, team.id, project.id)
|
|
})
|
|
|
|
it('appends a new version when target is in an existing version stack', async () => {
|
|
const user = await prisma.user.create({
|
|
data: { name: 'VersionUser2', email: `veruser2-${Date.now()}@example.com` },
|
|
})
|
|
const team = await prisma.team.create({ data: { name: 'VersionTeam2' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'VersionProject2', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'RootFolder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const stack = await prisma.asset.create({
|
|
data: {
|
|
name: 'image.png',
|
|
type: AssetType.version_stack,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 2000n,
|
|
fileCount: 1,
|
|
},
|
|
})
|
|
|
|
const v1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'image.png',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: stack.id,
|
|
status: AssetStatus.uploaded,
|
|
sortIndex: 'a0',
|
|
sizeByte: 2000n,
|
|
},
|
|
})
|
|
|
|
const triggerSpy = vi
|
|
.spyOn(uploadService, 'triggerPostUploadWorkflows')
|
|
.mockResolvedValue(undefined)
|
|
|
|
const v2Info = await assetService.createVersion({
|
|
parentId: v1.id,
|
|
name: 'image_v2.png',
|
|
key: 'files/test/v2.png',
|
|
sizeByte: 3000,
|
|
contentType: 'image/png',
|
|
creatorId: user.id,
|
|
})
|
|
|
|
expect(v2Info.id).toBeDefined()
|
|
|
|
const v2Record = await prisma.asset.findUnique({ where: { id: v2Info.id } })
|
|
expect(v2Record?.parentId).toBe(stack.id)
|
|
|
|
const stackAfter = await prisma.asset.findUnique({ where: { id: stack.id } })
|
|
expect(stackAfter?.fileCount).toBe(2)
|
|
expect(stackAfter?.sizeByte).toBe(5000n)
|
|
|
|
expect(triggerSpy).toHaveBeenCalledWith(expect.anything(), v2Info.id, team.id, project.id)
|
|
})
|
|
|
|
it('rolls back completely if workflow triggering fails in createVersion', async () => {
|
|
const user = await prisma.user.create({
|
|
data: { name: 'VersionRollbackUser', email: `verrollback-${Date.now()}@example.com` },
|
|
})
|
|
const team = await prisma.team.create({ data: { name: 'VersionRollbackTeam' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'VersionRollbackProject', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'RootFolder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 2000n,
|
|
fileCount: 1,
|
|
},
|
|
})
|
|
|
|
const v1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'image.png',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 2000n,
|
|
},
|
|
})
|
|
|
|
vi.spyOn(uploadService, 'triggerPostUploadWorkflows').mockRejectedValueOnce(
|
|
new Error('Simulated version workflow failure'),
|
|
)
|
|
|
|
await expect(
|
|
assetService.createVersion({
|
|
parentId: v1.id,
|
|
name: 'image_v2_fail.png',
|
|
key: 'files/test/v2_fail.png',
|
|
sizeByte: 3000,
|
|
contentType: 'image/png',
|
|
creatorId: user.id,
|
|
}),
|
|
).rejects.toThrow('Simulated version workflow failure')
|
|
|
|
// Check that new version was rolled back
|
|
const createdV2 = await prisma.asset.findFirst({
|
|
where: { name: 'image_v2_fail.png', projectId: project.id },
|
|
})
|
|
expect(createdV2).toBeNull()
|
|
|
|
// Check that v1 remains under rootFolder (no orphaned version stack created)
|
|
const v1After = await prisma.asset.findUnique({ where: { id: v1.id } })
|
|
expect(v1After?.parentId).toBe(rootFolder.id)
|
|
|
|
// Check root folder counters remained unchanged
|
|
const rootFolderAfter = await prisma.asset.findUnique({ where: { id: rootFolder.id } })
|
|
expect(rootFolderAfter?.fileCount).toBe(1)
|
|
expect(rootFolderAfter?.sizeByte).toBe(2000n)
|
|
})
|
|
|
|
it('creates a version with agentId and preserves agent provenance in version stack', async () => {
|
|
const user = await prisma.user.create({
|
|
data: { name: 'VersionUser2', email: `veruser2-${Date.now()}@example.com` },
|
|
})
|
|
const team = await prisma.team.create({ data: { name: 'VersionTeam2' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'VersionProject2', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'RootFolder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const agentUser = await prisma.user.create({
|
|
data: {
|
|
name: 'VersionBot',
|
|
email: `verbot-${Date.now()}@example.com`,
|
|
type: 'agent',
|
|
},
|
|
})
|
|
const agent = await prisma.agent.create({
|
|
data: {
|
|
id: agentUser.id,
|
|
teamId: team.id,
|
|
type: 'chat',
|
|
config: { provider: 'test', model: 'test' },
|
|
},
|
|
})
|
|
|
|
const v1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'document.txt',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
status: AssetStatus.uploaded,
|
|
sizeByte: 100n,
|
|
creatorId: user.id,
|
|
},
|
|
})
|
|
|
|
vi.spyOn(uploadService, 'triggerPostUploadWorkflows').mockResolvedValue(undefined)
|
|
|
|
const v2Info = await assetService.createVersion({
|
|
parentId: v1.id,
|
|
name: 'document.txt',
|
|
key: 'files/test/v2.txt',
|
|
sizeByte: 200,
|
|
contentType: 'text/plain',
|
|
creatorId: user.id,
|
|
agentId: agent.id,
|
|
})
|
|
|
|
expect(v2Info.id).toBeDefined()
|
|
expect(v2Info.agentId).toBe(agent.id)
|
|
expect(v2Info.agent).toEqual({
|
|
id: agent.id,
|
|
name: 'VersionBot',
|
|
})
|
|
|
|
const stack = await prisma.asset.findFirst({
|
|
where: { parentId: rootFolder.id, type: AssetType.version_stack },
|
|
})
|
|
expect(stack).toBeDefined()
|
|
|
|
const stackVersions = await assetService.getStackVersions(stack!.id)
|
|
expect(stackVersions).toHaveLength(2)
|
|
const v2StackVersion = stackVersions.find((v) => v.id === v2Info.id)
|
|
expect(v2StackVersion?.agent).toEqual({
|
|
id: agent.id,
|
|
name: 'VersionBot',
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('listChildren type filtering', () => {
|
|
it('returns files, folders, and version stacks when assetType is all or omitted', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'ListTypeTeam' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'ListTypeProject', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'RootFolder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const subFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'subfolder',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'file.txt',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const stack = await prisma.asset.create({
|
|
data: {
|
|
name: 'stack.png',
|
|
type: AssetType.version_stack,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
// Query with assetType: 'all'
|
|
const allRes = await assetService.listChildren({
|
|
assetId: rootFolder.id,
|
|
assetType: 'all',
|
|
})
|
|
const allIds = allRes.data.map((a) => a.id)
|
|
expect(allIds).toContain(subFolder.id)
|
|
expect(allIds).toContain(file.id)
|
|
expect(allIds).toContain(stack.id)
|
|
expect(allRes.data.length).toBe(3)
|
|
|
|
// Query with assetType: 'file'
|
|
const fileRes = await assetService.listChildren({
|
|
assetId: rootFolder.id,
|
|
assetType: 'file',
|
|
})
|
|
const fileIds = fileRes.data.map((a) => a.id)
|
|
expect(fileIds).toContain(file.id)
|
|
expect(fileIds).toContain(stack.id)
|
|
expect(fileIds).not.toContain(subFolder.id)
|
|
expect(fileRes.data.length).toBe(2)
|
|
|
|
// Query with assetType: 'folder'
|
|
const folderRes = await assetService.listChildren({
|
|
assetId: rootFolder.id,
|
|
assetType: 'folder',
|
|
})
|
|
const folderIds = folderRes.data.map((a) => a.id)
|
|
expect(folderIds).toContain(subFolder.id)
|
|
expect(folderIds).not.toContain(file.id)
|
|
expect(folderIds).not.toContain(stack.id)
|
|
expect(folderRes.data.length).toBe(1)
|
|
})
|
|
})
|
|
|
|
describe('commentsCount in toAssetInfos', () => {
|
|
it('returns commentsCount 0 when file has no comments', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
|
})
|
|
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'file.mp4',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
creatorId: user.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const res = await assetService.listChildren({
|
|
assetId: rootFolder.id,
|
|
assetType: 'file',
|
|
})
|
|
const fileInfo = res.data.find((a) => a.id === file.id)
|
|
expect(fileInfo).toBeDefined()
|
|
expect(fileInfo?.commentsCount).toBe(0)
|
|
})
|
|
|
|
it('returns total comments count including replies for a file', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
|
})
|
|
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'video.mp4',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
creatorId: user.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
const rootComment = await prisma.assetComment.create({
|
|
data: {
|
|
assetId: file.id,
|
|
creatorId: user.id,
|
|
message: 'Root comment 1',
|
|
},
|
|
})
|
|
|
|
await prisma.assetComment.create({
|
|
data: {
|
|
assetId: file.id,
|
|
creatorId: user.id,
|
|
message: 'Reply to comment 1',
|
|
replyToId: rootComment.id,
|
|
},
|
|
})
|
|
|
|
await prisma.assetComment.create({
|
|
data: {
|
|
assetId: file.id,
|
|
creatorId: user.id,
|
|
message: 'Root comment 2',
|
|
},
|
|
})
|
|
|
|
const res = await assetService.listChildren({
|
|
assetId: rootFolder.id,
|
|
assetType: 'file',
|
|
})
|
|
const fileInfo = res.data.find((a) => a.id === file.id)
|
|
expect(fileInfo).toBeDefined()
|
|
expect(fileInfo?.commentsCount).toBe(3)
|
|
})
|
|
|
|
it('returns comments count of the active version for version stack', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Test Project', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const user = await prisma.user.create({
|
|
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
|
})
|
|
|
|
const stack = await prisma.asset.create({
|
|
data: {
|
|
name: 'video_stack',
|
|
type: AssetType.version_stack,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
creatorId: user.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
|
|
// Older version (v1): larger sortIndex
|
|
const fileV1 = await prisma.asset.create({
|
|
data: {
|
|
name: 'video_v1.mp4',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: stack.id,
|
|
creatorId: user.id,
|
|
status: AssetStatus.uploaded,
|
|
sortIndex: 'b',
|
|
},
|
|
})
|
|
|
|
// Active / latest version (v2): smaller sortIndex
|
|
const fileV2 = await prisma.asset.create({
|
|
data: {
|
|
name: 'video_v2.mp4',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: stack.id,
|
|
creatorId: user.id,
|
|
status: AssetStatus.uploaded,
|
|
sortIndex: 'a',
|
|
},
|
|
})
|
|
|
|
// Add 2 comments to v1
|
|
await prisma.assetComment.create({
|
|
data: {
|
|
assetId: fileV1.id,
|
|
creatorId: user.id,
|
|
message: 'Comment on v1',
|
|
},
|
|
})
|
|
await prisma.assetComment.create({
|
|
data: {
|
|
assetId: fileV1.id,
|
|
creatorId: user.id,
|
|
message: 'Another comment on v1',
|
|
},
|
|
})
|
|
|
|
// Add 1 comment to v2 (active version)
|
|
await prisma.assetComment.create({
|
|
data: {
|
|
assetId: fileV2.id,
|
|
creatorId: user.id,
|
|
message: 'Comment on v2',
|
|
},
|
|
})
|
|
|
|
const res = await assetService.listChildren({
|
|
assetId: rootFolder.id,
|
|
assetType: 'file',
|
|
})
|
|
const stackInfo = res.data.find((a) => a.id === stack.id)
|
|
expect(stackInfo).toBeDefined()
|
|
// Should reflect active version (v2) comments count
|
|
expect(stackInfo?.commentsCount).toBe(1)
|
|
})
|
|
})
|
|
|
|
describe('restoreAssets with in-flight transcoding', () => {
|
|
it('should restore asset with status "processing" if a transcode task is still active', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Restore Transcode Team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Restore Transcode Project', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'transcoding-video.mp4',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
status: AssetStatus.trashed,
|
|
isDeleted: true,
|
|
deletedAt: new Date(),
|
|
},
|
|
})
|
|
|
|
// Create an active transcode task
|
|
await prisma.workflowTask.create({
|
|
data: {
|
|
assetId: file.id,
|
|
type: WorkflowTaskType.transcode_video,
|
|
status: WorkflowTaskStatus.processing,
|
|
},
|
|
})
|
|
|
|
await assetService.restoreAssets([file.id])
|
|
|
|
const restored = await prisma.asset.findUnique({ where: { id: file.id } })
|
|
expect(restored?.isDeleted).toBe(false)
|
|
expect(restored?.status).toBe(AssetStatus.processing)
|
|
})
|
|
|
|
it('should restore asset with status "processed" if no active transcode task exists', async () => {
|
|
const team = await prisma.team.create({ data: { name: 'Restore Completed Team' } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'Restore Completed Project', teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.uploaded,
|
|
},
|
|
})
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'completed-video.mp4',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
status: AssetStatus.trashed,
|
|
isDeleted: true,
|
|
deletedAt: new Date(),
|
|
},
|
|
})
|
|
|
|
// Completed transcode task
|
|
await prisma.workflowTask.create({
|
|
data: {
|
|
assetId: file.id,
|
|
type: WorkflowTaskType.transcode_video,
|
|
status: WorkflowTaskStatus.completed,
|
|
},
|
|
})
|
|
|
|
await assetService.restoreAssets([file.id])
|
|
|
|
const restored = await prisma.asset.findUnique({ where: { id: file.id } })
|
|
expect(restored?.isDeleted).toBe(false)
|
|
expect(restored?.status).toBe(AssetStatus.processed)
|
|
})
|
|
})
|
|
|
|
describe('HLS playlists', () => {
|
|
const setupHlsAssets = async () => {
|
|
const team = await prisma.team.create({ data: { name: 'hls-team-' + Date.now() } })
|
|
const project = await prisma.project.create({
|
|
data: { name: 'hls-proj-' + Date.now(), teamId: team.id },
|
|
})
|
|
const rootFolder = await prisma.asset.create({
|
|
data: {
|
|
name: 'root',
|
|
type: AssetType.folder,
|
|
projectId: project.id,
|
|
status: AssetStatus.processed,
|
|
},
|
|
})
|
|
return { team, project, rootFolder }
|
|
}
|
|
|
|
it('getHlsMasterPlaylist throws 404 if asset is not HLS', async () => {
|
|
const { project, rootFolder } = await setupHlsAssets()
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'regular.mp4',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
status: AssetStatus.processed,
|
|
media: {
|
|
isHls: false,
|
|
} as PrismaJson.MediaInfo,
|
|
},
|
|
})
|
|
|
|
await expect(assetService.getHlsMasterPlaylist({ assetId: file.id })).rejects.toThrow(
|
|
'HLS playlist not found',
|
|
)
|
|
})
|
|
|
|
it('getHlsMasterPlaylist returns playlist string when asset is HLS', async () => {
|
|
const { project, rootFolder } = await setupHlsAssets()
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'hls-video.mp4',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
status: AssetStatus.processed,
|
|
media: {
|
|
isHls: true,
|
|
hls: {
|
|
key: 'files/asset/hls/master.m3u8',
|
|
resolutions: [
|
|
{ resolution: '1080p', width: 1920, height: 1080 },
|
|
{ resolution: '720p', width: 1280, height: 720 },
|
|
],
|
|
},
|
|
} as PrismaJson.MediaInfo,
|
|
},
|
|
})
|
|
|
|
const masterContent = '#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=4628000\n1080p/index.m3u8\n'
|
|
vi.mocked(s3Service.getObject).mockResolvedValueOnce({
|
|
buffer: Buffer.from(masterContent),
|
|
} as unknown as Awaited<ReturnType<typeof s3Service.getObject>>)
|
|
|
|
const res = await assetService.getHlsMasterPlaylist({ assetId: file.id })
|
|
expect(res).toBe(masterContent)
|
|
expect(s3Service.getObject).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
'files/asset/hls/master.m3u8',
|
|
)
|
|
})
|
|
|
|
it('getHlsVariantPlaylist fetches and rewrites segment URLs', async () => {
|
|
const { project, rootFolder } = await setupHlsAssets()
|
|
const file = await prisma.asset.create({
|
|
data: {
|
|
name: 'hls-video.mp4',
|
|
type: AssetType.file,
|
|
projectId: project.id,
|
|
parentId: rootFolder.id,
|
|
status: AssetStatus.processed,
|
|
media: {
|
|
isHls: true,
|
|
hls: {
|
|
key: 'files/asset/hls/master.m3u8',
|
|
resolutions: [{ resolution: '1080p', width: 1920, height: 1080 }],
|
|
},
|
|
} as PrismaJson.MediaInfo,
|
|
},
|
|
})
|
|
|
|
const sampleVariant = '#EXTM3U\n#EXT-X-MAP:URI="init.mp4"\nsegment_000.m4s\n'
|
|
vi.mocked(s3Service.getObject).mockResolvedValueOnce({
|
|
buffer: Buffer.from(sampleVariant),
|
|
} as unknown as Awaited<ReturnType<typeof s3Service.getObject>>)
|
|
vi.mocked(s3Service.presign).mockResolvedValue('http://mock-s3-url')
|
|
|
|
const variant = await assetService.getHlsVariantPlaylist({
|
|
assetId: file.id,
|
|
resolution: '1080p',
|
|
})
|
|
expect(variant).toBeDefined()
|
|
expect(variant).toContain('#EXT-X-MAP:URI="http://mock-s3-url"')
|
|
expect(variant).toContain('http://mock-s3-url')
|
|
})
|
|
})
|
|
})
|