| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import type { Project, Pull, Repo, Result, WorkItem, Workspace } from "@g1t/contracts"; |
| 5 | |
| 6 | import { RENDER_VERSION, cacheKey } from "./cache.ts"; |
| 7 | import { type Sources, cardPath, 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 | isPrivate, |
| 19 | ownerId: "w", |
| 20 | defaultBranch: "main", |
| 21 | forkOf: null, |
| 22 | protected: true, |
| 23 | createdAt: "", |
| 24 | topics: [], |
| 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 | // Work item 41 of acme, filed in acme/web as its issue #7. |
| 34 | const ITEM = { workspace: "acme", number: 41, title: "Fix the thing", state: "closed", reason: "not_planned", author, requestedBy: null } as WorkItem; |
| 35 | // A change g1t made for ada. |
| 36 | const PULL = { number: 8, title: "Make the thing", status: "merged", author: { id: "usr_g1t_agent", username: "g1t", kind: "agent" }, requestedBy: author } as Pull; |
| 37 | |
| 38 | /** Services that see what an anonymous visitor sees, and record that they were asked as one. */ |
| 39 | function sources(overrides: Partial<Sources> = {}) { |
| 40 | const viewers: unknown[] = []; |
| 41 | const seen = <T>(viewer: unknown, value: T): T => { |
| 42 | viewers.push(viewer); |
| 43 | return value; |
| 44 | }; |
| 45 | const hidden = (path: { namespace: string; name: string }) => { |
| 46 | const found = REPOS[`${path.namespace}/${path.name}`.toLowerCase()]; |
| 47 | return !found || found.isPrivate; |
| 48 | }; |
| 49 | const base: Sources = { |
| 50 | identity: { |
| 51 | getWorkspace: async (slug) => |
| 52 | slug === "acme" ? ({ slug: "acme", name: "Acme", description: "Rockets", id: "w", createdAt: "", memberCount: 3, avatar: null } as Workspace) : null, |
| 53 | profile: async (username) => |
| 54 | username === "ada" |
| 55 | ? { username: "ada", name: "Ada Lovelace", bio: "Engines.", location: null, website: null, pronouns: null, timezone: null, avatar: null, createdAt: "" } |
| 56 | : null, |
| 57 | }, |
| 58 | repos: { |
| 59 | // Like the service: a private repository can come back to a member, |
| 60 | // so the resolver must check `isPrivate` itself as well. |
| 61 | get: async (path, viewer) => seen(viewer, REPOS[`${path.namespace}/${path.name}`.toLowerCase()] ? ok(REPOS[`${path.namespace}/${path.name}`.toLowerCase()]) : notFound), |
| 62 | }, |
| 63 | work: { |
| 64 | counts: async (path, viewer) => seen(viewer, hidden(path) ? notFound : ok({ items: 3, pulls: 2 })), |
| 65 | // Item 42 was filed in the private acme/vault: no one outside sees it. |
| 66 | getWorkItem: async (workspace, number, viewer) => |
| 67 | seen(viewer, workspace === "acme" && number === 41 ? ok({ item: ITEM, pulls: [], comments: [] }) : notFound), |
| 68 | resolveRef: async (viewer, context, number) => |
| 69 | seen( |
| 70 | viewer, |
| 71 | "repo" in context && !hidden(context.repo) && number === 7 |
| 72 | ? { kind: "work_item" as const, workspace: "acme", number: 41, itemId: "iss_7" } |
| 73 | : { kind: "none" as const }, |
| 74 | ), |
| 75 | getPull: async (path, number, viewer) => |
| 76 | seen(viewer, !hidden(path) && number === 8 ? ok({ pull: PULL, item: null, comments: [] } as never) : notFound), |
| 77 | byAuthor: async (_username, viewer) => |
| 78 | seen(viewer, ok({ items: [], next: null, repos: [], counts: { pullsMerged: 4, pullsOpen: 1, pulls: 6, items: 2, itemsOpen: 1 } })), |
| 79 | }, |
| 80 | projects: { |
| 81 | get: async (workspace, slug, viewer) => |
| 82 | seen(viewer, workspace === "acme" && slug === "web" ? ok({ name: "Web app", description: "The storefront", private: false } as Project) : notFound), |
| 83 | list: async (workspace, viewer) => |
| 84 | seen(viewer, ok([{ private: false }, { private: false }, { private: true }] as Project[])), |
| 85 | }, |
| 86 | }; |
| 87 | return { sources: { ...base, ...overrides }, viewers }; |
| 88 | } |
| 89 | |
| 90 | test("paths are split and decoded, and anything else is refused", () => { |
| 91 | assert.deepEqual(segments("/acme/web/pull/8"), ["acme", "web", "pull", "8"]); |
| 92 | assert.deepEqual(segments("/acme/web?tab=code#top"), ["acme", "web"]); |
| 93 | assert.deepEqual(segments("/"), []); |
| 94 | assert.equal(segments("https://evil.example/x"), null); |
| 95 | assert.equal(segments("//evil.example/x"), null); |
| 96 | assert.equal(segments("/%E0%A4%A"), null); |
| 97 | }); |
| 98 | |
| 99 | test("the site's own pages get their own cards, or the brand card", async () => { |
| 100 | const { sources: s } = sources(); |
| 101 | assert.equal((await resolve("/", s)).kind, "brand"); |
| 102 | const pricing = await resolve("/pricing", s); |
| 103 | assert.equal(pricing.kind, "page"); |
| 104 | assert.equal(pricing.kind === "page" && pricing.title, "What it costs us, plus a markup"); |
| 105 | assert.equal((await resolve("/explore", s)).kind, "page"); |
| 106 | assert.equal((await resolve("/login", s)).kind, "brand"); |
| 107 | assert.equal((await resolve("/settings/keys", s)).kind, "brand"); |
| 108 | const privacy = await resolve("/policies/privacy", s); |
| 109 | assert.equal(privacy.kind === "page" && privacy.title, "Privacy Policy"); |
| 110 | assert.equal((await resolve("/status", s)).kind, "page"); |
| 111 | assert.equal((await resolve("/security", s)).kind, "page"); |
| 112 | assert.equal((await resolve("/policies/nothing", s)).kind, "brand"); |
| 113 | }); |
| 114 | |
| 115 | test("a workspace shows its name and how many projects are public", async () => { |
| 116 | const { sources: s } = sources(); |
| 117 | assert.deepEqual(await resolve("/acme", s), { |
| 118 | kind: "workspace", |
| 119 | slug: "acme", |
| 120 | name: "Acme", |
| 121 | description: "Rockets", |
| 122 | projects: 2, |
| 123 | avatar: null, |
| 124 | }); |
| 125 | // Its members-only pages show the same public card. |
| 126 | assert.equal((await resolve("/acme/-/billing", s)).kind, "workspace"); |
| 127 | assert.equal((await resolve("/nobody", s)).kind, "brand"); |
| 128 | }); |
| 129 | |
| 130 | test("a person shows their name, bio and public work, looked up as no one", async () => { |
| 131 | const { sources: s, viewers } = sources(); |
| 132 | assert.deepEqual(await resolve("/u/Ada", s), { |
| 133 | kind: "person", |
| 134 | username: "ada", |
| 135 | name: "Ada Lovelace", |
| 136 | bio: "Engines.", |
| 137 | pullsMerged: 4, |
| 138 | pullsOpen: 1, |
| 139 | items: 2, |
| 140 | avatar: null, |
| 141 | }); |
| 142 | assert.ok(viewers.every((viewer) => viewer === null)); |
| 143 | assert.equal((await resolve("/u/nobody", s)).kind, "brand"); |
| 144 | assert.equal((await resolve("/u", s)).kind, "brand"); |
| 145 | assert.equal((await resolve("/u/ada/extra", s)).kind, "brand"); |
| 146 | }); |
| 147 | |
| 148 | test("a public project shows its name, description and open work", async () => { |
| 149 | const { sources: s } = sources(); |
| 150 | assert.deepEqual(await resolve("/acme/web/commits", s), { |
| 151 | kind: "project", |
| 152 | owner: "acme", |
| 153 | repo: "web", |
| 154 | name: "Web app", |
| 155 | description: "The storefront", |
| 156 | items: 3, |
| 157 | pulls: 2, |
| 158 | }); |
| 159 | }); |
| 160 | |
| 161 | test("work items, pull requests and Soon pages get cards of their own", async () => { |
| 162 | const { sources: s } = sources(); |
| 163 | const item = { |
| 164 | kind: "item", |
| 165 | workspace: "acme", |
| 166 | number: 41, |
| 167 | title: "Fix the thing", |
| 168 | state: "not_planned", |
| 169 | author: "ada", |
| 170 | requestedBy: null, |
| 171 | }; |
| 172 | assert.deepEqual(await resolve("/acme/-/work/41", s), item); |
| 173 | assert.deepEqual(await resolve("/Acme/-/work/41", s), item); |
| 174 | // An old issue's address shows the work item it became. |
| 175 | assert.deepEqual(await resolve("/acme/web/issues/7", s), item); |
| 176 | // One no one outside the workspace may see gets the generic card. |
| 177 | assert.deepEqual(await resolve("/acme/-/work/42", s), { kind: "brand" }); |
| 178 | const pull = await resolve("/acme/web/pull/8", s); |
| 179 | assert.equal(pull.kind, "pull"); |
| 180 | assert.equal(pull.kind === "pull" && pull.state, "merged"); |
| 181 | // g1t made it, for ada: the card says both. |
| 182 | assert.equal(pull.kind === "pull" && pull.author, "g1t"); |
| 183 | assert.equal(pull.kind === "pull" && pull.requestedBy, "ada"); |
| 184 | const soon = await resolve("/acme/web/soon/storage", s); |
| 185 | assert.equal(soon.kind, "soon"); |
| 186 | assert.equal(soon.kind === "soon" && soon.title, "Storage"); |
| 187 | // One that does not exist falls back to the project. |
| 188 | assert.equal((await resolve("/acme/web/issues/99", s)).kind, "project"); |
| 189 | assert.equal((await resolve("/acme/web/soon/nothing", s)).kind, "project"); |
| 190 | assert.equal((await resolve("/acme/web/issues/007x", s)).kind, "project"); |
| 191 | }); |
| 192 | |
| 193 | test("nothing private ever reaches a card", async () => { |
| 194 | const { sources: s } = sources(); |
| 195 | for (const path of ["/acme/vault", "/acme/vault/issues/7", "/acme/vault/pull/8", "/acme/vault/soon/storage", "/acme/vault/settings"]) { |
| 196 | assert.deepEqual(await resolve(path, s), { kind: "brand" }, path); |
| 197 | } |
| 198 | // A missing repository says nothing either. |
| 199 | assert.deepEqual(await resolve("/acme/ghost/pull/8", s), { kind: "brand" }); |
| 200 | }); |
| 201 | |
| 202 | test("a private project on a public repository keeps its name to itself", async () => { |
| 203 | const { sources: s } = sources({ |
| 204 | projects: { |
| 205 | get: async () => ok({ name: "Secret name", description: "Secret plans", private: true } as Project), |
| 206 | list: async () => ok([]), |
| 207 | }, |
| 208 | }); |
| 209 | const card = await resolve("/acme/web", s); |
| 210 | assert.equal(card.kind === "project" && card.name, "web"); |
| 211 | assert.equal(card.kind === "project" && card.description, null); |
| 212 | }); |
| 213 | |
| 214 | test("every lookup is made with no viewer", async () => { |
| 215 | const { sources: s, viewers } = sources(); |
| 216 | for (const path of ["/acme", "/acme/web", "/acme/-/work/41", "/acme/web/issues/7", "/acme/web/pull/8", "/acme/vault/pull/8"]) { |
| 217 | await resolve(path, s); |
| 218 | } |
| 219 | assert.ok(viewers.length > 0); |
| 220 | assert.ok(viewers.every((viewer) => viewer === null)); |
| 221 | }); |
| 222 | |
| 223 | test("a service that fails gives the brand card, marked so it is not kept", async () => { |
| 224 | const { sources: s } = sources({ |
| 225 | repos: { |
| 226 | get: async () => { |
| 227 | throw new Error("down"); |
| 228 | }, |
| 229 | }, |
| 230 | }); |
| 231 | assert.deepEqual(await resolve("/acme/web", s), { kind: "brand", failed: true }); |
| 232 | }); |
| 233 | |
| 234 | test("a docs card is bounded, and has a title even without one", () => { |
| 235 | const card = docsCard(new URLSearchParams({ title: " Quick\nstart ", section: "Get started", description: "x".repeat(400) })); |
| 236 | assert.equal(card.kind === "docs" && card.title, "Quick start"); |
| 237 | assert.equal(card.kind === "docs" && card.description?.length, 300); |
| 238 | assert.equal(docsCard(new URLSearchParams()).kind === "docs" && docsCard(new URLSearchParams()).title, "g1t docs"); |
| 239 | assert.equal(clean(" ", 10), null); |
| 240 | }); |
| 241 | |
| 242 | test("the cache key keeps only what changes the card", () => { |
| 243 | const v = `${RENDER_VERSION}.1z141z3`; |
| 244 | const a = cacheKey(new URL(`https://og.g1t.sh/image?utm=1&path=/acme/web&v=${v}`)); |
| 245 | const b = cacheKey(new URL(`https://og.g1t.sh/image?path=/acme/web&v=${v}&x=2`)); |
| 246 | assert.equal(a, b); |
| 247 | assert.notEqual(a, cacheKey(new URL(`https://og.g1t.sh/image?path=/acme/web&v=${RENDER_VERSION}.abc`))); |
| 248 | assert.match(a, new RegExp(`render=${RENDER_VERSION}`)); |
| 249 | // The keys cards were kept under before the render version was. |
| 250 | assert.notEqual(a, "https://og.g1t.sh/image?design=1&path=%2Facme%2Fweb&v=3"); |
| 251 | const docs = cacheKey(new URL(`https://og.g1t.sh/docs?title=Quickstart&v=${RENDER_VERSION}&utm=x`)); |
| 252 | assert.equal(docs, `https://og.g1t.sh/docs?render=${RENDER_VERSION}&title=Quickstart&v=${RENDER_VERSION}`); |
| 253 | // The docs' text as the card draws it. |
| 254 | assert.equal(cacheKey(new URL("https://og.g1t.sh/docs?title=%20Quick%0Astart%20")), cacheKey(new URL("https://og.g1t.sh/docs?title=Quick%20start"))); |
| 255 | }); |
| 256 | |
| 257 | test("a v the site never writes is left out of the key", () => { |
| 258 | const plain = cacheKey(new URL("https://og.g1t.sh/image?path=/acme/web")); |
| 259 | for (const v of ["random", "999", `${RENDER_VERSION}.TOOLONG12`, `${RENDER_VERSION}.ab-c`, `x${RENDER_VERSION}`]) { |
| 260 | assert.equal(cacheKey(new URL(`https://og.g1t.sh/image?path=/acme/web&v=${encodeURIComponent(v)}`)), plain, v); |
| 261 | } |
| 262 | }); |
| 263 | |
| 264 | test("a path is keyed by the page whose card it shows", () => { |
| 265 | const key = (path: string) => cacheKey(new URL(`https://og.g1t.sh/image?path=${encodeURIComponent(path)}`)); |
| 266 | assert.equal(key("/acme/web/tree/main/src?x=1"), key("/acme/web")); |
| 267 | assert.equal(key("/acme/web/pull/3/files"), key("/acme/web/pull/3")); |
| 268 | assert.notEqual(key("/acme/web/pull/3"), key("/acme/web")); |
| 269 | assert.equal(key("/Acme/-/settings"), key("/acme")); |
| 270 | assert.equal(key("/pricing/"), key("/pricing")); |
| 271 | assert.equal(key("/not a name/web"), key("/")); |
| 272 | assert.equal(key("/login/extra/parts"), key("/")); |
| 273 | assert.equal(cardPath("/u/Ada"), "/u/ada"); |
| 274 | assert.equal(cardPath("/acme/web/issues/12"), "/acme/web/issues/12"); |
| 275 | assert.equal(cardPath("/acme/web/issues/012"), "/acme/web"); |
| 276 | assert.equal(cardPath("/Acme/-/work/12"), "/acme/-/work/12"); |
| 277 | assert.equal(cardPath("/acme/-/work/12/extra"), "/acme"); |
| 278 | assert.equal(cardPath("/acme/-/work/labels"), "/acme"); |
| 279 | assert.equal(cardPath("/acme/web/soon/no-such-thing"), "/acme/web"); |
| 280 | assert.equal(cardPath("//evil"), "/"); |
| 281 | }); |
| 282 | |
| 283 | test("a path resolves to the same card as the page it is keyed by", async () => { |
| 284 | const { sources: s } = sources(); |
| 285 | for (const path of ["/acme/web/tree/main", "/acme/web/pull/7/files", "/acme/-/settings", "/acme/web/issues/3", "/Acme/-/work/41"]) { |
| 286 | assert.deepEqual(await resolve(path, s), await resolve(cardPath(path), s), path); |
| 287 | } |
| 288 | }); |