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.

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