Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ts/src/cli/insight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions ts/tests/insight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Loading