testFinishSiteNamesTheStylesheet

tcmts.site.testFinishSiteNamesTheStylesheet · #ced2z3xy · term

finishSite names the stylesheet by its content in every page, at any depth, and adds the speculation rules

import { finishSite } from "#codebase/tcmts/site";
import fs from "node:fs";
import path from "node:path";

export const testFinishSiteNamesTheStylesheet = () => {
  const out = fs.mkdtempSync(
    path.join(fs.realpathSync(process.env.TMPDIR ?? "/tmp"), "finish-site-"),
  );
  try {
    const page = (href: string) =>
      `<!doctype html>\n<html><head>\n<link rel="stylesheet" href="${href}">\n</head><body><a href="style.css">the css</a></body></html>\n`;
    fs.writeFileSync(path.join(out, "style.css"), "body { color: red }");
    fs.writeFileSync(path.join(out, "index.html"), page("style.css"));
    fs.mkdirSync(path.join(out, "code", "tcm"), { recursive: true });
    fs.writeFileSync(path.join(out, "code", "tcm", "a.html"), page("../../style.css"));
    const style = finishSite(out);
    if (!/^style\.[0-9a-f]{12}\.css$/.test(style)) throw new Error(`named ${style}`);
    if (fs.existsSync(path.join(out, "style.css"))) throw new Error("style.css is still there");
    if (fs.readFileSync(path.join(out, style), "utf8") !== "body { color: red }")
      throw new Error("its content changed");
    const index = fs.readFileSync(path.join(out, "index.html"), "utf8");
    const deep = fs.readFileSync(path.join(out, "code", "tcm", "a.html"), "utf8");
    if (!index.includes(`href="${style}"`) || !deep.includes(`href="../../${style}"`))
      throw new Error("a page still links style.css");
    if (!index.includes('<a href="style.css">'))
      throw new Error("only the stylesheet's link changes");
    if (!deep.includes('<script type="speculationrules">')) throw new Error("no speculation rules");
  } finally {
    fs.rmSync(out, { recursive: true, force: true });
  }
}

uses