Skip to content
166 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 inline,
14 mentionQuery,
15 mergeMessages,
16 safeHref,
17 sections,
18 timeline,
19 unreadTotals,
20 type ShownMessage,
21} from "./chat.ts";
22
23const ana = { kind: "user" as const, id: "u1", name: "ana", display_name: "Ana", avatar: null, role: null };
24const bot = { kind: "agent" as const, id: "a1", name: "reviewer", display_name: "Reviewer", avatar: null, role: "Reviews pull requests" };
25
26function message(id: string, at: string, over: Partial<ChatMessage> = {}): ShownMessage {
27 return {
28 id,
29 channel_id: "c1",
30 author: ana,
31 kind: "text",
32 body: "hi",
33 card: null,
34 thread_root: null,
35 reply_count: 0,
36 last_reply_at: null,
37 created_at: at,
38 edited_at: null,
39 deleted_at: null,
40 ...over,
41 };
42}
43
44test("messages from one author close together share a header; days get a rule", () => {
45 const now = new Date("2026-10-08T15:00:00Z");
46 const rows = timeline(
47 [
48 message("01", "2026-10-07T10:00:00Z"),
49 message("02", "2026-10-08T10:00:00Z"),
50 message("03", "2026-10-08T10:02:00Z"),
51 message("04", "2026-10-08T10:03:00Z", { author: bot }),
52 message("05", "2026-10-08T10:30:00Z", { author: bot }),
53 message("06", "2026-10-08T10:31:00Z", { author: bot, kind: "card" }),
54 message("07", "2026-10-08T10:32:00Z", { deleted_at: "2026-10-08T10:33:00Z" }),
55 ],
56 now,
57 "UTC",
58 );
59 assert.deepEqual(
60 rows.map((row) => (row.kind === "day" ? row.label : `${row.message.id}${row.head ? "*" : ""}`)),
61 ["Yesterday", "01*", "Today", "02*", "03", "04*", "05*", "06*"],
62 );
63 assert.equal(dayLabel("2026-09-01T10:00:00Z", now, "UTC"), "Tuesday, September 1");
64});
65
66test("merging keeps one copy of each message, and drops the pending one once saved", () => {
67 const pending = { ...message("tmp-1", "2026-10-08T10:00:00Z"), pending: true, client_id: "tmp-1" };
68 const saved = { ...message("05", "2026-10-08T10:00:01Z"), client_id: "tmp-1" };
69 const merged = mergeMessages([message("02", "2026-10-08T09:00:00Z"), pending], [saved, message("02", "2026-10-08T09:00:00Z", { body: "edited" })]);
70 assert.deepEqual(merged.map((m) => m.id), ["02", "05"]);
71 assert.equal(merged[0]!.body, "edited");
72 // Still pending: last, after everything saved.
73 assert.deepEqual(mergeMessages([pending], [message("09", "2026-10-08T11:00:00Z")]).map((m) => m.id), ["09", "tmp-1"]);
74});
75
76test("an @ being typed suggests people and agents", () => {
77 const people = [
78 { kind: "user" as const, name: "ana", display_name: "Ana Lima", avatar: null },
79 { kind: "agent" as const, name: "reviewer", display_name: "Reviewer", avatar: null },
80 { kind: "user" as const, name: "bo", display_name: "Robert Reed", avatar: null },
81 ];
82 assert.deepEqual(mentionQuery("hey @re", 7, people)?.options.map((p) => p.name), ["reviewer", "bo"]);
83 assert.equal(mentionQuery("hey @re", 7, people)?.start, 4);
84 assert.equal(mentionQuery("mail me@an", 10, people), null);
85 assert.deepEqual(mentionQuery("@", 1, people)?.options.length, 3);
86 assert.equal(mentionQuery("@zz", 3, people), null);
87});
88
89test("message text: mentions, references, links and code, never HTML", () => {
90 assert.deepEqual(inline("ask @reviewer about #12 and g1t#3"), [
91 { t: "text", v: "ask " },
92 { t: "mention", name: "reviewer" },
93 { t: "text", v: " about " },
94 { t: "ref", repo: null, number: 12 },
95 { t: "text", v: " and " },
96 { t: "ref", repo: "g1t", number: 3 },
97 ]);
98 assert.deepEqual(inline("see #general, `a <b>` **bold**"), [
99 { t: "text", v: "see " },
100 { t: "channel", name: "general" },
101 { t: "text", v: ", " },
102 { t: "code", v: "a <b>" },
103 { t: "text", v: " " },
104 { t: "strong", c: [{ t: "text", v: "bold" }] },
105 ]);
106 assert.ok(inline("[x](javascript:alert(1))").every((span) => span.t === "text"));
107 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: "." }]);
108 assert.deepEqual(inline("me@example.com"), [{ t: "text", v: "me@example.com" }]);
109 assert.equal(safeHref("//evil.example"), null);
110 assert.equal(safeHref("/acme/web/pull/3"), "/acme/web/pull/3");
111});
112
113test("message blocks: fenced code, lists, quotes and paragraphs", () => {
114 const parsed = blocks("Plan:\n- one\n- two\n\n```ts\nconst a = 1;\n```\n> quoted\nlast");
115 assert.deepEqual(parsed.map((block) => block.t), ["p", "list", "code", "quote", "p"]);
116 assert.deepEqual(parsed[2], { t: "code", lang: "ts", v: "const a = 1;" });
117});
118
119function entry(id: string, over: Partial<ChatSidebarEntry> & { kind?: "channel" | "dm"; last?: string } = {}): ChatSidebarEntry {
120 return {
121 channel: {
122 id,
123 workspace_id: "w",
124 kind: over.kind ?? "channel",
125 name: over.kind === "dm" ? null : id,
126 topic: null,
127 private: false,
128 created_by: { kind: "user", id: "u1" },
129 created_at: "2026-10-01T00:00:00Z",
130 archived_at: null,
131 last_message_at: over.last ?? null,
132 },
133 title: over.title ?? id,
134 others: over.others ?? [],
135 starred: over.starred ?? false,
136 muted: over.muted ?? false,
137 unread: over.unread ?? 0,
138 mentions: over.mentions ?? 0,
139 };
140}
141
142test("the sidebar: sections, filters and the rail's count", () => {
143 const all = [
144 entry("random"),
145 entry("general", { starred: true, unread: 2 }),
146 entry("d1", { kind: "dm", title: "reviewer", others: [bot], last: "2026-10-02T00:00:00Z", mentions: 1, unread: 1 }),
147 entry("d2", { kind: "dm", title: "ana", others: [ana], last: "2026-10-05T00:00:00Z" }),
148 entry("noisy", { muted: true, unread: 9 }),
149 ];
150 const { starred, channels, dms } = sections(all);
151 assert.deepEqual(starred.map((e) => e.title), ["general"]);
152 assert.deepEqual(channels.map((e) => e.title), ["noisy", "random"]);
153 assert.deepEqual(dms.map((e) => e.title), ["ana", "reviewer"]);
154 assert.deepEqual(filterEntries(all, "unread", "").map((e) => e.title), ["general", "reviewer", "noisy"]);
155 assert.deepEqual(filterEntries(all, "mentions", "").map((e) => e.title), ["reviewer"]);
156 assert.deepEqual(filterEntries(all, "all", "@Review").map((e) => e.title), ["reviewer"]);
157 assert.deepEqual(unreadTotals(all), { unread: 3, mentions: 1 });
158});
159
160test("addresses, names, money and reconnecting", () => {
161 assert.equal(channelPath("acme", { id: "c1", kind: "channel", name: "general" }), "/acme/-/chat/general");
162 assert.equal(channelPath("acme", { id: "d1", kind: "dm", name: null }), "/acme/-/chat/dm/d1");
163 assert.equal(channelName("#Release Train_2!"), "release-train-2");
164 assert.equal(backoff(0, () => 0.5), 1000);
165 assert.equal(backoff(10, () => 0.5), 30_000);
166});