g1t/services/og/src/resolve.test.ts
Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Social cards for every page: og.g1t.sh | 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; | |
| 3 | ||
| 4 | import type { Issue, Project, Pull, Repo, Result, Workspace } from "@g1t/contracts"; | |
| 5 | ||
| 6 | import { cacheKey } from "./cache.ts"; | |
| 7 | import { type Sources, clean, docsCard, resolve, segments } from "./resolve.ts"; | |
| 8 | ||
| 9 | const ok = <T>(value: T): Result<T> => ({ ok: true, value }); | |
| 10 | const notFound: Result<never> = { ok: false, error: { code: "not_found", message: "Not found" } }; | |
| 11 | const author = { id: "u1", username: "ada" }; | |
| 12 | ||
| 13 | function repo(namespace: string, name: string, isPrivate = false): Repo { | |
| 14 | return { | |
| 15 | id: `${namespace}/${name}`, | |
| 16 | namespace, | |
| 17 | name, | |
| 18 | description: `About ${name}`, | |
| 19 | isPrivate, | |
| 20 | ownerId: "w", | |
| 21 | defaultBranch: "main", | |
| 22 | forkOf: null, | |
| 23 | protected: true, | |
| 24 | createdAt: "", | |
| 25 | }; | |
| 26 | } | |
| 27 | ||
| 28 | const REPOS: Record<string, Repo> = { | |
| 29 | "acme/web": repo("acme", "web"), | |
| 30 | "acme/vault": repo("acme", "vault", true), | |
| 31 | }; | |
| 32 | ||
| 33 | const ISSUE = { number: 7, title: "Fix the thing", state: "closed", reason: "not_planned", author } as Issue; | |
| 34 | const PULL = { number: 8, title: "Make the thing", status: "merged", author } as Pull; | |
| 35 | ||
| 36 | /** Services that see what an anonymous visitor sees, and record that they were asked as one. */ | |
| 37 | function sources(overrides: Partial<Sources> = {}) { | |
| 38 | const viewers: unknown[] = []; | |
| 39 | const seen = <T>(viewer: unknown, value: T): T => { | |
| 40 | viewers.push(viewer); | |
| 41 | return value; | |
| 42 | }; | |
| 43 | const hidden = (path: { namespace: string; name: string }) => { | |
| 44 | const found = REPOS[`${path.namespace}/${path.name}`.toLowerCase()]; | |
| 45 | return !found || found.isPrivate; | |
| 46 | }; | |
| 47 | const base: Sources = { | |
| 48 | identity: { | |
| 49 | getWorkspace: async (slug) => | |
| 50 | slug === "acme" ? ({ slug: "acme", name: "Acme", description: "Rockets", id: "w", createdAt: "", memberCount: 3 } as Workspace) : null, | |
| 51 | }, | |
| 52 | repos: { | |
| 53 | // Like the service: a private repository can come back to a member, | |
| 54 | // so the resolver must check `isPrivate` itself as well. | |
| 55 | get: async (path, viewer) => seen(viewer, REPOS[`${path.namespace}/${path.name}`.toLowerCase()] ? ok(REPOS[`${path.namespace}/${path.name}`.toLowerCase()]) : notFound), | |
| 56 | }, | |
| 57 | work: { | |
| 58 | counts: async (path, viewer) => seen(viewer, hidden(path) ? notFound : ok({ issues: 3, pulls: 2 })), | |
| 59 | getIssue: async (path, number, viewer) => | |
| 60 | seen(viewer, !hidden(path) && number === 7 ? ok({ issue: ISSUE, pulls: [], comments: [] }) : notFound), | |
| 61 | getPull: async (path, number, viewer) => | |
| 62 | seen(viewer, !hidden(path) && number === 8 ? ok({ pull: PULL, issue: null, comments: [] } as never) : notFound), | |
| 63 | }, | |
| 64 | projects: { | |
| 65 | get: async (workspace, slug, viewer) => | |
| 66 | seen(viewer, workspace === "acme" && slug === "web" ? ok({ name: "Web app", description: "The storefront", private: false } as Project) : notFound), | |
| 67 | list: async (workspace, viewer) => | |
| 68 | seen(viewer, ok([{ private: false }, { private: false }, { private: true }] as Project[])), | |
| 69 | }, | |
| 70 | }; | |
| 71 | return { sources: { ...base, ...overrides }, viewers }; | |
| 72 | } | |
| 73 | ||
| 74 | test("paths are split and decoded, and anything else is refused", () => { | |
| 75 | assert.deepEqual(segments("/acme/web/pull/8"), ["acme", "web", "pull", "8"]); | |
| 76 | assert.deepEqual(segments("/acme/web?tab=code#top"), ["acme", "web"]); | |
| 77 | assert.deepEqual(segments("/"), []); | |
| 78 | assert.equal(segments("https://evil.example/x"), null); | |
| 79 | assert.equal(segments("//evil.example/x"), null); | |
| 80 | assert.equal(segments("/%E0%A4%A"), null); | |
| 81 | }); | |
| 82 | ||
| 83 | test("the site's own pages get their own cards, or the brand card", async () => { | |
| 84 | const { sources: s } = sources(); | |
| 85 | assert.equal((await resolve("/", s)).kind, "brand"); | |
| 86 | const pricing = await resolve("/pricing", s); | |
| 87 | assert.equal(pricing.kind, "page"); | |
| 88 | assert.equal(pricing.kind === "page" && pricing.title, "What it costs us, plus a markup"); | |
| 89 | assert.equal((await resolve("/explore", s)).kind, "page"); | |
| 90 | assert.equal((await resolve("/login", s)).kind, "brand"); | |
| 91 | assert.equal((await resolve("/settings/keys", s)).kind, "brand"); | |
| 92 | }); | |
| 93 | ||
| 94 | test("a workspace shows its name and how many projects are public", async () => { | |
| 95 | const { sources: s } = sources(); | |
| 96 | assert.deepEqual(await resolve("/acme", s), { | |
| 97 | kind: "workspace", | |
| 98 | slug: "acme", | |
| 99 | name: "Acme", | |
| 100 | description: "Rockets", | |
| 101 | projects: 2, | |
| 102 | }); | |
| 103 | // Its members-only pages show the same public card. | |
| 104 | assert.equal((await resolve("/acme/-/billing", s)).kind, "workspace"); | |
| 105 | assert.equal((await resolve("/nobody", s)).kind, "brand"); | |
| 106 | }); | |
| 107 | ||
| 108 | test("a public project shows its name, description and open work", async () => { | |
| 109 | const { sources: s } = sources(); | |
| 110 | assert.deepEqual(await resolve("/acme/web/commits", s), { | |
| 111 | kind: "project", | |
| 112 | owner: "acme", | |
| 113 | repo: "web", | |
| 114 | name: "Web app", | |
| 115 | description: "The storefront", | |
| 116 | issues: 3, | |
| 117 | pulls: 2, | |
| 118 | }); | |
| 119 | }); | |
| 120 | ||
| 121 | test("issues, pull requests and Soon pages get cards of their own", async () => { | |
| 122 | const { sources: s } = sources(); | |
| 123 | assert.deepEqual(await resolve("/acme/web/issues/7", s), { | |
| 124 | kind: "issue", | |
| 125 | owner: "acme", | |
| 126 | repo: "web", | |
| 127 | number: 7, | |
| 128 | title: "Fix the thing", | |
| 129 | state: "not_planned", | |
| 130 | author: "ada", | |
| 131 | }); | |
| 132 | const pull = await resolve("/acme/web/pull/8", s); | |
| 133 | assert.equal(pull.kind, "pull"); | |
| 134 | assert.equal(pull.kind === "pull" && pull.state, "merged"); | |
| 135 | const soon = await resolve("/acme/web/soon/board", s); | |
| 136 | assert.equal(soon.kind, "soon"); | |
| 137 | assert.equal(soon.kind === "soon" && soon.title, "Board"); | |
| 138 | // One that does not exist falls back to the project. | |
| 139 | assert.equal((await resolve("/acme/web/issues/99", s)).kind, "project"); | |
| 140 | assert.equal((await resolve("/acme/web/soon/nothing", s)).kind, "project"); | |
| 141 | assert.equal((await resolve("/acme/web/issues/007x", s)).kind, "project"); | |
| 142 | }); | |
| 143 | ||
| 144 | test("nothing private ever reaches a card", async () => { | |
| 145 | const { sources: s } = sources(); | |
| 146 | for (const path of ["/acme/vault", "/acme/vault/issues/7", "/acme/vault/pull/8", "/acme/vault/soon/board", "/acme/vault/settings"]) { | |
| 147 | assert.deepEqual(await resolve(path, s), { kind: "brand" }, path); | |
| 148 | } | |
| 149 | // A missing repository says nothing either. | |
| 150 | assert.deepEqual(await resolve("/acme/ghost/pull/8", s), { kind: "brand" }); | |
| 151 | }); | |
| 152 | ||
| 153 | test("a private project on a public repository keeps its name to itself", async () => { | |
| 154 | const { sources: s } = sources({ | |
| 155 | projects: { | |
| 156 | get: async () => ok({ name: "Secret name", description: "Secret plans", private: true } as Project), | |
| 157 | list: async () => ok([]), | |
| 158 | }, | |
| 159 | }); | |
| 160 | const card = await resolve("/acme/web", s); | |
| 161 | assert.equal(card.kind === "project" && card.name, "web"); | |
| 162 | assert.equal(card.kind === "project" && card.description, "About web"); | |
| 163 | }); | |
| 164 | ||
| 165 | test("every lookup is made with no viewer", async () => { | |
| 166 | const { sources: s, viewers } = sources(); | |
| 167 | for (const path of ["/acme", "/acme/web", "/acme/web/issues/7", "/acme/web/pull/8", "/acme/vault/pull/8"]) { | |
| 168 | await resolve(path, s); | |
| 169 | } | |
| 170 | assert.ok(viewers.length > 0); | |
| 171 | assert.ok(viewers.every((viewer) => viewer === null)); | |
| 172 | }); | |
| 173 | ||
| 174 | test("a service that fails gives the brand card, marked so it is not kept", async () => { | |
| 175 | const { sources: s } = sources({ | |
| 176 | repos: { | |
| 177 | get: async () => { | |
| 178 | throw new Error("down"); | |
| 179 | }, | |
| 180 | }, | |
| 181 | }); | |
| 182 | assert.deepEqual(await resolve("/acme/web", s), { kind: "brand", failed: true }); | |
| 183 | }); | |
| 184 | ||
| 185 | test("a docs card is bounded, and has a title even without one", () => { | |
| 186 | const card = docsCard(new URLSearchParams({ title: " Quick\nstart ", section: "Get started", description: "x".repeat(400) })); | |
| 187 | assert.equal(card.kind === "docs" && card.title, "Quick start"); | |
| 188 | assert.equal(card.kind === "docs" && card.description?.length, 300); | |
| 189 | assert.equal(docsCard(new URLSearchParams()).kind === "docs" && docsCard(new URLSearchParams()).title, "g1t docs"); | |
| 190 | assert.equal(clean(" ", 10), null); | |
| 191 | }); | |
| 192 | ||
| 193 | test("the cache key keeps only what changes the card", () => { | |
| 194 | const a = cacheKey(new URL("https://og.g1t.sh/image?utm=1&path=/acme/web&v=3")); | |
| 195 | const b = cacheKey(new URL("https://og.g1t.sh/image?path=/acme/web&v=3&x=2")); | |
| 196 | assert.equal(a, b); | |
| 197 | assert.notEqual(a, cacheKey(new URL("https://og.g1t.sh/image?path=/acme/web&v=4"))); | |
| 198 | assert.match(a, /design=/); | |
| 199 | }); |