Skip to content
229 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { ChatMessage, ChatSidebarEntry } from "@g1t/contracts";
5
6import {
7 backoff,
8 blocks,
9 channelName,
10 channelPath,
11 dayLabel,
12 dmPlaceholder,
13 filterEntries,
14 inline,
15 mixedDm,
16 mixedNotice,
17 mentionNames,
18 mentionQuery,
19 mergeMessages,
20 safeHref,
21 sections,
22 shownHandle,
23 shownName,
24 timeline,
25 typingLabel,
26 unreadTotals,
27 WORKING_AFTER_MS,
28 agentDoing,
29 type ShownMessage,
30} from "./chat.ts";
31
32const ana = { kind: "user" as const, id: "u1", name: "ana", display_name: "Ana", avatar: null, role: null };
33const bot = { kind: "agent" as const, id: "a1", name: "reviewer", display_name: "Reviewer", avatar: null, role: "Reviews pull requests" };
34
35function message(id: string, at: string, over: Partial<ChatMessage> = {}): ShownMessage {
36 return {
37 id,
38 channel_id: "c1",
39 author: ana,
40 kind: "text",
41 body: "hi",
42 card: null,
43 thread_root: null,
44 reply_count: 0,
45 last_reply_at: null,
46 created_at: at,
47 edited_at: null,
48 deleted_at: null,
49 ...over,
50 };
51}
52
53test("messages from one author close together share a header; days get a rule", () => {
54 const now = new Date("2026-10-08T15:00:00Z");
55 const rows = timeline(
56 [
57 message("01", "2026-10-07T10:00:00Z"),
58 message("02", "2026-10-08T10:00:00Z"),
59 message("03", "2026-10-08T10:02:00Z"),
60 message("04", "2026-10-08T10:03:00Z", { author: bot }),
61 message("05", "2026-10-08T10:30:00Z", { author: bot }),
62 message("06", "2026-10-08T10:31:00Z", { author: bot, kind: "card" }),
63 message("07", "2026-10-08T10:32:00Z", { deleted_at: "2026-10-08T10:33:00Z" }),
64 ],
65 now,
66 "UTC",
67 );
68 assert.deepEqual(
69 rows.map((row) => (row.kind === "day" ? row.label : `${row.message.id}${row.head ? "*" : ""}`)),
70 ["Yesterday", "01*", "Today", "02*", "03", "04*", "05*", "06*"],
71 );
72 assert.equal(dayLabel("2026-09-01T10:00:00Z", now, "UTC"), "Tuesday, September 1");
73});
74
75test("merging keeps one copy of each message, and drops the pending one once saved", () => {
76 const pending = { ...message("tmp-1", "2026-10-08T10:00:00Z"), pending: true, client_id: "tmp-1" };
77 const saved = { ...message("05", "2026-10-08T10:00:01Z"), client_id: "tmp-1" };
78 const merged = mergeMessages([message("02", "2026-10-08T09:00:00Z"), pending], [saved, message("02", "2026-10-08T09:00:00Z", { body: "edited" })]);
79 assert.deepEqual(merged.map((m) => m.id), ["02", "05"]);
80 assert.equal(merged[0]!.body, "edited");
81 // Still pending: last, after everything saved.
82 assert.deepEqual(mergeMessages([pending], [message("09", "2026-10-08T11:00:00Z")]).map((m) => m.id), ["09", "tmp-1"]);
83});
84
85test("an @ being typed suggests people and agents", () => {
86 const people = [
87 { kind: "user" as const, name: "ana", display_name: "Ana Lima", avatar: null },
88 { kind: "agent" as const, name: "reviewer", display_name: "Reviewer", avatar: null },
89 { kind: "user" as const, name: "bo", display_name: "Robert Reed", avatar: null },
90 ];
91 assert.deepEqual(mentionQuery("hey @re", 7, people)?.options.map((p) => p.name), ["reviewer", "bo"]);
92 assert.equal(mentionQuery("hey @re", 7, people)?.start, 4);
93 assert.equal(mentionQuery("mail me@an", 10, people), null);
94 assert.deepEqual(mentionQuery("@", 1, people)?.options.length, 3);
95 assert.equal(mentionQuery("@zz", 3, people), null);
96});
97
98test("message text: mentions, references, links and code, never HTML", () => {
99 assert.deepEqual(inline("ask @reviewer about #12 and g1t#3"), [
100 { t: "text", v: "ask " },
101 { t: "mention", name: "reviewer" },
102 { t: "text", v: " about " },
103 { t: "ref", repo: null, number: 12 },
104 { t: "text", v: " and " },
105 { t: "ref", repo: "g1t", number: 3 },
106 ]);
107 assert.deepEqual(inline("see #general, `a <b>` **bold**"), [
108 { t: "text", v: "see " },
109 { t: "channel", name: "general" },
110 { t: "text", v: ", " },
111 { t: "code", v: "a <b>" },
112 { t: "text", v: " " },
113 { t: "strong", c: [{ t: "text", v: "bold" }] },
114 ]);
115 assert.ok(inline("[x](javascript:alert(1))").every((span) => span.t === "text"));
116 assert.deepEqual(inline("https://g1t.sh/a."), [{ t: "link", href: "https://g1t.sh/a", c: [{ t: "text", v: "https://g1t.sh/a" }] }, { t: "text", v: "." }]);
117 assert.deepEqual(inline("me@example.com"), [{ t: "text", v: "me@example.com" }]);
118 assert.equal(safeHref("//evil.example"), null);
119 assert.equal(safeHref("/acme/web/pull/3"), "/acme/web/pull/3");
120});
121
122test("message blocks: fenced code, lists, quotes and paragraphs", () => {
123 const parsed = blocks("Plan:\n- one\n- two\n\n```ts\nconst a = 1;\n```\n> quoted\nlast");
124 assert.deepEqual(parsed.map((block) => block.t), ["p", "list", "code", "quote", "p"]);
125 assert.deepEqual(parsed[2], { t: "code", lang: "ts", v: "const a = 1;" });
126});
127
128function entry(id: string, over: Partial<ChatSidebarEntry> & { kind?: "channel" | "dm"; last?: string } = {}): ChatSidebarEntry {
129 return {
130 channel: {
131 id,
132 workspace_id: "w",
133 kind: over.kind ?? "channel",
134 name: over.kind === "dm" ? null : id,
135 topic: null,
136 private: false,
137 created_by: { kind: "user", id: "u1" },
138 created_at: "2026-10-01T00:00:00Z",
139 archived_at: null,
140 last_message_at: over.last ?? null,
141 },
142 title: over.title ?? id,
143 others: over.others ?? [],
144 starred: over.starred ?? false,
145 muted: over.muted ?? false,
146 unread: over.unread ?? 0,
147 mentions: over.mentions ?? 0,
148 };
149}
150
151test("the sidebar: sections, filters and the rail's count", () => {
152 const all = [
153 entry("random"),
154 entry("general", { starred: true, unread: 2 }),
155 entry("d1", { kind: "dm", title: "reviewer", others: [bot], last: "2026-10-02T00:00:00Z", mentions: 1, unread: 1 }),
156 entry("d2", { kind: "dm", title: "ana", others: [ana], last: "2026-10-05T00:00:00Z" }),
157 entry("noisy", { muted: true, unread: 9 }),
158 // Ana and the reviewer together: a person is in it, so it lists under People, marked as mixed.
159 entry("d3", { kind: "dm", title: "ana, reviewer", others: [ana, bot], last: "2026-10-03T00:00:00Z" }),
160 entry("d4", { kind: "dm", title: "notes", others: [], last: "2026-10-01T00:00:00Z" }),
161 ];
162 const { pinned, channels, agentDms, people, agents } = sections(all);
163 assert.deepEqual(pinned.map((e) => e.title), ["general"]);
164 assert.deepEqual(channels.map((e) => e.title), ["noisy", "random"]);
165 // Direct messages in two buckets, each the latest first: people (mixed ones and your notes among them), and agents alone.
166 assert.deepEqual(people.map((e) => e.title), ["ana", "ana, reviewer", "notes"]);
167 assert.deepEqual(agents.map((e) => e.title), ["reviewer"]);
168 assert.equal(agentDms.get("a1")?.title, "reviewer", "the agent's own conversation, not the mixed one");
169 assert.deepEqual(all.map(mixedDm), [false, false, false, false, false, true, false]);
170 assert.deepEqual(filterEntries(all, "unread", "").map((e) => e.title), ["general", "reviewer", "noisy"]);
171 assert.deepEqual(filterEntries(all, "mentions", "").map((e) => e.title), ["reviewer"]);
172 // Jump to finds the agent's own conversation and the mixed one it is in.
173 assert.deepEqual(filterEntries(all, "all", "@Review").map((e) => e.title), ["reviewer", "ana, reviewer"]);
174 assert.deepEqual(unreadTotals(all), { unread: 3, mentions: 1 });
175});
176
177test("a mixed conversation says who the agents are, in the composer and above its first message", () => {
178 assert.equal(dmPlaceholder([ana, bot]), "Message @ana and @reviewer (agent)");
179 assert.equal(dmPlaceholder([{ ...ana, display_username: "Ana" }, bot, { ...bot, id: "a2", name: "scribe", display_name: "Scribe" }]), "Message @Ana, @reviewer (agent) and @scribe (agent)");
180 assert.equal(dmPlaceholder([]), "Message yourself");
181 assert.equal(mixedNotice([ana, bot]), "Reviewer is an agent and reads everything here; it can act on what you say.");
182 assert.equal(mixedNotice([ana, bot, { ...bot, id: "a2", name: "scribe", display_name: "Scribe" }]), "Reviewer and Scribe are agents and read everything here; they can act on what you say.");
183 assert.equal(mixedNotice([ana]), "");
184});
185
186test("addresses, names, money and reconnecting", () => {
187 assert.equal(channelPath("acme", { id: "c1", kind: "channel", name: "general" }), "/acme/-/chat/general");
188 assert.equal(channelPath("acme", { id: "d1", kind: "dm", name: null }), "/acme/-/chat/dm/d1");
189 assert.equal(channelName("#Release Train_2!"), "release-train-2");
190 assert.equal(backoff(0, () => 0.5), 1000);
191 assert.equal(backoff(10, () => 0.5), 30_000);
192});
193
194test("a member shows by display name, else by their username as they wrote it", () => {
195 assert.equal(shownName({ name: "ana", display_name: "Ana Lima", display_username: "Ana" }), "Ana Lima");
196 assert.equal(shownName({ name: "ana", display_name: " ", display_username: "Ana" }), "Ana");
197 assert.equal(shownName({ name: "ana", display_name: "" }), "ana");
198 assert.equal(shownHandle({ name: "ana", display_username: "Ana" }), "Ana");
199 assert.equal(shownHandle({ name: "ana", display_username: "Bo" }), "ana");
200});
201
202test("mentions read as the names people know, found by the lowercased handle", () => {
203 const names = mentionNames([
204 { kind: "user", name: "ana", display_username: "Ana", display_name: "Ana Lima", avatar: null },
205 { kind: "user", name: "bo", display_username: "Bo", display_name: "", avatar: null },
206 { kind: "agent", name: "reviewer", display_name: "Reviewer", avatar: null },
207 ]);
208 assert.equal(names.get("ana"), "Ana Lima");
209 assert.equal(names.get("bo"), "Bo");
210 assert.equal(names.get("reviewer"), "Reviewer");
211 assert.equal(names.get("cy"), undefined);
212});
213
214test("the typing line: people type, an agent does what its phrase says, then is still working on it", () => {
215 const now = 100_000;
216 const mike = { kind: "agent" as const, id: "a2", name: "mike", display_name: "Mike", avatar: null, role: null };
217 assert.equal(typingLabel([], now), null);
218 assert.deepEqual(typingLabel([{ member: ana, since: now - 60_000 }], now), { who: "Ana", doing: "is typing" }, "a person is never 'working'");
219 assert.deepEqual(typingLabel([{ member: bot, since: now - 2_000 }], now), { who: "Reviewer", doing: agentDoing("a1") });
220 assert.match(agentDoing("a1"), /^is /);
221 assert.deepEqual(typingLabel([{ member: bot, since: now - WORKING_AFTER_MS }], now), { who: "Reviewer", doing: "is still working on it" });
222 // Mike and @g1t on one reply each: on it, then still working, as the user saw them vanish after eight seconds before.
223 assert.deepEqual(typingLabel([{ member: mike, since: now - 3_000 }, { member: bot, since: now - 1_000 }], now), { who: "Mike and Reviewer", doing: "are on it" });
224 assert.deepEqual(typingLabel([{ member: mike, since: now - 30_000 }, { member: bot, since: now - 1_000 }], now), { who: "Mike and Reviewer", doing: "are still working on it" });
225 assert.deepEqual(typingLabel([{ member: ana, since: now }, { member: bot, since: now - 30_000 }], now), { who: "Ana and Reviewer", doing: "are typing" }, "people and agents together");
226 const third = { ...ana, id: "u3", name: "bo", display_name: "Bo" };
227 assert.deepEqual(typingLabel([{ member: ana, since: now }, { member: bot, since: now }, { member: third, since: now }], now), { who: "Several people", doing: "are typing" });
228 assert.deepEqual(typingLabel([{ member: mike, since: now }, { member: bot, since: now }, { member: { ...mike, id: "a3", name: "sam", display_name: "Sam" }, since: now }], now), { who: "Several agents", doing: "are on it" });
229});