From e9f5c711d1a4839a4ffa9359d32fd20536fe34e3 Mon Sep 17 00:00:00 2001 From: Fabio <161820267+fabiomaienschein@users.noreply.github.com> Date: Fri, 25 Jul 2025 11:36:39 +0200 Subject: [PATCH] tests --- tests/cli.spec.ts | 72 +++++++++++++++++++++++++++++++++++++++----- tests/config.spec.ts | 50 ++++++++++++++++++++++++++++++ tsconfig.json | 3 +- 3 files changed, 117 insertions(+), 8 deletions(-) create mode 100644 tests/config.spec.ts diff --git a/tests/cli.spec.ts b/tests/cli.spec.ts index 4549028..76062c3 100644 --- a/tests/cli.spec.ts +++ b/tests/cli.spec.ts @@ -1,8 +1,22 @@ import { program } from "commander"; import { buildCmd } from "../src/cli"; import { executeTests } from "../src/tools"; +import { runDebugtopus } from "../src/debugtopus"; +import { loadConfig } from "../src/config"; jest.mock("../src/tools"); +jest.mock("../src/debugtopus"); +jest.mock("../src/config", () => ({ + ...jest.requireActual("../src/config"), + loadConfig: jest.fn(), +})); + +beforeAll(() => { + buildCmd(); + program.exitOverride((err) => { + throw err; + }); +}); describe("CLI Commands parsing options", () => { const stdArgs = [ @@ -15,13 +29,6 @@ describe("CLI Commands parsing options", () => { "test-target-123", ]; - beforeAll(() => { - buildCmd(); - program.exitOverride((err) => { - throw err; - }); - }); - beforeEach(() => { jest.clearAllMocks(); }); @@ -75,3 +82,54 @@ describe("CLI Commands parsing options", () => { ); }); }); + +describe("config overwrite behaviour", () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + it("should resolve testTargetId from parameter when provided", async () => { + const providedTestTargetId = "provided-test-target-123"; + + await program.parseAsync([ + "node", + "cli.js", + "debug", + "--url", + "https://example.com", + "--testTargetId", + providedTestTargetId, + ]); + + expect(runDebugtopus).toHaveBeenCalledWith( + expect.objectContaining({ + url: "https://example.com", + testTargetId: providedTestTargetId, + }), + ); + }); + + it("should resolve testTargetId from config when not provided as parameter", async () => { + const mockResolvedId = "config-test-target-456"; + const mockConfig = { + apiKey: "test-api-key", + testTargetId: mockResolvedId, + }; + (loadConfig as jest.Mock).mockResolvedValue(mockConfig); + + await program.parseAsync([ + "node", + "cli.js", + "debug", + "--url", + "https://example.com", + ]); + + expect(runDebugtopus).toHaveBeenCalledWith( + expect.objectContaining({ + url: "https://example.com", + testTargetId: mockResolvedId, + }), + ); + }); +}); diff --git a/tests/config.spec.ts b/tests/config.spec.ts new file mode 100644 index 0000000..3e221e2 --- /dev/null +++ b/tests/config.spec.ts @@ -0,0 +1,50 @@ +import fs from "fs/promises"; +import path from "path"; +import { loadConfig, Config } from "../src/config"; + +jest.mock("fs/promises"); +const mockedFs = fs as jest.Mocked; + +const originalConsoleError = console.error; + +describe("Config", () => { + beforeEach(() => { + console.error = jest.fn(); + }); + + afterEach(() => { + console.error = originalConsoleError; + }); + + describe("loadConfig", () => { + it("should load and parse a valid config file", async () => { + const mockConfig: Config = { + apiKey: "test-api-key-12345", + testTargetId: "test-target-id-67890", + }; + + mockedFs.readFile.mockResolvedValue(JSON.stringify(mockConfig)); + + const result = await loadConfig(); + + expect(result).toEqual(mockConfig); + expect(mockedFs.readFile).toHaveBeenCalledWith( + path.join(process.cwd(), "octomind.config.json"), + "utf8", + ); + }); + + it("should error 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(); + + expect(console.error).toHaveBeenCalled(); + }); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 64641af..06eb7d2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,8 @@ "strict": true, "noImplicitAny": true, "esModuleInterop": true, - "resolveJsonModule": true + "resolveJsonModule": true, + "isolatedModules": true }, "exclude": [ "node_modules",