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.

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