|
|
@@ -1,18 +1,23 @@
|
|
|
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
|
|
|
-import { createServer } from '@/server';
|
|
|
+import { testClient } from 'hono/testing';
|
|
|
import { DataSource } from 'typeorm';
|
|
|
import { File } from '@/server/modules/files/file.entity';
|
|
|
import { FileService } from '@/server/modules/files/file.service';
|
|
|
import { MinioService } from '@/server/modules/files/minio.service';
|
|
|
import { authMiddleware } from '@/server/middleware/auth.middleware';
|
|
|
+import { fileApiRoutes } from '@/server/api';
|
|
|
|
|
|
// Mock dependencies
|
|
|
-vi.mock('@/server/modules/files/file.service');
|
|
|
+vi.mock('@/server/modules/files/file.service', () => ({
|
|
|
+ FileService: vi.fn().mockImplementation(() => ({
|
|
|
+ createFile: vi.fn()
|
|
|
+ }))
|
|
|
+}));
|
|
|
vi.mock('@/server/modules/files/minio.service');
|
|
|
vi.mock('@/server/middleware/auth.middleware');
|
|
|
|
|
|
describe('File API Integration Tests', () => {
|
|
|
- let app: any;
|
|
|
+ let client: ReturnType<typeof testClient<typeof fileApiRoutes>>['api']['v1'];
|
|
|
let mockFileService: FileService;
|
|
|
let mockMinioService: MinioService;
|
|
|
let mockDataSource: DataSource;
|
|
|
@@ -21,13 +26,15 @@ describe('File API Integration Tests', () => {
|
|
|
vi.clearAllMocks();
|
|
|
|
|
|
mockDataSource = {} as DataSource;
|
|
|
- mockFileService = new FileService(mockDataSource);
|
|
|
mockMinioService = new MinioService();
|
|
|
|
|
|
// Mock auth middleware to bypass authentication
|
|
|
vi.mocked(authMiddleware).mockImplementation((_, next) => next());
|
|
|
|
|
|
- app = createServer();
|
|
|
+ // Get the mocked FileService instance
|
|
|
+ mockFileService = new FileService(mockDataSource);
|
|
|
+
|
|
|
+ client = testClient(fileApiRoutes).api.v1;
|
|
|
});
|
|
|
|
|
|
afterEach(() => {
|
|
|
@@ -40,6 +47,7 @@ describe('File API Integration Tests', () => {
|
|
|
name: 'test.txt',
|
|
|
type: 'text/plain',
|
|
|
size: 1024,
|
|
|
+ path: '/uploads/test.txt',
|
|
|
description: 'Test file',
|
|
|
uploadUserId: 1
|
|
|
};
|
|
|
@@ -60,17 +68,20 @@ describe('File API Integration Tests', () => {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- vi.mocked(mockFileService.createFile).mockResolvedValue(mockResponse);
|
|
|
+ mockFileService.createFile = vi.fn().mockResolvedValue(mockResponse);
|
|
|
|
|
|
- const response = await app.request('/api/v1/files/upload-policy', {
|
|
|
- method: 'POST',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
+ const response = await client.files['upload-policy'].$post({
|
|
|
+ json: mockFileData,
|
|
|
+ header: {
|
|
|
'Authorization': 'Bearer test-token'
|
|
|
- },
|
|
|
- body: JSON.stringify(mockFileData)
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
+ if (response.status !== 200) {
|
|
|
+ const error = await response.json();
|
|
|
+ console.debug('Error response:', JSON.stringify(error, null, 2));
|
|
|
+ console.debug('Response status:', response.status);
|
|
|
+ }
|
|
|
expect(response.status).toBe(200);
|
|
|
const result = await response.json();
|
|
|
expect(result).toEqual(mockResponse);
|
|
|
@@ -102,7 +113,7 @@ describe('File API Integration Tests', () => {
|
|
|
uploadUserId: 1
|
|
|
};
|
|
|
|
|
|
- vi.mocked(mockFileService.createFile).mockRejectedValue(new Error('Service error'));
|
|
|
+ mockFileService.createFile = vi.fn().mockRejectedValue(new Error('Service error'));
|
|
|
|
|
|
const response = await app.request('/api/v1/files/upload-policy', {
|
|
|
method: 'POST',
|