Browse Source

✅ test(files): refactor file API integration tests to use client

- replace direct app.request calls with client.files API calls
- remove unused mockMinioService variable
- update authMiddleware mock to be async
- fix header parameter name (header -> headers)
- update search test to use getList instead of search method
- adjust test parameters to match API client requirements
yourname 2 months ago
parent
commit
e458995c63
1 changed files with 85 additions and 63 deletions
  1. 85 63
      src/server/api/files/__tests__/files.integration.test.ts

+ 85 - 63
src/server/api/files/__tests__/files.integration.test.ts

@@ -19,17 +19,17 @@ vi.mock('@/server/middleware/auth.middleware');
 describe('File API Integration Tests', () => {
   let client: ReturnType<typeof testClient<typeof fileApiRoutes>>['api']['v1'];
   let mockFileService: FileService;
-  let mockMinioService: MinioService;
   let mockDataSource: DataSource;
 
   beforeEach(async () => {
     vi.clearAllMocks();
 
     mockDataSource = {} as DataSource;
-    mockMinioService = new MinioService();
 
     // Mock auth middleware to bypass authentication
-    vi.mocked(authMiddleware).mockImplementation((_, next) => next());
+    vi.mocked(authMiddleware).mockImplementation(async (_, next) => {
+      await next();
+    });
 
     // Get the mocked FileService instance
     mockFileService = new FileService(mockDataSource);
@@ -71,8 +71,10 @@ describe('File API Integration Tests', () => {
       mockFileService.createFile = vi.fn().mockResolvedValue(mockResponse);
 
       const response = await client.files['upload-policy'].$post({
-        json: mockFileData,
-        header: {
+        json: mockFileData
+      },
+      {
+        headers: {
           'Authorization': 'Bearer test-token'
         }
       });
@@ -94,13 +96,13 @@ describe('File API Integration Tests', () => {
         type: 'text/plain'
       };
 
-      const response = await app.request('/api/v1/files/upload-policy', {
-        method: 'POST',
+      const response = await client.files['upload-policy'].$post({
+        json: invalidData
+      },
+      {
         headers: {
-          'Content-Type': 'application/json',
           'Authorization': 'Bearer test-token'
-        },
-        body: JSON.stringify(invalidData)
+        }
       });
 
       expect(response.status).toBe(400);
@@ -115,13 +117,13 @@ describe('File API Integration Tests', () => {
 
       mockFileService.createFile = vi.fn().mockRejectedValue(new Error('Service error'));
 
-      const response = await app.request('/api/v1/files/upload-policy', {
-        method: 'POST',
+      const response = await client.files['upload-policy'].$post({
+        json: mockFileData
+      },
+      {
         headers: {
-          'Content-Type': 'application/json',
           'Authorization': 'Bearer test-token'
-        },
-        body: JSON.stringify(mockFileData)
+        }
       });
 
       expect(response.status).toBe(500);
@@ -133,8 +135,10 @@ describe('File API Integration Tests', () => {
       const mockUrl = 'https://minio.example.com/presigned-url';
       vi.mocked(mockFileService.getFileUrl).mockResolvedValue(mockUrl);
 
-      const response = await app.request('/api/v1/files/1/url', {
-        method: 'GET',
+      const response = await client.files[':id']['url'].$get({
+        param: { id: 1 }
+      },
+      {
         headers: {
           'Authorization': 'Bearer test-token'
         }
@@ -149,8 +153,10 @@ describe('File API Integration Tests', () => {
     it('should return 404 when file not found', async () => {
       vi.mocked(mockFileService.getFileUrl).mockRejectedValue(new Error('文件不存在'));
 
-      const response = await app.request('/api/v1/files/999/url', {
-        method: 'GET',
+      const response = await client.files[':id']['url'].$get({
+        param: { id: 999 }
+      },
+      {
         headers: {
           'Authorization': 'Bearer test-token'
         }
@@ -168,8 +174,10 @@ describe('File API Integration Tests', () => {
       };
       vi.mocked(mockFileService.getFileDownloadUrl).mockResolvedValue(mockDownloadInfo);
 
-      const response = await app.request('/api/v1/files/1/download', {
-        method: 'GET',
+      const response = await client.files[':id']['download'].$get({
+        param: { id: 1 }
+      },
+      {
         headers: {
           'Authorization': 'Bearer test-token'
         }
@@ -184,8 +192,10 @@ describe('File API Integration Tests', () => {
     it('should return 404 when file not found for download', async () => {
       vi.mocked(mockFileService.getFileDownloadUrl).mockRejectedValue(new Error('文件不存在'));
 
-      const response = await app.request('/api/v1/files/999/download', {
-        method: 'GET',
+      const response = await client.files[':id']['download'].$get({
+        param: { id: 999 }
+      },
+      {
         headers: {
           'Authorization': 'Bearer test-token'
         }
@@ -199,8 +209,10 @@ describe('File API Integration Tests', () => {
     it('should delete file successfully', async () => {
       vi.mocked(mockFileService.deleteFile).mockResolvedValue(true);
 
-      const response = await app.request('/api/v1/files/1', {
-        method: 'DELETE',
+      const response = await client.files[':id'].$delete({
+        param: { id: 1 }
+      },
+      {
         headers: {
           'Authorization': 'Bearer test-token'
         }
@@ -215,8 +227,10 @@ describe('File API Integration Tests', () => {
     it('should return 404 when file not found for deletion', async () => {
       vi.mocked(mockFileService.deleteFile).mockRejectedValue(new Error('文件不存在'));
 
-      const response = await app.request('/api/v1/files/999', {
-        method: 'DELETE',
+      const response = await client.files[':id'].$delete({
+        param: { id: 999 }
+      },
+      {
         headers: {
           'Authorization': 'Bearer test-token'
         }
@@ -228,8 +242,10 @@ describe('File API Integration Tests', () => {
     it('should handle deletion errors', async () => {
       vi.mocked(mockFileService.deleteFile).mockRejectedValue(new Error('删除失败'));
 
-      const response = await app.request('/api/v1/files/1', {
-        method: 'DELETE',
+      const response = await client.files[':id'].$delete({
+        param: { id: 1 }
+      },
+      {
         headers: {
           'Authorization': 'Bearer test-token'
         }
@@ -266,13 +282,13 @@ describe('File API Integration Tests', () => {
 
       vi.mocked(mockFileService.createMultipartUploadPolicy).mockResolvedValue(mockResponse);
 
-      const response = await app.request('/api/v1/files/multipart-policy', {
-        method: 'POST',
+      const response = await client.files['multipart-policy'].$post({
+        json: mockRequestData
+      },
+      {
         headers: {
-          'Content-Type': 'application/json',
           'Authorization': 'Bearer test-token'
-        },
-        body: JSON.stringify(mockRequestData)
+        }
       });
 
       expect(response.status).toBe(200);
@@ -295,13 +311,13 @@ describe('File API Integration Tests', () => {
         // Missing required fields
       };
 
-      const response = await app.request('/api/v1/files/multipart-policy', {
-        method: 'POST',
+      const response = await client.files['multipart-policy'].$post({
+        json: invalidData
+      },
+      {
         headers: {
-          'Content-Type': 'application/json',
           'Authorization': 'Bearer test-token'
-        },
-        body: JSON.stringify(invalidData)
+        }
       });
 
       expect(response.status).toBe(400);
@@ -329,13 +345,13 @@ describe('File API Integration Tests', () => {
 
       vi.mocked(mockFileService.completeMultipartUpload).mockResolvedValue(mockResponse);
 
-      const response = await app.request('/api/v1/files/multipart-complete', {
-        method: 'POST',
+      const response = await client.files['multipart-complete'].$post({
+        json: mockCompleteData
+      },
+      {
         headers: {
-          'Content-Type': 'application/json',
           'Authorization': 'Bearer test-token'
-        },
-        body: JSON.stringify(mockCompleteData)
+        }
       });
 
       expect(response.status).toBe(200);
@@ -350,13 +366,13 @@ describe('File API Integration Tests', () => {
         // Missing required fields
       };
 
-      const response = await app.request('/api/v1/files/multipart-complete', {
-        method: 'POST',
+      const response = await client.files['multipart-complete'].$post({
+        json: invalidData
+      },
+      {
         headers: {
-          'Content-Type': 'application/json',
           'Authorization': 'Bearer test-token'
-        },
-        body: JSON.stringify(invalidData)
+        }
       });
 
       expect(response.status).toBe(400);
@@ -372,13 +388,13 @@ describe('File API Integration Tests', () => {
 
       vi.mocked(mockFileService.completeMultipartUpload).mockRejectedValue(new Error('Completion failed'));
 
-      const response = await app.request('/api/v1/files/multipart-complete', {
-        method: 'POST',
+      const response = await client.files['multipart-complete'].$post({
+        json: completeData
+      },
+      {
         headers: {
-          'Content-Type': 'application/json',
           'Authorization': 'Bearer test-token'
-        },
-        body: JSON.stringify(completeData)
+        }
       });
 
       expect(response.status).toBe(500);
@@ -404,10 +420,12 @@ describe('File API Integration Tests', () => {
         }
       ];
 
-      vi.spyOn(mockFileService, 'getAll').mockResolvedValue(mockFiles as File[]);
+      vi.spyOn(mockFileService, 'getList').mockResolvedValue(mockFiles as File[]);
 
-      const response = await app.request('/api/v1/files', {
-        method: 'GET',
+      const response = await client.files.$get({
+        query: {}
+      },
+      {
         headers: {
           'Authorization': 'Bearer test-token'
         }
@@ -429,8 +447,10 @@ describe('File API Integration Tests', () => {
 
       vi.spyOn(mockFileService, 'getById').mockResolvedValue(mockFile as File);
 
-      const response = await app.request('/api/v1/files/1', {
-        method: 'GET',
+      const response = await client.files[':id'].$get({
+        param: { id: 1 }
+      },
+      {
         headers: {
           'Authorization': 'Bearer test-token'
         }
@@ -452,10 +472,12 @@ describe('File API Integration Tests', () => {
         }
       ];
 
-      vi.spyOn(mockFileService, 'search').mockResolvedValue(mockFiles as File[]);
+      vi.spyOn(mockFileService, 'getList').mockResolvedValue(mockFiles as File[]);
 
-      const response = await app.request('/api/v1/files?search=document', {
-        method: 'GET',
+      const response = await client.files.$get({
+        query: { keyword: 'document' }
+      },
+      {
         headers: {
           'Authorization': 'Bearer test-token'
         }
@@ -464,7 +486,7 @@ describe('File API Integration Tests', () => {
       expect(response.status).toBe(200);
       const result = await response.json();
       expect(result).toEqual(mockFiles);
-      expect(mockFileService.search).toHaveBeenCalledWith('document', ['name', 'type', 'description']);
+      expect(mockFileService.getList).toHaveBeenCalledWith(1, 10, 'document', ['name', 'type', 'description'], undefined, [], {}, undefined);
     });
   });
 });