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

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