Something went wrong. Try again.
A proposal for resolving a short, human-readable name of the form @handle/name into loaded, attributable web content, using AT Protocol iden atproto-web-names.szzt.app
Something went wrong. Try again.
4.9 kB · 89 lines
JavaScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990import assert from "node:assert/strict";import { readFile, mkdir, stat } from "node:fs/promises";import { resolve } from "node:path";import { load } from "cheerio";import { Marked } from "marked";import puppeteer from "puppeteer";import { configureBrowser } from "./browser.mjs";import { serve } from "./server.mjs";
const root = resolve(import.meta.dirname, "..");process.chdir(root);const $ = load(await readFile("dist/index.html", "utf8"));const report = JSON.parse(await readFile("dist/build-report.json", "utf8"));const publication = JSON.parse(await readFile("publication.json", "utf8"));const tableCount = new Marked().lexer(await readFile("spec.md", "utf8")).filter(t => t.type === "table").length;assert.equal($("h1").text(), publication.title);assert.ok($(`.head a[href="${publication.latestVersion}"]`).length > 0, "Expected latest-version URL");assert.ok($(`time[datetime="${publication.publishDate}"]`).length > 0, "Configured publication date");assert.equal($(".preview-notice").length, 0, "No local workflow banner in the document");assert.deepEqual(report.unresolvedSourceReferences, [], "Unresolved source section references");const ids = $("[id]").toArray().map(el => $(el).attr("id"));assert.equal(ids.length, new Set(ids).size, "Duplicate HTML IDs");assert.equal($("h1").length, 1, "One document title");assert.equal($("script").length, 0, "Document must be readable without scripts");assert.equal($("[rel=license]").length, 0, "No accidental license grant");assert.equal(report.preview, publication.preview, "Build mode matches publication metadata");assert.equal($(".copyright").text(), publication.rightsNotice, "Explicit rights notice is preserved");assert.equal($("figure img").length, 1);assert.equal($("table").length, tableCount);assert.equal($("dfn").length, 25);const broken = [];for (const el of $("a[href^='#']").toArray()) { const id = decodeURIComponent($(el).attr("href").slice(1)); if (id && !ids.includes(id)) broken.push(id);}assert.deepEqual([...new Set(broken)], [], "Broken internal links");for (const s of report.sections) { assert.equal(s.sourceNumber, s.number, `Source-number drift for ${s.id}`); const actual = $(`[id="${s.id}"]`).find(".secno").first().text().trim().replace(/\.$/, ""); assert.equal(actual, s.number, `Section-number drift for ${s.id}`);}for (const el of $("[src],link[rel=stylesheet]").toArray()) { const path = $(el).attr("src") ?? $(el).attr("href"); assert.ok(!/^(https?:|\/\/|\/)/.test(path), `Nonlocal rendering dependency: ${path}`); await stat(resolve("dist", path));}const server = await serve("dist");await configureBrowser();// The sandbox is off because the page is this repo's own build served on// loopback with no remote dependencies (asserted above), and hosts that// restrict unprivileged user namespaces cannot start a sandboxed Chrome.const browser = await puppeteer.launch({ headless: true, args: ["--no-sandbox"] });const failures = [];try { const page = await browser.newPage(); await page.setRequestInterception(true); page.on("request", req => { const url = req.url(); if (!url.startsWith(server.url) && !url.startsWith("data:")) { failures.push(`External request: ${url}`); void req.abort(); } else void req.continue(); }); page.on("response", response => { if (response.status() >= 400) failures.push(`${response.status()} ${response.url()}`); }); page.on("pageerror", error => failures.push(error.message)); await page.setJavaScriptEnabled(false); await mkdir(".preview", { recursive: true }); for (const [name, width, height] of [["desktop", 1440, 1000], ["mobile", 390, 844]]) { await page.setViewport({ width, height, deviceScaleFactor: 1 }); await page.goto(server.url, { waitUntil: "networkidle0" }); const layout = await page.evaluate(() => ({ width: document.documentElement.clientWidth, content: document.documentElement.scrollWidth, images: [...document.images].every(img => img.complete && img.naturalWidth > 0), background: getComputedStyle(document.body).backgroundColor, })); assert.ok(layout.content <= layout.width + 1, `${name} page overflow: ${JSON.stringify(layout)}`); assert.ok(layout.images, `${name}: missing image`); await page.screenshot({ path: `.preview/${name}.png` }); console.log(`${name}: no page overflow, images loaded, zero-JS rendering.`); } await page.goto(new URL("#fig-lifecycle", server.url).href, { waitUntil: "networkidle0" }); await page.screenshot({ path: ".preview/lifecycle.png" }); assert.deepEqual(failures, [], "Failed/remote rendering requests");} finally { await browser.close(); await server.close();}console.log(`Checked ${ids.length} unique anchors, ${report.sectionCount} section numbers, ${tableCount} tables, 25 definitions, and local rendering dependencies.`);console.log("No unresolved source section references.");