|
|
@@ -7,6 +7,20 @@ import { logger } from '@/server/utils/logger';
|
|
|
vi.mock('minio');
|
|
|
vi.mock('@/server/utils/logger');
|
|
|
|
|
|
+// Mock process.env using vi.stubEnv for proper isolation
|
|
|
+beforeEach(() => {
|
|
|
+ vi.stubEnv('MINIO_HOST', 'localhost');
|
|
|
+ vi.stubEnv('MINIO_PORT', '9000');
|
|
|
+ vi.stubEnv('MINIO_USE_SSL', 'false');
|
|
|
+ vi.stubEnv('MINIO_ACCESS_KEY', 'minioadmin');
|
|
|
+ vi.stubEnv('MINIO_SECRET_KEY', 'minioadmin');
|
|
|
+ vi.stubEnv('MINIO_BUCKET_NAME', 'test-bucket');
|
|
|
+});
|
|
|
+
|
|
|
+afterEach(() => {
|
|
|
+ vi.unstubAllEnvs();
|
|
|
+});
|
|
|
+
|
|
|
describe('MinIO Integration Tests', () => {
|
|
|
let minioService: MinioService;
|
|
|
let mockClient: Client;
|
|
|
@@ -14,17 +28,10 @@ describe('MinIO Integration Tests', () => {
|
|
|
beforeEach(() => {
|
|
|
mockClient = new Client({} as any);
|
|
|
(Client as any).mockClear();
|
|
|
-
|
|
|
- // Mock environment variables for testing
|
|
|
- vi.stubEnv('MINIO_HOST', 'localhost');
|
|
|
- vi.stubEnv('MINIO_PORT', '9000');
|
|
|
- vi.stubEnv('MINIO_USE_SSL', 'false');
|
|
|
- vi.stubEnv('MINIO_BUCKET_NAME', 'test-bucket');
|
|
|
+ (Client as any).mockImplementation(() => mockClient);
|
|
|
|
|
|
// Create MinioService with mock client
|
|
|
minioService = new MinioService();
|
|
|
- // Replace the internal client with our mock
|
|
|
- minioService['client'] = mockClient;
|
|
|
});
|
|
|
|
|
|
afterEach(() => {
|
|
|
@@ -211,6 +218,10 @@ describe('MinIO Integration Tests', () => {
|
|
|
|
|
|
it('should handle file operation errors', async () => {
|
|
|
const operationError = new Error('Operation failed');
|
|
|
+
|
|
|
+ // 确保桶存在成功
|
|
|
+ mockClient.bucketExists = vi.fn().mockResolvedValue(true);
|
|
|
+ // 但文件操作失败
|
|
|
mockClient.putObject = vi.fn().mockRejectedValue(operationError);
|
|
|
|
|
|
await expect(minioService.createObject(
|
|
|
@@ -274,7 +285,8 @@ describe('MinIO Integration Tests', () => {
|
|
|
it('should handle large file operations', async () => {
|
|
|
// Use smaller buffer size to avoid memory issues
|
|
|
const largeBuffer = Buffer.alloc(1 * 1024 * 1024); // 1MB instead of 10MB
|
|
|
- mockClient.putObject = vi.fn().mockResolvedValue(undefined);
|
|
|
+ mockClient.bucketExists = vi.fn().mockResolvedValue(true);
|
|
|
+ mockClient.putObject = vi.fn().mockResolvedValue({ etag: 'etag123', versionId: null });
|
|
|
|
|
|
await minioService.createObject('test-bucket', 'large-file.bin', largeBuffer, 'application/octet-stream');
|
|
|
|