Something went wrong. Try again.
Web frontend and supporting services for lance.blue
Something went wrong. Try again.
3.2 kB · 95 lines
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596/** * Two things profile.ts gets out of a stranger's data. * * The first is the avatar's CID. A blob written today carries `ref.$link` and * one written before the format settled carries a bare `cid`; both are sitting * in live repositories, and reading only the modern shape would silently drop * the picture for the oldest accounts on the network. Everything in a profile * record comes off the network, so anything else it might hold has to come * back as null rather than as a URL built out of junk. * * The second is the name. A handle that did not verify is null, and the * control still has to have something to write and one letter to draw. * * The modules under test are TypeScript with extensionless imports, so this * bundles them the way the site does. Run with `npm test`. */import { test } from "node:test";import assert from "node:assert/strict";import { mkdtemp, rm } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import { execFile } from "node:child_process";import { promisify } from "node:util";
const run = promisify(execFile);const web = join(new URL(".", import.meta.url).pathname, "..");
const dir = await mkdtemp(join(tmpdir(), "profile-"));const bundle = join(dir, "fixture.mjs");await run( join(web, "node_modules/.bin/rolldown"), [ join(web, "scripts/profile.fixture.ts"), "--format", "esm", "--platform", "node", "--file", bundle, ], { cwd: web },);
const { accountName, avatarCid, monogram } = await import(bundle);
const CID = "bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku";
test("a modern blob's CID is read", () => { assert.equal(avatarCid({ avatar: { ref: { $link: CID } } }), CID);});
test("a blob written before the format settled is read too", () => { assert.equal(avatarCid({ avatar: { cid: CID } }), CID);});
test("anything that is not a picture is no picture", () => { for (const value of [ undefined, null, "", 42, {}, { avatar: null }, { avatar: {} }, { avatar: { ref: {} } }, { avatar: { cid: "" } }, { avatar: { ref: { $link: 7 } } }, { avatar: "https://example.com/me.png" }, ]) { assert.equal( avatarCid(value), null, `expected null for ${JSON.stringify(value)}`, ); }});
test("an account is named by its handle, and by its DID when it has none", () => { const did = "did:plc:nlzmjyfv6loqtxyzvdcznwgf"; assert.equal(accountName({ did, handle: "lance.blue" }), "@lance.blue"); assert.equal(accountName({ did, handle: null }), did);});
test("every account has a monogram, including one with no handle", () => { const did = "did:plc:nlzmjyfv6loqtxyzvdcznwgf"; assert.equal(monogram({ did, handle: "lance.blue" }), "L"); // The method prefix is not the account. Without stripping it, every // unverified account in the masthead would be a D. assert.equal(monogram({ did, handle: null }), "N"); // A handle is a domain name, and a domain can start with a digit. assert.equal(monogram({ did, handle: "4thsuccession.war" }), "4"); assert.equal(monogram({ did: "did:web:-.example", handle: null }), "E");});
test.after(() => rm(dir, { recursive: true, force: true }));