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

200 lines8,192 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 },
52 repos: {
53 // Like the service: a private repository can come back to a member,
54 // so the resolver must check `isPrivate` itself as well.
55 get: async (path, viewer) => seen(viewer, REPOS[`${path.namespace}/${path.name}`.toLowerCase()] ? ok(REPOS[`${path.namespace}/${path.name}`.toLowerCase()]) : notFound),
56 },
57 work: {
58 counts: async (path, viewer) => seen(viewer, hidden(path) ? notFound : ok({ issues: 3, pulls: 2 })),
59 getIssue: async (path, number, viewer) =>
60 seen(viewer, !hidden(path) && number === 7 ? ok({ issue: ISSUE, pulls: [], comments: [] }) : notFound),
61 getPull: async (path, number, viewer) =>
62 seen(viewer, !hidden(path) && number === 8 ? ok({ pull: PULL, issue: null, comments: [] } as never) : notFound),
63 },
64 projects: {
65 get: async (workspace, slug, viewer) =>
66 seen(viewer, workspace === "acme" && slug === "web" ? ok({ name: "Web app", description: "The storefront", private: false } as Project) : notFound),
67 list: async (workspace, viewer) =>
68 seen(viewer, ok([{ private: false }, { private: false }, { private: true }] as Project[])),
69 },
70 };
71 return { sources: { ...base, ...overrides }, viewers };
72}
73
74test("paths are split and decoded, and anything else is refused", () => {
75 assert.deepEqual(segments("/acme/web/pull/8"), ["acme", "web", "pull", "8"]);
76 assert.deepEqual(segments("/acme/web?tab=code#top"), ["acme", "web"]);
77 assert.deepEqual(segments("/"), []);
78 assert.equal(segments("https://evil.example/x"), null);
79 assert.equal(segments("//evil.example/x"), null);
80 assert.equal(segments("/%E0%A4%A"), null);
81});
82
83test("the site's own pages get their own cards, or the brand card", async () => {
84 const { sources: s } = sources();
85 assert.equal((await resolve("/", s)).kind, "brand");
86 const pricing = await resolve("/pricing", s);
87 assert.equal(pricing.kind, "page");
88 assert.equal(pricing.kind === "page" && pricing.title, "What it costs us, plus a markup");
89 assert.equal((await resolve("/explore", s)).kind, "page");
90 assert.equal((await resolve("/login", s)).kind, "brand");
91 assert.equal((await resolve("/settings/keys", s)).kind, "brand");
92});
93
94test("a workspace shows its name and how many projects are public", async () => {
95 const { sources: s } = sources();
96 assert.deepEqual(await resolve("/acme", s), {
97 kind: "workspace",
98 slug: "acme",
99 name: "Acme",
100 description: "Rockets",
101 projects: 2,
102 avatar: null,
103 });
104 // Its members-only pages show the same public card.
105 assert.equal((await resolve("/acme/-/billing", s)).kind, "workspace");
106 assert.equal((await resolve("/nobody", s)).kind, "brand");
107});
108
109test("a public project shows its name, description and open work", async () => {
110 const { sources: s } = sources();
111 assert.deepEqual(await resolve("/acme/web/commits", s), {
112 kind: "project",
113 owner: "acme",
114 repo: "web",
115 name: "Web app",
116 description: "The storefront",
117 issues: 3,
118 pulls: 2,
119 });
120});
121
122test("issues, pull requests and Soon pages get cards of their own", async () => {
123 const { sources: s } = sources();
124 assert.deepEqual(await resolve("/acme/web/issues/7", s), {
125 kind: "issue",
126 owner: "acme",
127 repo: "web",
128 number: 7,
129 title: "Fix the thing",
130 state: "not_planned",
131 author: "ada",
132 });
133 const pull = await resolve("/acme/web/pull/8", s);
134 assert.equal(pull.kind, "pull");
135 assert.equal(pull.kind === "pull" && pull.state, "merged");
136 const soon = await resolve("/acme/web/soon/board", s);
137 assert.equal(soon.kind, "soon");
138 assert.equal(soon.kind === "soon" && soon.title, "Board");
139 // One that does not exist falls back to the project.
140 assert.equal((await resolve("/acme/web/issues/99", s)).kind, "project");
141 assert.equal((await resolve("/acme/web/soon/nothing", s)).kind, "project");
142 assert.equal((await resolve("/acme/web/issues/007x", s)).kind, "project");
143});
144
145test("nothing private ever reaches a card", async () => {
146 const { sources: s } = sources();
147 for (const path of ["/acme/vault", "/acme/vault/issues/7", "/acme/vault/pull/8", "/acme/vault/soon/board", "/acme/vault/settings"]) {
148 assert.deepEqual(await resolve(path, s), { kind: "brand" }, path);
149 }
150 // A missing repository says nothing either.
151 assert.deepEqual(await resolve("/acme/ghost/pull/8", s), { kind: "brand" });
152});
153
154test("a private project on a public repository keeps its name to itself", async () => {
155 const { sources: s } = sources({
156 projects: {
157 get: async () => ok({ name: "Secret name", description: "Secret plans", private: true } as Project),
158 list: async () => ok([]),
159 },
160 });
161 const card = await resolve("/acme/web", s);
162 assert.equal(card.kind === "project" && card.name, "web");
163 assert.equal(card.kind === "project" && card.description, "About web");
164});
165
166test("every lookup is made with no viewer", async () => {
167 const { sources: s, viewers } = sources();
168 for (const path of ["/acme", "/acme/web", "/acme/web/issues/7", "/acme/web/pull/8", "/acme/vault/pull/8"]) {
169 await resolve(path, s);
170 }
171 assert.ok(viewers.length > 0);
172 assert.ok(viewers.every((viewer) => viewer === null));
173});
174
175test("a service that fails gives the brand card, marked so it is not kept", async () => {
176 const { sources: s } = sources({
177 repos: {
178 get: async () => {
179 throw new Error("down");
180 },
181 },
182 });
183 assert.deepEqual(await resolve("/acme/web", s), { kind: "brand", failed: true });
184});
185
186test("a docs card is bounded, and has a title even without one", () => {
187 const card = docsCard(new URLSearchParams({ title: " Quick\nstart ", section: "Get started", description: "x".repeat(400) }));
188 assert.equal(card.kind === "docs" && card.title, "Quick start");
189 assert.equal(card.kind === "docs" && card.description?.length, 300);
190 assert.equal(docsCard(new URLSearchParams()).kind === "docs" && docsCard(new URLSearchParams()).title, "g1t docs");
191 assert.equal(clean(" ", 10), null);
192});
193
194test("the cache key keeps only what changes the card", () => {
195 const a = cacheKey(new URL("https://og.g1t.sh/image?utm=1&path=/acme/web&v=3"));
196 const b = cacheKey(new URL("https://og.g1t.sh/image?path=/acme/web&v=3&x=2"));
197 assert.equal(a, b);
198 assert.notEqual(a, cacheKey(new URL("https://og.g1t.sh/image?path=/acme/web&v=4")));
199 assert.match(a, /design=/);
200});