diff --git a/pm-stack/aggregator/scripts/produce-charge-candidates.ts b/pm-stack/aggregator/scripts/produce-charge-candidates.ts index 6c9a3a9c8f..91333b1f55 100644 --- a/pm-stack/aggregator/scripts/produce-charge-candidates.ts +++ b/pm-stack/aggregator/scripts/produce-charge-candidates.ts @@ -108,6 +108,47 @@ export function stagedManifestPeriod(manifest: { start?: unknown; end?: unknown /** Plaid categories that are money movement, not merchant charges. */ const EXCLUDED_CATEGORIES = new Set(["Transfer", "Payment", "Interest", "Bank Fees"]); +const USAGE = "usage: bun run scripts/produce-charge-candidates.ts [--start YYYY-MM-DD] [--end YYYY-MM-DD] [--out ] [--skip-gmail]"; + +// Reject unknown, misspelled, joined, duplicated, or positional +// arguments BEFORE any connector call, staged-file read, or file +// write (#7302): argv used to be probed token-by-token only +// (argValue indexOf, the --skip-gmail includes check in main), so a +// typo --skip-gmial silently ran the Gmail read the operator meant +// to skip, and a typo or joined (--start=...) --start silently fell +// back to the default current-month period. Mirrors rejectUnknownArgs +// in generate-digest.ts (#7097) / collect-github-billing.ts (#7254). +// Called at the top of main() rather than at module scope because +// unit tests import this module pure helpers; main() only runs as +// the entry point, and its first action is still this check, before +// any I/O. Value validation itself stays in argValue/main (#3333). +function rejectUnknownArgs(): void { + const args = process.argv.slice(2); + const seen = new Set(); + for (let i = 0; i < args.length; i++) { + const token = args[i] as string; + if (token === "--start" || token === "--end" || token === "--out") { + if (seen.has(token)) { + console.error("producer: duplicate " + token + " (" + USAGE + ")"); + process.exit(2); + } + seen.add(token); + i++; // skip the value; argValue validates it + continue; + } + if (token === "--skip-gmail") { + if (seen.has(token)) { + console.error("producer: duplicate " + token + " (" + USAGE + ")"); + process.exit(2); + } + seen.add(token); + continue; + } + console.error("producer: unknown argument " + JSON.stringify(token) + " (" + USAGE + ")"); + process.exit(2); + } +} + function argValue(flag: string): string | undefined { const i = process.argv.indexOf(flag); if (i < 0) return undefined; @@ -410,6 +451,7 @@ async function gmailCandidates( } async function main(): Promise { + rejectUnknownArgs(); // Parse values before any connector call or file write. Staged periods // come from their manifest, so --start/--end remain ignored there. const outArg = argValue("--out"); diff --git a/pm-stack/aggregator/test/produce-charge-candidates.test.ts b/pm-stack/aggregator/test/produce-charge-candidates.test.ts index 52f8c0a498..578c8dde81 100644 --- a/pm-stack/aggregator/test/produce-charge-candidates.test.ts +++ b/pm-stack/aggregator/test/produce-charge-candidates.test.ts @@ -356,6 +356,33 @@ describe("live mode CLI validation (#3333)", () => { ); }); + test("an unknown flag is a usage error before any side effect (#7302)", () => { + expectCliUsageError(() => ["--bogus-flag"], "unknown argument"); + }); + + test("a misspelled --skip-gmial is a usage error, not a Gmail run (#7302)", () => { + expectCliUsageError(() => ["--skip-gmial"], "unknown argument"); + }); + + test("a joined --start=... is a usage error, not the default period (#7302)", () => { + expectCliUsageError(() => ["--start=2026-10-01"], "unknown argument"); + }); + + test("a duplicate --start is a usage error, not first-wins (#7302)", () => { + expectCliUsageError( + (h) => ["--start", "2026-10-01", "--start", "2026-09-01", "--out", h.out, "--skip-gmail"], + "duplicate --start", + ); + }); + + test("a duplicate --skip-gmail is a usage error (#7302)", () => { + expectCliUsageError(() => ["--skip-gmail", "--skip-gmail"], "duplicate --skip-gmail"); + }); + + test("a positional argument is a usage error (#7302)", () => { + expectCliUsageError(() => ["candidates.json"], "unknown argument"); + }); + test("valid explicit flags keep the period, output path, and summary", () => { const h = liveHarness(); writeFileSync(h.defaultOut, "preserve existing candidates");