|
| 1 | +// @vitest-environment jsdom |
| 2 | +import "@testing-library/jest-dom/vitest"; |
| 3 | +import { cleanup, render, screen, waitFor } from "@testing-library/react"; |
| 4 | +import userEvent from "@testing-library/user-event"; |
| 5 | +import type { EventTemplate } from "nostr-tools"; |
| 6 | +import { afterEach, assert, expect, it, vi } from "vitest"; |
| 7 | +import type { RelayEvent } from "../relay/events"; |
| 8 | +import { createRelaySession } from "../relay/session"; |
| 9 | +import { keypair, message, metadata, roster, signed } from "../relay/testing"; |
| 10 | +import { MediaReviewViewer } from "./MediaReviewViewer"; |
| 11 | + |
| 12 | +const owners: ReturnType<typeof createRelaySession>[] = []; |
| 13 | + |
| 14 | +afterEach(() => { |
| 15 | + cleanup(); |
| 16 | + for (const owner of owners.splice(0)) owner.dispose(); |
| 17 | + localStorage.clear(); |
| 18 | +}); |
| 19 | + |
| 20 | +type MediaKind = "image" | "video"; |
| 21 | +type ReviewFixtureOptions = { |
| 22 | + kind: MediaKind; |
| 23 | + replies?: ( |
| 24 | + viewer: ReturnType<typeof keypair>, |
| 25 | + root: RelayEvent, |
| 26 | + ) => readonly RelayEvent[]; |
| 27 | + extraRootTags?: readonly string[][]; |
| 28 | +}; |
| 29 | + |
| 30 | +function mime(kind: MediaKind) { |
| 31 | + return kind === "video" ? "video/mp4" : "image/png"; |
| 32 | +} |
| 33 | + |
| 34 | +async function setupReview({ |
| 35 | + kind, |
| 36 | + replies: buildReplies = () => [], |
| 37 | + extraRootTags = [], |
| 38 | +}: ReviewFixtureOptions) { |
| 39 | + const viewer = keypair(); |
| 40 | + const relay = keypair(); |
| 41 | + const attachment = { |
| 42 | + url: `https://fixture.test/${kind}.${kind === "video" ? "mp4" : "png"}`, |
| 43 | + kind, |
| 44 | + }; |
| 45 | + const root = message(viewer, "one", `${kind} root`, 1, [ |
| 46 | + ["imeta", `url ${attachment.url}`, `m ${mime(kind)}`], |
| 47 | + ...extraRootTags, |
| 48 | + ]); |
| 49 | + const replies = buildReplies(viewer, root); |
| 50 | + const sign = vi.fn(async (template: EventTemplate) => |
| 51 | + signed(viewer, template), |
| 52 | + ); |
| 53 | + const publish = vi.fn(async () => {}); |
| 54 | + const events = [root, ...replies]; |
| 55 | + const owner = createRelaySession( |
| 56 | + { |
| 57 | + viewer: viewer.pubkey, |
| 58 | + relayAuthor: relay.pubkey, |
| 59 | + media: (url) => url, |
| 60 | + writer: { sign, publish }, |
| 61 | + async query(filters) { |
| 62 | + return filters.flatMap((filter) => { |
| 63 | + if (filter.kinds?.includes(39002) || filter.kinds?.includes(39000)) |
| 64 | + return [ |
| 65 | + roster(relay, "one", [viewer.pubkey]), |
| 66 | + metadata(relay, "one", "One"), |
| 67 | + ]; |
| 68 | + if (filter.ids) |
| 69 | + return events.filter((event) => filter.ids?.includes(event.id)); |
| 70 | + if (filter.depth_limit) |
| 71 | + return replies.filter( |
| 72 | + (event) => |
| 73 | + filter.thread_cursor === undefined || |
| 74 | + event.created_at > filter.thread_cursor, |
| 75 | + ); |
| 76 | + return []; |
| 77 | + }); |
| 78 | + }, |
| 79 | + }, |
| 80 | + { outboxStorage: { load: () => [], save: () => {} } }, |
| 81 | + ); |
| 82 | + owners.push(owner); |
| 83 | + await owner.session.read([ |
| 84 | + { kinds: [39002, 39000], "#d": ["one"], limit: 10 }, |
| 85 | + ]); |
| 86 | + const user = userEvent.setup(); |
| 87 | + render( |
| 88 | + <MediaReviewViewer |
| 89 | + attachment={attachment} |
| 90 | + session={owner.session} |
| 91 | + scope={`review-${kind}`} |
| 92 | + channelId="one" |
| 93 | + channelName="One" |
| 94 | + messageId={root.id} |
| 95 | + initialTime={72} |
| 96 | + close={() => {}} |
| 97 | + />, |
| 98 | + ); |
| 99 | + return { owner, root, sign, user }; |
| 100 | +} |
| 101 | + |
| 102 | +async function signedTemplate(sign: ReturnType<typeof vi.fn>) { |
| 103 | + await waitFor(() => expect(sign).toHaveBeenCalled()); |
| 104 | + return sign.mock.calls.at(-1)?.[0] as EventTemplate; |
| 105 | +} |
| 106 | + |
| 107 | +async function typeAndSend( |
| 108 | + user: ReturnType<typeof userEvent.setup>, |
| 109 | + text: string, |
| 110 | +) { |
| 111 | + await user.type( |
| 112 | + await screen.findByRole("textbox", { name: "Reply to thread" }), |
| 113 | + text, |
| 114 | + ); |
| 115 | + await user.click(screen.getByRole("button", { name: "Send message" })); |
| 116 | +} |
| 117 | + |
| 118 | +it("publishes checked video review comments as time-prefixed kind 9 replies to the root", async () => { |
| 119 | + const { root, sign, user } = await setupReview({ kind: "video" }); |
| 120 | + await screen.findByRole("dialog", { name: "Video review" }); |
| 121 | + expect( |
| 122 | + await screen.findByRole("checkbox", { name: "Comment at current frame" }), |
| 123 | + ).toBeChecked(); |
| 124 | + |
| 125 | + await typeAndSend(user, "adjust the cut"); |
| 126 | + |
| 127 | + const event = await signedTemplate(sign); |
| 128 | + expect(event.kind).toBe(9); |
| 129 | + expect(event.content).toBe("⏱ 1:12 — adjust the cut"); |
| 130 | + expect(event.tags).toEqual( |
| 131 | + expect.arrayContaining([ |
| 132 | + ["h", "one"], |
| 133 | + ["e", root.id, "", "reply"], |
| 134 | + ]), |
| 135 | + ); |
| 136 | +}); |
| 137 | + |
| 138 | +it("publishes unchecked video review comments without a time prefix", async () => { |
| 139 | + const { root, sign, user } = await setupReview({ kind: "video" }); |
| 140 | + await user.click( |
| 141 | + await screen.findByRole("checkbox", { name: "Comment at current frame" }), |
| 142 | + ); |
| 143 | + |
| 144 | + await typeAndSend(user, "plain comment"); |
| 145 | + |
| 146 | + const event = await signedTemplate(sign); |
| 147 | + expect(event.content).toBe("plain comment"); |
| 148 | + expect(event.tags).toEqual( |
| 149 | + expect.arrayContaining([["e", root.id, "", "reply"]]), |
| 150 | + ); |
| 151 | +}); |
| 152 | + |
| 153 | +it("publishes image review comments as plain replies without a frame checkbox", async () => { |
| 154 | + const { root, sign, user } = await setupReview({ kind: "image" }); |
| 155 | + await screen.findByRole("dialog", { name: "Image viewer" }); |
| 156 | + expect( |
| 157 | + screen.queryByLabelText("Comment at current frame"), |
| 158 | + ).not.toBeInTheDocument(); |
| 159 | + |
| 160 | + await typeAndSend(user, "image note"); |
| 161 | + |
| 162 | + const event = await signedTemplate(sign); |
| 163 | + expect(event.content).toBe("image note"); |
| 164 | + expect(event.tags).toEqual( |
| 165 | + expect.arrayContaining([["e", root.id, "", "reply"]]), |
| 166 | + ); |
| 167 | +}); |
| 168 | + |
| 169 | +it("seeks the review video from a timecode reply when the thread has one video", async () => { |
| 170 | + const { user } = await setupReview({ |
| 171 | + kind: "video", |
| 172 | + replies: (viewer, root) => [ |
| 173 | + message(viewer, "one", "⏱ 0:42 — jump here", 2, [ |
| 174 | + ["e", root.id, "", "reply"], |
| 175 | + ]), |
| 176 | + ], |
| 177 | + }); |
| 178 | + await screen.findByText("jump here"); |
| 179 | + const dialog = await screen.findByRole("dialog", { name: "Video review" }); |
| 180 | + const video = dialog.querySelector("video"); |
| 181 | + assert.exists(video); |
| 182 | + const play = vi.fn(async () => {}); |
| 183 | + // jsdom does not implement media playback, so play() needs a test stub. |
| 184 | + Object.defineProperty(video, "play", { configurable: true, value: play }); |
| 185 | + |
| 186 | + await user.click(screen.getByRole("button", { name: "0:42" })); |
| 187 | + |
| 188 | + // jsdom media time remains at initialTime until the seek handler sets it. |
| 189 | + expect(video.currentTime).toBe(42); |
| 190 | + expect(play).toHaveBeenCalledOnce(); |
| 191 | +}); |
| 192 | + |
| 193 | +it("preserves timecode reply text without a seek chip when the thread has multiple videos", async () => { |
| 194 | + await setupReview({ |
| 195 | + kind: "video", |
| 196 | + extraRootTags: [ |
| 197 | + ["imeta", "url https://fixture.test/other.mp4", "m video/mp4"], |
| 198 | + ], |
| 199 | + replies: (viewer, root) => [ |
| 200 | + message(viewer, "one", "⏱ 0:42 — keep text", 2, [ |
| 201 | + ["e", root.id, "", "reply"], |
| 202 | + ]), |
| 203 | + ], |
| 204 | + }); |
| 205 | + |
| 206 | + expect(await screen.findByText("⏱ 0:42 — keep text")).toBeVisible(); |
| 207 | + expect( |
| 208 | + screen.queryByRole("button", { name: "0:42" }), |
| 209 | + ).not.toBeInTheDocument(); |
| 210 | +}); |
0 commit comments