From e89f7199f0e2cca87e5ab6d99cc4e3bf43efb1dc Mon Sep 17 00:00:00 2001 From: Rishabh Bhardwaj Date: Tue, 8 Sep 2026 00:23:34 +0530 Subject: [PATCH] Fix top zero report handling --- src/report.js | 12 +++++++----- test/ctxtrim.test.js | 9 +++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/report.js b/src/report.js index 002fe2a..e1387d7 100644 --- a/src/report.js +++ b/src/report.js @@ -27,10 +27,12 @@ export function textReport(scan, { price, top, wrote }) { const cats = Object.entries(t.byCategory).sort((a, b) => b[1].tokens - a[1].tokens); L.push(c("90", " by category: ") + cats.map(([k, v]) => `${k} ${fmtTokens(v.tokens)}`).join(c("90", " · "))); L.push(""); - L.push(c("1", " Top offenders")); - for (const f of scan.files.filter((f) => f.trim).slice(0, top)) { - const tok = ("~" + fmtTokens(f.tokens)).padStart(7); - L.push(` ${c("33", tok)} ${c("90", usd(dollars(f.tokens, price)).padStart(7))} ${f.rel} ${c("90", "— " + f.reason)}`); + if (top > 0) { + L.push(c("1", " Top offenders")); + for (const f of scan.files.filter((f) => f.trim).slice(0, top)) { + const tok = ("~" + fmtTokens(f.tokens)).padStart(7); + L.push(` ${c("33", tok)} ${c("90", usd(dollars(f.tokens, price)).padStart(7))} ${f.rel} ${c("90", "— " + f.reason)}`); + } } L.push(""); if (wrote && wrote.length) { @@ -57,4 +59,4 @@ export function jsonReport(scan, { price, wrote }) { offenders: scan.files.filter((f) => f.trim).map((f) => ({ path: f.rel, tokens: f.tokens, category: f.category, reason: f.reason })), wrote: wrote || [], }, null, 2); -} +} \ No newline at end of file diff --git a/test/ctxtrim.test.js b/test/ctxtrim.test.js index ca72059..a3681ab 100644 --- a/test/ctxtrim.test.js +++ b/test/ctxtrim.test.js @@ -9,6 +9,7 @@ import { dirname, join } from "node:path"; import { scanRepo, estimateTokens } from "../src/scan.js"; import { classify, classifyPath } from "../src/classify.js"; import { merge, block } from "../src/ignore.js"; +import { textReport } from "../src/report.js"; const repo = join(dirname(fileURLToPath(import.meta.url)), "fixtures", "sample-repo"); const cli = join(dirname(fileURLToPath(import.meta.url)), "..", "bin", "ctxtrim.js"); @@ -185,3 +186,11 @@ test("unknown --targets values fail before writing ignore files", (t) => { assert.equal(existsSync(join(root, ".aiexclude")), false); assert.equal(existsSync(join(root, ".aiignore")), false); }); +test("textReport with top: 0 omits Top offenders header", () => { + const scan = scanRepo(repo); + const out = textReport(scan, { price: 3, top: 0 }); + assert.ok( + !out.includes("Top offenders"), + "should not show Top offenders header when top is 0", + ); +}); \ No newline at end of file