import { describe, it, expect, beforeEach, vi } from 'vitest'; import { testClient } from 'hono/testing'; import { IntegrationTestDatabase, setupIntegrationDatabaseHooks, TestDataFactory } from '../utils/integration-test-db'; import { fileApiRoutes } from '../../src/api'; import { AuthService } from '@d8d/auth-module'; import { UserService as UserServiceMt } from '@d8d/user-module'; import { MinioService } from '@d8d/file-module'; // Mock MinIO service to avoid real connections in tests vi.mock('@d8d/file-module', async (importOriginal) => { const actual = await importOriginal(); return { ...actual, fileRoutes: actual.fileRoutes, // 确保导出fileRoutesMt MinioService: vi.fn(() => ({ bucketName: 'd8dai', ensureBucketExists: vi.fn().mockResolvedValue(true), objectExists: vi.fn().mockResolvedValue(false), deleteObject: vi.fn().mockResolvedValue(true), generateUploadPolicy: vi.fn().mockResolvedValue({ 'x-amz-algorithm': 'AWS4-HMAC-SHA256', 'x-amz-credential': 'test-credential', 'x-amz-date': '20250101T120000Z', policy: 'test-policy', 'x-amz-signature': 'test-signature', host: 'https://minio.example.com', key: 'test-key', bucket: 'd8dai' }), getPresignedFileUrl: vi.fn().mockResolvedValue('https://minio.example.com/presigned-url'), getPresignedFileDownloadUrl: vi.fn().mockResolvedValue('https://minio.example.com/download-url'), createMultipartUpload: vi.fn().mockResolvedValue('test-upload-id'), generateMultipartUploadUrls: vi.fn().mockResolvedValue(['https://minio.example.com/part1', 'https://minio.example.com/part2']), completeMultipartUpload: vi.fn().mockResolvedValue({ size: 104857600 }), createObject: vi.fn().mockResolvedValue('https://minio.example.com/d8dai/test-file'), getFileUrl: vi.fn().mockReturnValue('https://minio.example.com/d8dai/test-file') })) }; }); // 设置集成测试钩子 setupIntegrationDatabaseHooks() describe.skip('文件API连通性测试', () => { let client: ReturnType>['api']['v1']; let testToken: string; beforeEach(async () => { // 创建测试客户端 client = testClient(fileApiRoutes).api.v1; // 创建测试用户并生成token const dataSource = await IntegrationTestDatabase.getDataSource(); const userService = new UserServiceMt(dataSource); const authService = new AuthService(userService); // 确保admin用户存在 const user = await authService.ensureAdminExists(); // 生成admin用户的token testToken = authService.generateToken(user); }); describe('文件上传策略API连通性', () => { it('应该成功响应文件上传策略请求', async () => { const fileData = { name: 'test.txt', type: 'text/plain', size: 1024, path: '/uploads/test.txt', description: 'Test file' }; const response = await client.files['upload-policy'].$post({ json: fileData }, { headers: { 'Authorization': `Bearer ${testToken}` } }); expect(response.status).toBe(200); }); it('应该拒绝无认证令牌的请求', async () => { const fileData = { name: 'test.txt', type: 'text/plain', size: 1024, path: '/uploads/test.txt' }; const response = await client.files['upload-policy'].$post({ json: fileData }); expect(response.status).toBe(401); }); }); describe('文件URL生成API连通性', () => { it('应该成功响应文件URL生成请求', async () => { const dataSource = await IntegrationTestDatabase.getDataSource(); if (!dataSource) throw new Error('Database not initialized'); const testFile = await TestDataFactory.createTestFile(dataSource, { name: 'testfile_url.txt', type: 'text/plain', size: 1024, path: 'testfile_url.txt' }); const response = await client.files[':id']['url'].$get({ param: { id: testFile.id } }, { headers: { 'Authorization': `Bearer ${testToken}` } }); expect(response.status).toBe(200); }); it('应该返回404当文件不存在时', async () => { const response = await client.files[':id']['url'].$get({ param: { id: 999999 } }, { headers: { 'Authorization': `Bearer ${testToken}` } }); expect(response.status).toBe(404); }); }); describe('文件下载URL生成API连通性', () => { it('应该成功响应文件下载URL生成请求', async () => { const dataSource = await IntegrationTestDatabase.getDataSource(); if (!dataSource) throw new Error('Database not initialized'); const testFile = await TestDataFactory.createTestFile(dataSource, { name: 'testfile_download.txt', type: 'text/plain', size: 1024, path: 'testfile_download.txt' }); const response = await client.files[':id']['download'].$get({ param: { id: testFile.id } }, { headers: { 'Authorization': `Bearer ${testToken}` } }); expect(response.status).toBe(200); }); it('应该返回404当文件不存在时', async () => { const response = await client.files[':id']['download'].$get({ param: { id: 999999 } }, { headers: { 'Authorization': `Bearer ${testToken}` } }); expect(response.status).toBe(404); }); }); describe('文件CRUD API连通性', () => { it('应该成功响应文件列表请求', async () => { const dataSource = await IntegrationTestDatabase.getDataSource(); if (!dataSource) throw new Error('Database not initialized'); await TestDataFactory.createTestFile(dataSource, { name: 'file1.txt', type: 'text/plain', size: 1024, path: 'file1.txt' }); const response = await client.files.$get({ query: {} }, { headers: { 'Authorization': `Bearer ${testToken}` } }); expect(response.status).toBe(200); }); it('应该成功响应单个文件详情请求', async () => { const dataSource = await IntegrationTestDatabase.getDataSource(); if (!dataSource) throw new Error('Database not initialized'); const testFile = await TestDataFactory.createTestFile(dataSource, { name: 'testfile_detail.txt', type: 'text/plain', size: 1024, path: 'testfile_detail.txt' }); const response = await client.files[':id'].$get({ param: { id: testFile.id } }, { headers: { 'Authorization': `Bearer ${testToken}` } }); expect(response.status).toBe(200); }); }); describe('多部分上传API连通性', () => { it('应该成功响应多部分上传策略请求', async () => { const multipartData = { fileKey: 'large-file.zip', totalSize: 1024 * 1024 * 100, // 100MB partSize: 1024 * 1024 * 20, // 20MB name: 'large-file.zip', type: 'application/zip' }; const response = await client.files['multipart-policy'].$post({ json: multipartData }, { headers: { 'Authorization': `Bearer ${testToken}` } }); expect(response.status).toBe(200); }); }); });