Something went wrong. Try again.
Web frontend and supporting services for lance.blue
Something went wrong. Try again.
4.4 kB · 130 lines
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131/** * The two things standard.site reads out of a publication's HTML: the * well-known key naming the publication record, and the link tag naming a * post's document record. * * The absent cases are the ones worth pinning. An href with nothing behind it * is a claim a verifier follows, and CloudFront answers a genuinely missing * key with the app's index.html and a 200, so from outside the network a * wrong answer and a right one look the same. * * Run with `npm test`. */import { test, after } from "node:test";import assert from "node:assert/strict";import { execFileSync } from "node:child_process";import { existsSync, readFileSync, readdirSync, rmSync, writeFileSync,} from "node:fs";import { join } from "node:path";import { fileURLToPath } from "node:url";
const web = fileURLToPath(new URL("../", import.meta.url));const WELL_KNOWN = join(web, "public/.well-known/site.standard.publication");const POSTS = join(web, "src/content/blog");
// Nothing resolvable. A DID that looked real would be followed by the first// person who copied it out of here.const DOCUMENT = "at://did:plc:example/site.standard.document/fakerkey";
// A post that has been published, which the tree may or may not have one of on// any given day. Written for the build and taken away again. Under web/.astro,// which is gitignored and already the build's scratch - not os.tmpdir(),// because the last thing a build does is rename its output into place and a// rename across filesystems is EXDEV.const fixture = join(POSTS, "fixture-borborygmus.md");const outDir = join(web, ".astro/standard-site");
writeFileSync( fixture, `---title: Fixture borborygmusdescription: Fixture for the standard.site tests. Removed when they finish.publishDate: 2026-01-01atUri: ${DOCUMENT}---
Borborygmus quincunx widdershins.`,);
// Which posts claim a record, read while the fixture is still on disk. Slugs// are filenames, which is what Astro's glob loader makes its ids from. Sequoia// writes the value quoted, so the quotes come off.const published = new Map( readdirSync(POSTS) .filter((file) => file.endsWith(".md")) .map((file) => [ file.replace(/\.md$/, ""), /^atUri:\s*["']?(at:\/\/\S+?)["']?\s*$/m.exec( readFileSync(join(POSTS, file), "utf8"), )?.[1], ]),);
try { execFileSync( join(web, "node_modules/.bin/astro"), ["build", "--outDir", outDir], { cwd: web, stdio: "pipe", }, );} finally { rmSync(fixture, { force: true });}
after(() => rmSync(outDir, { recursive: true, force: true }));
test("a post links to its record, and only if it has one", () => { const built = readdirSync(join(outDir, "blog"), { withFileTypes: true }) .filter((entry) => entry.isDirectory()) .map((entry) => entry.name); assert.ok(built.length > 1, "the fixture and at least one post should build");
for (const slug of built) { const html = readFileSync(join(outDir, "blog", slug, "index.html"), "utf8"); const atUri = published.get(slug); if (atUri) { assert.match( html, new RegExp(`<link rel="site\\.standard\\.document" href="${atUri}"`), `${slug} has an atUri and did not link to it`, ); } else { assert.ok( !html.includes("site.standard.document"), `${slug} has no atUri and rendered a document link`, ); } }});
test("the publication key, once it exists, says what sequoia.json says", () => { // `sequoia init` writes this file and the URI in sequoia.json in the same // step, and nothing rewrites either afterwards. They drift when one is // edited by hand, and the symptom is a publication nobody can verify. if (!existsSync(WELL_KNOWN)) return;
// Trimmed: the end-of-file-fixer hook adds a newline to every file in the // repo, this one included, so the bytes on disk are never exactly the URI. const body = readFileSync(WELL_KNOWN, "utf8").trim(); assert.match( body, /^at:\/\/did:\S+\/site\.standard\.publication\/\S+$/, "the key must be one AT-URI and nothing else", ); assert.equal( body, JSON.parse(readFileSync(join(web, "sequoia.json"), "utf8")).publicationUri, ); assert.ok( existsSync(join(outDir, ".well-known/site.standard.publication")), "public/.well-known did not survive the build", );});