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.

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