~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/atlibs-marque

0
0
main
54 lines · 4.1 KB Raw History
// node --test web/marque/marque.test.mjs  (after `cargo wasm`; skips without Chrome)
// index.html as served, in headless Chrome: the module graph loads, the page renders signed out, and
// viewing an account runs the real marque module in the page. The PLC directory, the PDS and esm.sh
// (@atproto/oauth-client-browser) are stubbed.
import assert from "node:assert/strict";
import { test } from "node:test";

import { browse, CHROME } from "../../../atlibs/docs/browser.mjs";

const ROOT = new URL("../../", import.meta.url).pathname;
const did = "did:plc:lpfuqerea3deuoyrn7ojser4";
const domain = { uri: `at://${did}/at.marque.domain/example.com`, cid: "bafyd", value: { $type: "at.marque.domain", domain: "example.com", status: "active", expiresAt: "2027-09-24T00:00:00Z" } };
const zone = { uri: `at://${did}/at.marque.dns/example.com`, cid: "bafyz", value: { $type: "at.marque.dns", domain: "example.com", records: [{ name: "@", recordType: "MX", value: "mail.example.com", ttl: 3600, priority: 10 }] } };

// url prefix -> [content type, body]
const STUBS = {
	[`https://plc.directory/${did}`]: ["application/json", JSON.stringify({ service: [{ id: "#atproto_pds", serviceEndpoint: "https://pds.example" }] })],
	"https://pds.example/xrpc/com.atproto.repo.listRecords?": ["application/json", JSON.stringify({ records: [domain] })],
	"https://pds.example/xrpc/com.atproto.repo.getRecord?collection=at.marque.domain&": ["application/json", JSON.stringify(domain)],
	"https://pds.example/xrpc/com.atproto.repo.getRecord?collection=at.marque.dns&": ["application/json", JSON.stringify(zone)],
	"https://esm.sh/": ["text/javascript", "export const BrowserOAuthClient = { load: async () => ({ init: async () => undefined }) }"],
};

test("the page boots in Chrome, signed out, and shows an account's zone", { skip: !CHROME && "no Chrome" }, () =>
	browse(async ({ send, on, open, evaluate }) => {
		const errors = [];
		const urls = [];
		on("Runtime.exceptionThrown", ({ exceptionDetails: e }) => errors.push(e.exception?.description ?? e.text));
		on("Log.entryAdded", ({ entry }) => entry.level === "error" && !entry.url?.endsWith("/favicon.ico") && errors.push(`${entry.text} ${entry.url ?? ""}`));
		on("Fetch.requestPaused", ({ requestId, request }) => {
			urls.push(request.url);
			const stub = Object.entries(STUBS).find(([prefix]) => request.url.startsWith(prefix))?.[1];
			const [type, body] = stub ?? ["text/plain", "not stubbed"];
			const responseHeaders = [{ name: "content-type", value: type }, { name: "access-control-allow-origin", value: "*" }, { name: "access-control-allow-headers", value: "*" }];
			send("Fetch.fulfillRequest", { requestId, responseCode: stub ? 200 : 404, responseHeaders, body: Buffer.from(body).toString("base64") });
		});
		await send("Runtime.enable");
		await send("Log.enable");
		await send("Fetch.enable", { patterns: ["https://plc.directory/", "https://pds.example/", "https://esm.sh/"].map((prefix) => ({ urlPattern: `${prefix}*` })) });
		await open("/web/marque/index.html");

		const until = async (expression) => {
			for (const end = Date.now() + 15_000; Date.now() < end; await new Promise((ok) => setTimeout(ok, 100))) if (await evaluate(expression)) return;
			assert.fail(`timed out: ${expression}\n${errors.join("\n")}\n${urls.join("\n")}`);
		};
		await until(`!!customElements.get("marque-dns") && !document.querySelector("marque-dns .view").hidden && document.querySelector("marque-dns .who").hidden`);
		await evaluate(`(() => { const form = document.querySelector("marque-dns .view"); form.elements.did.value = ${JSON.stringify(did)}; form.requestSubmit() })()`);
		await until(`document.querySelector("marque-dns .domains button")?.textContent === "example.com"`);
		await evaluate(`document.querySelector("marque-dns .domains button").click()`);
		await until(`document.querySelectorAll("marque-dns tbody tr").length === 1`);
		assert.equal(await evaluate(`document.querySelector("marque-dns tbody tr").textContent`), "@MXmail.example.com360010");
		assert.equal(await evaluate(`document.querySelector("marque-dns .record").hidden`), true, "signed out: no editing");
		assert.deepEqual(errors, []);
	}, { root: ROOT }));