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