77 lines
3.0 KiB
TypeScript
77 lines
3.0 KiB
TypeScript
import { assert, assertEquals, assertStringIncludes } from "@std/assert";
|
|
import { prepareLocalCache } from "./local_cache.ts";
|
|
|
|
Deno.test("local cache downloads resource references and preserves reader links", async () => {
|
|
const root = await Deno.makeTempDir();
|
|
const publication = `${root}/publication`;
|
|
const templates = `${root}/templates`;
|
|
await Deno.mkdir(publication, { recursive: true });
|
|
await Deno.mkdir(templates, { recursive: true });
|
|
const assetUrl = "https://cdn.example.test/asset.png";
|
|
const cssUrl = "https://cdn.example.test/theme.css";
|
|
const fontUrl = "https://cdn.example.test/font.woff2";
|
|
const htmlUrl = "https://cdn.example.test/widget.html";
|
|
const nestedImageUrl = "https://cdn.example.test/widget.png";
|
|
await Deno.writeTextFile(
|
|
`${publication}/article.md`,
|
|
`\n\n[reader link](${assetUrl})`,
|
|
);
|
|
await Deno.writeTextFile(
|
|
`${templates}/cover.html`,
|
|
`<img src="${assetUrl}"><iframe src="${htmlUrl}"></iframe><link href="${cssUrl}"><a href="https://example.com/page.png">reader</a>`,
|
|
);
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
const fetched: string[] = [];
|
|
globalThis.fetch = async (input) => {
|
|
const url = String(input);
|
|
fetched.push(url);
|
|
assert(
|
|
url === assetUrl || url === cssUrl || url === fontUrl ||
|
|
url === htmlUrl || url === nestedImageUrl,
|
|
);
|
|
if (url === cssUrl) {
|
|
return new Response(`@font-face { src: url("${fontUrl}"); }`, {
|
|
headers: { "content-type": "text/css" },
|
|
});
|
|
}
|
|
if (url === htmlUrl) {
|
|
return new Response(
|
|
`<img src="https://cdn.example.test/widget.png"><a href="https://reader.example.test/story.png">story</a>`,
|
|
{ headers: { "content-type": "text/html" } },
|
|
);
|
|
}
|
|
if (url === fontUrl) {
|
|
return new Response(new Uint8Array([1, 2, 3]), {
|
|
headers: { "content-type": "font/woff2" },
|
|
});
|
|
}
|
|
return new Response(new Uint8Array([1, 2, 3]), {
|
|
headers: { "content-type": "image/png" },
|
|
});
|
|
};
|
|
try {
|
|
assertEquals(await prepareLocalCache(publication, templates), 5);
|
|
const rewrittenMd = await Deno.readTextFile(`${publication}/article.md`);
|
|
const rewrittenHtml = await Deno.readTextFile(`${templates}/cover.html`);
|
|
assertStringIncludes(rewrittenMd, "file://");
|
|
assertStringIncludes(rewrittenMd, `[reader link](${assetUrl})`);
|
|
assertStringIncludes(rewrittenHtml, "file://");
|
|
assertStringIncludes(rewrittenHtml, 'iframe src="file://');
|
|
assertStringIncludes(rewrittenHtml, "https://example.com/page.png");
|
|
assertEquals(
|
|
fetched.sort(),
|
|
[assetUrl, cssUrl, fontUrl, htmlUrl, nestedImageUrl].sort(),
|
|
);
|
|
|
|
const cache = await Deno.readTextFile(`${publication}/.cache.toml`);
|
|
assertStringIncludes(cache, assetUrl);
|
|
assertEquals(await prepareLocalCache(publication, templates), 0);
|
|
assertEquals(fetched.length, 5);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
await Deno.remove(root, { recursive: true });
|
|
}
|
|
assert(true);
|
|
});
|