Skip to content
137 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, cleanNotification, decide, levelFor, mergePreferences, pushPayload, readPreferences, 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", "agent_waiting", "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 assert.equal(cleanNotification({ kind: "dm", title: "t" }), null);
67 const n = cleanNotification({
68 id: "n1",
69 kind: "mention",
70 workspace: "Acme",
71 title: " Ana in #design ",
72 body: "x".repeat(1000),
73 href: "//evil.example/",
74 actor: { kind: "user", id: "usr_1", name: "Ana", avatar: "abc" },
75 channel_id: "chn_1",
76 })!;
77 assert.equal(n.workspace, "acme");
78 assert.equal(n.title, "Ana in #design");
79 assert.equal(n.body.length, 300);
80 assert.equal(n.href, "/");
81 assert.equal(n.actor.avatar, "abc");
82 assert.equal(n.actor.avatar_seed, null);
83 assert.equal(cleanNotification({ id: "n2", kind: "dm", title: "t", href: "/acme/-/chat/dm/chn_1", actor: { kind: "robot" } })!.actor.kind, "system");
84});
85
86test("a push is small and collapses by conversation", () => {
87 const n: FeedNotification = {
88 id: "msg_1",
89 kind: "dm",
90 workspace: "acme",
91 title: "Ana",
92 body: "y".repeat(400),
93 href: "/acme/-/chat/dm/chn_1",
94 actor: { kind: "user", id: "usr_1", name: "Ana" },
95 channel_id: "chn_1",
96 created_at: "2026-10-08T00:00:00Z",
97 };
98 const payload = pushPayload(n);
99 assert.equal(payload.tag, "chat:chn_1");
100 assert.equal(payload.body.length, 240);
101 assert.equal(payload.urgent, true);
102 assert.equal(pushPayload({ ...n, kind: "inbox", channel_id: null }).tag, "inbox:msg_1");
103});
104
105test("counts add up, replace on set, never go below zero, and keep mute", () => {
106 let c = applyCounts(null, { channel_id: "c", unread: 1, mentions: 0 });
107 c = applyCounts(c, { channel_id: "c", unread: 1, mentions: 1, muted: true });
108 assert.deepEqual(c, { channel_id: "c", unread: 2, mentions: 1, muted: true });
109 c = applyCounts(c, { channel_id: "c", unread: -5, mentions: -5 });
110 assert.deepEqual(c, { channel_id: "c", unread: 0, mentions: 0, muted: true });
111 c = applyCounts(c, { channel_id: "c", unread: 3, mentions: 0, set: true });
112 assert.deepEqual(c, { channel_id: "c", unread: 3, mentions: 0, muted: true });
113});
114
115test("totals leave muted unread out, as the rail does, but count their mentions", () => {
116 const t = totals(
117 "acme",
118 [
119 { channel_id: "a", unread: 3, mentions: 1, muted: false },
120 { channel_id: "b", unread: 5, mentions: 2, muted: true },
121 { channel_id: "c", unread: 0, mentions: 0, muted: false },
122 ],
123 4,
124 true,
125 );
126 assert.deepEqual(t, {
127 workspace: "acme",
128 chat_unread: 3,
129 chat_mentions: 3,
130 inbox_unread: 4,
131 per_channel: [
132 { channel_id: "a", unread: 3, mentions: 1 },
133 { channel_id: "b", unread: 5, mentions: 2 },
134 ],
135 complete: true,
136 });
137});