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.

278 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { ChatSidebarEntry, FeedCounts, FeedNotification } from "@g1t/contracts";
5
6import {
7 heldOpen,
8 MAX_TOASTS,
9 RECENT_KEPT,
10 WAITING_MS,
11 addRecent,
12 addToast,
13 notificationActions,
14 waitingCards,
15 attentionCount,
16 badgesOf,
17 canQuickReply,
18 dismissToast,
19 hiddenToFit,
20 isViewing,
21 markReadLocally,
22 offerPush,
23 overlayEntries,
24 quickReplyRequest,
25 reconnectDelay,
26 titleWith,
27 unlisted,
28} from "./notify-store.ts";
29
30const note = (id: string, over: Partial<FeedNotification> = {}): FeedNotification => ({
31 id,
32 kind: "dm",
33 workspace: "acme",
34 title: "Ana Lima",
35 body: "ship it?",
36 href: "/acme/-/chat/dm/chn_1",
37 actor: { kind: "user", id: "u1", name: "Ana Lima" },
38 channel_id: "chn_1",
39 created_at: "2026-10-08T00:00:00Z",
40 ...over,
41});
42const away = { path: "/acme/api" };
43
44test("toasts: newest last, at most three, never twice", () => {
45 let toasts = addToast([], note("1", { channel_id: "a", href: "/acme/-/chat/a" }), away, 1);
46 toasts = addToast(toasts, note("1", { channel_id: "a", href: "/acme/-/chat/a" }), away, 2);
47 assert.equal(toasts.length, 1);
48 for (const id of ["2", "3", "4"]) toasts = addToast(toasts, note(id, { channel_id: `c${id}`, href: `/acme/-/chat/c${id}` }), away, Number(id));
49 assert.equal(toasts.length, MAX_TOASTS);
50 assert.deepEqual(
51 toasts.map((t) => t.notification.id),
52 ["2", "3", "4"],
53 );
54 assert.deepEqual(
55 dismissToast(toasts, "3").map((t) => t.notification.id),
56 ["2", "4"],
57 );
58});
59
60test("a newer message in the same conversation replaces its toast", () => {
61 let toasts = addToast([], note("1"), away, 1);
62 toasts = addToast(toasts, note("2"), away, 2);
63 assert.deepEqual(
64 toasts.map((t) => t.notification.id),
65 ["2"],
66 );
67});
68
69test("no toast for the conversation on screen, by page or by channel; inbox items always", () => {
70 assert.deepEqual(addToast([], note("1"), { path: "/acme/-/chat/dm/chn_1/" }, 1), []);
71 assert.deepEqual(addToast([], note("1", { href: "/acme/-/chat/ops?thread=m1", channel_id: "c9" }), { path: "/ACME/-/chat/ops" }, 1), []);
72 assert.ok(isViewing(note("1"), { path: "/elsewhere", channel_id: "chn_1" }));
73 assert.equal(addToast([], note("1", { kind: "inbox", channel_id: null, href: "/acme/api/pull/4" }), { path: "/acme/api/pull/4" }, 1).length, 1);
74});
75
76test("no toast for a conversation another surface shows: g1t's answer on Home, the Ask panel", () => {
77 const home = { path: "/acme", shown: new Set(["chn_1"]) };
78 assert.ok(isViewing(note("1"), home));
79 assert.deepEqual(addToast([], note("1"), home, 1), []);
80 assert.equal(addToast([], note("2", { channel_id: "chn_2", href: "/acme/-/chat/dm/chn_2" }), home, 1).length, 1, "another conversation still toasts");
81 assert.equal(addToast([], note("1"), { path: "/acme" }, 1).length, 1, "once the card is gone, it toasts again");
82});
83
84test("quick replies post to the conversation, in its thread when there is one", () => {
85 assert.ok(canQuickReply(note("1")));
86 assert.ok(!canQuickReply(note("1", { kind: "inbox" })));
87 assert.ok(!canQuickReply(note("1", { channel_id: null })));
88 assert.equal(quickReplyRequest(note("1"), " "), null);
89 assert.deepEqual(quickReplyRequest(note("1", { kind: "mention", thread_root: "m1" }), " on it "), {
90 url: "/acme/-/chat/api",
91 body: { intent: "post", channel_id: "chn_1", body: "on it", thread_root: "m1" },
92 });
93});
94
95const counts = (over: Partial<FeedCounts> = {}): FeedCounts => ({
96 workspace: "acme",
97 chat_unread: 4,
98 chat_mentions: 1,
99 inbox_unread: 2,
100 per_channel: [
101 { channel_id: "a", unread: 3, mentions: 1 },
102 { channel_id: "b", unread: 1, mentions: 0 },
103 ],
104 complete: true,
105 ...over,
106});
107
108test("badges: chat only once the feed's counts are complete; the inbox whenever known", () => {
109 assert.deepEqual(badgesOf(counts(), null), { chat: 4, mentions: 1, inbox: 2 });
110 assert.deepEqual(badgesOf(counts({ complete: false }), null), { inbox: 2 });
111 assert.deepEqual(badgesOf(null, 7), { inbox: 7 });
112 assert.equal(badgesOf(null, null), null);
113 assert.equal(attentionCount({ chat: 4, mentions: 1, inbox: 2 }), 6);
114 assert.equal(attentionCount({ chat: 0, mentions: 2 }), 2);
115 assert.equal(attentionCount(null), 0);
116});
117
118test("reading a conversation here zeroes it at once", () => {
119 const after = markReadLocally(counts(), "a");
120 assert.equal(after.chat_unread, 1);
121 assert.equal(after.chat_mentions, 0);
122 assert.deepEqual(
123 after.per_channel.map((c) => c.channel_id),
124 ["b"],
125 );
126 assert.equal(markReadLocally(counts(), "zzz").chat_unread, 4);
127});
128
129const entry = (id: string, unread: number, mentions = 0): ChatSidebarEntry =>
130 ({
131 channel: {
132 id,
133 workspace_id: "w",
134 kind: "channel",
135 name: id,
136 topic: null,
137 private: false,
138 created_by: { kind: "user", id: "u" },
139 created_at: "",
140 archived_at: null,
141 last_message_at: null,
142 },
143 title: id,
144 others: [],
145 starred: false,
146 muted: false,
147 unread,
148 mentions,
149 }) as ChatSidebarEntry;
150
151test("the sidebar takes the feed's counts once they are complete, and spots conversations it does not list", () => {
152 const entries = [entry("a", 0), entry("b", 5), entry("c", 2)];
153 const shown = overlayEntries(entries, counts());
154 assert.deepEqual(
155 shown.map((e) => [e.unread, e.mentions]),
156 [
157 [3, 1],
158 [1, 0],
159 [0, 0],
160 ],
161 );
162 assert.equal(overlayEntries(entries, counts({ complete: false })), entries);
163 const same = [entry("a", 3, 1), entry("b", 1)];
164 assert.equal(overlayEntries(same, counts()), same);
165 assert.deepEqual(unlisted([entry("a", 0)], counts()), ["b"]);
166});
167
168test("the tab title counts what needs you, once", () => {
169 assert.equal(titleWith("Chat · g1t", 3), "(3) Chat · g1t");
170 assert.equal(titleWith("(3) Chat · g1t", 5), "(5) Chat · g1t");
171 assert.equal(titleWith("(5) Chat · g1t", 0), "Chat · g1t");
172 assert.equal(titleWith("Inbox", 250), "(99+) Inbox");
173 assert.equal(titleWith("(99+) Inbox", 1), "(1) Inbox");
174});
175
176test("browser notifications are offered once, after a DM or mention, never after a no", () => {
177 const base = { kind: "dm" as const, supported: true, permission: "default" as const, choice: null, hasKey: true, desktop: false };
178 assert.ok(offerPush(base));
179 assert.ok(offerPush({ ...base, kind: "mention" }));
180 assert.ok(!offerPush({ ...base, kind: "inbox" }));
181 assert.ok(!offerPush({ ...base, choice: "declined" }));
182 assert.ok(!offerPush({ ...base, choice: "off" }));
183 assert.ok(!offerPush({ ...base, permission: "denied" }));
184 assert.ok(!offerPush({ ...base, permission: "granted" }));
185 assert.ok(!offerPush({ ...base, supported: false }));
186 assert.ok(!offerPush({ ...base, hasKey: false }));
187 assert.ok(!offerPush({ ...base, desktop: true }));
188});
189
190test("reconnecting waits a jittered, growing time, never past half a minute", () => {
191 assert.equal(
192 reconnectDelay(0, () => 0),
193 500,
194 );
195 assert.equal(
196 reconnectDelay(0, () => 1),
197 1000,
198 );
199 assert.equal(
200 reconnectDelay(3, () => 1),
201 8000,
202 );
203 assert.equal(
204 reconnectDelay(50, () => 1),
205 30_000,
206 );
207 const spread = new Set(Array.from({ length: 20 }, () => reconnectDelay(4)));
208 assert.ok(spread.size > 1);
209});
210
211test("a stack taller than the screen folds its oldest toasts into a pill, never the newest", () => {
212 // 3 toasts of 120 and 2 gaps: 376.
213 assert.equal(hiddenToFit([120, 120, 120], 400), 0);
214 // Too tall: the oldest folds; 2 toasts, the pill (30) and 2 gaps = 286.
215 assert.equal(hiddenToFit([120, 120, 120], 300), 1);
216 assert.equal(hiddenToFit([120, 120, 120], 200), 2);
217 // However small the screen, the newest stays.
218 assert.equal(hiddenToFit([120, 120, 120], 10), 2);
219 assert.equal(hiddenToFit([], 10), 0);
220 // The offer always shows and takes its room first.
221 assert.equal(hiddenToFit([100, 100], 276, 60), 0);
222 assert.equal(hiddenToFit([100, 100], 275, 60), 1);
223});
224
225const capCard = {
226 channel_id: "chn_9",
227 message_id: "msg_card",
228 actions: [
229 { id: "approve", label: "Approve more", style: "primary" as const, input: { kind: "money" as const, label: "New cap" } },
230 { id: "stop", label: "Stop", style: "danger" as const, confirm: "Stop this session?" },
231 { id: "open", label: "Open", href: "/acme/web/pull/4" },
232 ],
233};
234
235test("a card's notification offers its links alone: nothing answers its other buttons", () => {
236 const n = note("approval:ses_1:2000000", { kind: "approval", channel_id: null, card: capCard });
237 assert.deepEqual(
238 notificationActions(n).map((a) => a.id),
239 ["open"],
240 );
241 assert.deepEqual(notificationActions(note("m1")), []);
242});
243
244test("recent notifications are kept newest first, once each", () => {
245 const a = note("a", { created_at: "2026-10-08T00:00:01Z" });
246 const b = note("b", { created_at: "2026-10-08T00:00:02Z" });
247 assert.deepEqual(
248 addRecent([a], [b, a]).map((n) => n.id),
249 ["b", "a"],
250 );
251 const many = Array.from({ length: RECENT_KEPT + 5 }, (_, i) => note(`n${i}`, { created_at: new Date(Date.UTC(2026, 9, 8, 0, 0, i)).toISOString() }));
252 assert.equal(addRecent([], many).length, RECENT_KEPT);
253});
254
255test("the panel waits on the newest notification per card, for a day, until it is acted on", () => {
256 const now = Date.parse("2026-10-09T12:00:00Z");
257 const older = note("approval:ses_1:1000000", { kind: "approval", card: capCard, created_at: "2026-10-09T10:00:00Z" });
258 const newer = note("approval:ses_1:2000000", { kind: "approval", card: capCard, created_at: "2026-10-09T11:00:00Z" });
259 const draft = note("msg_d", { kind: "approval", workspace: "side", card: { ...capCard, message_id: "msg_draft" }, created_at: "2026-10-09T11:30:00Z" });
260 const stale = note("msg_s", { kind: "approval", card: { ...capCard, message_id: "msg_stale" }, created_at: new Date(now - WAITING_MS - 1).toISOString() });
261 const recent = addRecent([], [older, newer, draft, stale, note("plain")]);
262 assert.deepEqual(
263 waitingCards(recent, new Set(), now).map((n) => n.id),
264 ["msg_d", "approval:ses_1:2000000"],
265 );
266 assert.deepEqual(
267 waitingCards(recent, new Set(), now, "Acme").map((n) => n.id),
268 ["approval:ses_1:2000000"],
269 );
270 // Acting on the newest puts the card away; the older one about it does not come back.
271 assert.deepEqual(waitingCards(recent, new Set(["approval:ses_1:2000000"]), now, "acme"), []);
272});
273
274test("the backoff starts over only after a connection held for a while", () => {
275 assert.equal(heldOpen(null, 50_000), false);
276 assert.equal(heldOpen(45_000, 50_000), false, "dropped within seconds: keep backing off");
277 assert.equal(heldOpen(40_000, 50_000), true);
278});