|
|
@@ -93,79 +93,104 @@ describe('FileService', () => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
- // describe('deleteFile', () => {
|
|
|
- // it('should delete file successfully when file exists', async () => {
|
|
|
- // const mockFile = {
|
|
|
- // id: 1,
|
|
|
- // path: '1/test-file.txt',
|
|
|
- // name: 'test-file.txt'
|
|
|
- // } as File;
|
|
|
+ describe('deleteFile', () => {
|
|
|
+ it('should delete file successfully when file exists', async () => {
|
|
|
+ const mockFile = {
|
|
|
+ id: 1,
|
|
|
+ path: '1/test-file.txt',
|
|
|
+ name: 'test-file.txt'
|
|
|
+ } as File;
|
|
|
|
|
|
- // vi.spyOn(fileService, 'getById').mockResolvedValue(mockFile);
|
|
|
- // vi.mocked(mockMinioService.objectExists).mockResolvedValue(true);
|
|
|
- // vi.mocked(mockMinioService.deleteObject).mockResolvedValue(undefined);
|
|
|
- // vi.spyOn(fileService, 'delete').mockResolvedValue(undefined);
|
|
|
+ const mockObjectExists = vi.fn().mockResolvedValue(true);
|
|
|
+ const mockDeleteObject = vi.fn().mockResolvedValue(undefined);
|
|
|
|
|
|
- // const result = await fileService.deleteFile(1);
|
|
|
+ vi.mocked(MinioService).mockImplementation(() => ({
|
|
|
+ objectExists: mockObjectExists,
|
|
|
+ deleteObject: mockDeleteObject,
|
|
|
+ bucketName: 'd8dai'
|
|
|
+ } as unknown as MinioService));
|
|
|
|
|
|
- // expect(fileService.getById).toHaveBeenCalledWith(1);
|
|
|
- // expect(mockMinioService.objectExists).toHaveBeenCalledWith('d8dai', '1/test-file.txt');
|
|
|
- // expect(mockMinioService.deleteObject).toHaveBeenCalledWith('d8dai', '1/test-file.txt');
|
|
|
- // expect(fileService.delete).toHaveBeenCalledWith(1);
|
|
|
- // expect(result).toBe(true);
|
|
|
- // });
|
|
|
+ const fileService = new FileService(mockDataSource);
|
|
|
+ vi.spyOn(fileService, 'getById').mockResolvedValue(mockFile);
|
|
|
+ vi.spyOn(fileService, 'delete').mockResolvedValue(true);
|
|
|
|
|
|
- // it('should delete database record even when MinIO file not found', async () => {
|
|
|
- // const mockFile = {
|
|
|
- // id: 1,
|
|
|
- // path: '1/test-file.txt',
|
|
|
- // name: 'test-file.txt'
|
|
|
- // } as File;
|
|
|
+ const result = await fileService.deleteFile(1);
|
|
|
|
|
|
- // vi.spyOn(fileService, 'getById').mockResolvedValue(mockFile);
|
|
|
- // vi.mocked(mockMinioService.objectExists).mockResolvedValue(false);
|
|
|
- // vi.spyOn(fileService, 'delete').mockResolvedValue(undefined);
|
|
|
+ expect(fileService.getById).toHaveBeenCalledWith(1);
|
|
|
+ expect(mockObjectExists).toHaveBeenCalledWith('d8dai', '1/test-file.txt');
|
|
|
+ expect(mockDeleteObject).toHaveBeenCalledWith('d8dai', '1/test-file.txt');
|
|
|
+ expect(fileService.delete).toHaveBeenCalledWith(1);
|
|
|
+ expect(result).toBe(true);
|
|
|
+ });
|
|
|
|
|
|
- // const result = await fileService.deleteFile(1);
|
|
|
+ it('should delete database record even when MinIO file not found', async () => {
|
|
|
+ const mockFile = {
|
|
|
+ id: 1,
|
|
|
+ path: '1/test-file.txt',
|
|
|
+ name: 'test-file.txt'
|
|
|
+ } as File;
|
|
|
|
|
|
- // expect(mockMinioService.deleteObject).not.toHaveBeenCalled();
|
|
|
- // expect(fileService.delete).toHaveBeenCalledWith(1);
|
|
|
- // expect(result).toBe(true);
|
|
|
- // expect(logger.error).toHaveBeenCalled();
|
|
|
- // });
|
|
|
+ const mockObjectExists = vi.fn().mockResolvedValue(false);
|
|
|
|
|
|
- // it('should throw error when file not found', async () => {
|
|
|
- // vi.spyOn(fileService, 'getById').mockResolvedValue(null);
|
|
|
+ vi.mocked(MinioService).mockImplementation(() => ({
|
|
|
+ objectExists: mockObjectExists,
|
|
|
+ deleteObject: vi.fn(),
|
|
|
+ bucketName: 'd8dai'
|
|
|
+ } as unknown as MinioService));
|
|
|
|
|
|
- // await expect(fileService.deleteFile(999)).rejects.toThrow('文件不存在');
|
|
|
- // });
|
|
|
- // });
|
|
|
+ const fileService = new FileService(mockDataSource);
|
|
|
+ vi.spyOn(fileService, 'getById').mockResolvedValue(mockFile);
|
|
|
+ vi.spyOn(fileService, 'delete').mockResolvedValue(true);
|
|
|
|
|
|
- // describe('getFileUrl', () => {
|
|
|
- // it('should return file URL successfully', async () => {
|
|
|
- // const mockFile = {
|
|
|
- // id: 1,
|
|
|
- // path: '1/test-file.txt'
|
|
|
- // } as File;
|
|
|
+ const result = await fileService.deleteFile(1);
|
|
|
|
|
|
- // const mockPresignedUrl = 'https://minio.example.com/presigned-url';
|
|
|
+ expect(mockObjectExists).toHaveBeenCalledWith('d8dai', '1/test-file.txt');
|
|
|
+ expect(fileService.delete).toHaveBeenCalledWith(1);
|
|
|
+ expect(result).toBe(true);
|
|
|
+ expect(logger.error).toHaveBeenCalled();
|
|
|
+ });
|
|
|
|
|
|
- // vi.spyOn(fileService, 'getById').mockResolvedValue(mockFile);
|
|
|
- // vi.mocked(mockMinioService.getPresignedFileUrl).mockResolvedValue(mockPresignedUrl);
|
|
|
+ it('should throw error when file not found', async () => {
|
|
|
+ const fileService = new FileService(mockDataSource);
|
|
|
+ vi.spyOn(fileService, 'getById').mockResolvedValue(null);
|
|
|
|
|
|
- // const result = await fileService.getFileUrl(1);
|
|
|
+ await expect(fileService.deleteFile(999)).rejects.toThrow('文件不存在');
|
|
|
+ });
|
|
|
+ });
|
|
|
|
|
|
- // expect(fileService.getById).toHaveBeenCalledWith(1);
|
|
|
- // expect(mockMinioService.getPresignedFileUrl).toHaveBeenCalledWith('d8dai', '1/test-file.txt');
|
|
|
- // expect(result).toBe(mockPresignedUrl);
|
|
|
- // });
|
|
|
+ describe('getFileUrl', () => {
|
|
|
+ it('should return file URL successfully', async () => {
|
|
|
+ const mockFile = {
|
|
|
+ id: 1,
|
|
|
+ path: '1/test-file.txt'
|
|
|
+ } as File;
|
|
|
|
|
|
- // it('should throw error when file not found', async () => {
|
|
|
- // vi.spyOn(fileService, 'getById').mockResolvedValue(null);
|
|
|
+ const mockPresignedUrl = 'https://minio.example.com/presigned-url';
|
|
|
|
|
|
- // await expect(fileService.getFileUrl(999)).rejects.toThrow('文件不存在');
|
|
|
- // });
|
|
|
- // });
|
|
|
+ const mockGetPresignedFileUrl = vi.fn().mockResolvedValue(mockPresignedUrl);
|
|
|
+
|
|
|
+ vi.mocked(MinioService).mockImplementation(() => ({
|
|
|
+ getPresignedFileUrl: mockGetPresignedFileUrl,
|
|
|
+ bucketName: 'd8dai'
|
|
|
+ } as unknown as MinioService));
|
|
|
+
|
|
|
+ const fileService = new FileService(mockDataSource);
|
|
|
+ vi.spyOn(fileService, 'getById').mockResolvedValue(mockFile);
|
|
|
+
|
|
|
+ const result = await fileService.getFileUrl(1);
|
|
|
+
|
|
|
+ expect(fileService.getById).toHaveBeenCalledWith(1);
|
|
|
+ expect(mockGetPresignedFileUrl).toHaveBeenCalledWith('d8dai', '1/test-file.txt');
|
|
|
+ expect(result).toBe(mockPresignedUrl);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should throw error when file not found', async () => {
|
|
|
+ const fileService = new FileService(mockDataSource);
|
|
|
+ vi.spyOn(fileService, 'getById').mockResolvedValue(null);
|
|
|
+
|
|
|
+ await expect(fileService.getFileUrl(999)).rejects.toThrow('文件不存在');
|
|
|
+ });
|
|
|
+ });
|
|
|
|
|
|
// describe('getFileDownloadUrl', () => {
|
|
|
// it('should return download URL with filename', async () => {
|