diff --git a/ts/src/cli/insight.ts b/ts/src/cli/insight.ts index d2ddf5f4..473fd3b3 100644 --- a/ts/src/cli/insight.ts +++ b/ts/src/cli/insight.ts @@ -9,7 +9,8 @@ const { values, positionals } = parseArgs({ allowPositionals: true, options: { p const cmd = positionals[0]; const out = (o: unknown) => console.log(JSON.stringify(o, (_, v) => v, 2)); if (cmd === "cost") { - const log = process.env.AI_USAGE_LOG ?? "", hours = values.hours ? Number(values.hours) : undefined; + const log = process.env.AI_USAGE_LOG ?? "", hours = values.hours === undefined ? undefined : Number(values.hours); + if (hours !== undefined && !(Number.isFinite(hours) && hours > 0)) { console.error("--hours must be a positive number"); process.exit(2); } const rows = summarize(log, hours), skills = summarizeSkills(log, hours), tokenix = summarizeTokenix(log, hours); if (values.json) out({ cost: rows, skills, tokenix }); else { diff --git a/ts/tests/insight.test.ts b/ts/tests/insight.test.ts index c7aed245..ac862fec 100644 --- a/ts/tests/insight.test.ts +++ b/ts/tests/insight.test.ts @@ -16,3 +16,15 @@ test("explain: invalid ref, unavailable PR, live decision", async () => { expect(r2.pipeline_state).toBe("success"); expect(r2.reasons).toContain("merge_conflicts"); expect(r2.reasons).toContain("autonomy_mode:observe"); expect(r2.decision).toBe("blocked"); expect(jobHistory(s, "o/r#1")).toEqual([]); }); + +// NaN or a non-positive window used to be read as "no window" and printed all-time totals as if they were the last N hours. +test("pr-insight cost rejects a --hours value that is not a positive number", () => { + const cli = join(import.meta.dir, "../src/cli/insight.ts"); + const run = (h: string) => Bun.spawnSync(["bun", cli, "cost", `--hours=${h}`], { env: { ...process.env, AI_USAGE_LOG: "/nonexistent" } }); + for (const bad of ["abc", "0", "-5"]) { + const r = run(bad); + expect(r.exitCode).toBe(2); + expect(r.stderr.toString()).toContain("--hours"); + } + expect(run("24").exitCode).toBe(0); +});