浏览代码

✅ test(files): 实现文件分片上传策略生成接口测试

- 恢复并完善POST /api/v1/files/multipart-policy接口的测试用例
- 添加成功生成分片上传策略的测试场景
- 添加请求数据验证失败的测试场景
- 优化测试中的服务模拟方式和响应断言
yourname 2 月之前
父节点
当前提交
35f0c61b00
共有 1 个文件被更改,包括 60 次插入66 次删除
  1. 60 66
      src/server/api/files/__tests__/files.integration.test.ts

+ 60 - 66
src/server/api/files/__tests__/files.integration.test.ts

@@ -305,79 +305,73 @@ describe('File API Integration Tests', () => {
     });
   });
 
-  // describe('POST /api/v1/files/multipart-policy', () => {
-  //   it('should generate multipart upload policy successfully', async () => {
-  //     const mockRequestData = {
-  //       fileKey: 'large-file.zip',
-  //       totalSize: 1024 * 1024 * 100, // 100MB
-  //       partSize: 1024 * 1024 * 20, // 20MB
-  //       name: 'large-file.zip',
-  //       type: 'application/zip',
-  //       uploadUserId: 1
-  //     };
+  describe('POST /api/v1/files/multipart-policy', () => {
+    it('should generate multipart upload policy successfully', async () => {
+      const mockRequestData = {
+        fileKey: 'large-file.zip',
+        totalSize: 1024 * 1024 * 100, // 100MB
+        partSize: 1024 * 1024 * 20, // 20MB
+        name: 'large-file.zip',
+        type: 'application/zip',
+        uploadUserId: 1
+      };
 
-  //     const mockResponse = {
-  //       file: {
-  //         id: 1,
-  //         ...mockRequestData,
-  //         path: '1/test-uuid-123-large-file.zip',
-  //         description: null,
-  //         uploadUser: {} as any,
-  //         uploadTime: new Date(),
-  //         lastUpdated: null,
-  //         createdAt: new Date(),
-  //         updatedAt: new Date(),
-  //         fullUrl: Promise.resolve('https://minio.example.com/d8dai/1/test-uuid-123-large-file.zip')
-  //       },
-  //       uploadId: 'upload-123',
-  //       uploadUrls: ['url1', 'url2', 'url3', 'url4', 'url5'],
-  //       bucket: 'd8dai',
-  //       key: '1/test-uuid-123-large-file.zip'
-  //     };
+      const mockResponse = {
+        uploadId: 'upload-123',
+        bucket: 'd8dai',
+        key: '1/test-uuid-123-large-file.zip',
+        host: 'http://undefined:undefined',
+        partUrls: ['url1', 'url2', 'url3', 'url4', 'url5']
+      };
 
-  //     vi.mocked(mockFileService.createMultipartUploadPolicy).mockResolvedValue(mockResponse);
+      const mockCreateMultipartUploadPolicy = vi.fn().mockResolvedValue(mockResponse);
+      vi.mocked(FileService).mockImplementation(() => ({
+        createMultipartUploadPolicy: mockCreateMultipartUploadPolicy
+      } as unknown as FileService));
 
-  //     const response = await client.files['multipart-policy'].$post({
-  //       json: mockRequestData
-  //     },
-  //     {
-  //       headers: {
-  //         'Authorization': 'Bearer test-token'
-  //       }
-  //     });
+      const response = await client.files['multipart-policy'].$post({
+        json: mockRequestData
+      },
+      {
+        headers: {
+          'Authorization': 'Bearer test-token'
+        }
+      });
 
-  //     expect(response.status).toBe(200);
-  //     const result = await response.json();
-  //     expect(result).toEqual(mockResponse);
-  //     expect(mockFileService.createMultipartUploadPolicy).toHaveBeenCalledWith(
-  //       {
-  //         name: 'large-file.zip',
-  //         type: 'application/zip',
-  //         size: 104857600,
-  //         uploadUserId: 1
-  //       },
-  //       5
-  //     );
-  //   });
+      expect(response.status).toBe(200);
+      const result = await response.json();
+      expect(result).toEqual(mockResponse);
+      expect(mockCreateMultipartUploadPolicy).toHaveBeenCalledWith(
+        {
+          fileKey: 'large-file.zip',
+          totalSize: 104857600,
+          partSize: 20971520,
+          name: 'large-file.zip',
+          type: 'application/zip',
+          uploadUserId: 1
+        },
+        5
+      );
+    });
 
-  //   it('should validate multipart policy request data', async () => {
-  //     const invalidData = {
-  //       name: 'test.zip',
-  //       // Missing required fields: fileKey, totalSize, partSize
-  //     };
+    it('should validate multipart policy request data', async () => {
+      const invalidData = {
+        name: 'test.zip'
+        // Missing required fields: fileKey, totalSize, partSize
+      };
 
-  //     const response = await client.files['multipart-policy'].$post({
-  //       json: invalidData
-  //     },
-  //     {
-  //       headers: {
-  //         'Authorization': 'Bearer test-token'
-  //       }
-  //     });
+      const response = await client.files['multipart-policy'].$post({
+        json: invalidData as any
+      },
+      {
+        headers: {
+          'Authorization': 'Bearer test-token'
+        }
+      });
 
-  //     expect(response.status).toBe(400);
-  //   });
-  // });
+      expect(response.status).toBe(400);
+    });
+  });
 
   // describe('POST /api/v1/files/multipart-complete', () => {
   //   it('should complete multipart upload successfully', async () => {