Browse Source

✅ test(files): 实现文件删除API的集成测试

- 取消DELETE /api/v1/files/{id}测试的注释并完善实现
- 添加成功删除文件的测试用例,验证响应状态和返回消息
- 添加文件不存在时返回404的测试用例
- 添加处理删除错误返回500的测试用例
- 使用vi.mocked正确模拟FileService依赖
- 验证文件删除服务方法的调用参数和次数
yourname 2 months ago
parent
commit
4f383db032
1 changed files with 50 additions and 41 deletions
  1. 50 41
      src/server/api/files/__tests__/files.integration.test.ts

+ 50 - 41
src/server/api/files/__tests__/files.integration.test.ts

@@ -246,55 +246,64 @@ describe('File API Integration Tests', () => {
     });
   });
 
-  // describe('DELETE /api/v1/files/{id}', () => {
-  //   it('should delete file successfully', async () => {
-  //     vi.mocked(mockFileService.deleteFile).mockResolvedValue(true);
+  describe('DELETE /api/v1/files/{id}', () => {
+    it('should delete file successfully', async () => {
+      const mockDeleteFile = vi.fn().mockResolvedValue(true);
+      vi.mocked(FileService).mockImplementation(() => ({
+        deleteFile: mockDeleteFile
+      } as unknown as FileService));
 
-  //     const response = await client.files[':id'].$delete({
-  //       param: { id: 1 }
-  //     },
-  //     {
-  //       headers: {
-  //         'Authorization': 'Bearer test-token'
-  //       }
-  //     });
+      const response = await client.files[':id'].$delete({
+        param: { id: 1 }
+      },
+      {
+        headers: {
+          'Authorization': 'Bearer test-token'
+        }
+      });
 
-  //     expect(response.status).toBe(200);
-  //     const result = await response.json();
-  //     expect(result).toEqual({ success: true });
-  //     expect(mockFileService.deleteFile).toHaveBeenCalledWith(1);
-  //   });
+      expect(response.status).toBe(200);
+      const result = await response.json();
+      expect(result).toEqual({ success: true, message: '文件删除成功' });
+      expect(mockDeleteFile).toHaveBeenCalledWith(1);
+    });
 
-  //   it('should return 404 when file not found for deletion', async () => {
-  //     vi.mocked(mockFileService.deleteFile).mockRejectedValue(new Error('文件不存在'));
+    it('should return 404 when file not found for deletion', async () => {
+      const mockDeleteFile = vi.fn().mockRejectedValue(new Error('文件不存在'));
+      vi.mocked(FileService).mockImplementation(() => ({
+        deleteFile: mockDeleteFile
+      } as unknown as FileService));
 
-  //     const response = await client.files[':id'].$delete({
-  //       param: { id: 999 }
-  //     },
-  //     {
-  //       headers: {
-  //         'Authorization': 'Bearer test-token'
-  //       }
-  //     });
+      const response = await client.files[':id'].$delete({
+        param: { id: 999 }
+      },
+      {
+        headers: {
+          'Authorization': 'Bearer test-token'
+        }
+      });
 
-  //     expect(response.status).toBe(404);
-  //   });
+      expect(response.status).toBe(404);
+    });
 
-  //   it('should handle deletion errors', async () => {
-  //     vi.mocked(mockFileService.deleteFile).mockRejectedValue(new Error('删除失败'));
+    it('should handle deletion errors', async () => {
+      const mockDeleteFile = vi.fn().mockRejectedValue(new Error('删除失败'));
+      vi.mocked(FileService).mockImplementation(() => ({
+        deleteFile: mockDeleteFile
+      } as unknown as FileService));
 
-  //     const response = await client.files[':id'].$delete({
-  //       param: { id: 1 }
-  //     },
-  //     {
-  //       headers: {
-  //         'Authorization': 'Bearer test-token'
-  //       }
-  //     });
+      const response = await client.files[':id'].$delete({
+        param: { id: 1 }
+      },
+      {
+        headers: {
+          'Authorization': 'Bearer test-token'
+        }
+      });
 
-  //     expect(response.status).toBe(500);
-  //   });
-  // });
+      expect(response.status).toBe(500);
+    });
+  });
 
   // describe('POST /api/v1/files/multipart-policy', () => {
   //   it('should generate multipart upload policy successfully', async () => {