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