mirror of
https://github.com/garrytan/gstack.git
synced 2026-09-09 22:48:57 +02:00
asideClient.ts replaces the direct $B client with one render() call per PDF (the exact option mapping the browse pdf command had: paper, margins, header/footer/page numbers, tagged, outline, printBackground, preferCSSPageSize, Paged.js wait); the diagram pre-pass, oversized-image downscale and DOCX rasters each run as one render script with per-fence try/catch; exit 4 now means no browser is available and names both remedies; $P setup reports which engine it found. The e2e gates run on whichever engine is present, so the Linux lane exercises the fallback. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
235 lines
9.7 KiB
TypeScript
235 lines
9.7 KiB
TypeScript
/**
|
||
* Coverage-gap fills from the v1.58.0.0 ship audit — the branches the main
|
||
* suites couldn't reach without a live bundle page (mock runner here), plus the
|
||
* pure-function stragglers (WebP probing, landscape geometry, bundle path
|
||
* resolution, screen CSS).
|
||
*/
|
||
import { describe, expect, test } from "bun:test";
|
||
import * as fs from "node:fs";
|
||
import * as os from "node:os";
|
||
import * as path from "node:path";
|
||
|
||
import {
|
||
type BundleCall,
|
||
type BundleResult,
|
||
landscapeContentBox,
|
||
rasterizeDiagramFigures,
|
||
renderFenceSlots,
|
||
resolveBundlePath,
|
||
substituteSlots,
|
||
} from "../src/diagram-prepass";
|
||
import { imageDims } from "../src/image-size";
|
||
import { screenCss } from "../src/print-css";
|
||
|
||
/** Scripted BundleRun: a throwing script call becomes an ERR result, plus counters. */
|
||
function mockRun(script: (fn: string, ...args: unknown[]) => string) {
|
||
const calls: string[] = [];
|
||
let batches = 0;
|
||
const run = async (batch: BundleCall[]): Promise<BundleResult[]> => {
|
||
batches++;
|
||
return batch.map((c) => {
|
||
calls.push(c.fn);
|
||
try {
|
||
return { ok: true, value: script(c.fn, ...c.args) };
|
||
} catch (e: any) {
|
||
return { ok: false, error: e.message };
|
||
}
|
||
});
|
||
};
|
||
return { run, calls, batchCount: () => batches };
|
||
}
|
||
|
||
const fence = (over: Partial<{ lang: string; source: string; ordinal: number }>) => ({
|
||
lang: "mermaid",
|
||
source: "graph LR\n A --> B",
|
||
render: true as const,
|
||
token: `tok-${over.ordinal ?? 1}`,
|
||
ordinal: over.ordinal ?? 1,
|
||
title: undefined,
|
||
page: undefined,
|
||
...over,
|
||
});
|
||
|
||
// ─── renderFenceSlots: reset contract + excalidraw branches ───────────
|
||
|
||
describe("renderFenceSlots (mock runner)", () => {
|
||
test("one batch for all fences: a failure is a diagnostic block and the NEXT fence still renders", async () => {
|
||
const { run, batchCount } = mockRun((fn, ...args) => {
|
||
if (String(args[1] ?? "").includes("BROKEN")) throw new Error("Parse error on line 1");
|
||
return "<svg><g/></svg>";
|
||
});
|
||
const warnings: string[] = [];
|
||
const slots = await renderFenceSlots(
|
||
[
|
||
fence({ ordinal: 1 }),
|
||
fence({ ordinal: 2, source: "BROKEN" }),
|
||
fence({ ordinal: 3 }),
|
||
],
|
||
run,
|
||
(m) => warnings.push(m),
|
||
);
|
||
expect(slots.get("tok-1")).toContain("<svg>");
|
||
expect(slots.get("tok-2")).toContain("diagram-error");
|
||
expect(slots.get("tok-2")).toContain("Parse error on line 1");
|
||
expect(slots.get("tok-3")).toContain("<svg>"); // post-failure fence rendered
|
||
expect(batchCount()).toBe(1); // one script for the whole document
|
||
expect(warnings[0]).toContain("failed to render");
|
||
});
|
||
|
||
test("excalidraw fence renders via __excalidrawToSvg", async () => {
|
||
const { run, calls } = mockRun(() => "<svg data-x><g/></svg>");
|
||
const slots = await renderFenceSlots(
|
||
[fence({ lang: "excalidraw", source: '{"type":"excalidraw","elements":[]}' })],
|
||
run,
|
||
() => {},
|
||
);
|
||
expect(calls).toEqual(["__excalidrawToSvg"]);
|
||
expect(slots.get("tok-1")).toContain("<svg");
|
||
});
|
||
|
||
test("invalid excalidraw JSON fails fast into a diagnostic WITHOUT a bundle call", async () => {
|
||
const { run, calls } = mockRun(() => "<svg/>");
|
||
const warnings: string[] = [];
|
||
const slots = await renderFenceSlots(
|
||
[fence({ lang: "excalidraw", source: "{not json" })],
|
||
run,
|
||
(m) => warnings.push(m),
|
||
);
|
||
expect(calls).toEqual([]); // JSON.parse threw before any bundle call
|
||
expect(slots.get("tok-1")).toContain("diagram-error");
|
||
expect(warnings).toHaveLength(1);
|
||
});
|
||
});
|
||
|
||
// ─── rasterizeDiagramFigures: svg-data-URI + error fallbacks ──────────
|
||
|
||
describe("rasterizeDiagramFigures (mock runner)", () => {
|
||
const figure = `<figure class="diagram" role="img" aria-label="flow"><svg viewBox="0 0 10 10"><g/></svg></figure>`;
|
||
|
||
test("figures and svg data-URI images rasterize to PNG in ONE batch", async () => {
|
||
const svgUri = `data:image/svg+xml;base64,${Buffer.from("<svg/>").toString("base64")}`;
|
||
const { run, calls, batchCount } = mockRun((_fn, svg) => `data:image/png;base64,${String(svg).includes("viewBox") ? "FIG" : "IMG"}`);
|
||
const out = await rasterizeDiagramFigures(`${figure}<img src="${svgUri}" alt="v">`, run, 6.5, () => {});
|
||
expect(calls).toEqual(["__rasterize", "__rasterize"]);
|
||
expect(batchCount()).toBe(1);
|
||
expect(out).toContain('<p><img src="data:image/png;base64,FIG" alt="flow"></p>');
|
||
expect(out).toContain('src="data:image/png;base64,IMG" alt="v"');
|
||
expect(out).not.toContain("gstack-raster-slot");
|
||
});
|
||
|
||
test("no rasterizable content → no bundle call at all", async () => {
|
||
const { run, batchCount } = mockRun(() => "x");
|
||
const html = `<p>plain</p><img src="data:image/png;base64,AAAA">`;
|
||
expect(await rasterizeDiagramFigures(html, run, 6.5, () => {})).toBe(html);
|
||
expect(batchCount()).toBe(0);
|
||
});
|
||
|
||
test("figure rasterization failure surfaces the SOURCE as text (never silent loss)", async () => {
|
||
// Returning the figure unchanged would make the diagram vanish in DOCX
|
||
// (the converter drops <figure>/<svg>) — the failure must be visible.
|
||
const { run } = mockRun(() => { throw new Error("tainted"); });
|
||
const warnings: string[] = [];
|
||
const srcFigure = figure.replace(
|
||
'<figure class="diagram"',
|
||
`<figure class="diagram" data-gstack-source="${Buffer.from("graph LR\n A --> B").toString("base64")}"`,
|
||
);
|
||
const out = await rasterizeDiagramFigures(srcFigure, run, 6.5, (m) => warnings.push(m));
|
||
expect(out).toContain("could not be rasterized");
|
||
expect(out).toContain("A --> B"); // source visible (escaped), not dropped
|
||
expect(out).not.toContain("<figure");
|
||
expect(warnings[0]).toContain("rasterization failed");
|
||
});
|
||
|
||
test("svg data-URI rasterization failure keeps the original tag", async () => {
|
||
const svgUri = `data:image/svg+xml;base64,${Buffer.from("<svg/>").toString("base64")}`;
|
||
const { run } = mockRun(() => { throw new Error("decode failed"); });
|
||
const tagIn = `<img src="${svgUri}">`;
|
||
const out = await rasterizeDiagramFigures(tagIn, run, 6.5, () => {});
|
||
expect(out).toBe(tagIn);
|
||
});
|
||
});
|
||
|
||
// ─── image-size: WebP variants ────────────────────────────────────────
|
||
|
||
describe("imageDims WebP", () => {
|
||
function riff(fmt: string, body: Buffer): Buffer {
|
||
const b = Buffer.alloc(12 + 4 + body.length);
|
||
b.write("RIFF", 0, "ascii");
|
||
b.writeUInt32LE(4 + body.length + 4, 4);
|
||
b.write("WEBP", 8, "ascii");
|
||
b.write(fmt, 12, "ascii");
|
||
body.copy(b, 16);
|
||
return b;
|
||
}
|
||
|
||
test("VP8 (lossy)", () => {
|
||
const body = Buffer.alloc(16);
|
||
body.writeUInt16LE(800 & 0x3fff, 10); // width at chunk offset 26 = body offset 10
|
||
body.writeUInt16LE(600 & 0x3fff, 12);
|
||
expect(imageDims(riff("VP8 ", body))).toEqual({ width: 800, height: 600, mime: "image/webp" });
|
||
});
|
||
|
||
test("VP8L (lossless)", () => {
|
||
const body = Buffer.alloc(10);
|
||
body[4] = 0x2f; // signature at chunk offset 20 = body offset 4
|
||
const w = 1023, h = 511;
|
||
const bits = (w - 1) | ((h - 1) << 14);
|
||
body.writeUInt32LE(bits >>> 0, 5);
|
||
expect(imageDims(riff("VP8L", body))).toEqual({ width: 1023, height: 511, mime: "image/webp" });
|
||
});
|
||
|
||
test("VP8X (extended)", () => {
|
||
const body = Buffer.alloc(14);
|
||
const w = 4000 - 1, h = 250 - 1; // 24-bit minus-one at offsets 24/27 = body 8/11
|
||
body[8] = w & 0xff; body[9] = (w >> 8) & 0xff; body[10] = (w >> 16) & 0xff;
|
||
body[11] = h & 0xff; body[12] = (h >> 8) & 0xff; body[13] = (h >> 16) & 0xff;
|
||
expect(imageDims(riff("VP8X", body))).toEqual({ width: 4000, height: 250, mime: "image/webp" });
|
||
});
|
||
|
||
test("unknown RIFF subtype → null", () => {
|
||
expect(imageDims(riff("XXXX", Buffer.alloc(14)))).toBeNull();
|
||
});
|
||
});
|
||
|
||
// ─── landscape geometry + slot fallback + bundle path + screen css ────
|
||
|
||
describe("pure-function stragglers", () => {
|
||
test("landscapeContentBox letter defaults: 9in × 6.5in", () => {
|
||
expect(landscapeContentBox({})).toEqual({ contentWIn: 9, contentHIn: 6.5 });
|
||
});
|
||
test("landscapeContentBox a4 + asymmetric margins", () => {
|
||
const box = landscapeContentBox({ pageSize: "a4", marginLeft: "0.5in", marginRight: "0.5in", marginTop: "25mm", marginBottom: "1in" });
|
||
expect(box.contentWIn).toBeCloseTo(11.69 - 1, 2);
|
||
expect(box.contentHIn).toBeCloseTo(8.27 - 25 / 25.4 - 1, 2);
|
||
});
|
||
|
||
test("substituteSlots bare-token fallback (token not <p>-wrapped)", () => {
|
||
const slots = new Map([["gstack-diagram-slot-x-1", "<figure>D</figure>"]]);
|
||
const out = substituteSlots("<li>gstack-diagram-slot-x-1</li>", slots);
|
||
expect(out).toBe("<li><figure>D</figure></li>");
|
||
});
|
||
|
||
test("resolveBundlePath honors the env override", () => {
|
||
const tmp = path.join(os.tmpdir(), `bundle-override-${process.pid}.html`);
|
||
fs.writeFileSync(tmp, "<!doctype html>");
|
||
try {
|
||
expect(resolveBundlePath({ GSTACK_DIAGRAM_BUNDLE: tmp } as NodeJS.ProcessEnv)).toBe(tmp);
|
||
} finally {
|
||
fs.unlinkSync(tmp);
|
||
}
|
||
});
|
||
// NOTE: resolveBundlePath's not-found error shape is untestable from inside
|
||
// this checkout (the repo-relative candidate always exists), and a vacuous
|
||
// if-guarded assertion was worse than none. The env-override test above is
|
||
// the honest coverage; the error path is exercised manually via
|
||
// GSTACK_DIAGRAM_BUNDLE pointing at a missing file outside a repo.
|
||
|
||
test("screenCss is media-scoped and readable-width", () => {
|
||
const css = screenCss();
|
||
expect(css).toContain("@media screen");
|
||
// 42em at 12pt ≈ 70-75 chars/line — the readable ceiling (design review).
|
||
expect(css).toContain("max-width: 42em");
|
||
expect(css).toContain(".watermark { display: none; }");
|
||
});
|
||
});
|