Skip to content
This repository was archived by the owner on Jul 14, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const buildCmd = (): Command => {
try {
console.log("🚀 Initializing configuration...\n");

const existingConfig = await loadConfig();
const existingConfig = await loadConfig(options.force);

if (existingConfig.apiKey && !options.force) {
console.log("⚠️ Configuration already exists.");
Expand Down Expand Up @@ -259,7 +259,7 @@ export const buildCmd = (): Command => {
.option(
"-l, --host-network",
"Use host network (default: false). If set you can use localhost directly",
false,
false
)
.action(startPrivateLocationWorker);

Expand Down Expand Up @@ -316,10 +316,10 @@ export const buildCmd = (): Command => {
.addOption(testTargetIdOption)
.action(async (options, command) => {
const resolvedTestTargetId = await resolveTestTargetId(
options.testTargetId,
options.testTargetId
);
command.setOptionValue("testTargetId", resolvedTestTargetId);
void listTestCases({...options, status: "ENABLED"});
void listTestCases({ ...options, status: "ENABLED" });
});

return program;
Expand Down
12 changes: 9 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ export function getConfigPath(): string {
return path.join(process.cwd(), OCTOMIND_CONFIG_FILE);
}

export async function loadConfig(): Promise<Config> {
export async function loadConfig(force?: boolean): Promise<Config> {
try {
const configPath = getConfigPath();
const data = await fs.readFile(configPath, "utf8");
return JSON.parse(data);
} catch (error) {
console.error("❌ Error parsing configuration:", (error as Error).message);

// only exit on overwrite attempt
if (force) {
console.error(
"❌ Error parsing configuration:",
(error as Error).message
);
process.exit(1);
}
return {};
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@ jest.mock("../src/config", () => ({
loadConfig: jest.fn(),
}));

const originalConsoleLog = console.log;
const originalConsoleError = console.error;

beforeAll(() => {
buildCmd();
program.exitOverride((err) => {
throw err;
});
});

beforeEach(() => {
console.log = jest.fn();
console.error = jest.fn();
});

afterEach(() => {
console.log = originalConsoleLog;
console.error = originalConsoleError;
});

describe("CLI Commands parsing options", () => {
const stdArgs = [
"node",
Expand Down
29 changes: 27 additions & 2 deletions tests/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,42 @@ describe("Config", () => {
);
});

it("should error when config file does not exist", async () => {
it("should return empty config when config file does not exist", async () => {
const fileNotFoundError = new Error(
"ENOENT: no such file or directory",
) as Error & { code?: string };
fileNotFoundError.code = "ENOENT";

mockedFs.readFile.mockRejectedValue(fileNotFoundError);

await loadConfig();
const result = await loadConfig();

expect(result).toEqual({});
expect(console.error).not.toHaveBeenCalled();
});

it("should error and exit when config file does not exist and force is true", async () => {
const fileNotFoundError = new Error(
"ENOENT: no such file or directory",
) as Error & { code?: string };
fileNotFoundError.code = "ENOENT";

mockedFs.readFile.mockRejectedValue(fileNotFoundError);

const mockExit = jest
.spyOn(process, "exit")
.mockImplementation((code?: string | number | null | undefined) => {
throw new Error(`Process exit with code: ${code}`);
});

const MOCK_FORCE_OPTION = true;
await expect(loadConfig(MOCK_FORCE_OPTION)).rejects.toThrow(
"Process exit with code: 1",
);
expect(console.error).toHaveBeenCalled();
expect(mockExit).toHaveBeenCalledWith(1);

mockExit.mockRestore();
});
});
});