/*! * @license * Copyright Squiz Australia Pty Ltd. All Rights Reserved. */ import { zipDirectory } from '@squiz/dx-common-lib'; import { Logger } from '@squiz/dx-logger-lib'; import { AxiosInstance } from 'axios'; import { handleResponse } from './responseHandler'; import { initializeUpload } from './uploadInitializer'; jest.mock('@squiz/dx-common-lib', () => ({ zipDirectory: jest.fn(), })); jest.mock('./responseHandler', () => ({ handleResponse: jest.fn(), })); describe('uploadInitializer', () => { let mockLogger: Logger; let mockApiClient: AxiosInstance; const mockZipDirectory = zipDirectory as jest.MockedFunction; const mockHandleResponse = handleResponse as jest.MockedFunction; beforeEach(() => { jest.clearAllMocks(); mockLogger = { info: jest.fn(), error: jest.fn(), warn: jest.fn(), debug: jest.fn(), } as any; mockApiClient = { post: jest.fn(), get: jest.fn(), } as any; }); describe('initializeUpload', () => { const testFolderPath = '/test/folder'; const testTmpDir = '/tmp/test'; const testZipPath = '/tmp/test/job.zip'; it('should successfully initialize upload', async () => { const mockUploadResponse = { id: 'upload-123', status: 'pending' }; mockZipDirectory.mockResolvedValue(testZipPath); mockHandleResponse.mockResolvedValue(mockUploadResponse); const result = await initializeUpload(mockApiClient, testFolderPath, testTmpDir, mockLogger); expect(mockLogger.info).toHaveBeenCalledWith('Initial scanning'); expect(mockZipDirectory).toHaveBeenCalledWith(testFolderPath, testTmpDir); expect(mockHandleResponse).toHaveBeenCalled(); expect(mockLogger.info).toHaveBeenCalledWith(`deployment id: ${mockUploadResponse.id} status: transferring`); expect(result).toEqual({ zip: testZipPath, upload: mockUploadResponse, }); }); it('should handle errors during zip creation', async () => { const zipError = new Error('Failed to create zip'); mockZipDirectory.mockRejectedValue(zipError); await expect(initializeUpload(mockApiClient, testFolderPath, testTmpDir, mockLogger)).rejects.toThrow( 'Failed to create zip', ); expect(mockLogger.info).toHaveBeenCalledWith('Initial scanning'); expect(mockZipDirectory).toHaveBeenCalledWith(testFolderPath, testTmpDir); expect(mockHandleResponse).not.toHaveBeenCalled(); }); it('should handle errors during upload request', async () => { const uploadError = new Error('Upload failed'); mockZipDirectory.mockResolvedValue(testZipPath); mockHandleResponse.mockRejectedValue(uploadError); await expect(initializeUpload(mockApiClient, testFolderPath, testTmpDir, mockLogger)).rejects.toThrow( 'Upload failed', ); expect(mockZipDirectory).toHaveBeenCalledWith(testFolderPath, testTmpDir); expect(mockHandleResponse).toHaveBeenCalled(); }); it('should handle upload response with missing id gracefully', async () => { const mockUploadResponse = { status: 'pending' }; // no id property mockZipDirectory.mockResolvedValue(testZipPath); mockHandleResponse.mockResolvedValue(mockUploadResponse); const result = await initializeUpload(mockApiClient, testFolderPath, testTmpDir, mockLogger); expect(mockLogger.info).toHaveBeenCalledWith(`deployment id: undefined status: transferring`); expect(result).toEqual({ zip: testZipPath, upload: mockUploadResponse, }); }); }); });