44 lines
1.8 KiB
JavaScript
44 lines
1.8 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { execFileSync } from "node:child_process";
|
|
import { createServer } from "../server.js";
|
|
|
|
const siteDir = path.resolve("_site");
|
|
|
|
test("unknown routes render the themed 404 page with a real 404 status", async () => {
|
|
execFileSync("npm", ["run", "build"], {
|
|
stdio: "pipe",
|
|
env: { ...process.env, SITE_DOMAIN: process.env.SITE_DOMAIN || "riverrooks.dev" }
|
|
});
|
|
|
|
const html = fs.readFileSync(path.join(siteDir, "404.html"), "utf8");
|
|
assert.match(html, /<title>Page not found/);
|
|
assert.match(html, /<h1[^>]*>[^<]*404/);
|
|
assert.match(html, /href="\/"/);
|
|
assert.match(html, /href="\/dispatches\/"/);
|
|
assert.match(html, /href="\/assets\/css\/main\.css/);
|
|
|
|
const css = fs.readFileSync(path.join(siteDir, "assets/css/main.css"), "utf8");
|
|
assert.match(css, /components\/not-found\.css/);
|
|
const pageCss = fs.readFileSync(path.join(siteDir, "assets/css/components/not-found.css"), "utf8");
|
|
assert.doesNotMatch(pageCss, /(?:backdrop-filter|box-shadow|text-shadow|linear-gradient|radial-gradient)\s*:/);
|
|
|
|
const server = createServer(siteDir, path.resolve("src/posts"), path.resolve("src/assets/images/posts"));
|
|
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
try {
|
|
const origin = `http://127.0.0.1:${server.address().port}`;
|
|
for (const route of ["/missing-page/", "/assets/missing.css"]) {
|
|
const response = await fetch(`${origin}${route}`);
|
|
assert.equal(response.status, 404);
|
|
assert.match(response.headers.get("content-type"), /text\/html/);
|
|
assert.match(await response.text(), /Page not found/);
|
|
}
|
|
const home = await fetch(origin);
|
|
assert.equal(home.status, 200);
|
|
} finally {
|
|
await new Promise((resolve) => server.close(resolve));
|
|
}
|
|
});
|