Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

87 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { OG_RENDER_VERSION } from "@g1t/contracts/og";
5
6import { DESCRIPTION, cardUrl, excerpt, fingerprint, page } from "./meta.ts";
7
8type Tag = Record<string, string>;
9
10test("the homepage declares its canonical URL using the installation's origin", () => {
11 for (const site of ["https://g1t.sh", "https://forge.example"]) {
12 const descriptors = page({ location: { pathname: "/" }, matches: [{ id: "root", loaderData: { addresses: { site, og: null } } }] }, { title: "g1t" });
13 assert.deepEqual(descriptors.find((tag) => "rel" in tag && tag.rel === "canonical"), { tagName: "link", rel: "canonical", href: `${site}/` });
14 }
15});
16
17function tags(list: unknown[]): Map<string, string> {
18 const found = new Map<string, string>();
19 for (const tag of list as Tag[]) {
20 if ("title" in tag) found.set("title", tag.title);
21 else found.set(tag.property ?? tag.name, tag.content);
22 }
23 return found;
24}
25
26test("a page gets its title, description, Open Graph and Twitter tags", () => {
27 const meta = tags(page({ location: { pathname: "/pricing" }, matches: [] }, { title: "Pricing · g1t", description: "What it costs." }));
28 assert.equal(meta.get("title"), "Pricing · g1t");
29 assert.equal(meta.get("description"), "What it costs.");
30 assert.equal(meta.get("og:title"), "Pricing");
31 assert.equal(meta.get("og:url"), "https://g1t.sh/pricing");
32 assert.equal(meta.get("og:site_name"), "g1t");
33 assert.equal(meta.get("og:image"), `https://og.g1t.sh/image?path=%2Fpricing&v=${OG_RENDER_VERSION}`);
34 assert.equal(meta.get("twitter:card"), "summary_large_image");
35 assert.equal(meta.get("twitter:image"), meta.get("og:image"));
36});
37
38test("pages under a project describe the project, and their card follows its details", () => {
39 const project = {
40 id: "routes/repo/layout",
41 loaderData: { project: { name: "Web", description: "The storefront" }, open: { issues: 2, pulls: 1 } },
42 };
43 const meta = tags(page({ location: { pathname: "/acme/web/commits/" }, matches: [project] }, { title: "Commits · acme/web · g1t" }));
44 assert.equal(meta.get("description"), "The storefront");
45 assert.equal(meta.get("og:url"), "https://g1t.sh/acme/web/commits");
46 assert.match(meta.get("og:image") ?? "", new RegExp(`\\?path=%2Facme%2Fweb%2Fcommits&v=${OG_RENDER_VERSION}\\.[0-9a-z]+$`));
47});
48
49test("a page's address and card come from the root loader's addresses", () => {
50 const root = { id: "root", loaderData: { addresses: { site: "http://localhost:8787", og: "http://localhost:8799" } } };
51 const meta = tags(page({ location: { pathname: "/pricing" }, matches: [root] }, { title: "Pricing · g1t" }));
52 assert.equal(meta.get("og:url"), "http://localhost:8787/pricing");
53 assert.equal(meta.get("og:image"), `http://localhost:8799/image?path=%2Fpricing&v=${OG_RENDER_VERSION}`);
54 assert.equal(meta.get("twitter:image"), meta.get("og:image"));
55});
56
57test("without a card service, a page has no image tags", () => {
58 const root = { id: "root", loaderData: { addresses: { site: "http://localhost:8787", og: null } } };
59 const meta = tags(page({ location: { pathname: "/" }, matches: [root] }, { title: "g1t" }));
60 assert.equal(meta.get("og:url"), "http://localhost:8787/");
61 for (const name of ["og:image", "og:image:type", "og:image:width", "og:image:height", "og:image:alt", "twitter:image", "twitter:image:alt"]) {
62 assert.equal(meta.has(name), false, name);
63 }
64 assert.equal(meta.get("twitter:card"), "summary");
65});
66
67test("with nothing particular to say, a page says what g1t is", () => {
68 const meta = tags(page({ location: { pathname: "/login" }, matches: [] }, { title: "Sign in · g1t" }));
69 assert.equal(meta.get("description"), DESCRIPTION);
70});
71
72test("a card's version changes with what it shows", () => {
73 assert.equal(fingerprint(["a", "open"]), fingerprint(["a", "open"]));
74 assert.notEqual(fingerprint(["a", "open"]), fingerprint(["a", "merged"]));
75 assert.equal(cardUrl("/", undefined), `https://og.g1t.sh/image?path=%2F&v=${OG_RENDER_VERSION}`);
76});
77
78test("every card address carries the render version, so a new design is a new address", () => {
79 assert.match(cardUrl("/"), new RegExp(`[?&]v=${OG_RENDER_VERSION}$`));
80 assert.match(cardUrl("/acme", ["Acme"]), new RegExp(`[?&]v=${OG_RENDER_VERSION}\\.[0-9a-z]+$`));
81});
82
83test("an excerpt is plain text from the start of the Markdown", () => {
84 assert.equal(excerpt("## Why\n\nThe [queue](https://x) is **slow**.\n\n```ts\ncode()\n```"), "Why The queue is slow.");
85 assert.equal(excerpt("x".repeat(300), 10).length, 10);
86 assert.equal(excerpt(null), "");
87});