import { assertEquals } from "@std/assert";
import { DocuBuilder } from "./docu_builder.ts";
import { md } from "../mdit/md.ts";
Deno.test("PDF rendering loads relative assets from configured file base", async () => {
const chromiumPath = Deno.env.get("CHROMIUM_PATH");
if (!chromiumPath) {
console.warn("Skipping Chromium asset test: CHROMIUM_PATH is unset");
return;
}
const builder = new DocuBuilder({
asset_base: "../../assets",
journal: { build_js_script: false },
});
const pdfPath = await Deno.makeTempFile({ suffix: ".pdf" });
await builder.with_browser();
try {
await builder.build_as_pdf({ path: pdfPath, format: "A4" }, [
'
',
]);
} finally {
await builder.close_browser();
}
assertEquals((await Deno.stat(pdfPath)).size > 0, true);
await Deno.remove(pdfPath);
});
Deno.test("PDF rendering waits for remote Markdown images", async () => {
if (!Deno.env.get("CHROMIUM_PATH")) return;
const builder = new DocuBuilder({
asset_base: "../../assets",
journal: { build_js_script: false },
});
const pdfPath = await Deno.makeTempFile({ suffix: ".pdf" });
await builder.with_browser();
try {
await builder.build_as_pdf({ path: pdfPath, format: "A4" }, [
'
',
]);
} finally {
await builder.close_browser();
}
assertEquals((await Deno.stat(pdfPath)).size > 0, true);
await Deno.remove(pdfPath);
});
Deno.test("PDF rendering embeds publication-relative asset images", async () => {
if (!Deno.env.get("CHROMIUM_PATH")) return;
const builder = new DocuBuilder({
asset_base: "../../assets",
journal: { build_js_script: false },
});
const resolvedHtml = (builder as any).resolveLocalAssetImages(
'
' +
'
',
);
assertEquals(
resolvedHtml.includes('src="data:image/png;base64,'),
true,
);
assertEquals(
resolvedHtml.includes('src="https://example.test/remote.png"'),
true,
);
assertEquals(resolvedHtml.includes('loading="eager"'), false);
const markdownHtml = md.render(
"",
);
const eagerHtml = (builder as any).resolveLocalAssetImages(markdownHtml);
assertEquals(eagerHtml.includes('loading="lazy"'), false);
assertEquals(eagerHtml.includes('loading="eager"'), true);
const pdfPath = await Deno.makeTempFile({ suffix: ".pdf" });
await builder.with_browser();
try {
await builder.build_as_pdf({ path: pdfPath, format: "A4" }, [
(builder as any).resolveLocalAssetImages(
'
',
),
]);
} finally {
await builder.close_browser();
}
const pdfText = new TextDecoder().decode(await Deno.readFile(pdfPath));
assertEquals(pdfText.includes("/Subtype /Image"), true);
await Deno.remove(pdfPath);
});