import { describe, test, expect } from "bun:test"; import { runCLI } from "./helpers"; // These assert on validation error codes that are emitted BEFORE any network // call (or independently of it), so the suite is network-free: a valid-flag case // still runs offline because it only checks the ABSENCE of a validation error. function parsedStderr(stderr: string): { error?: string; code?: string } { try { return JSON.parse(stderr); } catch { return {}; } } describe("freehire CLI flag validation", () => { describe("numeric flag validation", () => { for (const name of ["jobage", "page", "limit"]) { test(`--${name} non-numeric exits 1 with BAD_ARG`, async () => { const result = await runCLI(["search", `--${name}`, "foo"]); expect(result.exitCode).not.toBe(0); const err = parsedStderr(result.stderr); expect(err.code).toBe("BAD_ARG"); expect(err.error).toMatch(new RegExp(name)); }); } test("valid integers produce no BAD_ARG", async () => { const result = await runCLI(["search", "--jobage", "7", "--page", "1", "--limit", "1"]); expect(parsedStderr(result.stderr).code).not.toBe("BAD_ARG"); }); }); describe("--facet validation", () => { test("a facet without '=' exits 1 with BAD_ARG", async () => { const result = await runCLI(["search", "--facet", "novalue"]); expect(result.exitCode).not.toBe(0); expect(parsedStderr(result.stderr).code).toBe("BAD_ARG"); }); }); describe("detail argument validation", () => { test("missing slug exits 1 with NO_ID", async () => { const result = await runCLI(["detail"]); expect(result.exitCode).not.toBe(0); expect(parsedStderr(result.stderr).code).toBe("NO_ID"); }); test("an unparseable slug exits 1 with BAD_ID (no network)", async () => { const result = await runCLI(["detail", "not a slug!"]); expect(result.exitCode).not.toBe(0); expect(parsedStderr(result.stderr).code).toBe("BAD_ID"); }); }); describe("command dispatch", () => { test("unknown command exits 1 with BAD_CMD", async () => { const result = await runCLI(["frobnicate"]); expect(result.exitCode).not.toBe(0); expect(parsedStderr(result.stderr).code).toBe("BAD_CMD"); }); test("no command prints help and exits 1", async () => { const result = await runCLI([]); expect(result.exitCode).toBe(1); expect(result.stdout).toMatch(/USAGE/); }); }); });