|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { act, cleanup, render, screen } from "@testing-library/react"; |
| 3 | +import { afterEach, expect, it, vi } from "vitest"; |
| 4 | +import { LinkLabel } from "../../bundled/links/InlineLink"; |
| 5 | +import type { ChannelMessage, Profile } from "../relay/contracts"; |
| 6 | +import type { RelaySession } from "../relay/session"; |
| 7 | +import type { ThreadSnapshot, ThreadView } from "../relay/threads"; |
| 8 | +import { MessageLink } from "./MessageLink"; |
| 9 | + |
| 10 | +afterEach(cleanup); |
| 11 | +// Lookups start after the commit that mounted their links. |
| 12 | +const started = () => act(async () => {}); |
| 13 | + |
| 14 | +const alice = "b".repeat(64); |
| 15 | +const id = (n: number) => n.toString(16).padStart(64, "a"); |
| 16 | +const href = (messageId: string, thread?: string) => |
| 17 | + `buzz://message?${new URLSearchParams({ channel: "design", id: messageId, ...(thread ? { thread } : {}) })}`; |
| 18 | +const message = (messageId: string, patch?: Partial<ChannelMessage>) => |
| 19 | + ({ |
| 20 | + id: messageId, |
| 21 | + channelId: "design", |
| 22 | + authorId: alice, |
| 23 | + content: "Linked body", |
| 24 | + createdAt: 1, |
| 25 | + mentions: [], |
| 26 | + participants: [], |
| 27 | + attachments: [], |
| 28 | + reactions: [], |
| 29 | + replyCount: 0, |
| 30 | + ...patch, |
| 31 | + }) satisfies ChannelMessage; |
| 32 | +const loading: ThreadSnapshot = { |
| 33 | + status: "loading", |
| 34 | + root: undefined, |
| 35 | + replies: [], |
| 36 | + error: undefined, |
| 37 | + canLoadMore: true, |
| 38 | + limited: false, |
| 39 | + targetStatus: "loading", |
| 40 | +}; |
| 41 | +const ready = (target: ChannelMessage): Partial<ThreadSnapshot> => ({ |
| 42 | + status: "ready", |
| 43 | + canLoadMore: false, |
| 44 | + target, |
| 45 | + targetStatus: "ready", |
| 46 | +}); |
| 47 | + |
| 48 | +/** Exact readers the test settles by hand; `open` counts undisposed ones. */ |
| 49 | +function fixture({ |
| 50 | + channels = [{ id: "design", name: "design" }] as { |
| 51 | + id: string; |
| 52 | + name: string; |
| 53 | + channelType?: string; |
| 54 | + }[], |
| 55 | + profiles = new Map<string, Profile>([[alice, { name: "Alice" }]]), |
| 56 | + capacity = Number.POSITIVE_INFINITY, |
| 57 | +} = {}) { |
| 58 | + const views: { |
| 59 | + messageId: string; |
| 60 | + publish(patch: Partial<ThreadSnapshot>): Promise<void>; |
| 61 | + disposed: boolean; |
| 62 | + }[] = []; |
| 63 | + const thread = vi.fn((_channelId: string, messageId: string): ThreadView => { |
| 64 | + if (views.filter((view) => !view.disposed).length >= capacity) |
| 65 | + throw new Error("Relay view capacity unavailable"); |
| 66 | + let snapshot = loading; |
| 67 | + const listeners = new Set<() => void>(); |
| 68 | + const entry = { |
| 69 | + messageId, |
| 70 | + disposed: false, |
| 71 | + async publish(patch: Partial<ThreadSnapshot>) { |
| 72 | + snapshot = { ...snapshot, ...patch }; |
| 73 | + await act(async () => { |
| 74 | + for (const listener of listeners) listener(); |
| 75 | + }); |
| 76 | + }, |
| 77 | + }; |
| 78 | + views.push(entry); |
| 79 | + return { |
| 80 | + snapshot: () => snapshot, |
| 81 | + subscribe(listener) { |
| 82 | + listeners.add(listener); |
| 83 | + return () => void listeners.delete(listener); |
| 84 | + }, |
| 85 | + refresh: async () => {}, |
| 86 | + loadMore: async () => {}, |
| 87 | + retarget: async () => "selected", |
| 88 | + dispose: () => { |
| 89 | + entry.disposed = true; |
| 90 | + }, |
| 91 | + }; |
| 92 | + }); |
| 93 | + const profileListeners = new Set<() => void>(); |
| 94 | + const list = { status: "ready", channels }; |
| 95 | + const ensure = vi.fn(async () => {}); |
| 96 | + const session = { |
| 97 | + unread: {}, |
| 98 | + thread, |
| 99 | + profiles: { |
| 100 | + snapshot: () => profiles, |
| 101 | + subscribe(listener: () => void) { |
| 102 | + profileListeners.add(listener); |
| 103 | + return () => void profileListeners.delete(listener); |
| 104 | + }, |
| 105 | + ensure, |
| 106 | + }, |
| 107 | + channels: { list: () => list, subscribeList: () => () => {} }, |
| 108 | + media: () => undefined, |
| 109 | + } as unknown as RelaySession; |
| 110 | + return { |
| 111 | + session, |
| 112 | + thread, |
| 113 | + views, |
| 114 | + ensure, |
| 115 | + open: () => views.filter((view) => !view.disposed), |
| 116 | + async setProfiles(next: Map<string, Profile>) { |
| 117 | + profiles = next; |
| 118 | + await act(async () => { |
| 119 | + for (const listener of profileListeners) listener(); |
| 120 | + }); |
| 121 | + }, |
| 122 | + }; |
| 123 | +} |
| 124 | +const renderers = [ |
| 125 | + { |
| 126 | + id: "link", |
| 127 | + title: "Links", |
| 128 | + key: "links/link", |
| 129 | + pluginId: "links", |
| 130 | + revision: "one", |
| 131 | + matches: () => true, |
| 132 | + component: ({ url }: { url: string }) => <LinkLabel href={url} />, |
| 133 | + }, |
| 134 | +]; |
| 135 | +const links = { snapshot: () => renderers, subscribe: () => () => {} }; |
| 136 | +const link = ( |
| 137 | + session: RelaySession, |
| 138 | + url: string, |
| 139 | + props?: Partial<Parameters<typeof MessageLink>[0]>, |
| 140 | +) => ( |
| 141 | + <MessageLink |
| 142 | + url={url} |
| 143 | + directoryLabel="design" |
| 144 | + registry={links} |
| 145 | + onOpenLink={() => true} |
| 146 | + session={session} |
| 147 | + {...props} |
| 148 | + /> |
| 149 | +); |
| 150 | + |
| 151 | +it("names the linked message's author and channel once its exact reader settles", async () => { |
| 152 | + const app = fixture(); |
| 153 | + render(link(app.session, href(id(1)))); |
| 154 | + await started(); |
| 155 | + // Loading keeps the existing channel label and icon. |
| 156 | + const anchor = screen.getByRole("link", { name: "design" }); |
| 157 | + expect(anchor.querySelector('[data-link-kind="message"] svg')).not.toBeNull(); |
| 158 | + expect(app.thread).toHaveBeenCalledWith("design", id(1), { exact: true }); |
| 159 | + await app.views[0]?.publish(ready(message(id(1)))); |
| 160 | + expect(screen.getByRole("link", { name: "Alice in #design" })).toBe(anchor); |
| 161 | + expect(anchor.textContent).toBe("Alice in #design"); |
| 162 | + expect(anchor.querySelector('[data-link-kind="message"] svg')).not.toBeNull(); |
| 163 | + expect(anchor.getAttribute("href")).toBe(href(id(1))); |
| 164 | + // Display only: selection copy still reads the raw destination. |
| 165 | + expect(anchor.dataset.linkLabel).toBe(""); |
| 166 | + // The lookup gives its reader back as soon as it has the author. |
| 167 | + expect(app.open()).toHaveLength(0); |
| 168 | + expect(app.ensure).toHaveBeenCalledWith([alice], "background"); |
| 169 | +}); |
| 170 | + |
| 171 | +it("names a reply's author with the thread icon and a workflow by its owner", async () => { |
| 172 | + const app = fixture(); |
| 173 | + render( |
| 174 | + <> |
| 175 | + {link(app.session, href(id(1), id(9)))} |
| 176 | + {link(app.session, href(id(2)))} |
| 177 | + </>, |
| 178 | + ); |
| 179 | + await started(); |
| 180 | + await app.views[0]?.publish(ready(message(id(1), { threadRootId: id(9) }))); |
| 181 | + await app.views[1]?.publish( |
| 182 | + ready(message(id(2), { authorId: "c".repeat(64), workflowOwnerId: alice })), |
| 183 | + ); |
| 184 | + const reply = screen.getByRole("link", { name: "Alice in #design" }); |
| 185 | + expect(reply.querySelector('[data-link-kind="thread"] svg')).not.toBeNull(); |
| 186 | + expect( |
| 187 | + screen.getByRole("link", { name: "Workflow · owned by Alice in #design" }), |
| 188 | + ).toBeTruthy(); |
| 189 | +}); |
| 190 | + |
| 191 | +it("keeps the channel label while the author has no name, and names them once loaded", async () => { |
| 192 | + const app = fixture({ |
| 193 | + profiles: new Map([ |
| 194 | + [alice, { name: alice.slice(0, 10), nameIsFallback: true }], |
| 195 | + ]), |
| 196 | + }); |
| 197 | + render(link(app.session, href(id(1)))); |
| 198 | + await started(); |
| 199 | + await app.views[0]?.publish(ready(message(id(1)))); |
| 200 | + expect(screen.getByRole("link", { name: "design" }).textContent).toBe( |
| 201 | + "design", |
| 202 | + ); |
| 203 | + await app.setProfiles(new Map([[alice, { name: "Alice" }]])); |
| 204 | + expect(screen.getByRole("link", { name: "Alice in #design" })).toBeTruthy(); |
| 205 | +}); |
| 206 | + |
| 207 | +it("keeps the channel label when the message is unavailable or no reader is free", async () => { |
| 208 | + const unavailable = fixture(); |
| 209 | + const view = render(link(unavailable.session, href(id(1)))); |
| 210 | + await started(); |
| 211 | + await unavailable.views[0]?.publish({ |
| 212 | + status: "idle", |
| 213 | + targetStatus: "unavailable", |
| 214 | + error: "Message unavailable", |
| 215 | + }); |
| 216 | + expect(screen.getByRole("link", { name: "design" })).toBeTruthy(); |
| 217 | + expect(unavailable.open()).toHaveLength(0); |
| 218 | + view.unmount(); |
| 219 | + const full = fixture({ capacity: 0 }); |
| 220 | + render(link(full.session, href(id(1)))); |
| 221 | + await started(); |
| 222 | + await started(); |
| 223 | + // A failed lookup waits for a later mount instead of retrying in a loop. |
| 224 | + expect(full.thread).toHaveBeenCalledOnce(); |
| 225 | + expect(screen.getByRole("link", { name: "design" })).toBeTruthy(); |
| 226 | +}); |
| 227 | + |
| 228 | +it("reads nothing for authored labels, drafts, unlisted channels, DMs or Solo chats", async () => { |
| 229 | + const app = fixture(); |
| 230 | + render( |
| 231 | + <> |
| 232 | + {link(app.session, href(id(1)), { label: "See this" })} |
| 233 | + {link(app.session, href(id(2)), { interactive: false })} |
| 234 | + </>, |
| 235 | + ); |
| 236 | + await started(); |
| 237 | + expect(screen.getByRole("link", { name: "See this" })).toBeTruthy(); |
| 238 | + expect(document.body.textContent).toContain("design"); |
| 239 | + expect(app.thread).not.toHaveBeenCalled(); |
| 240 | + cleanup(); |
| 241 | + for (const channels of [ |
| 242 | + [], |
| 243 | + [{ id: "design", name: "design", channelType: "dm" }], |
| 244 | + [{ id: "design", name: "design", channelType: "solo" }], |
| 245 | + ]) { |
| 246 | + const other = fixture({ channels }); |
| 247 | + render(link(other.session, href(id(3)))); |
| 248 | + await started(); |
| 249 | + expect(screen.getByRole("link", { name: "design" })).toBeTruthy(); |
| 250 | + expect(other.thread).not.toHaveBeenCalled(); |
| 251 | + cleanup(); |
| 252 | + } |
| 253 | +}); |
| 254 | + |
| 255 | +it("runs a few lookups at once, cancels unmounted ones and remembers found authors", async () => { |
| 256 | + const app = fixture(); |
| 257 | + const ids = [1, 2, 3, 4, 5, 6].map(id); |
| 258 | + const all = ids.map((messageId) => ( |
| 259 | + <span key={messageId}>{link(app.session, href(messageId))}</span> |
| 260 | + )); |
| 261 | + const tree = (again: boolean) => ( |
| 262 | + <> |
| 263 | + {all} |
| 264 | + {again && <span>{link(app.session, href(ids[1] ?? ""))}</span>} |
| 265 | + </> |
| 266 | + ); |
| 267 | + const view = render(tree(false)); |
| 268 | + await started(); |
| 269 | + expect(app.open().map((entry) => entry.messageId)).toEqual(ids.slice(0, 4)); |
| 270 | + await app.views[0]?.publish(ready(message(ids[0] ?? ""))); |
| 271 | + expect(app.open().map((entry) => entry.messageId)).toEqual(ids.slice(1, 5)); |
| 272 | + // Two links to one message share its lookup. |
| 273 | + view.rerender(tree(true)); |
| 274 | + await started(); |
| 275 | + expect(app.thread).toHaveBeenCalledTimes(5); |
| 276 | + // Links that unmount together cancel their lookups without starting others. |
| 277 | + view.unmount(); |
| 278 | + await started(); |
| 279 | + expect(app.open()).toHaveLength(0); |
| 280 | + expect(app.thread).toHaveBeenCalledTimes(5); |
| 281 | + // A remount shows a found author without reading it again. |
| 282 | + render(link(app.session, href(ids[0] ?? ""))); |
| 283 | + await started(); |
| 284 | + expect(screen.getByRole("link", { name: "Alice in #design" })).toBeTruthy(); |
| 285 | + expect(app.thread).toHaveBeenCalledTimes(5); |
| 286 | +}); |
0 commit comments