g1t/apps/web/app/lib/meta.test.ts
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { DESCRIPTION, cardUrl, excerpt, fingerprint, page } from "./meta.ts"; |
| 5 | |
| 6 | type Tag = Record<string, string>; |
| 7 | |
| 8 | function tags(list: unknown[]): Map<string, string> { |
| 9 | const found = new Map<string, string>(); |
| 10 | for (const tag of list as Tag[]) { |
| 11 | if ("title" in tag) found.set("title", tag.title); |
| 12 | else found.set(tag.property ?? tag.name, tag.content); |
| 13 | } |
| 14 | return found; |
| 15 | } |
| 16 | |
| 17 | test("a page gets its title, description, Open Graph and Twitter tags", () => { |
| 18 | const meta = tags(page({ location: { pathname: "/pricing" }, matches: [] }, { title: "Pricing · g1t", description: "What it costs." })); |
| 19 | assert.equal(meta.get("title"), "Pricing · g1t"); |
| 20 | assert.equal(meta.get("description"), "What it costs."); |
| 21 | assert.equal(meta.get("og:title"), "Pricing"); |
| 22 | assert.equal(meta.get("og:url"), "https://g1t.sh/pricing"); |
| 23 | assert.equal(meta.get("og:site_name"), "g1t"); |
| 24 | assert.equal(meta.get("og:image"), "https://og.g1t.sh/image?path=%2Fpricing"); |
| 25 | assert.equal(meta.get("twitter:card"), "summary_large_image"); |
| 26 | assert.equal(meta.get("twitter:image"), meta.get("og:image")); |
| 27 | }); |
| 28 | |
| 29 | test("pages under a project describe the project, and their card follows its details", () => { |
| 30 | const project = { |
| 31 | id: "routes/repo/layout", |
| 32 | loaderData: { repo: { description: "Repo words" }, project: { name: "Web", description: "The storefront" }, open: { issues: 2, pulls: 1 } }, |
| 33 | }; |
| 34 | const meta = tags(page({ location: { pathname: "/acme/web/commits/" }, matches: [project] }, { title: "Commits · acme/web · g1t" })); |
| 35 | assert.equal(meta.get("description"), "The storefront"); |
| 36 | assert.equal(meta.get("og:url"), "https://g1t.sh/acme/web/commits"); |
| 37 | assert.match(meta.get("og:image") ?? "", /\?path=%2Facme%2Fweb%2Fcommits&v=[0-9a-z]+$/); |
| 38 | }); |
| 39 | |
| 40 | test("with nothing particular to say, a page says what g1t is", () => { |
| 41 | const meta = tags(page({ location: { pathname: "/login" }, matches: [] }, { title: "Sign in · g1t" })); |
| 42 | assert.equal(meta.get("description"), DESCRIPTION); |
| 43 | }); |
| 44 | |
| 45 | test("a card's version changes with what it shows", () => { |
| 46 | assert.equal(fingerprint(["a", "open"]), fingerprint(["a", "open"])); |
| 47 | assert.notEqual(fingerprint(["a", "open"]), fingerprint(["a", "merged"])); |
| 48 | assert.equal(cardUrl("/", undefined), "https://og.g1t.sh/image?path=%2F"); |
| 49 | }); |
| 50 | |
| 51 | test("an excerpt is plain text from the start of the Markdown", () => { |
| 52 | assert.equal(excerpt("## Why\n\nThe [queue](https://x) is **slow**.\n\n```ts\ncode()\n```"), "Why The queue is slow."); |
| 53 | assert.equal(excerpt("x".repeat(300), 10).length, 10); |
| 54 | assert.equal(excerpt(null), ""); |
| 55 | }); |