g1t/services/og/src/resolve.test.ts

239 lines10,240 bytesCodeBlame

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.sh1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { Issue, Project, Pull, Repo, Result, Workspace } from "@g1t/contracts";
5
status.g1t.sh with incident management, invites that land you in the workspace, settings as pages, usage without quotas6import { RENDER_VERSION, cacheKey } from "./cache.ts";
Social cards for every page: og.g1t.sh7import { type Sources, 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 description: `About ${name}`,
19 isPrivate,
20 ownerId: "w",
21 defaultBranch: "main",
22 forkOf: null,
23 protected: true,
24 createdAt: "",
Search across all of g1t, Explore, and a command palette25 topics: [],
Social cards for every page: og.g1t.sh26 };
27}
28
29const REPOS: Record<string, Repo> = {
30 "acme/web": repo("acme", "web"),
31 "acme/vault": repo("acme", "vault", true),
32};
33
g1t is the stored author of what it opens; the person who asked is requested_by and keeps the author's rights34const ISSUE = { number: 7, title: "Fix the thing", state: "closed", reason: "not_planned", author, requestedBy: null } as Issue;
35// A change g1t made for ada.
36const PULL = { number: 8, title: "Make the thing", status: "merged", author: { id: "usr_g1t_agent", username: "g1t", kind: "agent" }, requestedBy: author } as Pull;
Social cards for every page: og.g1t.sh37
38/** Services that see what an anonymous visitor sees, and record that they were asked as one. */
39function 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) =>
Workspace names and icons, and a component kit for every control52 slug === "acme" ? ({ slug: "acme", name: "Acme", description: "Rockets", id: "w", createdAt: "", memberCount: 3, avatar: null } as Workspace) : null,
Agents and memory, checks and conflicts, profiles, slug renames, custom domains53 profile: async (username) =>
54 username === "ada"
55 ? { username: "ada", name: "Ada Lovelace", bio: "Engines.", location: null, website: null, pronouns: null, avatar: null, createdAt: "" }
56 : null,
Social cards for every page: og.g1t.sh57 },
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({ issues: 3, pulls: 2 })),
65 getIssue: async (path, number, viewer) =>
66 seen(viewer, !hidden(path) && number === 7 ? ok({ issue: ISSUE, pulls: [], comments: [] }) : notFound),
67 getPull: async (path, number, viewer) =>
68 seen(viewer, !hidden(path) && number === 8 ? ok({ pull: PULL, issue: null, comments: [] } as never) : notFound),
Agents and memory, checks and conflicts, profiles, slug renames, custom domains69 byAuthor: async (_username, viewer) =>
70 seen(viewer, ok({ items: [], next: null, repos: [], counts: { pullsMerged: 4, pullsOpen: 1, pulls: 6, issues: 2, issuesOpen: 1 } })),
Social cards for every page: og.g1t.sh71 },
72 projects: {
73 get: async (workspace, slug, viewer) =>
74 seen(viewer, workspace === "acme" && slug === "web" ? ok({ name: "Web app", description: "The storefront", private: false } as Project) : notFound),
75 list: async (workspace, viewer) =>
76 seen(viewer, ok([{ private: false }, { private: false }, { private: true }] as Project[])),
77 },
78 };
79 return { sources: { ...base, ...overrides }, viewers };
80}
81
82test("paths are split and decoded, and anything else is refused", () => {
83 assert.deepEqual(segments("/acme/web/pull/8"), ["acme", "web", "pull", "8"]);
84 assert.deepEqual(segments("/acme/web?tab=code#top"), ["acme", "web"]);
85 assert.deepEqual(segments("/"), []);
86 assert.equal(segments("https://evil.example/x"), null);
87 assert.equal(segments("//evil.example/x"), null);
88 assert.equal(segments("/%E0%A4%A"), null);
89});
90
91test("the site's own pages get their own cards, or the brand card", async () => {
92 const { sources: s } = sources();
93 assert.equal((await resolve("/", s)).kind, "brand");
94 const pricing = await resolve("/pricing", s);
95 assert.equal(pricing.kind, "page");
96 assert.equal(pricing.kind === "page" && pricing.title, "What it costs us, plus a markup");
97 assert.equal((await resolve("/explore", s)).kind, "page");
98 assert.equal((await resolve("/login", s)).kind, "brand");
99 assert.equal((await resolve("/settings/keys", s)).kind, "brand");
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look100 const privacy = await resolve("/policies/privacy", s);
101 assert.equal(privacy.kind === "page" && privacy.title, "Privacy Policy");
102 assert.equal((await resolve("/status", s)).kind, "page");
103 assert.equal((await resolve("/security", s)).kind, "page");
104 assert.equal((await resolve("/policies/nothing", s)).kind, "brand");
Social cards for every page: og.g1t.sh105});
106
107test("a workspace shows its name and how many projects are public", async () => {
108 const { sources: s } = sources();
109 assert.deepEqual(await resolve("/acme", s), {
110 kind: "workspace",
111 slug: "acme",
112 name: "Acme",
113 description: "Rockets",
114 projects: 2,
Workspace names and icons, and a component kit for every control115 avatar: null,
Social cards for every page: og.g1t.sh116 });
117 // Its members-only pages show the same public card.
118 assert.equal((await resolve("/acme/-/billing", s)).kind, "workspace");
119 assert.equal((await resolve("/nobody", s)).kind, "brand");
120});
121
Agents and memory, checks and conflicts, profiles, slug renames, custom domains122test("a person shows their name, bio and public work, looked up as no one", async () => {
123 const { sources: s, viewers } = sources();
124 assert.deepEqual(await resolve("/u/Ada", s), {
125 kind: "person",
126 username: "ada",
127 name: "Ada Lovelace",
128 bio: "Engines.",
129 pullsMerged: 4,
130 pullsOpen: 1,
131 issues: 2,
132 avatar: null,
133 });
134 assert.ok(viewers.every((viewer) => viewer === null));
135 assert.equal((await resolve("/u/nobody", s)).kind, "brand");
136 assert.equal((await resolve("/u", s)).kind, "brand");
137 assert.equal((await resolve("/u/ada/extra", s)).kind, "brand");
138});
139
Social cards for every page: og.g1t.sh140test("a public project shows its name, description and open work", async () => {
141 const { sources: s } = sources();
142 assert.deepEqual(await resolve("/acme/web/commits", s), {
143 kind: "project",
144 owner: "acme",
145 repo: "web",
146 name: "Web app",
147 description: "The storefront",
148 issues: 3,
149 pulls: 2,
150 });
151});
152
153test("issues, pull requests and Soon pages get cards of their own", async () => {
154 const { sources: s } = sources();
155 assert.deepEqual(await resolve("/acme/web/issues/7", s), {
156 kind: "issue",
157 owner: "acme",
158 repo: "web",
159 number: 7,
160 title: "Fix the thing",
161 state: "not_planned",
162 author: "ada",
g1t is the stored author of what it opens; the person who asked is requested_by and keeps the author's rights163 requestedBy: null,
Social cards for every page: og.g1t.sh164 });
165 const pull = await resolve("/acme/web/pull/8", s);
166 assert.equal(pull.kind, "pull");
167 assert.equal(pull.kind === "pull" && pull.state, "merged");
g1t is the stored author of what it opens; the person who asked is requested_by and keeps the author's rights168 // g1t made it, for ada: the card says both.
169 assert.equal(pull.kind === "pull" && pull.author, "g1t");
170 assert.equal(pull.kind === "pull" && pull.requestedBy, "ada");
Social cards for every page: og.g1t.sh171 const soon = await resolve("/acme/web/soon/board", s);
172 assert.equal(soon.kind, "soon");
173 assert.equal(soon.kind === "soon" && soon.title, "Board");
174 // One that does not exist falls back to the project.
175 assert.equal((await resolve("/acme/web/issues/99", s)).kind, "project");
176 assert.equal((await resolve("/acme/web/soon/nothing", s)).kind, "project");
177 assert.equal((await resolve("/acme/web/issues/007x", s)).kind, "project");
178});
179
180test("nothing private ever reaches a card", async () => {
181 const { sources: s } = sources();
182 for (const path of ["/acme/vault", "/acme/vault/issues/7", "/acme/vault/pull/8", "/acme/vault/soon/board", "/acme/vault/settings"]) {
183 assert.deepEqual(await resolve(path, s), { kind: "brand" }, path);
184 }
185 // A missing repository says nothing either.
186 assert.deepEqual(await resolve("/acme/ghost/pull/8", s), { kind: "brand" });
187});
188
189test("a private project on a public repository keeps its name to itself", async () => {
190 const { sources: s } = sources({
191 projects: {
192 get: async () => ok({ name: "Secret name", description: "Secret plans", private: true } as Project),
193 list: async () => ok([]),
194 },
195 });
196 const card = await resolve("/acme/web", s);
197 assert.equal(card.kind === "project" && card.name, "web");
198 assert.equal(card.kind === "project" && card.description, "About web");
199});
200
201test("every lookup is made with no viewer", async () => {
202 const { sources: s, viewers } = sources();
203 for (const path of ["/acme", "/acme/web", "/acme/web/issues/7", "/acme/web/pull/8", "/acme/vault/pull/8"]) {
204 await resolve(path, s);
205 }
206 assert.ok(viewers.length > 0);
207 assert.ok(viewers.every((viewer) => viewer === null));
208});
209
210test("a service that fails gives the brand card, marked so it is not kept", async () => {
211 const { sources: s } = sources({
212 repos: {
213 get: async () => {
214 throw new Error("down");
215 },
216 },
217 });
218 assert.deepEqual(await resolve("/acme/web", s), { kind: "brand", failed: true });
219});
220
221test("a docs card is bounded, and has a title even without one", () => {
222 const card = docsCard(new URLSearchParams({ title: " Quick\nstart ", section: "Get started", description: "x".repeat(400) }));
223 assert.equal(card.kind === "docs" && card.title, "Quick start");
224 assert.equal(card.kind === "docs" && card.description?.length, 300);
225 assert.equal(docsCard(new URLSearchParams()).kind === "docs" && docsCard(new URLSearchParams()).title, "g1t docs");
226 assert.equal(clean(" ", 10), null);
227});
228
229test("the cache key keeps only what changes the card", () => {
230 const a = cacheKey(new URL("https://og.g1t.sh/image?utm=1&path=/acme/web&v=3"));
231 const b = cacheKey(new URL("https://og.g1t.sh/image?path=/acme/web&v=3&x=2"));
232 assert.equal(a, b);
233 assert.notEqual(a, cacheKey(new URL("https://og.g1t.sh/image?path=/acme/web&v=4")));
status.g1t.sh with incident management, invites that land you in the workspace, settings as pages, usage without quotas234 assert.match(a, new RegExp(`render=${RENDER_VERSION}`));
235 // The keys cards were kept under before the render version was.
236 assert.notEqual(a, "https://og.g1t.sh/image?design=1&path=%2Facme%2Fweb&v=3");
237 const docs = cacheKey(new URL("https://og.g1t.sh/docs?title=Quickstart&v=2&utm=x"));
238 assert.equal(docs, `https://og.g1t.sh/docs?render=${RENDER_VERSION}&title=Quickstart&v=2`);
Social cards for every page: og.g1t.sh239});