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.

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