2026-05-19 16:05:09 +08:00
|
|
|
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
|
|
|
|
|
import { Hono } from 'hono'
|
|
|
|
|
import folderRoute from './folder'
|
2026-06-01 23:16:22 +08:00
|
|
|
import { assetService } from '@shumai/core/src/asset/asset'
|
|
|
|
|
import { searchService } from '@shumai/core/src/search/search'
|
|
|
|
|
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
|
2026-06-01 23:35:00 +08:00
|
|
|
import { authMiddleware } from './middleware/auth'
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-06-01 23:16:22 +08:00
|
|
|
vi.mock('@shumai/core/src/authz/authz', () => ({
|
2026-05-19 16:05:09 +08:00
|
|
|
authzService: {
|
2026-05-25 23:51:37 +08:00
|
|
|
hasPermission: vi.fn().mockResolvedValue(undefined),
|
2026-05-19 16:05:09 +08:00
|
|
|
},
|
|
|
|
|
Permission: {
|
|
|
|
|
Read: 'Read',
|
|
|
|
|
Edit: 'Edit',
|
|
|
|
|
Admin: 'Admin',
|
|
|
|
|
},
|
2026-05-25 23:51:37 +08:00
|
|
|
ResourceType: {
|
|
|
|
|
Asset: 'asset',
|
|
|
|
|
},
|
2026-05-19 16:05:09 +08:00
|
|
|
}))
|
|
|
|
|
|
2026-06-01 23:35:00 +08:00
|
|
|
vi.mock('./middleware/auth', () => ({
|
2026-05-19 16:05:09 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
authMiddleware: async (c: any, next: any) => {
|
|
|
|
|
c.set('user', { id: 'user1', name: 'Test User' })
|
|
|
|
|
await next()
|
|
|
|
|
},
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
describe('folder api', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.mocked(authzService.hasPermission).mockClear()
|
|
|
|
|
|
|
|
|
|
vi.spyOn(assetService, 'getAsset').mockImplementation(vi.fn())
|
|
|
|
|
vi.spyOn(assetService, 'createAsset').mockImplementation(vi.fn())
|
|
|
|
|
vi.spyOn(assetService, 'updateAssetName').mockImplementation(vi.fn())
|
|
|
|
|
vi.spyOn(assetService, 'deleteAssets').mockImplementation(vi.fn())
|
|
|
|
|
vi.spyOn(assetService, 'restoreAssets').mockImplementation(vi.fn())
|
|
|
|
|
vi.spyOn(assetService, 'listChildren').mockImplementation(vi.fn())
|
|
|
|
|
vi.spyOn(searchService, 'search').mockImplementation(vi.fn())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
vi.restoreAllMocks()
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
it('GET /folders/:folderId', async () => {
|
2026-05-19 16:05:09 +08:00
|
|
|
vi.mocked(assetService.getAsset).mockResolvedValue({
|
|
|
|
|
id: 'test-id',
|
|
|
|
|
name: 'test-folder',
|
|
|
|
|
sizeByte: 10,
|
|
|
|
|
fileCount: 1,
|
|
|
|
|
type: 'folder',
|
|
|
|
|
status: 'active',
|
|
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
|
2026-05-25 23:51:37 +08:00
|
|
|
const res = await app.request('/folders/test-id')
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
|
const json = await res.json()
|
|
|
|
|
expect(json.name).toBe('test-folder')
|
|
|
|
|
|
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
|
|
|
user: { id: 'user1', name: 'Test User' },
|
2026-05-25 23:51:37 +08:00
|
|
|
permission: Permission.Read,
|
|
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id: 'test-id',
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
it('PUT /folders/:folderId', async () => {
|
2026-05-19 16:05:09 +08:00
|
|
|
vi.mocked(assetService.updateAssetName).mockResolvedValue({
|
|
|
|
|
id: 'test-id',
|
|
|
|
|
name: 'new-name',
|
|
|
|
|
sizeByte: 10,
|
|
|
|
|
fileCount: 1,
|
|
|
|
|
type: 'folder',
|
|
|
|
|
status: 'active',
|
|
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
|
2026-05-25 23:51:37 +08:00
|
|
|
const res = await app.request('/folders/test-id', {
|
2026-05-19 16:05:09 +08:00
|
|
|
method: 'PUT',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ name: 'new-name' }),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
|
const json = await res.json()
|
|
|
|
|
expect(json.name).toBe('new-name')
|
|
|
|
|
|
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
|
|
|
user: { id: 'user1', name: 'Test User' },
|
2026-05-25 23:51:37 +08:00
|
|
|
permission: Permission.Edit,
|
|
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id: 'test-id',
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
it('POST /folders', async () => {
|
2026-05-19 16:05:09 +08:00
|
|
|
vi.mocked(assetService.createAsset).mockResolvedValue({
|
|
|
|
|
id: 'new-id',
|
|
|
|
|
name: 'new-folder',
|
|
|
|
|
sizeByte: 0,
|
|
|
|
|
fileCount: 0,
|
|
|
|
|
type: 'folder',
|
|
|
|
|
status: 'active',
|
|
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
|
2026-05-25 23:51:37 +08:00
|
|
|
const res = await app.request('/folders', {
|
2026-05-19 16:05:09 +08:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ name: 'new-folder', parentId: 'parent-id' }),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
|
const json = await res.json()
|
|
|
|
|
expect(json.name).toBe('new-folder')
|
|
|
|
|
|
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
|
|
|
user: { id: 'user1', name: 'Test User' },
|
2026-05-25 23:51:37 +08:00
|
|
|
permission: Permission.Edit,
|
|
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id: 'parent-id',
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
it('GET /folders/:folderId/children', async () => {
|
2026-05-19 16:05:09 +08:00
|
|
|
vi.mocked(assetService.listChildren).mockResolvedValue({
|
|
|
|
|
data: [
|
|
|
|
|
{
|
|
|
|
|
id: 'child-id',
|
|
|
|
|
name: 'child-folder',
|
|
|
|
|
sizeByte: 0,
|
|
|
|
|
fileCount: 0,
|
|
|
|
|
type: 'folder',
|
|
|
|
|
status: 'active',
|
|
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
pageInfo: { total: 1, cursor: '' },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
|
2026-05-25 23:51:37 +08:00
|
|
|
const res = await app.request('/folders/test-id/children?assetType=folder')
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
|
const json = await res.json()
|
|
|
|
|
expect(json.data).toHaveLength(1)
|
|
|
|
|
expect(json.data[0].name).toBe('child-folder')
|
|
|
|
|
|
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
|
|
|
user: { id: 'user1', name: 'Test User' },
|
2026-05-25 23:51:37 +08:00
|
|
|
permission: Permission.Read,
|
|
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id: 'test-id',
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
it('DELETE /folders', async () => {
|
2026-05-19 16:05:09 +08:00
|
|
|
vi.mocked(assetService.deleteAssets).mockResolvedValue(undefined)
|
|
|
|
|
|
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
|
2026-05-25 23:51:37 +08:00
|
|
|
const res = await app.request('/folders', {
|
2026-05-19 16:05:09 +08:00
|
|
|
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' },
|
2026-05-25 23:51:37 +08:00
|
|
|
permission: Permission.Edit,
|
|
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id: 'test-id',
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
expect(assetService.deleteAssets).toHaveBeenCalledWith(['test-id'])
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
it('POST /folders/restore', async () => {
|
2026-05-19 16:05:09 +08:00
|
|
|
vi.mocked(assetService.restoreAssets).mockResolvedValue(undefined)
|
|
|
|
|
|
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
|
2026-05-25 23:51:37 +08:00
|
|
|
const res = await app.request('/folders/restore', {
|
2026-05-19 16:05:09 +08:00
|
|
|
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' },
|
2026-05-25 23:51:37 +08:00
|
|
|
permission: Permission.Edit,
|
|
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id: 'test-id',
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
expect(assetService.restoreAssets).toHaveBeenCalledWith(['test-id'])
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
it('POST /folders/:folderId/search', async () => {
|
2026-05-19 16:05:09 +08:00
|
|
|
vi.mocked(searchService.search).mockResolvedValue({
|
|
|
|
|
data: [
|
|
|
|
|
{
|
|
|
|
|
id: 'result-id',
|
|
|
|
|
name: 'result-folder',
|
|
|
|
|
sizeByte: 0,
|
|
|
|
|
fileCount: 0,
|
|
|
|
|
type: 'folder',
|
|
|
|
|
status: 'active',
|
|
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
pageInfo: { total: 1, cursor: '' },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
|
2026-05-25 23:51:37 +08:00
|
|
|
const res = await app.request('/folders/test-id/search', {
|
2026-05-19 16:05:09 +08:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
conditions: [{ field: 'name', operator: 'eq', value: 'result-folder' }],
|
|
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
|
const json = await res.json()
|
|
|
|
|
expect(json.data).toHaveLength(1)
|
|
|
|
|
expect(json.data[0].name).toBe('result-folder')
|
|
|
|
|
|
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
|
|
|
user: { id: 'user1', name: 'Test User' },
|
2026-05-25 23:51:37 +08:00
|
|
|
permission: Permission.Read,
|
|
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id: 'test-id',
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
it('PATCH /folders/:folderId/order', async () => {
|
2026-05-19 16:05:09 +08:00
|
|
|
vi.spyOn(assetService, 'updateAssetOrder').mockResolvedValue({
|
|
|
|
|
id: 'test-id',
|
|
|
|
|
name: 'test-folder',
|
|
|
|
|
sizeByte: 10,
|
|
|
|
|
fileCount: 1,
|
|
|
|
|
type: 'folder',
|
|
|
|
|
status: 'active',
|
|
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
|
2026-05-25 23:51:37 +08:00
|
|
|
const res = await app.request('/folders/test-id/order', {
|
2026-05-19 16:05:09 +08:00
|
|
|
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' },
|
2026-05-25 23:51:37 +08:00
|
|
|
permission: Permission.Edit,
|
|
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id: 'test-id',
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(assetService.updateAssetOrder).toHaveBeenCalledWith('test-id', {
|
|
|
|
|
beforeIndex: 'index-1',
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|