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.

g1t.sh
127 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4
5import {
6 INSTALL_COOKIE,
7 STATE_COOKIE,
8 connectStep,
9 cookie,
10 githubConnected,
11 installCookieValue,
12 installWorkspace,
13 installationSummary,
14 newState,
15 notYetAdded,
16 readCookie,
17 sameString,
18 setupChoices,
19 stateMatches,
20} from "./github.ts";
21
22test("the state must come back exactly as it was given", () => {
23 const state = newState();
24 assert.match(state, /^[0-9a-f]{64}$/);
25 assert.notEqual(state, newState());
26 assert.ok(stateMatches(state, state));
27 assert.ok(!stateMatches(state, state.slice(0, -1) + (state.endsWith("0") ? "1" : "0")));
28 assert.ok(!stateMatches(null, state));
29 assert.ok(!stateMatches(state, null));
30 assert.ok(!stateMatches("", ""));
31 assert.ok(!sameString("abc", "abcd"));
32});
33
34test("cookies are read by name, and odd values are ignored", () => {
35 const header = `g1t_session=${"a".repeat(64)}; ${STATE_COOKIE}=abc123; other=x`;
36 assert.equal(readCookie(header, STATE_COOKIE), "abc123");
37 assert.equal(readCookie(header, "missing"), null);
38 assert.equal(readCookie(`${STATE_COOKIE}=<script>`, STATE_COOKIE), null);
39 assert.equal(readCookie(null, STATE_COOKIE), null);
40 const set = cookie(STATE_COOKIE, "abc", 600);
41 assert.match(set, /HttpOnly/);
42 assert.match(set, /Secure/);
43 assert.match(set, /SameSite=Lax/);
44 assert.match(set, /Max-Age=600/);
45});
46
47test("an installation returns to the workspace it was started for", () => {
48 const state = newState();
49 const value = installCookieValue(state, "acme");
50 assert.equal(readCookie(`${INSTALL_COOKIE}=${value}`, INSTALL_COOKIE), value);
51 assert.equal(installWorkspace(value, state), "acme");
52 assert.equal(installWorkspace(value, newState()), null);
53 assert.equal(installWorkspace(value, null), null);
54 assert.equal(installWorkspace(null, state), null);
55 assert.equal(installWorkspace(`${state}.`, state), null);
56});
57
58const seen = (id: number, account: string, recordedIn: string[], accountType = "Organization") => ({
59 id,
60 account,
61 accountType,
62 repositorySelection: "all",
63 suspended: false,
64 settingsUrl: `https://github.com/organizations/${account}/settings/installations/${id}`,
65 recordedIn,
66});
67
68test("an installation made on GitHub directly is offered to the workspaces that lack it", () => {
69 const listed = [seen(1, "flagon-io", []), seen(2, "syntaqx", ["syntaqx"], "User"), seen(3, "acme", ["flagon-io"])];
70 assert.deepEqual(
71 notYetAdded(listed, "flagon-io").map((item) => item.account),
72 ["flagon-io", "syntaqx"],
73 );
74 assert.deepEqual(
75 notYetAdded(listed, "syntaqx").map((item) => item.id),
76 [1, 3],
77 );
78 assert.equal(installationSummary(listed[0]!), "Organization · all repositories");
79 assert.equal(installationSummary({ accountType: "User", repositorySelection: "selected" }), "Personal · selected repositories");
80});
81
82test("the setup page offers only workspaces the person owns, saying which have it", () => {
83 const workspaces = [
84 { slug: "flagon-io", name: "Flagon", role: "owner" },
85 { slug: "acme", role: "member" },
86 { slug: "syntaqx", name: null, role: "owner" },
87 ];
88 assert.deepEqual(setupChoices(workspaces, seen(1, "flagon-io", ["syntaqx"])), [
89 { slug: "flagon-io", name: "Flagon", added: false },
90 { slug: "syntaqx", name: "syntaqx", added: true },
91 ]);
92 assert.deepEqual(
93 setupChoices(workspaces, null).map((choice) => choice.added),
94 [false, false],
95 );
96});
97
98test("once an installation is recorded, Integrations says GitHub is connected", () => {
99 assert.equal(githubConnected([]), null);
100 const state = githubConnected([{ account: "flagon-io", suspended: false }]);
101 assert.deepEqual(state, { detail: "On flagon-io", problem: null, manage: null });
102 assert.equal(
103 githubConnected([
104 { account: "a", suspended: false },
105 { account: "b", suspended: true },
106 ])?.problem,
107 "The installation on b is suspended on GitHub.",
108 );
109});
110
111test("Connect GitHub links first, adds what is there, and installs only when nothing is", () => {
112 const none: { id: number; suspended: boolean }[] = [];
113 assert.deepEqual(connectStep({ linked: false, justLinked: false, another: false, unadded: none }), { kind: "link" });
114 // A link that did not take goes back to the page, not round again.
115 assert.deepEqual(connectStep({ linked: false, justLinked: true, another: false, unadded: none }), { kind: "choose" });
116 assert.deepEqual(connectStep({ linked: true, justLinked: true, another: false, unadded: none }), { kind: "install" });
117 assert.deepEqual(connectStep({ linked: true, justLinked: false, another: false, unadded: [{ id: 7, suspended: false }] }), {
118 kind: "add",
119 installation: 7,
120 });
121 assert.deepEqual(connectStep({ linked: true, justLinked: false, another: false, unadded: [{ id: 7, suspended: true }] }), { kind: "choose" });
122 assert.deepEqual(
123 connectStep({ linked: true, justLinked: false, another: false, unadded: [{ id: 7, suspended: false }, { id: 8, suspended: false }] }),
124 { kind: "choose" },
125 );
126 assert.deepEqual(connectStep({ linked: true, justLinked: false, another: true, unadded: [{ id: 7, suspended: false }] }), { kind: "install" });
127});