ソースを参照

✅ test(files): update file API integration tests

- 更新ConcreteCrudService的导入路径和mock实现
- 扩展文件列表测试的mock数据,添加完整的文件属性
- 完善测试结果断言,验证日期字段的ISO格式转换
- 修复查询参数验证逻辑,确保正确调用ConcreteCrudService方法
yourname 2 ヶ月 前
コミット
f6e1654d6d
1 ファイル変更89 行追加22 行削除
  1. 89 22
      src/server/api/files/__tests__/files.integration.test.ts

+ 89 - 22
src/server/api/files/__tests__/files.integration.test.ts

@@ -5,12 +5,12 @@ import { FileService } from '@/server/modules/files/file.service';
 import { authMiddleware } from '@/server/middleware/auth.middleware';
 import { fileApiRoutes } from '@/server/api';
 import { AppDataSource } from '@/server/data-source';
-import { ConcreteCrudService } from '@/server/utils/generic-crud.routes';
+import { ConcreteCrudService } from '@/server/utils/concrete-crud.service';
 
 vi.mock('@/server/modules/files/file.service');
 vi.mock('@/server/middleware/auth.middleware');
 vi.mock('@/server/data-source');
-// vi.mock('@/server/utils/generic-crud.routes');
+vi.mock('@/server/utils/concrete-crud.service');
 
 describe('File API Integration Tests', () => {
   let client: ReturnType<typeof testClient<typeof fileApiRoutes>>['api']['v1'];
@@ -50,6 +50,7 @@ describe('File API Integration Tests', () => {
     });
 
 
+
     client = testClient(fileApiRoutes).api.v1;
   });
 
@@ -490,21 +491,41 @@ describe('File API Integration Tests', () => {
           name: 'file1.txt',
           type: 'text/plain',
           size: 1024,
-          uploadUserId: 1
+          path: '/uploads/file1.txt',
+          fullUrl: 'https://minio.example.com/d8dai/uploads/file1.txt',
+          description: null,
+          uploadUserId: 1,
+          uploadUser: user1Response,
+          uploadTime: new Date(),
+          lastUpdated: null,
+          createdAt: new Date(),
+          updatedAt: new Date()
         },
         {
           id: 2,
           name: 'file2.txt',
           type: 'text/plain',
           size: 2048,
-          uploadUserId: 1
+          path: '/uploads/file2.txt',
+          fullUrl: 'https://minio.example.com/d8dai/uploads/file2.txt',
+          description: null,
+          uploadUserId: 1,
+          uploadUser: user1Response,
+          uploadTime: new Date(),
+          lastUpdated: null,
+          createdAt: new Date(),
+          updatedAt: new Date()
         }
       ];
 
