import { expect, test, type Page } from "@playwright/test"; import { installMockBridge, TEST_IDENTITIES } from "../helpers/bridge"; /** * Copying a mention and pasting it back must preserve the identity. * * The reported failure is specific to a **multi-word, non-member** display * name: the rendered chip drops the `@`, so a plain copy yields "John Smith", * and nothing downstream can tell that from two ordinary words. These tests * bind the production copy/paste seams — real `copy`/`cut`/`paste` DOM events * against the timeline and the composer — and assert both clipboard flavors: * a readable plain flavor with no pubkey in it, and an HTML sidecar that * carries one. */ /** `mockDisplayNames` maps this to "John Smith"; it joins no mock channel. */ const JOHN_SMITH_PUBKEY = "7c1f2ad0b4e93856a1d0c2f4e6b8093a5d7f1c3e5a79b1d3f5072a4c6e80931b"; /** * A second account this community also names "John Smith". * * Seeded per-test, and no more forged than the first: two people really can * share a display name, so a paste of one is not evidence about the other. */ const NAMESAKE_PUBKEY = "5e2c9b710f4a836d2b5c0e7d8a1f4c63b9d0725e1a8c3f6407d9b2e5c8a1f403"; const MENTION_LABEL = "John Smith"; const MENTION_SIGIL = `@${MENTION_LABEL}`; const MESSAGE_BODY = "@John Smith fixed the bug"; /** A copied sentence with words either side of its mention, as most have. */ const SURROUNDED_MENTION_BODY = "Hello @John Smith fixed the bug"; /** A second copied sentence naming the same person, appended to the first. */ const SECOND_MENTION_BODY = " and @John Smith agrees"; const FORUM_REPLY_BODY = "Agreed, @John Smith should confirm"; /** The mock `general` channel — a feed item needs its UUID, not its name. */ const GENERAL_CHANNEL_ID = "9a1657ac-f7aa-5db0-b632-d8bbeb6dfb50"; /** A pubkey must never reach the flavor an external app pastes. */ const ANY_64_HEX = /[0-9a-f]{64}/i; /** Nobody's key — what a crafted clipboard sidecar would name instead. */ const IMPOSTOR_PUBKEY = "1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f"; /** * What a pasteboard round trip can swap a copied chip's spaces for. * * Built rather than written as an escape: a U+00A0 that decays into an * invisible literal is unreadable in a diff and silently changes the fixture. */ const NBSP = String.fromCharCode(0xa0); type ClipboardFlavors = { defaultPrevented: boolean; html: string; text: string; }; test.beforeEach(async ({ page }) => { await installMockBridge(page); }); async function waitForMockLiveSubscription(page: Page, channelName: string) { await expect .poll(() => page.evaluate( (currentChannelName) => window.__BUZZ_E2E_HAS_MOCK_LIVE_SUBSCRIPTION__?.({ channelName: currentChannelName, }) ?? false, channelName, ), ) .toBe(true); } // The timeline renders off a `useDeferredValue` snapshot; the list wrapper // carries `data-render-pending` until that commit lands. async function waitForTimelineSettled(page: Page) { await expect(page.locator("[data-render-pending]")).toHaveCount(0); } async function emitMentionMessage(page: Page, channelName: string) { const event = await page.evaluate( ({ channel, content, mentionPubkey, pubkey }) => window.__BUZZ_E2E_EMIT_MOCK_MESSAGE__?.({ channelName: channel, content, mentionPubkeys: [mentionPubkey], pubkey, }), { channel: channelName, content: MESSAGE_BODY, mentionPubkey: JOHN_SMITH_PUBKEY, pubkey: TEST_IDENTITIES.alice.pubkey, }, ); if (!event) throw new Error("Mock message emitter is not installed"); // The chip is what every copy in this file selects against, so wait for the // resolved identity rather than for the row. A non-member's profile is not // in the channel roster, so this waits on a profile round trip — give it // room beyond the default, since this is setup and not the assertion. const chip = page .getByTestId("message-body") .locator(`[data-mention-pubkey="${JOHN_SMITH_PUBKEY}"]`); await expect(chip).toHaveText("John Smith", { timeout: 15_000 }); await waitForTimelineSettled(page); return event; } /** * Copy a range of the rendered timeline through the real `copy` event. * * `selectChip` narrows the range to the first four characters *inside* the * mention chip, which is how a user drags across half a name. */ async function copyFromTimeline( page: Page, { partialChip = false }: { partialChip?: boolean } = {}, ): Promise { return page.evaluate( ({ pubkey, selectPartialChip }) => { // Anchor on the chip, not on the first message body: `general` is seeded // with unrelated messages that would otherwise win the query. const chip = document.querySelector( `[data-testid="message-body"] [data-mention-pubkey="${pubkey}"]`, ); if (!chip) throw new Error("Message body rendered no mention chip."); const body = chip.closest(".message-markdown"); if (!body) throw new Error("Mention chip is outside a rendered body."); const selection = window.getSelection(); if (!selection) throw new Error("Selection API unavailable."); selection.removeAllRanges(); const range = document.createRange(); if (selectPartialChip) { const walker = document.createTreeWalker(chip, NodeFilter.SHOW_TEXT); const label = walker.nextNode(); if (!label?.nodeValue?.startsWith("John")) { throw new Error("Mention chip has no leading John text node."); } range.setStart(label, 0); range.setEnd(label, 4); } else { range.selectNodeContents(body); } selection.addRange(range); const clipboardData = new DataTransfer(); const event = new ClipboardEvent("copy", { bubbles: true, cancelable: true, clipboardData, }); (selectPartialChip ? chip : body).dispatchEvent(event); return { defaultPrevented: event.defaultPrevented, html: clipboardData.getData("text/html"), text: clipboardData.getData("text/plain"), }; }, { pubkey: JOHN_SMITH_PUBKEY, selectPartialChip: partialChip }, ); } /** * Seed the home inbox with a mention message and open it. * * The message mentions the viewer, which is what routes it to the inbox, and * John Smith, whose identity every assertion here is about. The feed item is * pushed separately: a live channel message alone does not enter the feed the * home surface reads. */ async function openInboxMentionItem(page: Page) { const item = await page.evaluate( ({ channelId, content, mentionPubkey, pubkey, viewerPubkey }) => { const emit = window.__BUZZ_E2E_EMIT_MOCK_MESSAGE__; const push = window.__BUZZ_E2E_PUSH_MOCK_FEED_ITEM__; if (!emit || !push) throw new Error("Mock feed helpers are not installed"); const event = emit({ channelName: "general", content, mentionPubkeys: [viewerPubkey, mentionPubkey], pubkey, }); push({ category: "mention", channel_id: channelId, channel_name: "general", content: event.content, created_at: event.created_at, id: event.id, kind: event.kind, pubkey: event.pubkey, tags: event.tags, }); return event; }, { channelId: GENERAL_CHANNEL_ID, content: MESSAGE_BODY, mentionPubkey: JOHN_SMITH_PUBKEY, pubkey: TEST_IDENTITIES.alice.pubkey, viewerPubkey: TEST_IDENTITIES.tyler.pubkey, }, ); const preview = page .getByTestId(`home-inbox-item-${item.id}`) .locator("[data-mention]"); await expect(preview).toHaveText("John Smith"); expect(await preview.ariaSnapshot()).toContain("John"); await page.getByTestId(`home-inbox-item-${item.id}`).click(); // The inbox resolves a non-member's display name off its own profile batch, // so wait for the chip's identity rather than for the row — same setup // allowance the channel-timeline seed makes. const chip = page .getByTestId("home-inbox-detail-scroll") .locator(`[data-mention-pubkey="${JOHN_SMITH_PUBKEY}"]`); await expect(chip).toHaveText("John Smith", { timeout: 15_000 }); expect(await chip.ariaSnapshot()).toContain("John"); return item; } /** * Copy a whole rendered forum body — post card or thread reply — through the * real `copy` event. The event bubbles to the surface container, which is * where the forum wires `handleTimelineMentionCopy`. */ async function copyFromForumMarkdown( page: Page, marker: string, ): Promise { return page.evaluate((bodyMarker) => { const body = [ ...document.querySelectorAll(".message-markdown"), ].find((candidate) => candidate.textContent?.includes(bodyMarker)); if (!body) throw new Error(`No rendered forum body contains: ${bodyMarker}`); const selection = window.getSelection(); if (!selection) throw new Error("Selection API unavailable."); selection.removeAllRanges(); const range = document.createRange(); range.selectNodeContents(body); selection.addRange(range); const clipboardData = new DataTransfer(); const event = new ClipboardEvent("copy", { bubbles: true, cancelable: true, clipboardData, }); body.dispatchEvent(event); return { defaultPrevented: event.defaultPrevented, html: clipboardData.getData("text/html"), text: clipboardData.getData("text/plain"), }; }, marker); } /** * Copy the rendered body holding the mention chip inside `containerTestId`. * * The event is dispatched on the body and bubbles to that container, which is * where a surface wires `handleTimelineMentionCopy` — so a missing wiring shows * up as a declined copy rather than as a lookup failure. */ async function copyMentionBodyWithin( page: Page, containerTestId: string, ): Promise { return page.evaluate( ({ pubkey, testId }) => { const container = document.querySelector( `[data-testid="${testId}"]`, ); if (!container) throw new Error(`No container: ${testId}`); const chip = container.querySelector( `[data-mention-pubkey="${pubkey}"]`, ); if (!chip) throw new Error(`${testId} rendered no mention chip.`); const body = chip.closest(".message-markdown"); if (!body) throw new Error("Mention chip is outside a rendered body."); const selection = window.getSelection(); if (!selection) throw new Error("Selection API unavailable."); selection.removeAllRanges(); const range = document.createRange(); range.selectNodeContents(body); selection.addRange(range); const clipboardData = new DataTransfer(); const event = new ClipboardEvent("copy", { bubbles: true, cancelable: true, clipboardData, }); body.dispatchEvent(event); return { defaultPrevented: event.defaultPrevented, html: clipboardData.getData("text/html"), text: clipboardData.getData("text/plain"), }; }, { pubkey: JOHN_SMITH_PUBKEY, testId: containerTestId }, ); } /** Copy or cut the composer's current selection through the real DOM event. */ async function copyFromComposer( page: Page, type: "copy" | "cut", ): Promise { return page.getByTestId("message-input").evaluate((element, eventType) => { const clipboardData = new DataTransfer(); const event = new ClipboardEvent(eventType, { bubbles: true, cancelable: true, clipboardData, }); element.dispatchEvent(event); return { defaultPrevented: event.defaultPrevented, html: clipboardData.getData("text/html"), text: clipboardData.getData("text/plain"), }; }, type); } async function pasteIntoComposer( page: Page, flavors: { html: string; text: string }, ) { const input = page.getByTestId("message-input"); await input.click(); await input.evaluate((element, { html, text }) => { const clipboardData = new DataTransfer(); clipboardData.setData("text/plain", text); clipboardData.setData("text/html", html); element.dispatchEvent( new ClipboardEvent("paste", { bubbles: true, cancelable: true, clipboardData, }), ); }, flavors); } /** Paste at the end of whatever the composer already holds. */ async function pasteAfterComposerText( page: Page, flavors: { html: string; text: string }, ) { const input = page.getByTestId("message-input"); await input.click(); // A click lands the caret wherever it hit; the append is the point here. await input.press("End"); await input.evaluate((element, { html, text }) => { const clipboardData = new DataTransfer(); clipboardData.setData("text/plain", text); clipboardData.setData("text/html", html); element.dispatchEvent( new ClipboardEvent("paste", { bubbles: true, cancelable: true, clipboardData, }), ); }, flavors); } /** The flavors a Buzz copy of `body` writes for a "John Smith" chip. */ function mentionFlavors(pubkey: string, body: string) { const at = body.indexOf(MENTION_SIGIL); if (at < 0) throw new Error(`Body names nobody: ${body}`); return { html: '' + body.slice(0, at) + `${MENTION_SIGIL}` + body.slice(at + MENTION_SIGIL.length) + "", text: body, }; } /** * Pin every relay profile lookup open, or let the held ones through. * * The identity check behind a pasted mention is exactly such a lookup when the * person is not a member of the channel being pasted into — the case the * feature exists for. Holding it is what makes "the paste is still deciding" a * state a test can act inside rather than race. */ async function holdRelayProfileLookups(page: Page, hold: boolean) { return page.evaluate( (next) => window.__BUZZ_E2E_HOLD_USERS_BATCH__?.(next) ?? 0, hold, ); } /** * Select the first occurrence of `wanted` in the composer. * * The DOM range a mouse drag leaves behind, in one shot: ProseMirror adopts it * exactly as it adopts a real drag, and whatever key is pressed next still * goes through the editor's own keymap. A caret walked there with arrow keys * silently lost presses between runs and deleted the wrong words. */ async function selectComposerRange(page: Page, wanted: string) { const selected = await page .getByTestId("message-input") .evaluate((element, needle) => { const selection = window.getSelection(); if (!selection) throw new Error("Selection API unavailable."); const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT); for (let node = walker.nextNode(); node; node = walker.nextNode()) { const at = node.nodeValue?.indexOf(needle) ?? -1; if (at < 0) continue; const range = document.createRange(); range.setStart(node, at); range.setEnd(node, at + needle.length); selection.removeAllRanges(); selection.addRange(range); return selection.toString(); } throw new Error(`Composer holds no run reading ${needle}`); }, wanted); expect(selected).toBe(wanted); } /** Wait until a lookup naming `pubkey` is provably pinned open. */ async function waitForHeldProfileLookup(page: Page, pubkey: string) { await expect.poll(() => askedRelayAboutProfile(page, pubkey)).toBe(true); await expect .poll(() => page.evaluate(() => window.__BUZZ_E2E_USERS_BATCH_PENDING__?.() ?? 0), ) .toBeGreaterThan(0); } /** * The `p` tags of the outgoing message whose body is `content`. * * A DM is signed client-side and published over the socket rather than through * `send_channel_message`, so read the event handed to the signer. */ async function readSentMentionPubkeys(page: Page, content: string) { return page.evaluate((expectedContent) => { for (const entry of window.__BUZZ_E2E_COMMAND_LOG__ ?? []) { if (entry.command === "send_channel_message") { const payload = entry.payload as | { content?: string; mentionPubkeys?: string[] | null } | undefined; if (payload?.content !== expectedContent) continue; return payload.mentionPubkeys ?? []; } if (entry.command !== "sign_event") continue; const unsigned = entry.payload as | { content?: string; tags?: string[][] } | undefined; if (unsigned?.content !== expectedContent) continue; return (unsigned.tags ?? []) .filter((tag) => tag[0] === "p" && tag[1]) .map((tag) => tag[1]); } return null; }, content); } /** Whether a profile lookup naming `pubkey` reached the backend. */ async function askedRelayAboutProfile(page: Page, pubkey: string) { return page.evaluate( (wanted) => (window.__BUZZ_E2E_COMMAND_LOG__ ?? []).some( (entry) => entry.command === "get_users_batch" && ( (entry.payload as { pubkeys?: string[] } | undefined)?.pubkeys ?? [] ).includes(wanted), ), pubkey, ); } function expectCarriesJohnSmith(flavors: ClipboardFlavors) { expect(flavors.defaultPrevented).toBe(true); // Readable anywhere, and safe to hand an external app: the sigil is back and // no identifier rode along. expect(flavors.text).toContain("@John Smith"); expect(flavors.text).not.toMatch(ANY_64_HEX); // The identity travels in the sidecar flavor instead. expect(flavors.html).toContain(`data-mention-pubkey="${JOHN_SMITH_PUBKEY}"`); expect(flavors.html).toContain('data-mention-label="John Smith"'); } async function expectComposerChip(page: Page) { const input = page.getByTestId("message-input"); await expect(input).toHaveText(MESSAGE_BODY); await expect(input.locator(".mention-chip")).toHaveText("John Smith"); } test("timeline selection copy carries a multi-word mention into another channel", async ({ page, }) => { await page.goto("/"); await page.getByTestId("channel-general").click(); await expect(page.getByTestId("chat-title")).toHaveText("general"); await waitForMockLiveSubscription(page, "general"); await emitMentionMessage(page, "general"); const chip = page .getByTestId("message-row") .filter({ hasText: "John Smith fixed the bug" }) .locator("[data-mention]"); await expect(chip).toHaveAttribute("data-mention-pubkey", JOHN_SMITH_PUBKEY); await expect(chip).toHaveText("John Smith"); const flavors = await copyFromTimeline(page); expectCarriesJohnSmith(flavors); expect(flavors.text.trim()).toBe(MESSAGE_BODY); // A DM is the destination so the send is not intercepted by the non-member // invite prompt — the assertion under test is the recovered `p` tag. await page.getByTestId("channel-bob-tyler").click(); await expect(page.getByTestId("chat-title")).toHaveText("bob-tyler"); await pasteIntoComposer(page, flavors); await expectComposerChip(page); await page.getByTestId("send-message").click(); await expect(page.getByTestId("message-input")).toHaveText(""); await expect .poll(() => readSentMentionPubkeys(page, MESSAGE_BODY)) .toContain(JOHN_SMITH_PUBKEY); }); test("copy message writes the identity sidecar beside readable plain text", async ({ page, }) => { await page.goto("/"); await page.getByTestId("channel-general").click(); await expect(page.getByTestId("chat-title")).toHaveText("general"); await waitForMockLiveSubscription(page, "general"); const message = await emitMentionMessage(page, "general"); const row = page .getByTestId("message-row") .filter({ hasText: "John Smith fixed the bug" }); await row.hover(); await row.getByTestId(`more-actions-${message.id}`).click({ force: true }); await page.getByRole("menuitem", { name: "Copy message" }).click(); const written = await page.evaluate( () => window.__BUZZ_E2E_LAST_CLIPBOARD__ ?? null, ); expect(written?.text).toBe(MESSAGE_BODY); expect(written?.text).not.toMatch(ANY_64_HEX); expect(written?.html).toContain(`data-mention-pubkey="${JOHN_SMITH_PUBKEY}"`); // "Copy message" copies Markdown source, so paste must take the text path. expect(written?.html).toContain('data-buzz-copy="markdown"'); await page.getByTestId("channel-bob-tyler").click(); await expect(page.getByTestId("chat-title")).toHaveText("bob-tyler"); await pasteIntoComposer(page, { html: written?.html ?? "", text: written?.text ?? "", }); await expectComposerChip(page); }); test("composer copy and cut round-trip the mention they were pasted with", async ({ page, }) => { await page.goto("/"); await page.getByTestId("channel-general").click(); await expect(page.getByTestId("chat-title")).toHaveText("general"); await waitForMockLiveSubscription(page, "general"); await emitMentionMessage(page, "general"); const source = await copyFromTimeline(page); await pasteIntoComposer(page, source); await expectComposerChip(page); const input = page.getByTestId("message-input"); await input.press("ControlOrMeta+a"); expectCarriesJohnSmith(await copyFromComposer(page, "copy")); await expect(input).toHaveText(MESSAGE_BODY); const cut = await copyFromComposer(page, "cut"); expectCarriesJohnSmith(cut); await expect(input).toHaveText(""); // The cut flavors are a complete round trip on their own. await pasteIntoComposer(page, cut); await expectComposerChip(page); }); test("an identity the pasted content never shows binds no name", async ({ page, }) => { await page.goto("/"); await page.getByTestId("channel-bob-tyler").click(); await expect(page.getByTestId("chat-title")).toHaveText("bob-tyler"); // Any copied page can carry this: an empty span claiming a real display // name against a key of its choosing. A registration outlives its paste, so // accepting one the user cannot see would rebind the name for the session. await pasteIntoComposer(page, { html: '' + `look at this', text: "look at this", }); const input = page.getByTestId("message-input"); await expect(input).toHaveText("look at this"); // The name that sidecar tried to claim, written afterwards by hand. await input.press("ControlOrMeta+a"); await input.press("Backspace"); await expect(input).toHaveText(""); await pasteIntoComposer(page, { html: "", text: MESSAGE_BODY }); await expect(input).toHaveText(MESSAGE_BODY); await expect(input.locator(".mention-chip")).toHaveCount(0); await page.getByTestId("send-message").click(); await expect(input).toHaveText(""); await expect .poll(() => readSentMentionPubkeys(page, MESSAGE_BODY)) .not.toBeNull(); expect(await readSentMentionPubkeys(page, MESSAGE_BODY)).not.toContain( IMPOSTOR_PUBKEY, ); }); test("an identity vouched for only by dropped markup binds no name", async ({ page, }) => { await page.goto("/"); await page.getByTestId("channel-bob-tyler").click(); await expect(page.getByTestId("chat-title")).toHaveText("bob-tyler"); // The same claim as above, hidden where ProseMirror's parser throws it away // rather than in an empty span. No `data-buzz-copy` marker, so this takes // the rich HTML branch, where the gate reads the normalized markup's text. const input = page.getByTestId("message-input"); await pasteIntoComposer(page, { html: "visible " + `', text: "visible", }); // The premise the gate has to share: `