Selaa lähdekoodia

✅ test(files): 实现文件API的CRUD操作测试

- 恢复并完善文件列表查询测试,验证分页数据结构
- 实现文件ID查询测试,增加错误调试信息输出
- 完善文件搜索测试,验证搜索参数传递和结果返回
- 使用vi.mocked重构服务模拟方式,提高测试可靠性
yourname 2 kuukautta sitten
vanhempi
sitoutus
cecfd4b0d8
1 muutettua tiedostoa jossa 109 lisäystä ja 88 poistoa
  1. 109 88
      src/server/api/files/__tests__/files.integration.test.ts

+ 109 - 88
src/server/api/files/__tests__/files.integration.test.ts

@@ -477,92 +477,113 @@ describe('File API Integration Tests', () => {
     });
   });
 
-  // describe('CRUD Operations', () => {
-  //   it('should list files successfully', async () => {
-  //     const mockFiles = [
-  //       {
-  //         id: 1,
-  //         name: 'file1.txt',
-  //         type: 'text/plain',
-  //         size: 1024,
-  //         uploadUserId: 1
-  //       },
-  //       {
-  //         id: 2,
-  //         name: 'file2.txt',
-  //         type: 'text/plain',
-  //         size: 2048,
-  //         uploadUserId: 1
-  //       }
-  //     ];
-
-  //     vi.spyOn(mockFileService, 'getList').mockResolvedValue([mockFiles as File[], mockFiles.length]);
-
-  //     const response = await client.files.$get({
-  //       query: {}
-  //     },
-  //     {
-  //       headers: {
-  //         'Authorization': 'Bearer test-token'
-  //       }
-  //     });
-
-  //     expect(response.status).toBe(200);
-  //     const result = await response.json();
-  //     expect(result).toEqual(mockFiles);
-  //   });
-
-  //   it('should get file by ID successfully', async () => {
-  //     const mockFile = {
-  //       id: 1,
-  //       name: 'file.txt',
-  //       type: 'text/plain',
-  //       size: 1024,
-  //       uploadUserId: 1
-  //     };
-
-  //     vi.spyOn(mockFileService, 'getById').mockResolvedValue(mockFile as File);
-
-  //     const response = await client.files[':id'].$get({
-  //       param: { id: 1 }
-  //     },
-  //     {
-  //       headers: {
-  //         'Authorization': 'Bearer test-token'
-  //       }
-  //     });
-
-  //     expect(response.status).toBe(200);
-  //     const result = await response.json();
-  //     expect(result).toEqual(mockFile);
-  //   });
-
-  //   it('should search files successfully', async () => {
-  //     const mockFiles = [
-  //       {
-  //         id: 1,
-  //         name: 'document.pdf',
-  //         type: 'application/pdf',
-  //         size: 1024,
-  //         uploadUserId: 1
-  //       }
-  //     ];
-
-  //     vi.spyOn(mockFileService, 'getList').mockResolvedValue([mockFiles as File[], mockFiles.length]);
-
-  //     const response = await client.files.$get({
-  //       query: { keyword: 'document' }
-  //     },
-  //     {
-  //       headers: {
-  //         'Authorization': 'Bearer test-token'
-  //       }
-  //     });
-
-  //     expect(response.status).toBe(200);
-  //     const result = await response.json();
-  //     expect(result).toEqual(mockFiles);
-  //     expect(mockFileService.getList).toHaveBeenCalledWith(1, 10, 'document', ['name', 'type', 'description'], undefined, [], {}, undefined);
-  //   });
-  // });
+  describe('CRUD Operations', () => {
+    it('should list files successfully', async () => {
+      const mockFiles = [
+        {
+          id: 1,
+          name: 'file1.txt',
+          type: 'text/plain',
+          size: 1024,
+          uploadUserId: 1
+        },
+        {
+          id: 2,
+          name: 'file2.txt',
+          type: 'text/plain',
+          size: 2048,
+          uploadUserId: 1
+        }
+      ];
+
+      const mockGetList = vi.fn().mockResolvedValue([mockFiles as File[], mockFiles.length]);
+      vi.mocked(FileService).mockImplementation(() => ({
+        getList: mockGetList
+      } as unknown as FileService));
+
+      const response = await client.files.$get({
+        query: {}
+      },
+      {
+        headers: {
+          'Authorization': 'Bearer test-token'
+        }
+      });
+
+      expect(response.status).toBe(200);
+      const result = await response.json();
+      expect(result).toEqual({
+        data: mockFiles,
+        pagination: {
+          current: 1,
+          pageSize: 10,
+          total: mockFiles.length
+        }
+      });
+    });
+
+    it('should get file by ID successfully', async () => {
+      const mockFile = {
+        id: 1,
+        name: 'file.txt',
+        type: 'text/plain',
+        size: 1024,
+        uploadUserId: 1
+      };
+
+      const mockGetById = vi.fn().mockResolvedValue(mockFile as File);
+      vi.mocked(FileService).mockImplementation(() => ({
+        getById: mockGetById
+      } as unknown as FileService));
+
+      const response = await client.files[':id'].$get({
+        param: { id: 1 }
+      },
+      {
+        headers: {
+          'Authorization': 'Bearer test-token'
+        }
+      });
+
+      if (response.status !== 200) {
+        const error = await response.json();
+        console.debug('Error response:', JSON.stringify(error, null, 2));
+        console.debug('Response status:', response.status);
+      }
+      expect(response.status).toBe(200);
+      const result = await response.json();
+      expect(result).toEqual(mockFile);
+    });
+
+    it('should search files successfully', async () => {
+      const mockFiles = [
+        {
+          id: 1,
+          name: 'document.pdf',
+          type: 'application/pdf',
+          size: 1024,
+          uploadUserId: 1
+        }
+      ];
+
+      const mockGetList = vi.fn().mockResolvedValue([mockFiles as File[], mockFiles.length]);
+      vi.mocked(FileService).mockImplementation(() => ({
+        getList: mockGetList
+      } as unknown as FileService));
+
+      const response = await client.files.$get({
+        query: { keyword: 'document' }
+      },
+      {
+        headers: {
+          'Authorization': 'Bearer test-token'
+        }
+      });
+
+      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);
+    });
+  });
 });