mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 17:07:42 +00:00
Introduce @shumai/operations (pure definitions: Zod schemas, metadata, CLI hints) and @shumai/core/src/operations (implementOperation and the runOperation pipeline: validate, authorize, prepare, execute, validate output, audit). Migrate list_assets, create_folder, rename_asset, move_assets and delete_asset to Operations, and add list_projects. The agent now builds these tools from the registry, and the folder create/rename, file rename and reparent routes call the same Operations. Audit logs gain a nullable via column recording the surface. A lint rule keeps the Prisma client out of Operation implementations.
808 lines
27 KiB
TypeScript
808 lines
27 KiB
TypeScript
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
|
|
import { Hono, type Context, type Next } from 'hono'
|
|
import fileRoute from './file'
|
|
import { assetService } from '@shumai/core/src/asset/asset'
|
|
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
|
import { notificationService } from '@shumai/core/src/notification/notification'
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
|
import { transcodeService } from '@shumai/core'
|
|
import { authMiddleware } from './middleware/auth'
|
|
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
|
|
import { renameAssetOp, runOperation } from '@shumai/core/src/operations'
|
|
|
|
vi.mock('fs', async (importOriginal) => {
|
|
const actual = (await importOriginal()) as Record<string, unknown>
|
|
const actualDefault = actual.default as Record<string, unknown>
|
|
return {
|
|
...actual,
|
|
default: {
|
|
...actualDefault,
|
|
readFileSync: vi.fn().mockReturnValue(Buffer.from('transcoded content')),
|
|
existsSync: vi.fn().mockReturnValue(true),
|
|
unlinkSync: vi.fn().mockReturnValue(undefined),
|
|
},
|
|
}
|
|
})
|
|
|
|
vi.mock('@shumai/core/src/authz/authz', () => ({
|
|
authzService: {
|
|
hasPermission: vi.fn().mockResolvedValue(undefined),
|
|
},
|
|
Permission: {
|
|
Read: 'Read',
|
|
Edit: 'Edit',
|
|
Admin: 'Admin',
|
|
},
|
|
ResourceType: {
|
|
Asset: 'asset',
|
|
Team: 'team',
|
|
Project: 'project',
|
|
Comment: 'comment',
|
|
},
|
|
}))
|
|
|
|
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
|
|
|
vi.mock('@shumai/core/src/auditLog/auditLog', () => ({
|
|
auditLogService: {
|
|
logAction: vi.fn().mockResolvedValue({}),
|
|
},
|
|
}))
|
|
|
|
vi.mock('./middleware/auth', () => ({
|
|
authMiddleware: async (c: Context, next: Next) => {
|
|
c.set('user', { id: 'user1', name: 'Test User' })
|
|
await next()
|
|
},
|
|
}))
|
|
|
|
vi.mock('@shumai/core/src/operations', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('@shumai/core/src/operations')>()),
|
|
runOperation: vi.fn(),
|
|
}))
|
|
|
|
describe('file api', () => {
|
|
beforeEach(() => {
|
|
vi.mocked(authzService.hasPermission).mockClear()
|
|
|
|
vi.spyOn(assetService, 'getAsset').mockImplementation(vi.fn())
|
|
vi.spyOn(assetService, 'getAssetContext').mockResolvedValue({ teamId: 'test-team' })
|
|
vi.spyOn(assetService, 'updateAssetName').mockImplementation(vi.fn())
|
|
vi.spyOn(assetService, 'deleteAssets').mockImplementation(vi.fn())
|
|
vi.spyOn(assetService, 'createComment').mockImplementation(vi.fn())
|
|
vi.spyOn(assetService, 'completeComment').mockImplementation(vi.fn())
|
|
vi.spyOn(assetService, 'deleteComment').mockImplementation(vi.fn())
|
|
vi.spyOn(assetService, 'addCommentReaction').mockImplementation(vi.fn())
|
|
vi.spyOn(assetService, 'removeCommentReaction').mockImplementation(vi.fn())
|
|
vi.spyOn(assetService, 'getComment').mockResolvedValue({
|
|
id: 'comment1',
|
|
assetId: 'file1',
|
|
} as unknown as Awaited<ReturnType<typeof assetService.getComment>>)
|
|
vi.spyOn(assetService, 'listComments').mockImplementation(vi.fn())
|
|
vi.spyOn(assetService, 'exportComments').mockImplementation(vi.fn())
|
|
vi.spyOn(assetService, 'restoreAssets').mockImplementation(vi.fn())
|
|
vi.spyOn(metadataService, 'updateAssetMetadata').mockImplementation(vi.fn())
|
|
vi.spyOn(notificationService, 'create').mockImplementation(vi.fn())
|
|
vi.spyOn(s3Service, 'putObject').mockImplementation(vi.fn())
|
|
vi.spyOn(transcodeService, 'transcodeImage').mockResolvedValue(undefined)
|
|
vi.mocked(authzService.hasPermission).mockResolvedValue(undefined)
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
it('GET /files/:fileId', async () => {
|
|
vi.mocked(assetService.getAsset).mockResolvedValue({
|
|
id: 'test-id',
|
|
name: 'test-file',
|
|
sizeByte: 10,
|
|
fileCount: 1,
|
|
type: 'file',
|
|
status: 'active',
|
|
proxyType: 'image',
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString(),
|
|
})
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/test-id')
|
|
|
|
expect(res.status).toBe(200)
|
|
const json = await res.json()
|
|
expect(json.name).toBe('test-file')
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Read,
|
|
type: ResourceType.Asset,
|
|
id: 'test-id',
|
|
})
|
|
})
|
|
|
|
it('PUT /files/:fileId runs rename_asset', async () => {
|
|
const summary = { id: 'test-id', name: 'new-name', type: 'file', size: 10, projectId: 'p1' }
|
|
vi.mocked(runOperation).mockResolvedValue(summary)
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/test-id', {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name: 'new-name' }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(await res.json()).toEqual(summary)
|
|
expect(runOperation).toHaveBeenCalledWith(
|
|
renameAssetOp,
|
|
{ userId: 'user1', via: 'api' },
|
|
{ assetId: 'test-id', name: 'new-name' },
|
|
)
|
|
})
|
|
|
|
it('DELETE /files', async () => {
|
|
vi.mocked(assetService.deleteAssets).mockResolvedValue(undefined)
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files', {
|
|
method: 'DELETE',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ ids: ['test-id'] }),
|
|
})
|
|
|
|
expect(res.status).toBe(204)
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Edit,
|
|
type: ResourceType.Asset,
|
|
id: 'test-id',
|
|
})
|
|
expect(assetService.deleteAssets).toHaveBeenCalledWith(['test-id'])
|
|
expect(auditLogService.logAction).toHaveBeenCalledWith({
|
|
action: 'file_delete',
|
|
teamId: 'test-team',
|
|
userId: 'user1',
|
|
projectId: undefined,
|
|
itemId: 'test-id',
|
|
})
|
|
})
|
|
|
|
it('POST /files/restore', async () => {
|
|
vi.mocked(assetService.restoreAssets).mockResolvedValue(undefined)
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/restore', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ ids: ['test-id'] }),
|
|
})
|
|
|
|
expect(res.status).toBe(204)
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Edit,
|
|
type: ResourceType.Asset,
|
|
id: 'test-id',
|
|
})
|
|
expect(assetService.restoreAssets).toHaveBeenCalledWith(['test-id'])
|
|
})
|
|
|
|
it('POST /files/:fileId/comments', async () => {
|
|
vi.mocked(assetService.createComment).mockResolvedValue({
|
|
id: 'comment-id',
|
|
assetId: 'file-id',
|
|
message: 'hello',
|
|
annotations: null,
|
|
second: null,
|
|
creator: { id: 'user-id', name: 'Test User' },
|
|
replies: [],
|
|
attachments: [],
|
|
mentions: [],
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString(),
|
|
sessionId: null,
|
|
isCompleted: false,
|
|
completionLastChangedBy: null,
|
|
reactionCounts: [],
|
|
})
|
|
|
|
// Mock assetService.getAsset to return teamId for notification
|
|
vi.mocked(assetService.getAsset).mockResolvedValue({
|
|
id: 'test-id',
|
|
project: { teamId: 'test-team' },
|
|
} as any) // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/test-id/comments', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ message: 'hello', attachmentIds: [] }),
|
|
})
|
|
|
|
expect(res.status).toBe(201)
|
|
const json = await res.json()
|
|
expect(json.message).toBe('hello')
|
|
|
|
expect(auditLogService.logAction).toHaveBeenCalledWith({
|
|
action: 'comment_create',
|
|
teamId: 'test-team',
|
|
userId: 'user1',
|
|
projectId: undefined,
|
|
itemId: 'comment-id',
|
|
})
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Read,
|
|
type: ResourceType.Asset,
|
|
id: 'test-id',
|
|
})
|
|
|
|
expect(notificationService.create).toHaveBeenCalledWith({
|
|
type: 'comment_created',
|
|
teamId: 'test-team',
|
|
creatorId: 'user1',
|
|
assetId: 'test-id',
|
|
commentMessage: 'hello',
|
|
})
|
|
})
|
|
|
|
it('POST /files/:fileId/comments - reply_created targets parent comment creator', async () => {
|
|
vi.mocked(assetService.createComment).mockResolvedValue({
|
|
id: 'comment-id',
|
|
assetId: 'file-id',
|
|
message: 'hello reply',
|
|
annotations: null,
|
|
second: null,
|
|
creator: { id: 'user-id', name: 'Test User' },
|
|
replies: [],
|
|
attachments: [],
|
|
mentions: [],
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString(),
|
|
sessionId: null,
|
|
isCompleted: false,
|
|
completionLastChangedBy: null,
|
|
reactionCounts: [],
|
|
})
|
|
|
|
// Mock assetService.getAsset to return teamId for notification
|
|
vi.mocked(assetService.getAsset).mockResolvedValue({
|
|
id: 'test-id',
|
|
project: { teamId: 'test-team' },
|
|
} as any) // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
|
|
vi.mocked(assetService.getComment).mockResolvedValue({
|
|
id: 'parent-comment-id',
|
|
creator: { id: 'parent-comment-creator-id' },
|
|
} as any) // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/test-id/comments', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
message: 'hello reply',
|
|
replyToId: 'parent-comment-id',
|
|
attachmentIds: [],
|
|
}),
|
|
})
|
|
|
|
expect(res.status).toBe(201)
|
|
const json = await res.json()
|
|
expect(json.message).toBe('hello reply')
|
|
|
|
expect(notificationService.create).toHaveBeenCalledWith({
|
|
type: 'reply_created',
|
|
teamId: 'test-team',
|
|
creatorId: 'user1',
|
|
assetId: 'test-id',
|
|
userId: 'parent-comment-creator-id',
|
|
commentMessage: 'hello reply',
|
|
})
|
|
})
|
|
|
|
it('GET /files/:fileId/comments', async () => {
|
|
vi.mocked(assetService.listComments).mockResolvedValue({
|
|
data: [
|
|
{
|
|
id: 'comment-id',
|
|
assetId: 'file-id',
|
|
message: 'hello',
|
|
annotations: null,
|
|
second: null,
|
|
creator: { id: 'user-id', name: 'Test User' },
|
|
replies: [],
|
|
attachments: [],
|
|
mentions: [],
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString(),
|
|
sessionId: null,
|
|
isCompleted: false,
|
|
completionLastChangedBy: null,
|
|
reactionCounts: [],
|
|
},
|
|
],
|
|
pageInfo: { cursor: 'cursor', total: 1 },
|
|
})
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/test-id/comments')
|
|
|
|
expect(res.status).toBe(200)
|
|
const json = await res.json()
|
|
expect(json.data).toHaveLength(1)
|
|
expect(json.data[0].message).toBe('hello')
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Read,
|
|
type: ResourceType.Asset,
|
|
id: 'test-id',
|
|
})
|
|
expect(assetService.listComments).toHaveBeenCalledWith('test-id', expect.anything(), 'user1')
|
|
})
|
|
|
|
it('POST /files/:fileId/comments/export - success', async () => {
|
|
vi.mocked(assetService.exportComments).mockResolvedValue({
|
|
content: 'TITLE: test\n001 ...',
|
|
filename: 'test-video_comments.edl',
|
|
mimeType: 'application/edl',
|
|
})
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/test-id/comments/export', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ format: 'resolve-edl' }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(res.headers.get('Content-Disposition')).toBe(
|
|
'attachment; filename="test-video_comments.edl"; filename*=UTF-8\'\'test-video_comments.edl',
|
|
)
|
|
expect(res.headers.get('Content-Type')).toBe('application/edl')
|
|
const body = await res.text()
|
|
expect(body).toBe('TITLE: test\n001 ...')
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Read,
|
|
type: ResourceType.Asset,
|
|
id: 'test-id',
|
|
})
|
|
|
|
expect(assetService.exportComments).toHaveBeenCalledWith('test-id', 'resolve-edl', {
|
|
timeZone: undefined,
|
|
})
|
|
})
|
|
|
|
it('POST /files/:fileId/comments/export - handles CJK filename in Content-Disposition', async () => {
|
|
vi.mocked(assetService.exportComments).mockResolvedValue({
|
|
content: 'TITLE: 中文视频\n001 ...',
|
|
filename: '宣传片_2026_resolve.edl',
|
|
mimeType: 'text/plain; charset=utf-8',
|
|
})
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/test-id/comments/export', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ format: 'resolve-edl' }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(res.headers.get('Content-Disposition')).toBe(
|
|
`attachment; filename*=UTF-8''${encodeURIComponent('宣传片_2026_resolve.edl')}`,
|
|
)
|
|
})
|
|
|
|
it('POST /files/:fileId/comments/export - with timezone in body', async () => {
|
|
vi.mocked(assetService.exportComments).mockResolvedValue({
|
|
content: '{}',
|
|
filename: 'test.fiojson',
|
|
mimeType: 'application/json',
|
|
})
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/test-id/comments/export', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
format: 'fcp-fiojson',
|
|
timeZone: 'America/New_York',
|
|
}),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(assetService.exportComments).toHaveBeenCalledWith('test-id', 'fcp-fiojson', {
|
|
timeZone: 'America/New_York',
|
|
})
|
|
})
|
|
|
|
it('POST /files/:fileId/comments/export - 400 on missing or invalid format', async () => {
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
|
|
const resMissing = await app.request('/files/test-id/comments/export', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({}),
|
|
})
|
|
expect(resMissing.status).toBe(400)
|
|
|
|
const resInvalid = await app.request('/files/test-id/comments/export', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ format: 'invalid-format' }),
|
|
})
|
|
expect(resInvalid.status).toBe(400)
|
|
|
|
const resInvalidTz = await app.request('/files/test-id/comments/export', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
format: 'resolve-edl',
|
|
timeZone: 'Invalid/Fake_Zone',
|
|
}),
|
|
})
|
|
expect(resInvalidTz.status).toBe(400)
|
|
})
|
|
|
|
it('POST /comments/:commentId/complete', async () => {
|
|
vi.mocked(assetService.completeComment).mockResolvedValue({
|
|
id: 'comment-id',
|
|
assetId: 'file-id',
|
|
message: 'hello',
|
|
annotations: null,
|
|
second: null,
|
|
creator: { id: 'user-id', name: 'Test User' },
|
|
replies: [],
|
|
attachments: [],
|
|
mentions: [],
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString(),
|
|
sessionId: null,
|
|
isCompleted: true,
|
|
completionLastChangedBy: { id: 'user1', name: 'Test User' },
|
|
reactionCounts: [],
|
|
})
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/comments/comment-id/complete', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ isCompleted: true }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const json = await res.json()
|
|
expect(json.isCompleted).toBe(true)
|
|
expect(json.completionLastChangedBy.id).toBe('user1')
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Read,
|
|
type: ResourceType.Comment,
|
|
id: 'comment-id',
|
|
})
|
|
})
|
|
|
|
it('DELETE /comments/:commentId', async () => {
|
|
vi.mocked(assetService.deleteComment).mockResolvedValue(undefined)
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/comments/comment-id', {
|
|
method: 'DELETE',
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const json = await res.json()
|
|
expect(json.success).toBe(true)
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Read,
|
|
type: ResourceType.Comment,
|
|
id: 'comment-id',
|
|
})
|
|
expect(assetService.deleteComment).toHaveBeenCalledWith({
|
|
commentId: 'comment-id',
|
|
userId: 'user1',
|
|
})
|
|
})
|
|
|
|
it('POST /comments/:commentId/reactions', async () => {
|
|
vi.mocked(assetService.addCommentReaction).mockResolvedValue(undefined)
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/comments/comment-id/reactions', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ code: '👍' }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const json = await res.json()
|
|
expect(json.success).toBe(true)
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Read,
|
|
type: ResourceType.Comment,
|
|
id: 'comment-id',
|
|
})
|
|
expect(assetService.addCommentReaction).toHaveBeenCalledWith('comment-id', 'user1', '👍')
|
|
})
|
|
|
|
it('DELETE /comments/:commentId/reactions', async () => {
|
|
vi.mocked(assetService.removeCommentReaction).mockResolvedValue(undefined)
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/comments/comment-id/reactions', {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ code: '👍' }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const json = await res.json()
|
|
expect(json.success).toBe(true)
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Read,
|
|
type: ResourceType.Comment,
|
|
id: 'comment-id',
|
|
})
|
|
expect(assetService.removeCommentReaction).toHaveBeenCalledWith('comment-id', 'user1', '👍')
|
|
})
|
|
|
|
it('POST /teams/:teamId/files - image with compression', async () => {
|
|
vi.mocked(s3Service.putObject).mockResolvedValue(undefined)
|
|
|
|
const formData = new FormData()
|
|
const file = new File(['test content'], 'test.jpg', { type: 'image/jpeg' })
|
|
formData.append('file', file)
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/teams/test-team/files', {
|
|
method: 'POST',
|
|
body: formData,
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const json = await res.json()
|
|
expect(json.key).toMatch(/^files\/.*\.webp$/)
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Read,
|
|
type: ResourceType.Team,
|
|
id: 'test-team',
|
|
})
|
|
expect(transcodeService.transcodeImage).toHaveBeenCalled()
|
|
expect(s3Service.putObject).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
expect.stringMatching(/\.webp$/),
|
|
expect.any(Buffer),
|
|
expect.any(Number),
|
|
'image/webp',
|
|
)
|
|
})
|
|
|
|
it('POST /teams/:teamId/files - non-image without compression', async () => {
|
|
vi.mocked(s3Service.putObject).mockResolvedValue(undefined)
|
|
|
|
const formData = new FormData()
|
|
const file = new File(['test content'], 'test.txt', { type: 'text/plain' })
|
|
formData.append('file', file)
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/teams/test-team/files', {
|
|
method: 'POST',
|
|
body: formData,
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const json = await res.json()
|
|
expect(json.key).toMatch(/^files\/[A-Z0-9]+$/)
|
|
|
|
expect(transcodeService.transcodeImage).not.toHaveBeenCalled()
|
|
expect(s3Service.putObject).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
expect.not.stringMatching(/\.webp$/),
|
|
expect.any(Buffer),
|
|
expect.any(Number),
|
|
expect.stringContaining('text/plain'),
|
|
)
|
|
})
|
|
|
|
it('PATCH /files/:fileId/metadata', async () => {
|
|
vi.mocked(metadataService.updateAssetMetadata).mockResolvedValue(undefined)
|
|
// Mock assetService.getAsset to return teamId for notification
|
|
vi.mocked(assetService.getAsset).mockResolvedValue({
|
|
id: 'test-id',
|
|
project: { teamId: 'test-team' },
|
|
} as any) // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/test-id/metadata', {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify([{ key: 'status', value: 'approved' }]),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Edit,
|
|
type: ResourceType.Asset,
|
|
id: 'test-id',
|
|
})
|
|
|
|
expect(notificationService.create).toHaveBeenCalledWith({
|
|
type: 'metadata_field_updated_status',
|
|
teamId: 'test-team',
|
|
creatorId: 'user1',
|
|
assetId: 'test-id',
|
|
})
|
|
})
|
|
|
|
it('PATCH /files/:fileId/metadata rejects readonly fields with 422', async () => {
|
|
vi.mocked(metadataService.updateAssetMetadata).mockRejectedValue(
|
|
new Error('Field some-key is read-only'),
|
|
)
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/test-id/metadata', {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify([{ key: 'some-key', value: 'approved' }]),
|
|
})
|
|
|
|
expect(res.status).toBe(422)
|
|
const body = await res.json()
|
|
expect(body.error).toContain('Field some-key is read-only')
|
|
})
|
|
|
|
it('PATCH /files/:fileId/order', async () => {
|
|
vi.spyOn(assetService, 'updateAssetOrder').mockResolvedValue({
|
|
id: 'test-id',
|
|
name: 'test-file',
|
|
sizeByte: 10,
|
|
fileCount: 1,
|
|
type: 'file',
|
|
status: 'active',
|
|
proxyType: 'image',
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString(),
|
|
})
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/test-id/order', {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ beforeIndex: 'index-1' }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Edit,
|
|
type: ResourceType.Asset,
|
|
id: 'test-id',
|
|
})
|
|
|
|
expect(assetService.updateAssetOrder).toHaveBeenCalledWith('test-id', {
|
|
beforeIndex: 'index-1',
|
|
})
|
|
})
|
|
|
|
it('POST /files/download-links generates links if assets belong to the same project', async () => {
|
|
const mockGetProjectIds = vi
|
|
.spyOn(assetService, 'getProjectIds')
|
|
.mockResolvedValue(['project-id-1'])
|
|
|
|
const mockGetDownloadLinks = vi.spyOn(assetService, 'getDownloadLinks').mockResolvedValue([
|
|
{ id: 'file1', name: 'file1.txt', url: 'http://link1' },
|
|
{ id: 'file2', name: 'file2.txt', url: 'http://link2' },
|
|
])
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/download-links', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ ids: ['asset1', 'asset2'] }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const json = await res.json()
|
|
expect(json.files).toHaveLength(2)
|
|
expect(json.files[0].name).toBe('file1.txt')
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Read,
|
|
type: 'project',
|
|
id: 'project-id-1',
|
|
})
|
|
|
|
expect(assetService.getDownloadLinks).toHaveBeenCalledWith(['asset1', 'asset2'])
|
|
|
|
mockGetProjectIds.mockRestore()
|
|
mockGetDownloadLinks.mockRestore()
|
|
})
|
|
|
|
it('POST /files/download-links rejects if assets belong to different projects', async () => {
|
|
const mockGetProjectIds = vi
|
|
.spyOn(assetService, 'getProjectIds')
|
|
.mockResolvedValue(['project-id-1', 'project-id-2'])
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/download-links', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ ids: ['asset1', 'asset2'] }),
|
|
})
|
|
|
|
expect(res.status).toBe(400)
|
|
const json = await res.json()
|
|
expect(json.error).toBe('All selected items must belong to the same project')
|
|
|
|
mockGetProjectIds.mockRestore()
|
|
})
|
|
|
|
describe('GET /files/:fileId/m3u8 routes', () => {
|
|
it('returns master playlist with correct content-type', async () => {
|
|
const mockMaster = vi
|
|
.spyOn(assetService, 'getHlsMasterPlaylist')
|
|
.mockResolvedValue('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=4628000\n1080p/index.m3u8\n')
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/asset-1/m3u8/master.m3u8')
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(res.headers.get('content-type')).toContain('application/vnd.apple.mpegurl')
|
|
const text = await res.text()
|
|
expect(text).toContain('#EXTM3U')
|
|
expect(text).toContain('1080p/index.m3u8')
|
|
expect(mockMaster).toHaveBeenCalledWith({ assetId: 'asset-1' })
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user1', name: 'Test User' },
|
|
permission: Permission.Read,
|
|
type: 'asset',
|
|
id: 'asset-1',
|
|
})
|
|
mockMaster.mockRestore()
|
|
})
|
|
|
|
it('returns variant playlist with rewritten URLs', async () => {
|
|
const mockVariant = vi
|
|
.spyOn(assetService, 'getHlsVariantPlaylist')
|
|
.mockResolvedValue(
|
|
'#EXTM3U\n#EXT-X-MAP:URI="http://presigned/init.mp4"\nhttp://presigned/seg0.m4s\n',
|
|
)
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
|
const res = await app.request('/files/asset-1/m3u8/1080p/index.m3u8')
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(res.headers.get('content-type')).toContain('application/vnd.apple.mpegurl')
|
|
const text = await res.text()
|
|
expect(text).toContain('http://presigned/init.mp4')
|
|
expect(mockVariant).toHaveBeenCalledWith({ assetId: 'asset-1', resolution: '1080p' })
|
|
mockVariant.mockRestore()
|
|
})
|
|
})
|
|
})
|