diff --git a/src/cli.ts b/src/cli.ts index 9e52755..c9e8c4e 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -169,14 +169,14 @@ export async function main(argv: string[]): Promise { const args = parseArgs(argv); const { command, flags } = args; - if (flags["help"] || command === "help" || command === undefined) { - process.stdout.write(usage()); - return command === undefined && !flags["help"] ? 2 : 0; - } if (flags["version"]) { process.stdout.write(`${VERSION}\n`); return 0; } + if (flags["help"] || command === "help" || command === undefined) { + process.stdout.write(usage()); + return command === undefined && !flags["help"] ? 2 : 0; + } if (command === "rules") { printRules(); return 0; diff --git a/test/cli.test.ts b/test/cli.test.ts index f4f1cf8..7ac10d4 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -1,5 +1,32 @@ -import { describe, expect, it } from "vitest"; -import { collectHeaders, parseArgs } from "../src/cli.js"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { collectHeaders, main, parseArgs } from "../src/cli.js"; + +afterEach(() => { + vi.restoreAllMocks(); +}); + +describe("top-level information flags", () => { + it("prints the version and succeeds without a positional command", async () => { + const write = vi.spyOn(process.stdout, "write").mockImplementation(() => true); + + await expect(main(["--version"])).resolves.toBe(0); + + expect(write).toHaveBeenCalledOnce(); + expect(write).toHaveBeenCalledWith(expect.stringMatching(/^\d+\.\d+\.\d+\n$/)); + }); + + it("keeps bare invocation as a usage error", async () => { + vi.spyOn(process.stdout, "write").mockImplementation(() => true); + + await expect(main([])).resolves.toBe(2); + }); + + it("keeps explicit help successful", async () => { + vi.spyOn(process.stdout, "write").mockImplementation(() => true); + + await expect(main(["--help"])).resolves.toBe(0); + }); +}); describe("HTTP headers", () => { it("collects every repeated --header flag", () => {