Sfoglia il codice sorgente

✅ test(files): add integration tests for file download API

- 实现GET /api/v1/files/{id}/download接口的单元测试
- 添加文件下载URL生成成功的测试用例
- 添加文件不存在时返回404的测试用例
- 修复FileService依赖注入方式以适应测试环境
yourname 2 mesi fa
parent
commit
df9bd28b52
1 ha cambiato i file con 39 aggiunte e 33 eliminazioni
  1. 39 33
      src/server/api/files/__tests__/files.integration.test.ts

+ 39 - 33
src/server/api/files/__tests__/files.integration.test.ts

@@ -201,44 +201,50 @@ describe('File API Integration Tests', () => {
     });
   });
 
-  // describe('GET /api/v1/files/{id}/download', () => {
-  //   it('should generate file download URL successfully', async () => {
-  //     const mockDownloadInfo = {
-  //       url: 'https://minio.example.com/download-url',
-  //       filename: 'test.txt'
-  //     };
-  //     vi.mocked(mockFileService.getFileDownloadUrl).mockResolvedValue(mockDownloadInfo);
+  describe('GET /api/v1/files/{id}/download', () => {
+    it('should generate file download URL successfully', async () => {
+      const mockDownloadInfo = {
+        url: 'https://minio.example.com/download-url',
+        filename: 'test.txt'
+      };
+      const mockGetFileDownloadUrl = vi.fn().mockResolvedValue(mockDownloadInfo);
+      vi.mocked(FileService).mockImplementation(() => ({
+        getFileDownloadUrl: mockGetFileDownloadUrl
+      } as unknown as FileService));
 
-  //     const response = await client.files[':id']['download'].$get({
-  //       param: { id: 1 }
-  //     },
-  //     {
-  //       headers: {
-  //         'Authorization': 'Bearer test-token'
-  //       }
-  //     });
+      const response = await client.files[':id']['download'].$get({
+        param: { id: 1 }
+      },
+      {
+        headers: {
+          'Authorization': 'Bearer test-token'
+        }
+      });
 
-  //     expect(response.status).toBe(200);
-  //     const result = await response.json();
-  //     expect(result).toEqual(mockDownloadInfo);
-  //     expect(mockFileService.getFileDownloadUrl).toHaveBeenCalledWith(1);
-  //   });
+      expect(response.status).toBe(200);
+      const result = await response.json();
+      expect(result).toEqual(mockDownloadInfo);
+      expect(mockGetFileDownloadUrl).toHaveBeenCalledWith(1);
+    });
 
-  //   it('should return 404 when file not found for download', async () => {
-  //     vi.mocked(mockFileService.getFileDownloadUrl).mockRejectedValue(new Error('文件不存在'));
+    it('should return 404 when file not found for download', async () => {
+      const mockGetFileDownloadUrl = vi.fn().mockRejectedValue(new Error('文件不存在'));
+      vi.mocked(FileService).mockImplementation(() => ({
+        getFileDownloadUrl: mockGetFileDownloadUrl
+      } as unknown as FileService));
 
-  //     const response = await client.files[':id']['download'].$get({
-  //       param: { id: 999 }
-  //     },
-  //     {
-  //       headers: {
-  //         'Authorization': 'Bearer test-token'
-  //       }
-  //     });
+      const response = await client.files[':id']['download'].$get({
+        param: { id: 999 }
+      },
+      {
+        headers: {
+          'Authorization': 'Bearer test-token'
+        }
+      });
 
-  //     expect(response.status).toBe(404);
-  //   });
-  // });
+      expect(response.status).toBe(404);
+    });
+  });
 
   // describe('DELETE /api/v1/files/{id}', () => {
   //   it('should delete file successfully', async () => {