Test updates for mainmenu and datamenu

This commit is contained in:
Ben 2025-05-26 12:51:16 +02:00
parent b13089c0b2
commit 05daffc2f5
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
5 changed files with 145 additions and 3 deletions

View File

@ -64,3 +64,8 @@ export const mockCodexApp = {
export const mockMarketplaceSetup = {
runClientWizard: vi.fn(),
};
export const mockDataService = {
upload: vi.fn(),
download: vi.fn(),
};

View File

@ -7,3 +7,8 @@ export const mockInstallMenu = {
export const mockConfigMenu = {
show: vi.fn(),
};
export const mockDataMenu = {
performUpload: vi.fn(),
performDownload: vi.fn(),
};

View File

@ -20,7 +20,7 @@ export class DataService {
const metadata = { filename: filename, mimetype: contentType };
const strategy = new NodeUploadStategy(fileData, metadata);
const strategy = new NodeUploadStategy(fileData, metadata);
const uploadResponse = data.upload(strategy);
const res = await uploadResponse.result;
@ -31,6 +31,8 @@ export class DataService {
};
download = async (cid) => {
throw new Error("Waiting for fix of codex-js sdk");
const data = this.getCodexData();
const manifest = await data.fetchManifest(cid);
const filename = this.getFilename(manifest);

123
src/ui/dataMenu.test.js Normal file
View File

@ -0,0 +1,123 @@
import { describe, beforeEach, it, expect, vi } from "vitest";
import { DataMenu } from "./dataMenu.js";
import {
mockUiService,
mockFsService,
mockDataService,
} from "../__mocks__/service.mocks.js";
describe("DataMenu", () => {
let dataMenu;
const filePath = "testfilepath";
const cid = "testcid";
beforeEach(() => {
vi.resetAllMocks();
dataMenu = new DataMenu(mockUiService, mockFsService, mockDataService);
});
describe("performUpload", () => {
beforeEach(() => {
mockUiService.askPrompt.mockResolvedValue(filePath);
mockDataService.upload.mockResolvedValue(cid);
});
it("shows encryption warning", async () => {
await dataMenu.performUpload();
expect(mockUiService.showInfoMessage).toHaveBeenCalledWith(
"⚠️ Codex does not encrypt files. Anything uploaded will be available publicly on testnet.",
);
});
it("prompts the user for a filepath", async () => {
await dataMenu.performUpload();
expect(mockUiService.askPrompt).toHaveBeenCalledWith(
"Enter the file path",
);
});
it("checks that the provided path is a file", async () => {
await dataMenu.performUpload();
expect(mockFsService.isFile).toHaveBeenCalledWith(filePath);
});
it("shows an error when the provided path is not a file", async () => {
mockFsService.isFile.mockReturnValue(false);
await dataMenu.performUpload();
expect(mockUiService.showErrorMessage).toHaveBeenCalledWith(
"File not found",
);
});
it("calls the data service if the file does exist", async () => {
mockFsService.isFile.mockReturnValue(true);
await dataMenu.performUpload();
expect(mockDataService.upload).toHaveBeenCalledWith(filePath);
expect(mockUiService.showInfoMessage).toHaveBeenCalledWith(
`Upload successful.\n CID: '${cid}'`,
);
});
it("shows an error message when dataService throws", async () => {
const error = "testError";
mockFsService.isFile.mockReturnValue(true);
mockDataService.upload.mockRejectedValueOnce(new Error(error));
await dataMenu.performUpload();
expect(mockUiService.showErrorMessage).toHaveBeenCalledWith(
"Error during upload: Error: " + error,
);
});
});
describe("performDownload", () => {
beforeEach(() => {
mockUiService.askPrompt.mockResolvedValue(cid);
mockDataService.download.mockResolvedValue(filePath);
});
it("prompts the user for a cid", async () => {
await dataMenu.performDownload();
expect(mockUiService.askPrompt).toHaveBeenCalledWith("Enter the CID");
});
it("does nothing if provided input is empty", async () => {
mockUiService.askPrompt = vi.fn();
mockUiService.askPrompt.mockResolvedValue("");
await dataMenu.performDownload();
expect(mockDataService.download).not.toHaveBeenCalled();
});
it("calls the data service with the provided cid", async () => {
await dataMenu.performDownload();
expect(mockDataService.download).toHaveBeenCalledWith(cid);
expect(mockUiService.showInfoMessage).toHaveBeenCalledWith(
`Download successful.\n File: '${filePath}'`,
);
});
it("shows an error message when dataService throws", async () => {
const error = "testError";
mockDataService.download.mockRejectedValueOnce(new Error(error));
await dataMenu.performDownload();
expect(mockUiService.showErrorMessage).toHaveBeenCalledWith(
"Error during download: Error: " + error,
);
});
});
});

View File

@ -1,7 +1,11 @@
import { describe, beforeEach, it, expect, vi } from "vitest";
import { MainMenu } from "./mainMenu.js";
import { mockUiService, mockCodexApp } from "../__mocks__/service.mocks.js";
import { mockInstallMenu, mockConfigMenu } from "../__mocks__/ui.mocks.js";
import {
mockInstallMenu,
mockConfigMenu,
mockDataMenu,
} from "../__mocks__/ui.mocks.js";
import {
mockInstaller,
mockProcessControl,
@ -22,6 +26,7 @@ describe("mainmenu", () => {
mockInstaller,
mockProcessControl,
mockCodexApp,
mockDataMenu,
);
});
@ -102,7 +107,7 @@ describe("mainmenu", () => {
});
describe("showRunningMenu", () => {
it("shows a menu with options to stop Codex, open Codex app, or exit", async () => {
it("shows a menu with options to stop Codex, open Codex app, upload, download, or exit", async () => {
await mainmenu.showRunningMenu();
expect(mockUiService.askMultipleChoice).toHaveBeenCalledWith(
@ -110,6 +115,8 @@ describe("mainmenu", () => {
[
{ label: "Open Codex app", action: mockCodexApp.openCodexApp },
{ label: "Stop Codex", action: mainmenu.stopCodex },
{ label: "Upload a file", action: mockDataMenu.performUpload },
{ label: "Download a file", action: mockDataMenu.performDownload },
{
label: "Exit (Codex keeps running)",
action: mockMenuLoop.stopLoop,