|
|
@@ -15,7 +15,16 @@ describe('MinIO Integration Tests', () => {
|
|
|
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');
|
|
|
+
|
|
|
+ // Create MinioService with mock client
|
|
|
minioService = new MinioService();
|
|
|
+ // Replace the internal client with our mock
|
|
|
+ minioService['client'] = mockClient;
|
|
|
});
|
|
|
|
|
|
afterEach(() => {
|
|
|
@@ -25,9 +34,9 @@ describe('MinIO Integration Tests', () => {
|
|
|
describe('Bucket Operations', () => {
|
|
|
it('should ensure bucket exists and set policy', async () => {
|
|
|
// Mock bucket doesn't exist
|
|
|
- vi.mocked(mockClient.bucketExists).mockResolvedValue(false);
|
|
|
- vi.mocked(mockClient.makeBucket).mockResolvedValue(undefined);
|
|
|
- vi.mocked(mockClient.setBucketPolicy).mockResolvedValue(undefined);
|
|
|
+ mockClient.bucketExists = vi.fn().mockResolvedValue(false);
|
|
|
+ mockClient.makeBucket = vi.fn().mockResolvedValue(undefined);
|
|
|
+ mockClient.setBucketPolicy = vi.fn().mockResolvedValue(undefined);
|
|
|
|
|
|
const result = await minioService.ensureBucketExists();
|
|
|
|
|
|
@@ -39,7 +48,7 @@ describe('MinIO Integration Tests', () => {
|
|
|
});
|
|
|
|
|
|
it('should handle existing bucket', async () => {
|
|
|
- vi.mocked(mockClient.bucketExists).mockResolvedValue(true);
|
|
|
+ mockClient.bucketExists = vi.fn().mockResolvedValue(true);
|
|
|
|
|
|
const result = await minioService.ensureBucketExists();
|
|
|
|
|
|
@@ -56,10 +65,10 @@ describe('MinIO Integration Tests', () => {
|
|
|
const mockUrl = 'http://localhost:9000/test-bucket/test.txt';
|
|
|
|
|
|
// Mock bucket operations
|
|
|
- vi.mocked(mockClient.bucketExists).mockResolvedValue(true);
|
|
|
- vi.mocked(mockClient.putObject).mockResolvedValue(undefined);
|
|
|
- vi.mocked(mockClient.statObject).mockResolvedValue({ size: testContent.length } as any);
|
|
|
- vi.mocked(mockClient.getObject).mockReturnValue({
|
|
|
+ mockClient.bucketExists = vi.fn().mockResolvedValue(true);
|
|
|
+ mockClient.putObject = vi.fn().mockResolvedValue(undefined);
|
|
|
+ mockClient.statObject = vi.fn().mockResolvedValue({ size: testContent.length } as any);
|
|
|
+ mockClient.getObject = vi.fn().mockReturnValue({
|
|
|
on: (event: string, callback: Function) => {
|
|
|
if (event === 'data') callback(testContent);
|
|
|
if (event === 'end') callback();
|
|
|
@@ -85,15 +94,15 @@ describe('MinIO Integration Tests', () => {
|
|
|
|
|
|
it('should handle file not found', async () => {
|
|
|
const notFoundError = new Error('Object not found');
|
|
|
- notFoundError.message = 'The specified key does not exist';
|
|
|
- vi.mocked(mockClient.statObject).mockRejectedValue(notFoundError);
|
|
|
+ notFoundError.message = 'not found';
|
|
|
+ mockClient.statObject = vi.fn().mockRejectedValue(notFoundError);
|
|
|
|
|
|
const exists = await minioService.objectExists('test-bucket', 'nonexistent.txt');
|
|
|
expect(exists).toBe(false);
|
|
|
});
|
|
|
|
|
|
it('should delete file successfully', async () => {
|
|
|
- vi.mocked(mockClient.removeObject).mockResolvedValue(undefined);
|
|
|
+ mockClient.removeObject = vi.fn().mockResolvedValue(undefined);
|
|
|
|
|
|
await minioService.deleteObject('test-bucket', 'test.txt');
|
|
|
|
|
|
@@ -105,7 +114,7 @@ describe('MinIO Integration Tests', () => {
|
|
|
describe('Presigned URL Operations', () => {
|
|
|
it('should generate presigned URLs correctly', async () => {
|
|
|
const mockPresignedUrl = 'https://minio.example.com/presigned-url';
|
|
|
- vi.mocked(mockClient.presignedGetObject).mockResolvedValue(mockPresignedUrl);
|
|
|
+ mockClient.presignedGetObject = vi.fn().mockResolvedValue(mockPresignedUrl);
|
|
|
|
|
|
// Test regular presigned URL
|
|
|
const url = await minioService.getPresignedFileUrl('test-bucket', 'file.txt', 3600);
|
|
|
@@ -139,13 +148,13 @@ describe('MinIO Integration Tests', () => {
|
|
|
const mockStat = { size: 3072 };
|
|
|
|
|
|
// Mock multipart operations
|
|
|
- vi.mocked(mockClient.initiateNewMultipartUpload).mockResolvedValue(mockUploadId);
|
|
|
- vi.mocked(mockClient.presignedUrl)
|
|
|
+ mockClient.initiateNewMultipartUpload = vi.fn().mockResolvedValue(mockUploadId);
|
|
|
+ mockClient.presignedUrl = vi.fn()
|
|
|
.mockResolvedValueOnce('url1')
|
|
|
.mockResolvedValueOnce('url2')
|
|
|
.mockResolvedValueOnce('url3');
|
|
|
- vi.mocked(mockClient.completeMultipartUpload).mockResolvedValue(undefined);
|
|
|
- vi.mocked(mockClient.statObject).mockResolvedValue(mockStat as any);
|
|
|
+ mockClient.completeMultipartUpload = vi.fn().mockResolvedValue(undefined);
|
|
|
+ mockClient.statObject = vi.fn().mockResolvedValue(mockStat as any);
|
|
|
|
|
|
// Create multipart upload
|
|
|
const uploadId = await minioService.createMultipartUpload('test-bucket', 'large-file.zip');
|
|
|
@@ -191,7 +200,7 @@ describe('MinIO Integration Tests', () => {
|
|
|
describe('Error Handling', () => {
|
|
|
it('should handle MinIO connection errors', async () => {
|
|
|
const connectionError = new Error('Connection refused');
|
|
|
- vi.mocked(mockClient.bucketExists).mockRejectedValue(connectionError);
|
|
|
+ mockClient.bucketExists = vi.fn().mockRejectedValue(connectionError);
|
|
|
|
|
|
await expect(minioService.ensureBucketExists()).rejects.toThrow(connectionError);
|
|
|
expect(logger.error).toHaveBeenCalledWith(
|
|
|
@@ -202,7 +211,7 @@ describe('MinIO Integration Tests', () => {
|
|
|
|
|
|
it('should handle file operation errors', async () => {
|
|
|
const operationError = new Error('Operation failed');
|
|
|
- vi.mocked(mockClient.putObject).mockRejectedValue(operationError);
|
|
|
+ mockClient.putObject = vi.fn().mockRejectedValue(operationError);
|
|
|
|
|
|
await expect(minioService.createObject(
|
|
|
'test-bucket',
|
|
|
@@ -218,7 +227,7 @@ describe('MinIO Integration Tests', () => {
|
|
|
|
|
|
it('should handle permission errors gracefully', async () => {
|
|
|
const permissionError = new Error('Permission denied');
|
|
|
- vi.mocked(mockClient.statObject).mockRejectedValue(permissionError);
|
|
|
+ mockClient.statObject = vi.fn().mockRejectedValue(permissionError);
|
|
|
|
|
|
await expect(minioService.objectExists('test-bucket', 'file.txt')).rejects.toThrow(permissionError);
|
|
|
expect(logger.error).toHaveBeenCalledWith(
|
|
|
@@ -250,7 +259,7 @@ describe('MinIO Integration Tests', () => {
|
|
|
|
|
|
describe('Performance Testing', () => {
|
|
|
it('should handle concurrent operations', async () => {
|
|
|
- vi.mocked(mockClient.presignedGetObject).mockResolvedValue('https://minio.example.com/file');
|
|
|
+ mockClient.presignedGetObject = vi.fn().mockResolvedValue('https://minio.example.com/file');
|
|
|
|
|
|
// Test concurrent URL generation
|
|
|
const promises = Array(10).fill(0).map((_, i) =>
|
|
|
@@ -264,7 +273,7 @@ describe('MinIO Integration Tests', () => {
|
|
|
|
|
|
it('should handle large file operations', async () => {
|
|
|
const largeBuffer = Buffer.alloc(10 * 1024 * 1024); // 10MB
|
|
|
- vi.mocked(mockClient.putObject).mockResolvedValue(undefined);
|
|
|
+ mockClient.putObject = vi.fn().mockResolvedValue(undefined);
|
|
|
|
|
|
await minioService.createObject('test-bucket', 'large-file.bin', largeBuffer, 'application/octet-stream');
|
|
|
|