From a1219dfa9bed52882d47279a27b54a6addb2b1b9 Mon Sep 17 00:00:00 2001 From: Kevinjohn Gallagher Date: Sun, 19 Jul 2026 23:56:16 +0100 Subject: [PATCH] Add argument parser unit coverage --- tests/args.test.mjs | 67 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/args.test.mjs diff --git a/tests/args.test.mjs b/tests/args.test.mjs new file mode 100644 index 000000000..74b00db30 --- /dev/null +++ b/tests/args.test.mjs @@ -0,0 +1,67 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { parseArgs, splitRawArgumentString } from "../plugins/codex/scripts/lib/args.mjs"; + +const config = { + valueOptions: ["base", "scope"], + booleanOptions: ["wait", "background"], + aliasMap: { b: "base", w: "wait" } +}; + +test("parseArgs handles separate and inline long-option values", () => { + assert.deepEqual(parseArgs(["--base", "main", "--scope=branch", "focus"], config), { + options: { base: "main", scope: "branch" }, + positionals: ["focus"] + }); +}); + +test("parseArgs handles boolean values and short aliases", () => { + assert.deepEqual(parseArgs(["-w", "--background=false", "-b", "trunk"], config), { + options: { wait: true, background: false, base: "trunk" }, + positionals: [] + }); +}); + +test("parseArgs preserves unknown options as positionals", () => { + assert.deepEqual(parseArgs(["--unknown=value", "-x", "-"], config), { + options: {}, + positionals: ["--unknown=value", "-x", "-"] + }); +}); + +test("parseArgs treats every token after -- as positional", () => { + assert.deepEqual(parseArgs(["--wait", "--", "--base", "main"], config), { + options: { wait: true }, + positionals: ["--base", "main"] + }); +}); + +test("parseArgs rejects missing long-option values", () => { + assert.throws(() => parseArgs(["--base"], config), /Missing value for --base/); +}); + +test("parseArgs rejects missing short-option values", () => { + assert.throws(() => parseArgs(["-b"], config), /Missing value for -b/); +}); + +test("splitRawArgumentString groups single- and double-quoted text", () => { + assert.deepEqual(splitRawArgumentString(`review "two words" 'single quoted' plain`), [ + "review", + "two words", + "single quoted", + "plain" + ]); +}); + +test("splitRawArgumentString handles escaped spaces, quotes, and backslashes", () => { + assert.deepEqual(splitRawArgumentString(String.raw`one\ two \"quoted\" path\\tail`), [ + "one two", + '"quoted"', + "path\\tail" + ]); +}); + +test("splitRawArgumentString preserves a trailing backslash", () => { + assert.deepEqual(splitRawArgumentString("value\\"), ["value\\"]); +});