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