import { describe, expect, it } from "vitest"; import { COLLAPSED_LINES, collapseCodeblocks } from "$lib/markup/collapse"; import { renderDocument, renderMarkup } from "$lib/markup/render"; import type { MarkupContext } from "$lib/markup/paths"; const ctx: MarkupContext = { repo: "ada.test/infra", ref: "main" }; const fence = (lines: number): string => ["```ts", ...Array.from({ length: lines }, (_, i) => `const x${i} = ${i};`), "```"].join("\n"); describe("collapseCodeblocks", () => { it("leaves short blocks alone", () => { const html = `
${"a\n".repeat(COLLAPSED_LINES - 1)}`;
expect(collapseCodeblocks(html)).toBe(html);
});
it("wraps tall blocks with their line count and a toolbar slot", () => {
const html = `x
${"a\n".repeat(COLLAPSED_LINES)}`;
expect(collapseCodeblocks(html)).toBe(
`x
${"a\n".repeat(COLLAPSED_LINES)}${"a\n".repeat(COLLAPSED_LINES - 1)}a`;
expect(collapseCodeblocks(html)).toContain(`data-codeblock="${COLLAPSED_LINES}"`);
});
it("wraps only the innermost of nested pre elements", () => {
const inner = `${"a\n".repeat(COLLAPSED_LINES)}`;
const out = collapseCodeblocks(`x${inner}`);
expect(out.match(/data-codeblock=/g)).toHaveLength(1);
expect(out.startsWith("x{ it("collapses tall code in bodies", async () => { expect(await renderMarkup(fence(40), ctx)).toContain('data-codeblock="40"'); }); it("keeps tall code in documents expanded", async () => { expect(await renderDocument("README.md", fence(40), ctx)).not.toContain("data-codeblock"); }); it("drops a data-codeblock written in the source", async () => { expect(await renderMarkup('x', ctx)).not.toContain( "data-codeblock" ); }); });