import { describe, expect, test } from "bun:test"; import { existsSync } from "node:fs"; import { chromium } from "playwright-core"; import { buildPrintCss } from "./print-css"; import { PDFRenderer } from "./renderer"; // Real end-to-end render. Runs ONLY when a Chromium binary is already present // (e.g. a prior `playwright install chromium`). It never downloads a browser or // hits the network, so CI without a browser simply skips it. function chromiumAvailable(): boolean { try { const path = chromium.executablePath(); return typeof path === "string" && path.length > 0 && existsSync(path); } catch { return false; } } const available = chromiumAvailable(); describe("PDFRenderer smoke render", () => { test.skipIf(!available)( "renders a valid PDF from HTML using the local runtime", async () => { const html = `

OPL-2601 smoke

Shared HTML to PDF pipeline.

Second page content.
`; // Pin the local runtime so an autodetect can't pick serverless in a // CI/Lambda-like environment. const renderer = new PDFRenderer({ runtime: "local" }); const bytes = await renderer.render(html, { paperSize: "Letter", printBackground: true, }); expect(bytes).toBeInstanceOf(Uint8Array); expect(bytes.byteLength).toBeGreaterThan(1000); // PDF magic header: "%PDF-" const header = new TextDecoder().decode(bytes.slice(0, 5)); expect(header).toBe("%PDF-"); }, 60_000, ); test.skipIf(!available)( "blocks remote subresources and disables JS without hanging", async () => { // Untrusted HTML with a remote image, remote stylesheet, and an inline // script. With JS disabled and only data:/blob: allowed, every external // request must be aborted — the render completes promptly instead of // hanging on network that will never resolve. const html = `

isolation

`; const renderer = new PDFRenderer({ runtime: "local" }); const bytes = await renderer.render(html); expect(new TextDecoder().decode(bytes.slice(0, 5))).toBe("%PDF-"); expect(bytes.byteLength).toBeGreaterThan(1000); }, 30_000, ); });