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