|
1 | 1 | // @vitest-environment jsdom |
2 | 2 | import "@testing-library/jest-dom/vitest"; |
3 | | -import { cleanup, render, screen } from "@testing-library/react"; |
| 3 | +import { act, cleanup, render, screen, waitFor } from "@testing-library/react"; |
4 | 4 | import userEvent from "@testing-library/user-event"; |
5 | 5 | import { afterEach, expect, it, vi } from "vitest"; |
| 6 | +import { keypair, signed } from "../../features/relay/testing"; |
| 7 | +import type { ChannelCanvas } from "../../features/channel-templates/capability"; |
6 | 8 | import { ChannelSettingsPanel } from "./ChannelSettingsPanel"; |
7 | 9 |
|
8 | 10 | afterEach(cleanup); |
@@ -77,3 +79,210 @@ it("does not invent unknown metadata and still exposes diagnostics without a cha |
77 | 79 | await user.click(screen.getByText("Diagnostics")); |
78 | 80 | expect(screen.getByText("Diagnostics content")).toBeVisible(); |
79 | 81 | }); |
| 82 | + |
| 83 | +it("opens Canvas from its own keyboard-accessible row before setup actions", async () => { |
| 84 | + const user = userEvent.setup(); |
| 85 | + const openCanvas = vi.fn(); |
| 86 | + render( |
| 87 | + <ChannelSettingsPanel |
| 88 | + channel={{ id: "alpha", name: "Alpha", channelType: "stream" }} |
| 89 | + close={() => {}} |
| 90 | + openCanvas={openCanvas} |
| 91 | + setupTools={<button type="button">Leave channel</button>} |
| 92 | + > |
| 93 | + Diagnostics content |
| 94 | + </ChannelSettingsPanel>, |
| 95 | + ); |
| 96 | + const canvas = screen.getByRole("button", { name: "Canvas" }); |
| 97 | + expect(canvas).toHaveAttribute("aria-haspopup", "dialog"); |
| 98 | + expect(canvas).not.toHaveTextContent("Shared notes and plans"); |
| 99 | + expect(openCanvas).not.toHaveBeenCalled(); |
| 100 | + await user.tab(); |
| 101 | + expect(canvas).toHaveFocus(); |
| 102 | + await user.keyboard("{Enter}"); |
| 103 | + expect(openCanvas).toHaveBeenCalledTimes(1); |
| 104 | + expect(openCanvas).toHaveBeenLastCalledWith(canvas); |
| 105 | + await user.tab(); |
| 106 | + expect(screen.getByRole("button", { name: "Leave channel" })).toHaveFocus(); |
| 107 | + await user.click(canvas); |
| 108 | + expect(openCanvas).toHaveBeenCalledTimes(2); |
| 109 | +}); |
| 110 | + |
| 111 | +it("omits Canvas without a writable channel or an opener", () => { |
| 112 | + const channel = { id: "alpha", name: "Alpha" }; |
| 113 | + const { rerender } = render( |
| 114 | + <ChannelSettingsPanel channel={channel} close={() => {}}> |
| 115 | + Diagnostics content |
| 116 | + </ChannelSettingsPanel>, |
| 117 | + ); |
| 118 | + expect( |
| 119 | + screen.queryByRole("button", { name: "Canvas" }), |
| 120 | + ).not.toBeInTheDocument(); |
| 121 | + for (const unavailable of [ |
| 122 | + undefined, |
| 123 | + { ...channel, readOnly: true as const }, |
| 124 | + ]) { |
| 125 | + rerender( |
| 126 | + <ChannelSettingsPanel |
| 127 | + channel={unavailable} |
| 128 | + close={() => {}} |
| 129 | + openCanvas={() => {}} |
| 130 | + > |
| 131 | + Diagnostics content |
| 132 | + </ChannelSettingsPanel>, |
| 133 | + ); |
| 134 | + expect( |
| 135 | + screen.queryByRole("button", { name: "Canvas" }), |
| 136 | + ).not.toBeInTheDocument(); |
| 137 | + } |
| 138 | +}); |
| 139 | + |
| 140 | +const head = signed(keypair(), { |
| 141 | + kind: 40100, |
| 142 | + content: "# Google root-link preview submission deep dive", |
| 143 | + tags: [["h", "alpha"]], |
| 144 | +}); |
| 145 | +function previewFixture() { |
| 146 | + return { |
| 147 | + available: true, |
| 148 | + read: vi.fn<ChannelCanvas["read"]>().mockResolvedValue(head), |
| 149 | + save: vi.fn<ChannelCanvas["save"]>(), |
| 150 | + }; |
| 151 | +} |
| 152 | +function panel( |
| 153 | + canvas: ChannelCanvas, |
| 154 | + id = "alpha", |
| 155 | + canvasOpen = false, |
| 156 | + readOnly?: true, |
| 157 | +) { |
| 158 | + return ( |
| 159 | + <ChannelSettingsPanel |
| 160 | + canvas={canvas} |
| 161 | + channel={{ id, name: id, ...(readOnly ? { readOnly } : {}) }} |
| 162 | + canvasOpen={canvasOpen} |
| 163 | + openCanvas={() => {}} |
| 164 | + close={() => {}} |
| 165 | + > |
| 166 | + Diagnostics |
| 167 | + </ChannelSettingsPanel> |
| 168 | + ); |
| 169 | +} |
| 170 | + |
| 171 | +it("shows Canvas as the first line and saved plain text as the secondary preview", async () => { |
| 172 | + const canvas = previewFixture(); |
| 173 | + const { container } = render(panel(canvas)); |
| 174 | + expect(screen.getByText("Loading preview…")).toBeVisible(); |
| 175 | + await screen.findByText("Google root-link preview submission deep dive"); |
| 176 | + const row = screen.getByRole("button", { name: "Canvas" }); |
| 177 | + expect(row).toHaveAccessibleDescription( |
| 178 | + "Google root-link preview submission deep dive", |
| 179 | + ); |
| 180 | + expect(row.querySelector(".buzz-choice-row-label")).toHaveTextContent( |
| 181 | + /^Canvas$/, |
| 182 | + ); |
| 183 | + expect(row.querySelector(".buzz-choice-row-description")).toHaveTextContent( |
| 184 | + "Google root-link preview submission deep dive", |
| 185 | + ); |
| 186 | + expect(canvas.read).toHaveBeenCalledWith("alpha"); |
| 187 | + expect(canvas.save).not.toHaveBeenCalled(); |
| 188 | + expect(container.querySelector("img, script")).toBeNull(); |
| 189 | +}); |
| 190 | + |
| 191 | +it.each([ |
| 192 | + undefined, |
| 193 | + { ...head, content: " \n" }, |
| 194 | + { ...head, content: "<!-- comment -->" }, |
| 195 | +])("uses neutral empty copy for %j", async (event) => { |
| 196 | + const canvas = previewFixture(); |
| 197 | + canvas.read.mockResolvedValue(event); |
| 198 | + render(panel(canvas)); |
| 199 | + expect(await screen.findByText("No content yet")).toBeVisible(); |
| 200 | + expect(screen.getByRole("button", { name: "Canvas" })).toBeEnabled(); |
| 201 | +}); |
| 202 | + |
| 203 | +it("keeps a failed preview openable and refreshes saved content after closing the editor", async () => { |
| 204 | + const canvas = previewFixture(); |
| 205 | + canvas.read.mockRejectedValueOnce(new Error("Offline")); |
| 206 | + const { rerender } = render(panel(canvas)); |
| 207 | + expect( |
| 208 | + await screen.findByText("Preview unavailable. Open to retry."), |
| 209 | + ).toBeVisible(); |
| 210 | + expect(screen.getByRole("button", { name: "Canvas" })).toBeEnabled(); |
| 211 | + rerender(panel(canvas, "alpha", true)); |
| 212 | + expect(canvas.read).toHaveBeenCalledTimes(1); |
| 213 | + rerender(panel(canvas)); |
| 214 | + expect( |
| 215 | + await screen.findByText("Google root-link preview submission deep dive"), |
| 216 | + ).toBeVisible(); |
| 217 | + rerender(panel(canvas, "alpha", true)); |
| 218 | + canvas.read.mockResolvedValue({ ...head, content: "# Updated document" }); |
| 219 | + rerender(panel(canvas)); |
| 220 | + expect(await screen.findByText("Updated document")).toBeVisible(); |
| 221 | + expect(canvas.save).not.toHaveBeenCalled(); |
| 222 | +}); |
| 223 | + |
| 224 | +it("ignores an old channel's pending preview and hides content immediately on scope or access changes", async () => { |
| 225 | + const canvas = previewFixture(); |
| 226 | + let release!: (event: typeof head) => void; |
| 227 | + canvas.read.mockReturnValueOnce( |
| 228 | + new Promise((resolve) => { |
| 229 | + release = resolve; |
| 230 | + }), |
| 231 | + ); |
| 232 | + const { rerender } = render(panel(canvas)); |
| 233 | + try { |
| 234 | + canvas.read.mockResolvedValue({ ...head, content: "# Beta document" }); |
| 235 | + rerender(panel(canvas, "beta")); |
| 236 | + expect(await screen.findByText("Beta document")).toBeVisible(); |
| 237 | + } finally { |
| 238 | + await act(async () => release(head)); |
| 239 | + } |
| 240 | + expect( |
| 241 | + screen.queryByText("Google root-link preview submission deep dive"), |
| 242 | + ).not.toBeInTheDocument(); |
| 243 | + const otherCommunity = previewFixture(); |
| 244 | + otherCommunity.read.mockResolvedValue(undefined); |
| 245 | + rerender(panel(otherCommunity, "beta")); |
| 246 | + expect(screen.queryByText("Beta document")).not.toBeInTheDocument(); |
| 247 | + expect(await screen.findByText("No content yet")).toBeVisible(); |
| 248 | + rerender(panel(otherCommunity, "beta", false, true)); |
| 249 | + expect( |
| 250 | + screen.queryByRole("button", { name: /^Canvas/ }), |
| 251 | + ).not.toBeInTheDocument(); |
| 252 | + await waitFor(() => expect(otherCommunity.read).toHaveBeenCalledTimes(1)); |
| 253 | +}); |
| 254 | + |
| 255 | +it("replaces a saved preview with loading and failure states during refresh", async () => { |
| 256 | + const canvas = previewFixture(); |
| 257 | + const { rerender } = render(panel(canvas)); |
| 258 | + await screen.findByText("Google root-link preview submission deep dive"); |
| 259 | + rerender(panel(canvas, "alpha", true)); |
| 260 | + let reject!: (error: Error) => void; |
| 261 | + canvas.read.mockReturnValueOnce( |
| 262 | + new Promise((_, rejectRead) => { |
| 263 | + reject = rejectRead; |
| 264 | + }), |
| 265 | + ); |
| 266 | + try { |
| 267 | + rerender(panel(canvas)); |
| 268 | + expect(canvas.read).toHaveBeenCalledTimes(2); |
| 269 | + expect(screen.getByText("Loading preview…")).toBeVisible(); |
| 270 | + expect( |
| 271 | + screen.queryByText("Google root-link preview submission deep dive"), |
| 272 | + ).not.toBeInTheDocument(); |
| 273 | + expect( |
| 274 | + screen.getByRole("button", { name: "Canvas" }), |
| 275 | + ).toHaveAccessibleDescription("Loading preview…"); |
| 276 | + } finally { |
| 277 | + await act(async () => reject(new Error("Offline"))); |
| 278 | + } |
| 279 | + expect(screen.getByText("Preview unavailable. Open to retry.")).toBeVisible(); |
| 280 | + expect( |
| 281 | + screen.getByRole("button", { name: "Canvas" }), |
| 282 | + ).toHaveAccessibleDescription("Preview unavailable. Open to retry."); |
| 283 | + expect(screen.getByRole("button", { name: "Canvas" })).toBeEnabled(); |
| 284 | + expect( |
| 285 | + screen.queryByText("Google root-link preview submission deep dive"), |
| 286 | + ).not.toBeInTheDocument(); |
| 287 | + expect(canvas.save).not.toHaveBeenCalled(); |
| 288 | +}); |
0 commit comments