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.

265 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import {
5 FALLBACK_FOCUS_MS,
6 FALLBACK_POLL_MS,
7 LIVE_DEBOUNCE_MS,
8 LIVE_HOLD_CHECK_MS,
9 LIVE_MAX_WAIT_MS,
10 LIVE_MIN_GAP_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("a project's overview follows what it runs, who is working on it and what may need someone, not every push", () => {
39 const overview = repoPages.overview();
40 for (const c of [
41 change("deployment_status.created"),
42 change("status.created", { sha: "abc" }),
43 change("release.published"),
44 change("work.claimed", { number: 3 }),
45 change("session.started"),
46 change("session.progressed"),
47 change("pull.ready", { number: 4 }),
48 change("review.completed", { number: 4 }),
49 ]) {
50 assert.equal(overview(c), true, c.kind);
51 }
52 assert.equal(overview(push("feature")), false);
53 assert.equal(overview(change("comment.created", { number: 3 })), false);
54});
55
56test("the overview hears its own commit's checks, and every pull request's finished ones", () => {
57 const overview = repoPages.overview("abc");
58 assert.equal(overview(change("status.created", { sha: "abc" })), true);
59 assert.equal(overview(change("run.updated", { sha: "abc", run: "run_1" })), true);
60 assert.equal(overview(change("status.created", { sha: "f00d" })), false, "another commit's");
61 assert.equal(overview(change("run.updated", { sha: "f00d", run: "run_2" })), false);
62 assert.equal(overview(change("checks.completed", { sha: "f00d", number: 4 })), true, "how often checks pass");
63 assert.equal(overview(change("status.created")), true, "a check that names no commit may be its");
64});
65
66test("the files page follows refs, checks, releases and deployments", () => {
67 const files = repoPages.files();
68 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")]) {
69 assert.equal(files(c), true, c.kind);
70 }
71 assert.equal(files(change("work_item.opened", { number: 3 })), false);
72 assert.equal(files(change("comment.created", { number: 3 })), false);
73});
74
75test("a page of files hears its own branch and its commit's checks, not every branch's", () => {
76 const files = repoPages.files({ branch: "main", branches: ["main", "feature"], sha: "abc" });
77 assert.equal(files(push("main")), true, "its files and latest commit");
78 assert.equal(files(push("feature")), false, "another branch moving changes nothing here");
79 assert.equal(files(push("brand-new")), true, "a new branch: the menu and count");
80 assert.equal(files(change("git.deleted", { ref: "refs/heads/feature" })), true, "one of the menu's gone");
81 assert.equal(files(change("git.deleted", { ref: "refs/heads/never-listed" })), false);
82 assert.equal(files(change("git.push", { ref: "refs/tags/v2" })), true, "the About's tags");
83 assert.equal(files(change("branch.renamed", { ref: "refs/heads/feature" })), true);
84 assert.equal(files(change("repo.default_branch_changed")), true);
85 assert.equal(files(change("status.created", { sha: "abc" })), true, "its latest commit's checks");
86 assert.equal(files(change("check_run.completed", { sha: "f00d" })), false, "another commit's checks");
87 assert.equal(files(change("run.updated", { run: "run_1" })), true, "a check that names no commit may be its");
88 assert.equal(files(change("deployment_status.created")), true);
89 assert.equal(files(change("release.published")), true);
90 // A tag or a commit shown: no branch of its own moves it.
91 const tag = repoPages.files({ branch: null, branches: ["main"], sha: "abc" });
92 assert.equal(tag(push("main")), false);
93 // Without the branches, any branch may be new: only its own and deletions count.
94 const unknown = repoPages.files({ branch: "main", branches: null, sha: "abc" });
95 assert.equal(unknown(push("other")), false);
96 assert.equal(unknown(change("git.deleted", { ref: "refs/heads/other" })), true);
97});
98
99test("the commits page hears pushes to its own branch only", () => {
100 const commits = repoPages.commits("main");
101 assert.equal(commits(push("main")), true);
102 assert.equal(commits(push("feature")), false);
103 assert.equal(commits(change("git.push", { ref: "refs/tags/v1" })), false);
104 assert.equal(commits(change("workflow.completed", { sha: "abc", run: "run_1" })), true, "checks on its commits");
105 assert.equal(commits(change("pull.opened", { number: 1 })), false);
106 const shown = repoPages.commits("main", ["abc", "def"]);
107 assert.equal(shown(change("status.created", { sha: "def" })), true, "checks on a commit it lists");
108 assert.equal(shown(change("status.created", { sha: "f00d" })), false, "not on one it does not");
109});
110
111test("branches, tags, and the list of pull requests", () => {
112 assert.equal(repoPages.refs()(push("x")), true);
113 assert.equal(repoPages.refs()(change("pull.opened", { number: 1 })), true);
114 assert.equal(repoPages.refs()(change("work_item.opened", { number: 1 })), false);
115 assert.equal(repoPages.tags()(change("git.push", { ref: "refs/tags/v2" })), true);
116 assert.equal(repoPages.tags()(push("main")), false);
117 assert.equal(repoPages.tags()(change("release.published")), true);
118 assert.equal(repoPages.pulls()(change("pull.updated", { number: 4 })), true);
119 assert.equal(repoPages.pulls()(change("review.completed", { number: 4 })), true);
120 assert.equal(repoPages.pulls()(change("work_item.opened", { number: 5 })), false);
121});
122
123test("a pull request hears its own changes, its head branch and its head's checks", () => {
124 const pull = repoPages.pull(7, { branch: "feature", sha: "f00d" });
125 assert.equal(pull(change("pull.updated", { number: 7 })), true);
126 assert.equal(pull(change("comment.created", { number: 7 })), true);
127 assert.equal(pull(change("review.completed", { number: 7 })), true);
128 assert.equal(pull(change("checks.completed", { number: 7, sha: "f00d" })), true);
129 assert.equal(pull(change("pull.merged", { number: 7 })), true);
130 assert.equal(pull(push("feature")), true, "new commits");
131 assert.equal(pull(change("status.created", { sha: "f00d" })), true);
132 assert.equal(pull(change("run.updated", { run: "run_1", number: 7 })), true, "a run for it");
133 assert.equal(pull(change("comment.created", { number: 8 })), false);
134 assert.equal(pull(push("main")), false);
135 assert.equal(pull(change("status.created", { sha: "beef" })), false);
136});
137
138test("a pull request does not hear a work item that shares its number", () => {
139 assert.equal(repoPages.pull(3)(change("work_item.closed", { number: 3 })), false);
140});
141
142test("the Sessions page hears claims, sessions starting and ending, what the queue finds, and merges", () => {
143 const page = repoPages.work();
144 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"]) {
145 assert.equal(page(change(kind, { number: 3 })), true, kind);
146 }
147 assert.equal(page(change("session.progressed")), true, "a session going on");
148 assert.equal(page(change("session.appended", { number: 3 })), false);
149 assert.equal(page(change("comment.created", { number: 3 })), false);
150 assert.equal(page(push("main")), false);
151});
152
153test("a session going on reaches the pull request it is on, and no other", () => {
154 assert.equal(repoPages.pull(9)(change("session.progressed", { number: 9 })), true);
155 assert.equal(repoPages.pull(9)(change("session.progressed", { number: 3 })), false);
156 assert.equal(repoPages.pull(9)(change("session.progressed")), false);
157});
158
159test("a session's page hears every session, claim and overlap in the repository", () => {
160 const page = repoPages.session();
161 for (const kind of ["session.started", "session.progressed", "session.ended", "work.claimed", "work.released", "overlap.detected"]) {
162 assert.equal(page(change(kind)), true, kind);
163 }
164 assert.equal(page(change("comment.created", { number: 3 })), false);
165 assert.equal(page(change("pull.merged", { number: 3 })), false);
166 assert.equal(page(push("main")), false);
167});
168
169test("runs, one run, and security", () => {
170 assert.equal(repoPages.runs()(change("run.updated", { run: "run_1" })), true);
171 assert.equal(repoPages.runs()(change("status.created", { sha: "abc" })), true, "a new run reports pending on its commit");
172 assert.equal(repoPages.runs()(change("work_item.opened", { number: 1 })), false);
173 const run = repoPages.run("run_1", "abc");
174 assert.equal(run(change("run.updated", { run: "run_1" })), true);
175 assert.equal(run(change("workflow.completed", { run: "run_1" })), true);
176 assert.equal(run(change("run.updated", { run: "run_2" })), false);
177 assert.equal(run(change("status.created", { sha: "abc" })), true);
178 assert.equal(repoPages.security()(change("secret_scanning_alert.created")), true);
179 assert.equal(repoPages.security()(change("code_scanning_alert.fixed", { number: 2 })), true);
180 assert.equal(repoPages.security()(change("work_item.opened", { number: 1 })), false);
181});
182
183test("new commits are counted, and uncounted when a push was not", () => {
184 assert.equal(newCommits([push("main", 2), push("main", 3), push("other", 9)], "main"), 5);
185 assert.equal(newCommits([push("main", 2), push("main")], "main"), null);
186 assert.equal(newCommits([change("status.created")], "main"), 0);
187});
188
189test("a change waits a second for more, five at most, then refreshes", () => {
190 const idle = { now: NOW, visible: true, busy: false, navigating: false };
191 assert.deepEqual(gate({ pendingSince: null, lastChangeAt: null, lastRunAt: null }, idle), { action: "idle" });
192 assert.deepEqual(gate({ pendingSince: NOW, lastChangeAt: NOW, lastRunAt: null }, idle), { action: "wait", ms: LIVE_DEBOUNCE_MS });
193 assert.deepEqual(gate({ pendingSince: NOW, lastChangeAt: NOW, lastRunAt: null }, { ...idle, now: NOW + LIVE_DEBOUNCE_MS }), { action: "run" });
194 // A steady stream: refreshed after five seconds anyway.
195 const stream = { pendingSince: NOW, lastChangeAt: NOW + LIVE_MAX_WAIT_MS - 100, lastRunAt: null };
196 assert.deepEqual(gate(stream, { ...idle, now: NOW + LIVE_MAX_WAIT_MS - 50 }), { action: "wait", ms: 50 });
197 assert.deepEqual(gate(stream, { ...idle, now: NOW + LIVE_MAX_WAIT_MS }), { action: "run" });
198});
199
200test("never under someone typing, a dialog, or a navigation: it waits for them", () => {
201 const due = { pendingSince: NOW - 10_000, lastChangeAt: NOW - 10_000, lastRunAt: null };
202 for (const held of [{ busy: true, navigating: false }, { busy: false, navigating: true }]) {
203 assert.deepEqual(gate(due, { now: NOW, visible: true, ...held }), { action: "wait", ms: LIVE_HOLD_CHECK_MS });
204 }
205 assert.deepEqual(gate(due, { now: NOW, visible: true, busy: false, navigating: false }), { action: "run" });
206});
207
208test("a burst of changes refreshes at most every two seconds", () => {
209 const idle = { now: NOW, visible: true, busy: false, navigating: false };
210 // Quiet for a second, but the page refreshed half a second ago: it waits out the gap.
211 const justRan = { pendingSince: NOW - LIVE_DEBOUNCE_MS, lastChangeAt: NOW - LIVE_DEBOUNCE_MS, lastRunAt: NOW - 500 };
212 assert.deepEqual(gate(justRan, idle), { action: "wait", ms: LIVE_MIN_GAP_MS - 500 });
213 assert.deepEqual(gate(justRan, { ...idle, now: NOW - 500 + LIVE_MIN_GAP_MS }), { action: "run" });
214 // A steady stream after a refresh: one more at most five seconds on, never sooner than the gap.
215 const stream = { pendingSince: NOW - LIVE_MAX_WAIT_MS, lastChangeAt: NOW, lastRunAt: NOW - LIVE_MAX_WAIT_MS - 10 };
216 assert.deepEqual(gate(stream, idle), { action: "run" });
217 // Changes arriving every 100 ms for a minute refresh no more than once per gap.
218 let state = { pendingSince: null as number | null, lastChangeAt: null as number | null, lastRunAt: NOW };
219 let runs = 0;
220 for (let t = NOW; t < NOW + 60_000; t += 100) {
221 state = { ...state, pendingSince: state.pendingSince ?? t, lastChangeAt: t };
222 if (gate(state, { ...idle, now: t }).action === "run") {
223 runs++;
224 state = { pendingSince: null, lastChangeAt: null, lastRunAt: t };
225 }
226 }
227 assert.ok(runs <= 60_000 / LIVE_MIN_GAP_MS, `${runs} runs`);
228 assert.ok(runs >= 60_000 / LIVE_MAX_WAIT_MS - 1, `${runs} runs: still kept up to date`);
229});
230
231test("a tab out of view never refreshes; back in view, it catches up at once", () => {
232 const hidden = { now: NOW, visible: false, busy: false, navigating: false };
233 const waiting = { pendingSince: NOW - 5000, lastChangeAt: NOW - 5000, lastRunAt: NOW - 20 * 60_000 };
234 assert.deepEqual(gate(waiting, hidden), { action: "hold" }, "no timer: coming back asks again");
235 assert.deepEqual(gate({ pendingSince: null, lastChangeAt: null, lastRunAt: null }, hidden), { action: "idle" });
236 assert.deepEqual(gate(waiting, { ...hidden, visible: true }), { action: "run" });
237});
238
239test("without the socket: back in view every 30 s at most, a poll every minute in view, never with it", () => {
240 const base = { live: false, visible: true, now: NOW };
241 assert.equal(fallbackDue({ ...base, lastRunAt: null, reason: "focus" }), true);
242 assert.equal(fallbackDue({ ...base, lastRunAt: NOW - FALLBACK_FOCUS_MS + 1, reason: "focus" }), false);
243 assert.equal(fallbackDue({ ...base, lastRunAt: NOW - FALLBACK_FOCUS_MS, reason: "focus" }), true);
244 assert.equal(fallbackDue({ ...base, lastRunAt: NOW - FALLBACK_FOCUS_MS, reason: "poll" }), false);
245 assert.equal(fallbackDue({ ...base, lastRunAt: NOW - FALLBACK_POLL_MS, reason: "poll" }), true);
246 assert.equal(fallbackDue({ ...base, visible: false, lastRunAt: null, reason: "poll" }), false, "a hidden tab is not polled");
247 assert.equal(fallbackDue({ ...base, live: true, lastRunAt: null, reason: "focus" }), false);
248});
249
250test("typing is in text boxes, text areas and editors", () => {
251 assert.equal(isTyping({ tagName: "TEXTAREA" }), true);
252 assert.equal(isTyping({ tagName: "input", type: "text" }), true);
253 assert.equal(isTyping({ tagName: "INPUT", type: "search" }), true);
254 assert.equal(isTyping({ tagName: "INPUT" }), true, "an input is text unless it says otherwise");
255 assert.equal(isTyping({ tagName: "DIV", isContentEditable: true }), true);
256 assert.equal(isTyping({ tagName: "INPUT", type: "checkbox" }), false);
257 assert.equal(isTyping({ tagName: "BUTTON" }), false);
258 assert.equal(isTyping({ tagName: "TEXTAREA", readOnly: true }), false);
259 assert.equal(isTyping(null), false);
260});
261
262test("the bottom of the page, with some slack", () => {
263 assert.equal(atBottom({ top: 880, height: 2000, viewport: 1000 }), true);
264 assert.equal(atBottom({ top: 500, height: 2000, viewport: 1000 }), false);
265});