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