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.

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