From 6d677e7a2f4e9c70b995703e8c3491f3027a044c Mon Sep 17 00:00:00 2001 From: Victor <70475442+vsolano9@users.noreply.github.com> Date: Sun, 23 Aug 2026 15:38:35 +0200 Subject: [PATCH] fix(cli): handle top-level version flag Handle --version before the missing-command usage branch so a conventional top-level version probe succeeds. Add regression coverage for version, bare invocation, and explicit help behavior. --- src/cli.ts | 8 ++++---- test/cli.test.ts | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) 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..902e195 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -1,5 +1,34 @@ -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 only the version and succeeds without a 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 a bare invocation as a usage error", async () => { + const write = vi.spyOn(process.stdout, "write").mockImplementation(() => true); + + await expect(main([])).resolves.toBe(2); + expect(write).toHaveBeenCalledWith(expect.stringContaining("USAGE")); + }); + + it("keeps explicit help successful", async () => { + const write = vi.spyOn(process.stdout, "write").mockImplementation(() => true); + + await expect(main(["--help"])).resolves.toBe(0); + expect(write).toHaveBeenCalledWith(expect.stringContaining("USAGE")); + }); +}); describe("HTTP headers", () => { it("collects every repeated --header flag", () => {