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.

227 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { FeedNotification } from "@g1t/contracts";
5
6import { applyCounts, totals } from "./counts.ts";
7import {
8 STALE_MS,
9 anyFocused,
10 cleanCard,
11 cleanNotification,
12 decide,
13 levelFor,
14 mergePreferences,
15 pushActions,
16 pushPayload,
17 readPreferences,
18 settledThreads,
19 wants,
20} from "./prefs.ts";
21
22const NOW = 1_800_000_000_000;
23const dms = { level: "dms_mentions" as const, workspaces: {} };
24
25test("the default hears of what is said to you or waits on you, not everything", () => {
26 assert.deepEqual(readPreferences(null), dms);
27 for (const kind of ["dm", "mention", "thread_reply", "approval"] as const) assert.ok(wants("dms_mentions", kind), kind);
28 assert.ok(!wants("dms_mentions", "inbox"));
29 assert.ok(wants("all", "inbox"));
30 for (const kind of ["dm", "mention", "inbox"] as const) assert.ok(!wants("none", kind));
31});
32
33test("a workspace's own level wins over the general one", () => {
34 const prefs = mergePreferences(dms, { workspaces: { Acme: "none", side: "all" } });
35 assert.equal(levelFor(prefs, "acme"), "none");
36 assert.equal(levelFor(prefs, "side"), "all");
37 assert.equal(levelFor(prefs, "other"), "dms_mentions");
38});
39
40test("changes are checked: unknown levels ignored, null clears, the same as the general level is no override", () => {
41 let prefs = mergePreferences(dms, { level: "loud", workspaces: { a: "all", b: "bogus" } });
42 assert.deepEqual(prefs, { level: "dms_mentions", workspaces: { a: "all" } });
43 prefs = mergePreferences(prefs, { workspaces: { a: null } });
44 assert.deepEqual(prefs.workspaces, {});
45 prefs = mergePreferences(mergePreferences(dms, { workspaces: { a: "all" } }), { level: "all" });
46 assert.deepEqual(prefs, { level: "all", workspaces: {} });
47 assert.deepEqual(readPreferences("not json"), dms);
48 assert.deepEqual(mergePreferences(dms, "nonsense"), dms);
49});
50
51test("a tab counts as in front of you only while focused and recently heard from", () => {
52 assert.ok(anyFocused([{ focused: true, seen_at: NOW - 1000 }], NOW));
53 assert.ok(!anyFocused([{ focused: true, seen_at: NOW - STALE_MS - 1 }], NOW));
54 assert.ok(!anyFocused([{ focused: false, seen_at: NOW }], NOW));
55 assert.ok(!anyFocused([], NOW));
56});
57
58test("push only when no tab is focused, there is a browser to push to, and the level wants it", () => {
59 const dm = { kind: "dm" as const, workspace: "acme" };
60 const away = [{ focused: false, seen_at: NOW }];
61 const here = [{ focused: true, seen_at: NOW }];
62 assert.deepEqual(decide({ prefs: dms, notification: dm, tabs: away, subscriptions: 1, now: NOW }), { toast: true, push: true });
63 assert.deepEqual(decide({ prefs: dms, notification: dm, tabs: here, subscriptions: 1, now: NOW }), { toast: true, push: false });
64 assert.deepEqual(decide({ prefs: dms, notification: dm, tabs: [], subscriptions: 0, now: NOW }), { toast: true, push: false });
65 // No tab open at all: pushed.
66 assert.deepEqual(decide({ prefs: dms, notification: dm, tabs: [], subscriptions: 2, now: NOW }), { toast: true, push: true });
67 // A focused tab gone quiet (the laptop slept): pushed.
68 assert.equal(decide({ prefs: dms, notification: dm, tabs: [{ focused: true, seen_at: NOW - STALE_MS * 2 }], subscriptions: 1, now: NOW }).push, true);
69 const inbox = { kind: "inbox" as const, workspace: "acme" };
70 assert.deepEqual(decide({ prefs: dms, notification: inbox, tabs: away, subscriptions: 1, now: NOW }), { toast: false, push: false });
71 const muted = mergePreferences(dms, { workspaces: { acme: "none" } });
72 assert.deepEqual(decide({ prefs: muted, notification: dm, tabs: away, subscriptions: 1, now: NOW }), { toast: false, push: false });
73 // A test goes through whatever the preferences and focus.
74 assert.deepEqual(decide({ prefs: muted, notification: dm, tabs: here, subscriptions: 1, now: NOW, test: true }), { toast: true, push: true });
75});
76
77test("notifications are cleaned: kinds checked, links kept on the site, text trimmed", () => {
78 assert.equal(cleanNotification({ id: "x", kind: "shout", title: "t" }), null);
79 // Workspace agents are gone: what they once sent is no notification now.
80 assert.equal(cleanNotification({ id: "x", kind: "agent_waiting", title: "t" }), null);
81 assert.equal(cleanNotification({ kind: "dm", title: "t" }), null);
82 const n = cleanNotification({
83 id: "n1",
84 kind: "mention",
85 workspace: "Acme",
86 title: " Ana in #design ",
87 body: "x".repeat(1000),
88 href: "//evil.example/",
89 actor: { kind: "user", id: "usr_1", name: "Ana", avatar: "abc" },
90 channel_id: "chn_1",
91 })!;
92 assert.equal(n.workspace, "acme");
93 assert.equal(n.title, "Ana in #design");
94 assert.equal(n.body.length, 300);
95 assert.equal(n.href, "/");
96 assert.equal(n.actor.avatar, "abc");
97 assert.equal(n.actor.avatar_seed, null);
98 assert.equal(cleanNotification({ id: "n2", kind: "dm", title: "t", href: "/acme/-/chat/dm/chn_1", actor: { kind: "robot" } })!.actor.kind, "system");
99 // An inbox item keeps its thread, so it stops being replayed once it settles.
100 assert.equal(cleanNotification({ id: "inbox:e1", kind: "inbox", title: "t", inbox_thread: "rep_1#7" })!.inbox_thread, "rep_1#7");
101 assert.equal("inbox_thread" in cleanNotification({ id: "n3", kind: "dm", title: "t" })!, false);
102});
103
104test("settled threads are strings, each once, and bounded", () => {
105 assert.deepEqual(settledThreads(["rep_1#7", "rep_1#7", 3, "", "rep_1/deploy/*/7"]), ["rep_1#7", "rep_1/deploy/*/7"]);
106 assert.deepEqual(settledThreads("rep_1#7"), []);
107 assert.deepEqual(settledThreads(undefined), []);
108 assert.equal(settledThreads(Array.from({ length: 500 }, (_, i) => `t${i}`)).length, 200);
109});
110
111test("a push is small and collapses by conversation", () => {
112 const n: FeedNotification = {
113 id: "msg_1",
114 kind: "dm",
115 workspace: "acme",
116 title: "Ana",
117 body: "y".repeat(400),
118 href: "/acme/-/chat/dm/chn_1",
119 actor: { kind: "user", id: "usr_1", name: "Ana" },
120 channel_id: "chn_1",
121 created_at: "2026-10-08T00:00:00Z",
122 };
123 const payload = pushPayload(n);
124 assert.equal(payload.tag, "chat:chn_1");
125 assert.equal(payload.body.length, 240);
126 assert.equal(payload.urgent, true);
127 assert.equal(pushPayload({ ...n, kind: "inbox", channel_id: null }).tag, "inbox:msg_1");
128});
129
130test("counts add up, replace on set, never go below zero, and keep mute", () => {
131 let c = applyCounts(null, { channel_id: "c", unread: 1, mentions: 0 });
132 c = applyCounts(c, { channel_id: "c", unread: 1, mentions: 1, muted: true });
133 assert.deepEqual(c, { channel_id: "c", unread: 2, mentions: 1, muted: true });
134 c = applyCounts(c, { channel_id: "c", unread: -5, mentions: -5 });
135 assert.deepEqual(c, { channel_id: "c", unread: 0, mentions: 0, muted: true });
136 c = applyCounts(c, { channel_id: "c", unread: 3, mentions: 0, set: true });
137 assert.deepEqual(c, { channel_id: "c", unread: 3, mentions: 0, muted: true });
138});
139
140test("totals leave muted unread out, as the rail does, but count their mentions", () => {
141 const t = totals(
142 "acme",
143 [
144 { channel_id: "a", unread: 3, mentions: 1, muted: false },
145 { channel_id: "b", unread: 5, mentions: 2, muted: true },
146 { channel_id: "c", unread: 0, mentions: 0, muted: false },
147 ],
148 4,
149 true,
150 );
151 assert.deepEqual(t, {
152 workspace: "acme",
153 chat_unread: 3,
154 chat_mentions: 3,
155 inbox_unread: 4,
156 per_channel: [
157 { channel_id: "a", unread: 3, mentions: 1 },
158 { channel_id: "b", unread: 5, mentions: 2 },
159 ],
160 complete: true,
161 });
162});
163
164const capped = {
165 channel_id: "chn_1",
166 message_id: "msg_card",
167 actions: [
168 { id: "approve", label: "Approve more", style: "primary", input: { kind: "money", label: "New cap", initial: "4.00" } },
169 { id: "stop", label: "Stop", style: "danger", confirm: "Stop this session and everything under it?" },
170 { id: "open", label: "Open", href: "/acme/web/pull/1" },
171 ],
172};
173
174test("a notification's card is checked: its place, its actions, links kept on the site", () => {
175 const card = cleanCard(capped)!;
176 assert.equal(card.message_id, "msg_card");
177 assert.deepEqual(
178 card.actions.map((a) => [a.id, a.style, a.input?.kind ?? null, a.href ?? null]),
179 [
180 ["approve", "primary", "money", null],
181 ["stop", "danger", null, null],
182 ["open", "default", null, "/acme/web/pull/1"],
183 ],
184 );
185 assert.equal(card.actions[1].confirm, "Stop this session and everything under it?");
186 assert.equal(cleanCard({ ...capped, actions: [{ id: "x", label: "X", href: "https://evil.example/" }] })!.actions[0].href, null);
187 assert.equal(cleanCard({ ...capped, message_id: "" }), null);
188 assert.equal(cleanCard({ ...capped, actions: [{ id: "", label: "" }] }), null);
189 assert.equal(cleanCard("nope"), null);
190 // Carried on the notification when it is one; left off otherwise.
191 const n = cleanNotification({ id: "approval:ses_1:2000000", kind: "approval", workspace: "acme", title: "g1t needs more budget", card: capped })!;
192 assert.equal(n.card?.actions.length, 3);
193 assert.equal("card" in cleanNotification({ id: "n", kind: "dm", title: "t" })!, false);
194});
195
196test("a push shows only the card's actions that need nothing typed or confirmed, two at most", () => {
197 const card = cleanCard(capped)!;
198 // Stop asks first in the app; a push can't, so it isn't offered there.
199 assert.deepEqual(pushActions(card), [{ id: "open", label: "Open", href: "/acme/web/pull/1" }]);
200 assert.deepEqual(pushActions(null), []);
201 const payload = pushPayload({
202 id: "approval:ses_1:2000000",
203 kind: "approval",
204 workspace: "acme",
205 title: "g1t needs more budget",
206 body: "",
207 href: "/acme/web/pull/1",
208 actor: { kind: "agent", id: "a1", name: "g1t" },
209 channel_id: "chn_1",
210 card,
211 created_at: "2026-10-08T00:00:00Z",
212 });
213 assert.deepEqual(payload.card, { channel_id: "chn_1", message_id: "msg_card" });
214 assert.equal(payload.workspace, "acme");
215 // Its own notification: a later message in the conversation does not replace it.
216 assert.equal(payload.tag, "approval:approval:ses_1:2000000");
217});
218
219test("a person with only the phone app is pushed to: the feed counts browsers and phones together", () => {
220 const dm = { kind: "dm" as const, workspace: "acme" };
221 const browsers = 0;
222 const phones = 1;
223 assert.deepEqual(decide({ prefs: dms, notification: dm, tabs: [], subscriptions: browsers + phones, now: NOW }), { toast: true, push: true });
224 // The same rules hold: not while a tab is in front of them, nor in Do Not Disturb.
225 assert.equal(decide({ prefs: dms, notification: dm, tabs: [{ focused: true, seen_at: NOW }], subscriptions: browsers + phones, now: NOW }).push, false);
226 assert.equal(decide({ prefs: dms, notification: dm, tabs: [], subscriptions: browsers + phones, now: NOW, dnd: true }).push, false);
227});