flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

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

225 lines9,218 bytesCodeBlame
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { Issue, Project, Pull, Repo, Result, Workspace } from "@g1t/contracts";
5
6import { cacheKey } from "./cache.ts";
7import { 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: "",
25 topics: [],
26 };
27}
28
29const REPOS: Record<string, Repo> = {
30 "acme/web": repo("acme", "web"),
31 "acme/vault": repo("acme", "vault", true),
32};
33
34const ISSUE = { number: 7, title: "Fix the thing", state: "closed", reason: "not_planned", author } as Issue;
35const 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. */
38function 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
81test("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
90test("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});
100
101test("a workspace shows its name and how many projects are public", async () => {
102 const { sources: s } = sources();
103 assert.deepEqual(await resolve("/acme", s), {
104 kind: "workspace",
105 slug: "acme",
106 name: "Acme",
107 description: "Rockets",
108 projects: 2,
109 avatar: null,
110 });
111 // Its members-only pages show the same public card.
112 assert.equal((await resolve("/acme/-/billing", s)).kind, "workspace");
113 assert.equal((await resolve("/nobody", s)).kind, "brand");
114});
115
116test("a person shows their name, bio and public work, looked up as no one", async () => {
117 const { sources: s, viewers } = sources();
118 assert.deepEqual(await resolve("/u/Ada", s), {
119 kind: "person",
120 username: "ada",
121 name: "Ada Lovelace",
122 bio: "Engines.",
123 pullsMerged: 4,
124 pullsOpen: 1,
125 issues: 2,
126 avatar: null,
127 });
128 assert.ok(viewers.every((viewer) => viewer === null));
129 assert.equal((await resolve("/u/nobody", s)).kind, "brand");
130 assert.equal((await resolve("/u", s)).kind, "brand");
131 assert.equal((await resolve("/u/ada/extra", s)).kind, "brand");
132});
133
134test("a public project shows its name, description and open work", async () => {
135 const { sources: s } = sources();
136 assert.deepEqual(await resolve("/acme/web/commits", s), {
137 kind: "project",
138 owner: "acme",
139 repo: "web",
140 name: "Web app",
141 description: "The storefront",
142 issues: 3,
143 pulls: 2,
144 });
145});
146
147test("issues, pull requests and Soon pages get cards of their own", async () => {
148 const { sources: s } = sources();
149 assert.deepEqual(await resolve("/acme/web/issues/7", s), {
150 kind: "issue",
151 owner: "acme",
152 repo: "web",
153 number: 7,
154 title: "Fix the thing",
155 state: "not_planned",
156 author: "ada",
157 });
158 const pull = await resolve("/acme/web/pull/8", s);
159 assert.equal(pull.kind, "pull");
160 assert.equal(pull.kind === "pull" && pull.state, "merged");
161 const soon = await resolve("/acme/web/soon/board", s);
162 assert.equal(soon.kind, "soon");
163 assert.equal(soon.kind === "soon" && soon.title, "Board");
164 // One that does not exist falls back to the project.
165 assert.equal((await resolve("/acme/web/issues/99", s)).kind, "project");
166 assert.equal((await resolve("/acme/web/soon/nothing", s)).kind, "project");
167 assert.equal((await resolve("/acme/web/issues/007x", s)).kind, "project");
168});
169
170test("nothing private ever reaches a card", async () => {
171 const { sources: s } = sources();
172 for (const path of ["/acme/vault", "/acme/vault/issues/7", "/acme/vault/pull/8", "/acme/vault/soon/board", "/acme/vault/settings"]) {
173 assert.deepEqual(await resolve(path, s), { kind: "brand" }, path);
174 }
175 // A missing repository says nothing either.
176 assert.deepEqual(await resolve("/acme/ghost/pull/8", s), { kind: "brand" });
177});
178
179test("a private project on a public repository keeps its name to itself", async () => {
180 const { sources: s } = sources({
181 projects: {
182 get: async () => ok({ name: "Secret name", description: "Secret plans", private: true } as Project),
183 list: async () => ok([]),
184 },
185 });
186 const card = await resolve("/acme/web", s);
187 assert.equal(card.kind === "project" && card.name, "web");
188 assert.equal(card.kind === "project" && card.description, "About web");
189});
190
191test("every lookup is made with no viewer", async () => {
192 const { sources: s, viewers } = sources();
193 for (const path of ["/acme", "/acme/web", "/acme/web/issues/7", "/acme/web/pull/8", "/acme/vault/pull/8"]) {
194 await resolve(path, s);
195 }
196 assert.ok(viewers.length > 0);
197 assert.ok(viewers.every((viewer) => viewer === null));
198});
199
200test("a service that fails gives the brand card, marked so it is not kept", async () => {
201 const { sources: s } = sources({
202 repos: {
203 get: async () => {
204 throw new Error("down");
205 },
206 },
207 });
208 assert.deepEqual(await resolve("/acme/web", s), { kind: "brand", failed: true });
209});
210
211test("a docs card is bounded, and has a title even without one", () => {
212 const card = docsCard(new URLSearchParams({ title: " Quick\nstart ", section: "Get started", description: "x".repeat(400) }));
213 assert.equal(card.kind === "docs" && card.title, "Quick start");
214 assert.equal(card.kind === "docs" && card.description?.length, 300);
215 assert.equal(docsCard(new URLSearchParams()).kind === "docs" && docsCard(new URLSearchParams()).title, "g1t docs");
216 assert.equal(clean(" ", 10), null);
217});
218
219test("the cache key keeps only what changes the card", () => {
220 const a = cacheKey(new URL("https://og.g1t.sh/image?utm=1&path=/acme/web&v=3"));
221 const b = cacheKey(new URL("https://og.g1t.sh/image?path=/acme/web&v=3&x=2"));
222 assert.equal(a, b);
223 assert.notEqual(a, cacheKey(new URL("https://og.g1t.sh/image?path=/acme/web&v=4")));
224 assert.match(a, /design=/);
225});