|
| 1 | +// @vitest-environment jsdom |
| 2 | +import "@testing-library/jest-dom/vitest"; |
| 3 | +import { |
| 4 | + act, |
| 5 | + cleanup, |
| 6 | + fireEvent, |
| 7 | + render, |
| 8 | + screen, |
| 9 | + waitFor, |
| 10 | +} from "@testing-library/react"; |
| 11 | +import { StrictMode } from "react"; |
| 12 | +import { afterAll, afterEach, expect, it, vi } from "vitest"; |
| 13 | +import type { ChannelMessage } from "../relay/contracts"; |
| 14 | +import { createRelaySession } from "../relay/session"; |
| 15 | +import type { LiveCallbacks } from "../relay/live"; |
| 16 | +import { |
| 17 | + keypair, |
| 18 | + message, |
| 19 | + profile, |
| 20 | + scriptedTransport, |
| 21 | + signed, |
| 22 | +} from "../relay/testing"; |
| 23 | +import { ThreadPanel } from "./ThreadPanel"; |
| 24 | + |
| 25 | +const bodyRender = vi.fn(); |
| 26 | +vi.mock("./MessageMarkdown", () => ({ |
| 27 | + MessageMarkdown: ({ row }: { row: ChannelMessage }) => { |
| 28 | + bodyRender(row.content); |
| 29 | + return <span>{row.content}</span>; |
| 30 | + }, |
| 31 | +})); |
| 32 | +vi.mock("./MessageComposer", () => ({ MessageComposer: () => null })); |
| 33 | +vi.mock("./MediaAttachment", () => ({ |
| 34 | + MediaAttachment: ({ seekTo }: { seekTo?: number }) => ( |
| 35 | + <span data-testid="video-seek">{seekTo ?? "none"}</span> |
| 36 | + ), |
| 37 | +})); |
| 38 | + |
| 39 | +class TestResizeObserver { |
| 40 | + observe() {} |
| 41 | + unobserve() {} |
| 42 | + disconnect() {} |
| 43 | +} |
| 44 | +vi.stubGlobal("ResizeObserver", TestResizeObserver); |
| 45 | + |
| 46 | +afterAll(() => { |
| 47 | + vi.unstubAllGlobals(); |
| 48 | +}); |
| 49 | +afterEach(() => { |
| 50 | + cleanup(); |
| 51 | + vi.clearAllMocks(); |
| 52 | +}); |
| 53 | + |
| 54 | +it("retains mounted rows through a deferred real-session page and profile noise", async () => { |
| 55 | + const relay = keypair(); |
| 56 | + const viewer = keypair(); |
| 57 | + const aliceKey = keypair(); |
| 58 | + const bobKey = keypair(); |
| 59 | + const wire = scriptedTransport(viewer.pubkey, relay.pubkey); |
| 60 | + let traffic!: LiveCallbacks; |
| 61 | + const owner = createRelaySession({ |
| 62 | + ...wire.transport, |
| 63 | + subscribe(callbacks) { |
| 64 | + traffic = callbacks; |
| 65 | + return { update() {}, retry() {}, dispose() {} }; |
| 66 | + }, |
| 67 | + }); |
| 68 | + const root = message(aliceKey, "a", "Root body", 1, [ |
| 69 | + ["imeta", "url https://example.com/video.mp4", "m video/mp4"], |
| 70 | + ]); |
| 71 | + const reply = message(bobKey, "a", "⏱ 0:42 — Reply body", 2, [ |
| 72 | + ["e", root.id, "", "root"], |
| 73 | + ["e", root.id, "", "reply"], |
| 74 | + ]); |
| 75 | + traffic.receive([ |
| 76 | + root, |
| 77 | + reply, |
| 78 | + profile(aliceKey, { name: "Alice" }), |
| 79 | + profile(bobKey, { name: "Bob" }), |
| 80 | + ]); |
| 81 | + const session = owner.session; |
| 82 | + |
| 83 | + render( |
| 84 | + <StrictMode> |
| 85 | + <ThreadPanel |
| 86 | + session={session} |
| 87 | + scope="identity-test" |
| 88 | + channelName="A" |
| 89 | + channelId="a" |
| 90 | + messageId={root.id} |
| 91 | + close={() => {}} |
| 92 | + onOpenLink={() => false} |
| 93 | + /> |
| 94 | + </StrictMode>, |
| 95 | + ); |
| 96 | + |
| 97 | + await waitFor(() => |
| 98 | + expect(wire.pending.some((request) => !request.signal?.aborted)).toBe(true), |
| 99 | + ); |
| 100 | + const initial = wire.pending.find((request) => !request.signal?.aborted); |
| 101 | + if (!initial) throw new Error("Initial thread page was not requested"); |
| 102 | + initial.respond([root, reply]); |
| 103 | + expect(await screen.findByText("Alice")).toBeInTheDocument(); |
| 104 | + expect(screen.getByText("Bob")).toBeInTheDocument(); |
| 105 | + expect(await screen.findByText("Loading thread…")).toBeInTheDocument(); |
| 106 | + await waitFor(() => |
| 107 | + expect( |
| 108 | + wire.pending.filter( |
| 109 | + (request) => request !== initial && !request.signal?.aborted, |
| 110 | + ), |
| 111 | + ).toHaveLength(1), |
| 112 | + ); |
| 113 | + const page = wire.pending.find( |
| 114 | + (request) => request !== initial && !request.signal?.aborted, |
| 115 | + ); |
| 116 | + if (!page) throw new Error("Deferred thread page was not requested"); |
| 117 | + bodyRender.mockClear(); |
| 118 | + |
| 119 | + // An unrelated signed profile preserves the selected map, but the shared |
| 120 | + // name service still invalidates every mounted row. Do not claim zero renders. |
| 121 | + await act(async () => { |
| 122 | + traffic.receive([profile(keypair(), { name: "Other" })]); |
| 123 | + }); |
| 124 | + expect(bodyRender.mock.calls).toEqual([ |
| 125 | + ["Root body"], |
| 126 | + ["Root body"], |
| 127 | + ["Reply body"], |
| 128 | + ["Reply body"], |
| 129 | + ]); |
| 130 | + bodyRender.mockClear(); |
| 131 | + |
| 132 | + const appended = message(bobKey, "a", "Appended reply body", 3, [ |
| 133 | + ["e", root.id, "", "root"], |
| 134 | + ["e", root.id, "", "reply"], |
| 135 | + ]); |
| 136 | + await act(async () => page.respond([root, appended])); |
| 137 | + expect(await screen.findByText("Appended reply body")).toBeInTheDocument(); |
| 138 | + expect(bodyRender.mock.calls).toEqual([ |
| 139 | + ["Appended reply body"], |
| 140 | + ["Appended reply body"], |
| 141 | + ]); |
| 142 | + bodyRender.mockClear(); |
| 143 | + |
| 144 | + fireEvent.click(screen.getByRole("button", { name: "0:42" })); |
| 145 | + expect(screen.getByTestId("video-seek")).toHaveTextContent("42"); |
| 146 | + bodyRender.mockClear(); |
| 147 | + |
| 148 | + const edited = signed(bobKey, { |
| 149 | + kind: 40003, |
| 150 | + content: "Edited reply body", |
| 151 | + created_at: 4, |
| 152 | + tags: [["e", reply.id]], |
| 153 | + }); |
| 154 | + traffic.receive([edited]); |
| 155 | + expect(await screen.findByText("Edited reply body")).toBeInTheDocument(); |
| 156 | + expect(bodyRender).toHaveBeenCalledWith("Edited reply body"); |
| 157 | + bodyRender.mockClear(); |
| 158 | + |
| 159 | + await act(async () => { |
| 160 | + traffic.receive([profile(bobKey, { name: "Robert" }, 1_700_000_001)]); |
| 161 | + }); |
| 162 | + expect(await screen.findAllByText("Robert")).toHaveLength(2); |
| 163 | + expect(bodyRender).toHaveBeenCalled(); |
| 164 | + owner.dispose(); |
| 165 | +}); |
0 commit comments