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