Explorar el Código

✅ test(file): 添加MinIO服务的测试模拟

- 导入vi工具并添加MinioService的模拟实现
- 模拟MinIO服务所有方法以避免测试中真实连接
- 设置objectExists默认返回false,模拟文件不存在场景
- 为generateUploadPolicy提供测试用上传策略数据
- 为各种文件操作方法提供模拟返回值,确保测试环境隔离
yourname hace 4 semanas
padre
commit
730034ec77

+ 30 - 1
packages/file-module/tests/integration/file.routes.integration.test.ts

@@ -1,4 +1,4 @@
-import { describe, it, expect, beforeEach } from 'vitest';
+import { describe, it, expect, beforeEach, vi } from 'vitest';
 import { testClient } from 'hono/testing';
 import {
   IntegrationTestDatabase,
@@ -13,6 +13,35 @@ import { UserEntity, Role } from '@d8d/user-module';
 import { TestDataFactory } from '../utils/integration-test-db';
 import { AuthService } from '@d8d/auth-module';
 import { UserService } from '@d8d/user-module';
+import { MinioService } from '../../src/services/minio.service';
+
+// Mock MinIO service to avoid real connections in tests
+vi.mock('../../src/services/minio.service', () => {
+  const MockMinioService = vi.fn(() => ({
+    bucketName: 'test-bucket',
+    ensureBucketExists: vi.fn().mockResolvedValue(true),
+    objectExists: vi.fn().mockResolvedValue(false), // Assume files don't exist in MinIO for tests
+    deleteObject: vi.fn().mockResolvedValue(undefined),
+    generateUploadPolicy: vi.fn().mockResolvedValue({
+      'x-amz-algorithm': 'AWS4-HMAC-SHA256',
+      'x-amz-credential': 'test-credential',
+      'x-amz-date': '20230101T000000Z',
+      policy: 'test-policy',
+      'x-amz-signature': 'test-signature',
+      host: 'http://localhost:9000',
+      key: 'test-key',
+      bucket: 'test-bucket'
+    }),
+    getPresignedFileUrl: vi.fn().mockResolvedValue('http://localhost:9000/test-bucket/test-file'),
+    getPresignedFileDownloadUrl: vi.fn().mockResolvedValue('http://localhost:9000/test-bucket/test-file?download=true'),
+    createMultipartUpload: vi.fn().mockResolvedValue('test-upload-id'),
+    generateMultipartUploadUrls: vi.fn().mockResolvedValue(['http://localhost:9000/part1', 'http://localhost:9000/part2']),
+    completeMultipartUpload: vi.fn().mockResolvedValue({ size: 1024 }),
+    createObject: vi.fn().mockResolvedValue('http://localhost:9000/test-bucket/test-file'),
+    getFileUrl: vi.fn().mockReturnValue('http://localhost:9000/test-bucket/test-file')
+  }));
+  return { MinioService: MockMinioService };
+});
 
 // 设置集成测试钩子
 setupIntegrationDatabaseHooksWithEntities([File, UserEntity, Role])