Updates tests for installer, config service and process control

This commit is contained in:
thatben 2025-04-21 15:52:58 +02:00
parent 77a5da5be8
commit 0e2776f6a3
No known key found for this signature in database
GPG Key ID: 62C543548433D43E
6 changed files with 61 additions and 7 deletions

View File

@ -54,8 +54,13 @@ export const mockOsService = {
export const mockCodexGlobals = { export const mockCodexGlobals = {
getPublicIp: vi.fn(), getPublicIp: vi.fn(),
getTestnetSPRs: vi.fn(), getTestnetSPRs: vi.fn(),
getEthProvider: vi.fn(),
}; };
export const mockCodexApp = { export const mockCodexApp = {
openCodexApp: vi.fn(), openCodexApp: vi.fn(),
}; };
export const mockMarketplaceSetup = {
runClientWizard: vi.fn(),
};

View File

@ -3,8 +3,9 @@ import {
mockShellService, mockShellService,
mockOsService, mockOsService,
mockFsService, mockFsService,
mockConfigService,
mockMarketplaceSetup,
} from "../__mocks__/service.mocks.js"; } from "../__mocks__/service.mocks.js";
import { mockConfigService } from "../__mocks__/service.mocks.js";
import { Installer } from "./installer.js"; import { Installer } from "./installer.js";
describe("Installer", () => { describe("Installer", () => {
@ -32,6 +33,7 @@ describe("Installer", () => {
mockShellService, mockShellService,
mockOsService, mockOsService,
mockFsService, mockFsService,
mockMarketplaceSetup,
); );
}); });
@ -109,9 +111,22 @@ describe("Installer", () => {
expect(installer.installCodexUnix).not.toHaveBeenCalled(); expect(installer.installCodexUnix).not.toHaveBeenCalled();
}); });
it("returns early when marketplace client wizard returns false", async () => {
installer.arePrerequisitesCorrect.mockResolvedValue(true);
mockMarketplaceSetup.runClientWizard.mockResolvedValue(false);
await installer.installCodex(processCallbacks);
expect(processCallbacks.installStarts).not.toHaveBeenCalled();
expect(processCallbacks.installSuccessful).not.toHaveBeenCalled();
expect(processCallbacks.downloadSuccessful).not.toHaveBeenCalled();
expect(installer.isCodexInstalled).not.toHaveBeenCalled();
expect(installer.installCodexWindows).not.toHaveBeenCalled();
expect(installer.installCodexUnix).not.toHaveBeenCalled();
});
describe("prerequisites OK", () => { describe("prerequisites OK", () => {
beforeEach(() => { beforeEach(() => {
installer.arePrerequisitesCorrect.mockResolvedValue(true); installer.arePrerequisitesCorrect.mockResolvedValue(true);
mockMarketplaceSetup.runClientWizard.mockResolvedValue(true);
installer.isCodexInstalled.mockResolvedValue(true); installer.isCodexInstalled.mockResolvedValue(true);
}); });

View File

@ -10,9 +10,11 @@ import { ProcessControl } from "./processControl.js";
describe("ProcessControl", () => { describe("ProcessControl", () => {
let processControl; let processControl;
const mockEthProvider = "mockEthProvider";
beforeEach(() => { beforeEach(() => {
vi.resetAllMocks(); vi.resetAllMocks();
mockCodexGlobals.getEthProvider.mockReturnValue(mockEthProvider);
processControl = new ProcessControl( processControl = new ProcessControl(
mockConfigService, mockConfigService,
@ -195,7 +197,12 @@ describe("ProcessControl", () => {
expect(mockShellService.spawnDetachedProcess).toHaveBeenCalledWith( expect(mockShellService.spawnDetachedProcess).toHaveBeenCalledWith(
exe, exe,
config.codexRoot, config.codexRoot,
[`--config-file=${configFile}`], [
`--config-file=${configFile}`,
"persistence",
`--eth-provider=${mockEthProvider}`,
`--eth-private-key=eth.key`, // duplicated in configService.
],
); );
}); });
}); });

View File

@ -94,7 +94,7 @@ export class ConfigService {
this.fs.writeFile( this.fs.writeFile(
this.getCodexConfigFilePath(), this.getCodexConfigFilePath(),
`data-dir="${datadir}"${nl}` + `data-dir="${datadir}"${nl}` +
`log-level="TRACE"${nl}` + `log-level="DEBUG"${nl}` +
`log-file="${codexLogFile}"${nl}` + `log-file="${codexLogFile}"${nl}` +
`storage-quota=${this.config.storageQuota}${nl}` + `storage-quota=${this.config.storageQuota}${nl}` +
`disc-port=${this.config.ports.discPort}${nl}` + `disc-port=${this.config.ports.discPort}${nl}` +
@ -105,7 +105,7 @@ export class ConfigService {
`bootstrap-node=[${bootNodes}]${nl}` + `bootstrap-node=[${bootNodes}]${nl}` +
// Marketplace client parameters cannot be set via config file. // Marketplace client parameters cannot be set via config file.
// Open issue: https://github.com/codex-storage/nim-codex/issues/1206 // Open issue: https://github.com/codex-storage/nim-codex/issues/1206
`${nl}`, "",
); );
}; };
} }

View File

@ -112,6 +112,33 @@ describe("ConfigService", () => {
}); });
}); });
describe("getEthFilePaths", () => {
const result1 = "path/to/key";
const result2 = "path/to/address";
it("returns the key and address file paths", () => {
const configService = new ConfigService(mockFsService, mockOsService);
mockFsService.pathJoin = vi.fn();
mockFsService.pathJoin.mockReturnValueOnce(result1);
mockFsService.pathJoin.mockReturnValueOnce(result2);
expect(configService.getEthFilePaths()).toEqual({
key: result1,
address: result2,
});
expect(mockFsService.pathJoin).toHaveBeenCalledWith([
expectedDefaultConfig.codexRoot,
"eth.key",
]);
expect(mockFsService.pathJoin).toHaveBeenCalledWith([
expectedDefaultConfig.codexRoot,
"eth.address",
]);
});
});
describe("validateConfiguration", () => { describe("validateConfiguration", () => {
var configService; var configService;
var config; var config;

View File

@ -91,12 +91,12 @@ export class MainMenu {
}; };
startCodex = async () => { startCodex = async () => {
// const spinner = this.ui.createAndStartSpinner("Starting..."); const spinner = this.ui.createAndStartSpinner("Starting...");
try { try {
await this.processControl.startCodexProcess(); await this.processControl.startCodexProcess();
// this.ui.stopSpinnerSuccess(spinner); this.ui.stopSpinnerSuccess(spinner);
} catch (exception) { } catch (exception) {
// this.ui.stopSpinnerError(spinner); this.ui.stopSpinnerError(spinner);
this.ui.showErrorMessage(`Failed to start Codex. "${exception}"`); this.ui.showErrorMessage(`Failed to start Codex. "${exception}"`);
} }
}; };