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