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
203 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import {
5 FALLBACK_FOCUS_MS,
6 FALLBACK_POLL_MS,
7 HIDDEN_MIN_MS,
8 LIVE_DEBOUNCE_MS,
9 LIVE_HOLD_CHECK_MS,
10 LIVE_MAX_WAIT_MS,
11 type RepoChange,
12 atBottom,
13 branchOf,
14 fallbackDue,
15 gate,
16 isTyping,
17 newCommits,
18 repoPages,
19 tagOf,
20} from "./repo-live.ts";
21
22const AT = "2026-10-11T10:00:00Z";
23const NOW = Date.parse(AT);
24
25function change(kind: string, more: Partial<RepoChange> = {}): RepoChange {
26 return { repo_id: "rep_1", repo: "acme/rocket", kind, at: AT, ...more };
27}
28
29const push = (branch: string, commits?: number) => change("git.push", { ref: `refs/heads/${branch}`, sha: "abc", commits: commits ?? null });
30
31test("refs name their branch or tag", () => {
32 assert.equal(branchOf("refs/heads/feature/x"), "feature/x");
33 assert.equal(branchOf("refs/tags/v1"), null);
34 assert.equal(tagOf("refs/tags/v1.2"), "v1.2");
35 assert.equal(tagOf(null), null);
36});
37
38test("the files and overview pages follow refs, checks, releases and deployments", () => {
39 const files = repoPages.files();
40 for (const c of [push("main"), push("other"), change("git.deleted", { ref: "refs/heads/old" }), change("status.created", { sha: "abc" }), change("release.published"), change("deployment_status.created")]) {
41 assert.equal(files(c), true, c.kind);
42 }
43 assert.equal(files(change("issue.opened", { number: 3 })), false);
44 assert.equal(files(change("comment.created", { number: 3 })), false);
45});
46
47test("the commits page hears pushes to its own branch only", () => {
48 const commits = repoPages.commits("main");
49 assert.equal(commits(push("main")), true);
50 assert.equal(commits(push("feature")), false);
51 assert.equal(commits(change("git.push", { ref: "refs/tags/v1" })), false);
52 assert.equal(commits(change("workflow.completed", { sha: "abc", run: "run_1" })), true, "checks on its commits");
53 assert.equal(commits(change("pull.opened", { number: 1 })), false);
54});
55
56test("branches, tags, and the lists of pull requests and issues", () => {
57 assert.equal(repoPages.refs()(push("x")), true);
58 assert.equal(repoPages.refs()(change("pull.opened", { number: 1 })), true);
59 assert.equal(repoPages.refs()(change("issue.opened", { number: 1 })), false);
60 assert.equal(repoPages.tags()(change("git.push", { ref: "refs/tags/v2" })), true);
61 assert.equal(repoPages.tags()(push("main")), false);
62 assert.equal(repoPages.tags()(change("release.published")), true);
63 assert.equal(repoPages.pulls()(change("pull.updated", { number: 4 })), true);
64 assert.equal(repoPages.pulls()(change("review.completed", { number: 4 })), true);
65 assert.equal(repoPages.pulls()(change("issue.opened", { number: 5 })), false);
66 assert.equal(repoPages.issues()(change("issue.closed", { number: 5 })), true);
67 assert.equal(repoPages.issues()(change("comment.created", { number: 5 })), true);
68 assert.equal(repoPages.issues()(change("pull.merged", { number: 6 })), true, "a merge may close issues");
69 assert.equal(repoPages.issues()(push("main")), false);
70});
71
72test("a pull request hears its own changes, its head branch and its head's checks", () => {
73 const pull = repoPages.pull(7, { branch: "feature", sha: "f00d" });
74 assert.equal(pull(change("pull.updated", { number: 7 })), true);
75 assert.equal(pull(change("comment.created", { number: 7 })), true);
76 assert.equal(pull(change("review.completed", { number: 7 })), true);
77 assert.equal(pull(change("checks.completed", { number: 7, sha: "f00d" })), true);
78 assert.equal(pull(change("pull.merged", { number: 7 })), true);
79 assert.equal(pull(push("feature")), true, "new commits");
80 assert.equal(pull(change("status.created", { sha: "f00d" })), true);
81 assert.equal(pull(change("run.updated", { run: "run_1", number: 7 })), true, "a run for it");
82 assert.equal(pull(change("comment.created", { number: 8 })), false);
83 assert.equal(pull(push("main")), false);
84 assert.equal(pull(change("status.created", { sha: "beef" })), false);
85});
86
87test("an issue hears its own changes and its linked pull requests", () => {
88 const issue = repoPages.issue(3, [9]);
89 assert.equal(issue(change("comment.created", { number: 3 })), true);
90 assert.equal(issue(change("issue.closed", { number: 3 })), true);
91 assert.equal(issue(change("pull.merged", { number: 9 })), true);
92 assert.equal(issue(change("comment.created", { number: 4 })), false);
93 assert.equal(issue(push("main")), false);
94});
95
96test("the Work page hears claims, sessions starting and ending, what the queue finds, and merges", () => {
97 const page = repoPages.work();
98 for (const kind of ["work.claimed", "work.claim_expired", "session.started", "session.ended", "dependency.changed", "overlap.detected", "integration.failed", "decision.recorded", "queue.changed", "pull.merged"]) {
99 assert.equal(page(change(kind, { number: 3 })), true, kind);
100 }
101 assert.equal(page(change("session.progressed")), true, "a session going on");
102 assert.equal(page(change("session.appended", { number: 3 })), false);
103 assert.equal(page(change("comment.created", { number: 3 })), false);
104 assert.equal(page(push("main")), false);
105 // An issue hears coordination about itself and its pull requests by number.
106 assert.equal(repoPages.issue(3, [9])(change("integration.failed", { number: 9 })), true);
107});
108
109test("a session going on reaches the issue and pull request it is on, and no other", () => {
110 assert.equal(repoPages.issue(3, [9])(change("session.progressed", { number: 3 })), true);
111 assert.equal(repoPages.issue(3, [9])(change("session.progressed", { number: 9 })), true);
112 assert.equal(repoPages.issue(3)(change("session.progressed", { number: 4 })), false);
113 assert.equal(repoPages.issue(3)(change("session.progressed")), false);
114 assert.equal(repoPages.pull(9)(change("session.progressed", { number: 9 })), true);
115 assert.equal(repoPages.pull(9)(change("session.progressed", { number: 3 })), false);
116 assert.equal(repoPages.pull(9)(change("session.progressed")), false);
117});
118
119test("a session's page hears every session, claim and overlap in the repository", () => {
120 const page = repoPages.session();
121 for (const kind of ["session.started", "session.progressed", "session.ended", "work.claimed", "work.released", "overlap.detected"]) {
122 assert.equal(page(change(kind)), true, kind);
123 }
124 assert.equal(page(change("comment.created", { number: 3 })), false);
125 assert.equal(page(change("pull.merged", { number: 3 })), false);
126 assert.equal(page(push("main")), false);
127});
128
129test("runs, one run, and security", () => {
130 assert.equal(repoPages.runs()(change("run.updated", { run: "run_1" })), true);
131 assert.equal(repoPages.runs()(change("status.created", { sha: "abc" })), true, "a new run reports pending on its commit");
132 assert.equal(repoPages.runs()(change("issue.opened", { number: 1 })), false);
133 const run = repoPages.run("run_1", "abc");
134 assert.equal(run(change("run.updated", { run: "run_1" })), true);
135 assert.equal(run(change("workflow.completed", { run: "run_1" })), true);
136 assert.equal(run(change("run.updated", { run: "run_2" })), false);
137 assert.equal(run(change("status.created", { sha: "abc" })), true);
138 assert.equal(repoPages.security()(change("secret_scanning_alert.created")), true);
139 assert.equal(repoPages.security()(change("code_scanning_alert.fixed", { number: 2 })), true);
140 assert.equal(repoPages.security()(change("issue.opened", { number: 1 })), false);
141});
142
143test("new commits are counted, and uncounted when a push was not", () => {
144 assert.equal(newCommits([push("main", 2), push("main", 3), push("other", 9)], "main"), 5);
145 assert.equal(newCommits([push("main", 2), push("main")], "main"), null);
146 assert.equal(newCommits([change("status.created")], "main"), 0);
147});
148
149test("a change waits a second for more, five at most, then refreshes", () => {
150 const idle = { now: NOW, visible: true, busy: false, navigating: false };
151 assert.deepEqual(gate({ pendingSince: null, lastChangeAt: null, lastRunAt: null }, idle), { action: "idle" });
152 assert.deepEqual(gate({ pendingSince: NOW, lastChangeAt: NOW, lastRunAt: null }, idle), { action: "wait", ms: LIVE_DEBOUNCE_MS });
153 assert.deepEqual(gate({ pendingSince: NOW, lastChangeAt: NOW, lastRunAt: null }, { ...idle, now: NOW + LIVE_DEBOUNCE_MS }), { action: "run" });
154 // A steady stream: refreshed after five seconds anyway.
155 const stream = { pendingSince: NOW, lastChangeAt: NOW + LIVE_MAX_WAIT_MS - 100, lastRunAt: null };
156 assert.deepEqual(gate(stream, { ...idle, now: NOW + LIVE_MAX_WAIT_MS - 50 }), { action: "wait", ms: 50 });
157 assert.deepEqual(gate(stream, { ...idle, now: NOW + LIVE_MAX_WAIT_MS }), { action: "run" });
158});
159
160test("never under someone typing, a dialog, or a navigation: it waits for them", () => {
161 const due = { pendingSince: NOW - 10_000, lastChangeAt: NOW - 10_000, lastRunAt: null };
162 for (const held of [{ busy: true, navigating: false }, { busy: false, navigating: true }]) {
163 assert.deepEqual(gate(due, { now: NOW, visible: true, ...held }), { action: "wait", ms: LIVE_HOLD_CHECK_MS });
164 }
165 assert.deepEqual(gate(due, { now: NOW, visible: true, busy: false, navigating: false }), { action: "run" });
166});
167
168test("a tab out of view refreshes at most once a minute", () => {
169 const hidden = { now: NOW, visible: false, busy: false, navigating: false };
170 const ranLately = { pendingSince: NOW - 5000, lastChangeAt: NOW - 5000, lastRunAt: NOW - 20_000 };
171 assert.deepEqual(gate(ranLately, hidden), { action: "wait", ms: HIDDEN_MIN_MS - 20_000 });
172 assert.deepEqual(gate({ ...ranLately, lastRunAt: NOW - HIDDEN_MIN_MS }, hidden), { action: "run" });
173 // Back in view: at once.
174 assert.deepEqual(gate(ranLately, { ...hidden, visible: true }), { action: "run" });
175});
176
177test("without the socket: back in view every 30 s at most, a poll every minute in view, never with it", () => {
178 const base = { live: false, visible: true, now: NOW };
179 assert.equal(fallbackDue({ ...base, lastRunAt: null, reason: "focus" }), true);
180 assert.equal(fallbackDue({ ...base, lastRunAt: NOW - FALLBACK_FOCUS_MS + 1, reason: "focus" }), false);
181 assert.equal(fallbackDue({ ...base, lastRunAt: NOW - FALLBACK_FOCUS_MS, reason: "focus" }), true);
182 assert.equal(fallbackDue({ ...base, lastRunAt: NOW - FALLBACK_FOCUS_MS, reason: "poll" }), false);
183 assert.equal(fallbackDue({ ...base, lastRunAt: NOW - FALLBACK_POLL_MS, reason: "poll" }), true);
184 assert.equal(fallbackDue({ ...base, visible: false, lastRunAt: null, reason: "poll" }), false, "a hidden tab is not polled");
185 assert.equal(fallbackDue({ ...base, live: true, lastRunAt: null, reason: "focus" }), false);
186});
187
188test("typing is in text boxes, text areas and editors", () => {
189 assert.equal(isTyping({ tagName: "TEXTAREA" }), true);
190 assert.equal(isTyping({ tagName: "input", type: "text" }), true);
191 assert.equal(isTyping({ tagName: "INPUT", type: "search" }), true);
192 assert.equal(isTyping({ tagName: "INPUT" }), true, "an input is text unless it says otherwise");
193 assert.equal(isTyping({ tagName: "DIV", isContentEditable: true }), true);
194 assert.equal(isTyping({ tagName: "INPUT", type: "checkbox" }), false);
195 assert.equal(isTyping({ tagName: "BUTTON" }), false);
196 assert.equal(isTyping({ tagName: "TEXTAREA", readOnly: true }), false);
197 assert.equal(isTyping(null), false);
198});
199
200test("the bottom of the page, with some slack", () => {
201 assert.equal(atBottom({ top: 880, height: 2000, viewport: 1000 }), true);
202 assert.equal(atBottom({ top: 500, height: 2000, viewport: 1000 }), false);
203});