import { describe, expect, test } from "bun:test"; import { HELP_REGISTRY } from "./help.js"; import { booleanOption, BOOLEAN_FLAGS, KNOWN_FLAGS, numberOption, parseArgs, parseDelegationExpireHours, parseHoseExpireSeconds, splitOption, stringOption, VALUE_FLAGS } from "./parser.js"; describe("parseArgs flag handling", () => { test("boolean flags never consume the next token", () => { // Before the metadata registry, `--all now` fed "now" into --all and // booleanOption("now") threw `Invalid boolean: now`. const parsed = parseArgs(["corps", "list", "--all", "now"]); expect(parsed.options.all).toBe(true); expect(parsed.command).toEqual(["corps", "list", "now"]); }); test("boolean flags at the end of argv stay boolean", () => { expect(parseArgs(["corps", "list", "--all"]).options.all).toBe(true); }); test("value flags consume exactly one token", () => { const parsed = parseArgs(["approval-runs", "list", "--limit", "25", "--status", "completed"]); expect(parsed.options.limit).toBe("25"); expect(parsed.options.status).toBe("completed"); expect(parsed.command).toEqual(["approval-runs", "list"]); }); test("value flag with a missing value is a usage error, not a silent drop", () => { // numberOption(true) previously returned undefined, so a bare `--limit` // silently meant "no limit" and pulled the full dataset. expect(() => parseArgs(["approval-runs", "list", "--limit"])).toThrow( "Option --limit requires a value." ); expect(() => parseArgs(["approval-runs", "list", "--limit", "--status", "completed"])).toThrow( "Option --limit requires a value." ); }); test("value flags accept negative numbers as values", () => { expect(parseArgs(["corps", "search", "x", "--offset", "-5"]).options.offset).toBe("-5"); }); test("unknown flags are rejected with a did-you-mean suggestion", () => { // `corps list --limitt 5` used to exit 0 while silently ignoring the flag. expect(() => parseArgs(["corps", "list", "--limitt", "5"])).toThrow( "Unknown option: --limitt. Did you mean --limit?" ); expect(() => parseArgs(["corps", "list", "--bogus-flag"])).toThrow( "Unknown option: --bogus-flag." ); }); test("kebab and camel spellings of the same flag both stay registered", () => { const kebab = parseArgs(["corps", "list", "--record-id", "abc"]); const camel = parseArgs(["corps", "list", "--recordId", "abc"]); expect(kebab.options["record-id"]).toBe("abc"); expect(camel.options.recordId).toBe("abc"); }); test("the = value form bypasses next-token consumption", () => { expect(parseArgs(["rules", "get", "--code=0038"]).options.code).toBe("0038"); expect(parseArgs(["corps", "list", "--all=true"]).options.all).toBe("true"); }); test("ambiguous flags (f / h / version) keep the legacy heuristic", () => { expect(parseArgs(["rules", "lint", "--f", "/tmp/rules.json"]).options.f).toBe("/tmp/rules.json"); expect(parseArgs(["auth", "hose-link", "--f"]).options.f).toBe(true); expect(parseArgs(["http", "--h", "127.0.0.1"]).options.h).toBe("127.0.0.1"); expect(parseArgs(["corps", "list", "--help"]).options.help).toBe(true); // Bare --version (no area) must stay boolean: it is the version alias. expect(parseArgs(["--version"]).options.version).toBe(true); expect(parseArgs(["--v"]).options.v).toBe(true); expect(parseArgs(["--V"]).options.V).toBe(true); // With a value it must consume it: rules/update read --version as a value. expect(parseArgs(["rules", "get", "--version", "latest"]).options.version).toBe("latest"); }); test("--json still parses its payload and rejects non-objects downstream", () => { expect(parseArgs(["corps", "add", "--json", '{"name":"Acme"}']).json).toEqual({ name: "Acme" }); expect(() => parseArgs(["corps", "add", "--json"])).toThrow("--json requires a JSON object argument"); expect(() => parseArgs(["corps", "add", "--json", "not-json"])).toThrow("Invalid JSON:"); }); }); describe("option helpers", () => { test("numberOption rejects non-numeric input", () => { expect(numberOption("25")).toBe(25); expect(numberOption(undefined)).toBeUndefined(); expect(() => numberOption("abc")).toThrow("Invalid number: abc"); }); test("booleanOption accepts true/false strings and rejects other values", () => { expect(booleanOption("true")).toBe(true); expect(booleanOption("false")).toBe(false); expect(booleanOption(true)).toBe(true); expect(() => booleanOption("now")).toThrow("Invalid boolean: now"); }); test("stringOption drops bare booleans so a missing value cannot masquerade", () => { expect(stringOption("x")).toBe("x"); expect(stringOption(true)).toBeUndefined(); }); test("splitOption trims and drops empty entries", () => { expect(splitOption("a, b ,,c")).toEqual(["a", "b", "c"]); expect(splitOption("")).toBeUndefined(); expect(splitOption(true)).toBeUndefined(); }); test("parseDelegationExpireHours parses days, hours, rounds down with stderr notice, and clamps to 1..168", () => { expect(parseDelegationExpireHours(undefined)).toBe(24); expect(parseDelegationExpireHours("7d")).toBe(168); expect(parseDelegationExpireHours("1d")).toBe(24); expect(parseDelegationExpireHours("24h")).toBe(24); expect(parseDelegationExpireHours("2h")).toBe(2); expect(parseDelegationExpireHours("168")).toBe(168); expect(parseDelegationExpireHours(168)).toBe(168); expect(parseDelegationExpireHours("86400")).toBe(24); expect(parseDelegationExpireHours("7200s")).toBe(2); // Floor fractional hours and clamp minimum to 1h expect(parseDelegationExpireHours("90m")).toBe(1); expect(parseDelegationExpireHours("1.5h")).toBe(1); expect(parseDelegationExpireHours("30m")).toBe(1); expect(parseDelegationExpireHours("1800s")).toBe(1); expect(() => parseDelegationExpireHours("8d")).toThrow("最多为 7 天 (168h)"); expect(() => parseDelegationExpireHours("0h")).toThrow("必须为大于 0 的有效时长"); expect(() => parseDelegationExpireHours("invalid")).toThrow("必须为有效时长格式"); }); test("parseHoseExpireSeconds parses days, hours, minutes, raw seconds, and converts unitless small numbers to hours", () => { expect(parseHoseExpireSeconds(undefined)).toBe(7200); expect(parseHoseExpireSeconds("7200")).toBe(7200); expect(parseHoseExpireSeconds(7200)).toBe(7200); expect(parseHoseExpireSeconds("2h")).toBe(7200); expect(parseHoseExpireSeconds("7d")).toBe(604800); expect(parseHoseExpireSeconds("30m")).toBe(1800); // Unitless small numbers (<= 168) convert to hours automatically expect(parseHoseExpireSeconds(2)).toBe(7200); expect(parseHoseExpireSeconds("2")).toBe(7200); expect(parseHoseExpireSeconds("24")).toBe(86400); expect(() => parseHoseExpireSeconds("0s")).toThrow("必须为大于 0 的有效时长"); expect(() => parseHoseExpireSeconds("invalid")).toThrow("必须为有效时长"); }); test("overflowing --expire literals are usage errors before reaching auth", () => { for (const parse of [parseDelegationExpireHours, parseHoseExpireSeconds]) { for (const literal of ["9".repeat(309), "9".repeat(309) + "s", "9".repeat(308) + "d"]) { expect(() => parse(literal)).toThrow(); try { parse(literal); } catch (error) { expect((error as { exitCode: number }).exitCode).toBe(2); } } } }); test("both --expire flags read a unitless literal the same way", () => { // Regression: the two parsers used to each carry their own copy of the // unitless heuristic, so `--expire 300` meant 5 minutes on one path and // clamped to 1 hour on the other purely by implementation accident. for (const literal of [300, "300", 86400, "86400", 7200, "7200"]) { const hours = parseDelegationExpireHours(literal); const seconds = parseHoseExpireSeconds(literal); const unitlessSeconds = typeof literal === "number" ? literal : Number(literal); expect(seconds).toBe(unitlessSeconds); // Delegation floors to whole hours and never drops below 1h. expect(hours).toBe(Math.max(Math.floor(seconds / 3600), 1)); } // A small unitless value is hours on both paths. expect(parseDelegationExpireHours("24")).toBe(24); expect(parseHoseExpireSeconds("24")).toBe(86_400); expect(parseDelegationExpireHours(2)).toBe(2); expect(parseHoseExpireSeconds(2)).toBe(7200); }); test("sub-second input stays a usage error on both --expire flags", () => { // Regression: the shared tokenizer understands `ms` for the simulation // windows, and without a per-caller unit set that grammar leaked into // --expire, silently rounding 500ms up to a 1h / 1s link. expect(() => parseDelegationExpireHours("500ms")).toThrow("必须为有效时长格式"); expect(() => parseHoseExpireSeconds("500ms")).toThrow("必须为有效时长"); expect(() => parseHoseExpireSeconds("200ms")).toThrow("必须为有效时长"); }); }); describe("flag registry cross-check", () => { test("every flag documented in help.ts is registered", () => { const documented = new Set(HELP_REGISTRY.flatMap((area) => area.commands.flatMap((command) => (command.options ?? []).flatMap((option) => [...option.flag.matchAll(/--([a-zA-Z][a-zA-Z0-9-]*)/g)].map((match) => match[1]!)) ))); expect([...documented].filter((flag) => !KNOWN_FLAGS.has(flag))).toEqual([]); expect(documented.size).toBeGreaterThanOrEqual(20); }); test("every flag shown in a help usage line is registered, including bracket and pipe forms", () => { const usageFlags = new Set(HELP_REGISTRY.flatMap((area) => area.commands.flatMap((command) => command.usage.flatMap((usage) => [...usage.matchAll(/--([a-zA-Z][a-zA-Z0-9-]*)/g)].map((match) => match[1]!)) ))); expect([...usageFlags].filter((flag) => !KNOWN_FLAGS.has(flag))).toEqual([]); }); test("formerly rejected public flags retain their boolean and value arity", () => { const parsed = parseArgs(["report", "data", "--templates", "one,two", "--latest", "record"]); expect(parsed.options).toEqual({ templates: "one,two", latest: true }); expect(parsed.command).toEqual(["report", "data", "record"]); expect(parseArgs(["config", "test-dir", "--clear"]).options.clear).toBe(true); expect(parseArgs(["webhook", "get", "id", "--show-secret"]).options["show-secret"]).toBe(true); expect(parseArgs(["webhook", "update", "id", "--min-file-size", "10", "--clear-max-file-size"]).options) .toEqual({ "min-file-size": "10", "clear-max-file-size": true }); expect(() => parseArgs(["report", "data", "--templates"])).toThrow("requires a value"); expect(() => parseArgs(["corps", "list", "--limitt=5"])).toThrow("Unknown option: --limitt"); }); test("boolean and value sets partition the registry apart from the ambiguous three", () => { // Guards the invariant the parser relies on: no flag may be in both sets. const both = [...KNOWN_FLAGS].filter( (flag) => BOOLEAN_FLAGS.has(flag) && VALUE_FLAGS.has(flag) ); expect(both).toEqual([]); // The union plus the ambiguous three must cover the whole registry, so a // flag can never fall through to an unintended branch. const union = new Set([...BOOLEAN_FLAGS, ...VALUE_FLAGS, "f", "h", "version"]); expect([...KNOWN_FLAGS].every((flag) => union.has(flag))).toBe(true); expect(KNOWN_FLAGS.size).toBe(union.size); }); });