Files
wpep/tools/gp/pdf_builder/asset_loading_test.ts
T
2026-11-12 11:13:28 +08:00

90 lines
3.0 KiB
TypeScript

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" }, [
'<img src="pictures/rsf.png" alt="Asset">',
]);
} 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" }, [
'<img src="https://imgbed.cmlmuf.org/i/2026/08/31/wd5mok.jpg" alt="Remote asset">',
]);
} 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(
'<img src="../../../assets/pictures/rsf.png">' +
'<img src="https://example.test/remote.png">',
);
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(
"![Publication asset](../../../assets/pictures/rsf.png)",
);
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(
'<img src="../../../assets/pictures/26.11.0_pictures/ef1be4ca769fc52e513e69e1-129si8u.png" alt="Publication asset">',
),
]);
} finally {
await builder.close_browser();
}
const pdfText = new TextDecoder().decode(await Deno.readFile(pdfPath));
assertEquals(pdfText.includes("/Subtype /Image"), true);
await Deno.remove(pdfPath);
});