┌─ TS ───────────────────────────────────────────────────────────────────────┐
│ import { describe, expect, it } from "vitest"; │
│ import { layoutToc } from "./TableOfContents"; │
│ import { len } from "../../lib/ascii"; │
│ │
│ const H = (depth: number, slug: string, text: string) => ({ depth, slug, text }); │
│ const line = (r: ReturnType<typeof layoutToc>[number]) => │
│ `${r.indent}${r.title} ${r.leader} ${r.marker}`; │
│ │
│ describe("layoutToc", () => { │
│ it("numbers sections hierarchically", () => { │
│ const rows = layoutToc([H(1, "a", "A"), H(2, "b", "B"), H(2, "c", "C"), H(1, " │
│ d", "D")], 48); │
│ expect(rows.map((r) => r.marker)).toEqual(["1", "1.1", "1.2", "2"]); │
│ }); │
│ │
│ it("fills the exact width at any frame size", () => { │
│ for (const w of [48, 86]) { │
│ const rows = layoutToc([H(1, "a", "Alpha"), H(2, "b", "Beta section")], w); │
│ for (const r of rows) expect(len(line(r))).toBe(w); │
│ } │
│ }); │
│ │
│ it("normalizes indentation to the shallowest heading", () => { │
│ const rows = layoutToc([H(2, "a", "A"), H(3, "b", "B")], 48); │
│ expect(rows[0].indent).toBe(""); │
│ expect(rows[1].indent).toBe(" "); │
│ }); │
│ │
│ it("uppercases and collapses whitespace in titles", () => { │
│ const rows = layoutToc([H(1, "a", "wide tables")], 48); │
│ expect(rows[0].title).toBe("WIDE TABLES"); │
│ }); │
│ │
│ it("strips head-rule glyphs leaked into extracted heading text", () => { │
│ const rows = layoutToc([H(1, "a", `Purpose${"─".repeat(86)}`)], 48); │
│ expect(rows[0].title).toBe("PURPOSE"); │
│ }); │
│ │
│ it("truncates long titles with an ellipsis and still fits", () => { │
│ const rows = layoutToc([H(1, "a", "x".repeat(60))], 48); │
│ expect(rows[0].title.endsWith("...")).toBe(true); │
│ expect(len(line(rows[0]))).toBe(48); │
│ }); │
│ │
│ it("drops headings deeper than maxDepth", () => { │
│ expect(layoutToc([H(1, "a", "A"), H(4, "b", "B")], 48)).toHaveLength(1); │
│ }); │
│ }); │
└────────────────────────────────────────────────────────────────────────────────────┘
┌─ TS ─────────────────────────────────┐
│ import { describe, expect, it } from "vitest │
│ "; │
│ import { layoutToc } from "./TableOfContents │
│ "; │
│ import { len } from "../../lib/ascii"; │
│ │
│ const H = (depth: number, slug: string, text │
│ : string) => ({ depth, slug, text }); │
│ const line = (r: ReturnType<typeof layoutToc │
│ >[number]) => │
│ `${r.indent}${r.title} ${r.leader} ${r.mar │
│ ker}`; │
│ │
│ describe("layoutToc", () => { │
│ it("numbers sections hierarchically", () = │
│ > { │
│ const rows = layoutToc([H(1, "a", "A"), │
│ H(2, "b", "B"), H(2, "c", "C"), H(1, "d", "D │
│ ")], 48); │
│ expect(rows.map((r) => r.marker)).toEqua │
│ l(["1", "1.1", "1.2", "2"]); │
│ }); │
│ │
│ it("fills the exact width at any frame siz │
│ e", () => { │
│ for (const w of [48, 86]) { │
│ const rows = layoutToc([H(1, "a", "Alp │
│ ha"), H(2, "b", "Beta section")], w); │
│ for (const r of rows) expect(len(line( │
│ r))).toBe(w); │
│ } │
│ }); │
│ │
│ it("normalizes indentation to the shallowe │
│ st heading", () => { │
│ const rows = layoutToc([H(2, "a", "A"), │
│ H(3, "b", "B")], 48); │
│ expect(rows[0].indent).toBe(""); │
│ expect(rows[1].indent).toBe(" "); │
│ }); │
│ │
│ it("uppercases and collapses whitespace in │
│ titles", () => { │
│ const rows = layoutToc([H(1, "a", "wide │
│ tables")], 48); │
│ expect(rows[0].title).toBe("WIDE TABLES" │
│ ); │
│ }); │
│ │
│ it("strips head-rule glyphs leaked into ex │
│ tracted heading text", () => { │
│ const rows = layoutToc([H(1, "a", `Purpo │
│ se${"─".repeat(86)}`)], 48); │
│ expect(rows[0].title).toBe("PURPOSE"); │
│ }); │
│ │
│ it("truncates long titles with an ellipsis │
│ and still fits", () => { │
│ const rows = layoutToc([H(1, "a", "x".re │
│ peat(60))], 48); │
│ expect(rows[0].title.endsWith("...")).to │
│ Be(true); │
│ expect(len(line(rows[0]))).toBe(48); │
│ }); │
│ │
│ it("drops headings deeper than maxDepth", │
│ () => { │
│ expect(layoutToc([H(1, "a", "A"), H(4, " │
│ b", "B")], 48)).toHaveLength(1); │
│ }); │
│ }); │
└──────────────────────────────────────────────┘