import assert from "node:assert/strict"; import test from "node:test"; import { AssetInliner, splitSrcset } from "../src/asset-inliner.mjs"; import { findExternalAssetRefs } from "../src/archiver.mjs"; test("inlines real srcset attributes without reading escaped src text from srcdoc", async () => { const fetched = []; const inliner = new AssetInliner(); inliner.fetchAsset = async (rawUrl) => { fetched.push(rawUrl); return { bytes: Buffer.from("asset"), contentType: "image/png" }; }; const html = ` `; const output = await inliner.inlineHtml(html, "https://example.com/article"); assert.deepEqual(fetched.sort(), [ "https://example.com/large.png", "https://example.com/nested.png", "https://example.com/small.png", ]); assert.doesNotMatch(output, /js\.stripe\.com/); assert.equal(inliner.warnings.length, 0); }); test("external asset reporting ignores escaped nested attributes inside srcdoc", () => { const refs = findExternalAssetRefs(` `); assert.deepEqual(refs, ["https://cdn.example/picture.jpg"]); }); test("srcset parsing keeps image CDN transform commas inside URLs", async () => { assert.deepEqual(splitSrcset([ "https://media.example/photos/id/master/w_120,c_limit/photo.jpg 120w", "https://media.example/photos/id/master/w_240,c_limit/photo.jpg 240w" ].join(", ")), [ "https://media.example/photos/id/master/w_120,c_limit/photo.jpg 120w", "https://media.example/photos/id/master/w_240,c_limit/photo.jpg 240w" ]); const fetched = []; const inliner = new AssetInliner(); inliner.fetchAsset = async (rawUrl) => { fetched.push(rawUrl); return { bytes: Buffer.from("asset"), contentType: "image/jpeg" }; }; await inliner.inlineSrcset( "https://media.example/photos/id/master/w_120,c_limit/photo.jpg 120w, https://media.example/photos/id/master/w_240,c_limit/photo.jpg 240w", "https://example.com/article" ); assert.deepEqual(fetched, [ "https://media.example/photos/id/master/w_120,c_limit/photo.jpg", "https://media.example/photos/id/master/w_240,c_limit/photo.jpg" ]); }); test("external asset reporting parses srcset-like attributes without splitting URL commas", () => { const refs = findExternalAssetRefs(` `); assert.deepEqual(refs, [ "https://cdn.example/image.jpg", "https://media.example/photos/id/master/w_120,c_limit/photo.jpg", "https://media.example/photos/id/master/w_240,c_limit/photo.jpg" ]); }); test("asset inliner skips URLs blocked by the filter hook", async () => { const blocked = []; const inliner = new AssetInliner({ shouldBlockAsset: (url, resourceType) => { blocked.push([url, resourceType]); return true; } }); const output = await inliner.inlineHtml(` `, "https://publisher.example/article"); assert.doesNotMatch(output, /ad\.css/); assert.match(output, /data:image\/gif;base64/); assert.deepEqual(blocked, [ ["https://ads.example/ad.css", "stylesheet"], ["https://ads.example/ad.png", "image"] ]); });