|
| 1 | +import { CxInstaller } from "../main/osinstaller/CxInstaller"; |
| 2 | +import { anyString, mock, instance, when, verify } from "ts-mockito"; |
| 3 | +import { HttpClient } from "../main/client/HttpClient"; |
| 4 | + |
| 5 | +const clientMock = mock(HttpClient); |
| 6 | +const clientMockInstance = instance(clientMock); |
| 7 | + |
| 8 | +const cxInstallerLinux = new CxInstaller("linux", clientMockInstance); |
| 9 | +const cxInstallerMac = new CxInstaller("darwin", clientMockInstance); |
| 10 | +const cxInstallerWindows = new CxInstaller("win32", clientMockInstance); |
| 11 | + |
| 12 | +describe("CxInstaller cases", () => { |
| 13 | + it('CxInstaller getDownloadURL Linux Successful case', async () => { |
| 14 | + const url = await cxInstallerLinux.getDownloadURL(); |
| 15 | + const architecture = getArchitecture(cxInstallerLinux.getPlatform()); |
| 16 | + expect(url).toBe(`https://download.checkmarx.com/CxOne/CLI/2.2.5/ast-cli_2.2.5_linux_${architecture}.tar.gz`); |
| 17 | + }); |
| 18 | + |
| 19 | + it('CxInstaller getDownloadURL Mac Successful case', async () => { |
| 20 | + const url = await cxInstallerMac.getDownloadURL(); |
| 21 | + const architecture = getArchitecture(cxInstallerMac.getPlatform()); |
| 22 | + expect(url).toBe(`https://download.checkmarx.com/CxOne/CLI/2.2.5/ast-cli_2.2.5_darwin_${architecture}.tar.gz`); |
| 23 | + }); |
| 24 | + |
| 25 | + it('CxInstaller getDownloadURL Windows Successful case', async () => { |
| 26 | + const url = await cxInstallerWindows.getDownloadURL(); |
| 27 | + const architecture = getArchitecture(cxInstallerWindows.getPlatform()); |
| 28 | + expect(url).toBe(`https://download.checkmarx.com/CxOne/CLI/2.2.5/ast-cli_2.2.5_windows_${architecture}.zip`); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +describe("CxInstaller getExecutablePath cases", () => { |
| 33 | + it('CxInstaller getExecutablePath Linux Successful case', () => { |
| 34 | + const executablePath = cxInstallerLinux.getExecutablePath(); |
| 35 | + expect(executablePath).toContain(`src/main/wrapper/resources/cx`); |
| 36 | + }); |
| 37 | + |
| 38 | + it('CxInstaller getExecutablePath Mac Successful case', () => { |
| 39 | + const executablePath = cxInstallerMac.getExecutablePath(); |
| 40 | + expect(executablePath).toContain(`src/main/wrapper/resources/cx`); |
| 41 | + }); |
| 42 | + |
| 43 | + it('CxInstaller getExecutablePath Windows Successful case', () => { |
| 44 | + const executablePath = cxInstallerWindows.getExecutablePath(); |
| 45 | + expect(executablePath).toContain(`src/main/wrapper/resources/cx.exe`); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe("CxInstaller checkExecutableExists cases", () => { |
| 50 | + beforeAll(async () => { |
| 51 | + // Set up mock behavior |
| 52 | + when(clientMock.downloadFile(anyString(), anyString())).thenResolve(); |
| 53 | + |
| 54 | + // Trigger the download |
| 55 | + await cxInstallerWindows.downloadIfNotInstalledCLI(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('CxInstaller checkExecutableExists Windows Successful case', () => { |
| 59 | + const exists = cxInstallerWindows.checkExecutableExists(); |
| 60 | + expect(exists).toBe(false); |
| 61 | + |
| 62 | + // Verify if downloadFile was called with the expected arguments |
| 63 | + verify(clientMock.downloadFile(anyString(), anyString())).called(); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +function getArchitecture(platform: string): string { |
| 68 | + if (platform !== 'linux') { |
| 69 | + return 'x64'; |
| 70 | + } |
| 71 | + |
| 72 | + const archMap: Record<string, string> = { |
| 73 | + 'arm64': 'arm64', |
| 74 | + 'arm': 'armv6' |
| 75 | + }; |
| 76 | + |
| 77 | + return archMap[process.arch] || 'x64'; |
| 78 | +} |
0 commit comments