-      const mockGetList = vi.fn().mockResolvedValue([mockFiles as unknown as File[], mockFiles.length]);
-      vi.mocked(FileService).mockImplementation(() => ({
-        getList: mockGetList
-      } as unknown as FileService));
+      // 设置ConcreteCrudService的mock返回数据
+      vi.mocked(ConcreteCrudService).mockImplementation(() => ({
+        getList: vi.fn().mockResolvedValue([mockFiles, mockFiles.length]),
+        getById: vi.fn().mockResolvedValue(null),
+        create: vi.fn(),
+        update: vi.fn(),
+        delete: vi.fn()
+      } as unknown as ConcreteCrudService<any>));
 
       const response = await client.files.$get({
         query: {}
@@ -523,7 +544,12 @@ describe('File API Integration Tests', () => {
       expect(response.status).toBe(200);
       const result = await response.json();
       expect(result).toEqual({
-        data: mockFiles,
+        data: mockFiles.map(file => ({
+          ...file,
+          createdAt: file.createdAt.toISOString(),
+          updatedAt: file.updatedAt.toISOString(),
+          uploadTime: file.uploadTime.toISOString()
+        })),
         pagination: {
           current: 1,
           pageSize: 10,
@@ -538,13 +564,25 @@ describe('File API Integration Tests', () => {
         name: 'file.txt',
         type: 'text/plain',
         size: 1024,
-        uploadUserId: 1
+        path: '/uploads/file.txt',
+        fullUrl: 'https://minio.example.com/d8dai/uploads/file.txt',
+        description: null,
+        uploadUserId: 1,
+        uploadUser: user1Response,
+        uploadTime: new Date(),
+        lastUpdated: null,
+        createdAt: new Date(),
+        updatedAt: new Date()
       };
 
-      const mockGetById = vi.fn().mockResolvedValue(mockFile as unknown as File);
-      vi.mocked(FileService).mockImplementation(() => ({
-        getById: mockGetById
-      } as unknown as FileService));
+      // 设置ConcreteCrudService的mock返回数据
+      vi.mocked(ConcreteCrudService).mockImplementation(() => ({
+        getList: vi.fn().mockResolvedValue([[], 0]),
+        getById: vi.fn().mockResolvedValue(mockFile),
+        create: vi.fn(),
+        update: vi.fn(),
+        delete: vi.fn()
+      } as unknown as ConcreteCrudService<any>));
 
       const response = await client.files[':id'].$get({
         param: { id: 1 }
@@ -562,7 +600,12 @@ describe('File API Integration Tests', () => {
       }
       expect(response.status).toBe(200);
       const result = await response.json();
-      expect(result).toEqual(mockFile);
+      expect(result).toEqual({
+        ...mockFile,
+        createdAt: mockFile.createdAt.toISOString(),
+        updatedAt: mockFile.updatedAt.toISOString(),
+        uploadTime: mockFile.uploadTime.toISOString()
+      });
     });
 
     it('should search files successfully', async () => {
@@ -572,14 +615,26 @@ describe('File API Integration Tests', () => {
           name: 'document.pdf',
           type: 'application/pdf',
           size: 1024,
-          uploadUserId: 1
+          path: '/uploads/document.pdf',
+          fullUrl: 'https://minio.example.com/d8dai/uploads/document.pdf',
+          description: null,
+          uploadUserId: 1,
+          uploadUser: user1Response,
+          uploadTime: new Date(),
+          lastUpdated: null,
+          createdAt: new Date(),
+          updatedAt: new Date()
         }
       ];
 
-      const mockGetList = vi.fn().mockResolvedValue([mockFiles as unknown as File[], mockFiles.length]);
-      vi.mocked(FileService).mockImplementation(() => ({
-        getList: mockGetList
-      } as unknown as FileService));
+      // 设置ConcreteCrudService的mock返回数据
+      vi.mocked(ConcreteCrudService).mockImplementation(() => ({
+        getList: vi.fn().mockResolvedValue([mockFiles, mockFiles.length]),
+        getById: vi.fn().mockResolvedValue(null),
+        create: vi.fn(),
+        update: vi.fn(),
+        delete: vi.fn()
+      } as unknown as ConcreteCrudService<any>));
 
       const response = await client.files.$get({
         query: { keyword: 'document' }
@@ -592,8 +647,20 @@ describe('File API Integration Tests', () => {
 
       expect(response.status).toBe(200);
       const result = await response.json();
-      expect(result).toEqual(mockFiles);
-      expect(mockGetList).toHaveBeenCalledWith(1, 10, 'document', ['name', 'type', 'description'], undefined, [], {}, undefined);
+      expect(result).toEqual({
+        data: mockFiles.map(file => ({
+          ...file,
+          createdAt: file.createdAt.toISOString(),
+          updatedAt: file.updatedAt.toISOString(),
+          uploadTime: file.uploadTime.toISOString()
+        })),
+        pagination: {
+          current: 1,
+          pageSize: 10,
+          total: mockFiles.length
+        }
+      });
+      expect(vi.mocked(ConcreteCrudService).mock.results[0].value.getList).toHaveBeenCalledWith(1, 10, 'document', ['name', 'type', 'description'], undefined, ['uploadUser'], { id: 'DESC' }, undefined);
     });
   });
 });