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