|
1 | 1 | /* @vitest-environment jsdom */ |
2 | 2 |
|
3 | | -import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
4 | 4 | import { openEditor } from "../../../lib/editor-links.ts"; |
5 | 5 | import { hasUniformLineEndings } from "./chat-sidebar.ts"; |
6 | 6 |
|
@@ -217,3 +217,280 @@ describe("markdown sidebar", () => { |
217 | 217 | panel.remove(); |
218 | 218 | }); |
219 | 219 | }); |
| 220 | + |
| 221 | +describe("file sidebar clipboard feedback", () => { |
| 222 | + const originalExecCommand = Object.getOwnPropertyDescriptor(document, "execCommand"); |
| 223 | + const copyActions = [ |
| 224 | + { label: "Copy path", value: "src/example.ts" }, |
| 225 | + { label: "Copy file contents", value: "const answer = 42;" }, |
| 226 | + ]; |
| 227 | + |
| 228 | + type FilePanel = HTMLElement & { |
| 229 | + content: unknown; |
| 230 | + ensureFileEditor: () => Promise<void>; |
| 231 | + updateComplete: Promise<unknown>; |
| 232 | + }; |
| 233 | + |
| 234 | + async function mountFilePanel(): Promise<FilePanel> { |
| 235 | + const panel = document.createElement("openclaw-chat-detail-panel") as FilePanel; |
| 236 | + panel.content = { |
| 237 | + kind: "file", |
| 238 | + path: "src/example.ts", |
| 239 | + name: "example.ts", |
| 240 | + content: "const answer = 42;", |
| 241 | + }; |
| 242 | + vi.spyOn(panel, "ensureFileEditor").mockResolvedValue(); |
| 243 | + document.body.append(panel); |
| 244 | + await panel.updateComplete; |
| 245 | + return panel; |
| 246 | + } |
| 247 | + |
| 248 | + function findCopyButton(panel: FilePanel, label: string): HTMLButtonElement { |
| 249 | + const button = Array.from(panel.querySelectorAll<HTMLButtonElement>("button")).find( |
| 250 | + (candidate) => candidate.getAttribute("aria-label") === label, |
| 251 | + ); |
| 252 | + if (!button) { |
| 253 | + throw new Error(`Missing sidebar button: ${label}`); |
| 254 | + } |
| 255 | + return button; |
| 256 | + } |
| 257 | + |
| 258 | + function denyClipboard() { |
| 259 | + const writeText = vi.fn().mockRejectedValue(new DOMException("Clipboard access denied")); |
| 260 | + const execCommand = vi.fn(() => false); |
| 261 | + vi.stubGlobal("navigator", { clipboard: { writeText } }); |
| 262 | + Object.defineProperty(document, "execCommand", { configurable: true, value: execCommand }); |
| 263 | + return { execCommand, writeText }; |
| 264 | + } |
| 265 | + |
| 266 | + function captureFeedbackTimers() { |
| 267 | + const schedule = vi.spyOn(globalThis, "setTimeout"); |
| 268 | + return { |
| 269 | + schedule, |
| 270 | + run(delay: number, index = 0) { |
| 271 | + const timerIndex = schedule.mock.calls |
| 272 | + .map(([, timeout], callIndex) => (timeout === delay ? callIndex : -1)) |
| 273 | + .filter((callIndex) => callIndex >= 0)[index]; |
| 274 | + if (timerIndex === undefined) { |
| 275 | + throw new Error(`Missing sidebar clipboard reset timer after ${delay}ms`); |
| 276 | + } |
| 277 | + const reset = schedule.mock.calls[timerIndex]?.[0]; |
| 278 | + if (typeof reset !== "function") { |
| 279 | + throw new Error(`Expected sidebar clipboard reset timer after ${delay}ms`); |
| 280 | + } |
| 281 | + globalThis.clearTimeout(schedule.mock.results[timerIndex]?.value); |
| 282 | + reset(); |
| 283 | + }, |
| 284 | + }; |
| 285 | + } |
| 286 | + |
| 287 | + afterEach(() => { |
| 288 | + vi.restoreAllMocks(); |
| 289 | + vi.unstubAllGlobals(); |
| 290 | + if (originalExecCommand) { |
| 291 | + Object.defineProperty(document, "execCommand", originalExecCommand); |
| 292 | + } else { |
| 293 | + Reflect.deleteProperty(document, "execCommand"); |
| 294 | + } |
| 295 | + document.body.replaceChildren(); |
| 296 | + }); |
| 297 | + |
| 298 | + it.each(copyActions)( |
| 299 | + "shows and resets a visible accessible error when $label fails", |
| 300 | + async ({ label, value }) => { |
| 301 | + const { execCommand, writeText } = denyClipboard(); |
| 302 | + const panel = await mountFilePanel(); |
| 303 | + const button = findCopyButton(panel, label); |
| 304 | + const timers = captureFeedbackTimers(); |
| 305 | + |
| 306 | + button.click(); |
| 307 | + await vi.waitFor(() => expect(button.getAttribute("aria-label")).toBe("Copy failed")); |
| 308 | + |
| 309 | + expect(writeText).toHaveBeenCalledWith(value); |
| 310 | + expect(execCommand).toHaveBeenCalledWith("copy"); |
| 311 | + expect(panel.querySelector('[role="alert"]')?.textContent).toContain("Copy failed"); |
| 312 | + |
| 313 | + timers.run(2_000); |
| 314 | + await panel.updateComplete; |
| 315 | + |
| 316 | + expect(button.getAttribute("aria-label")).toBe(label); |
| 317 | + expect(panel.querySelector('[role="alert"]')).toBeNull(); |
| 318 | + }, |
| 319 | + ); |
| 320 | + |
| 321 | + it.each(copyActions)( |
| 322 | + "preserves and resets successful $label feedback", |
| 323 | + async ({ label, value }) => { |
| 324 | + const writeText = vi.fn().mockResolvedValue(undefined); |
| 325 | + vi.stubGlobal("navigator", { clipboard: { writeText } }); |
| 326 | + const panel = await mountFilePanel(); |
| 327 | + const button = findCopyButton(panel, label); |
| 328 | + const timers = captureFeedbackTimers(); |
| 329 | + |
| 330 | + button.click(); |
| 331 | + await vi.waitFor(() => expect(button.getAttribute("aria-label")).toBe("Copied!")); |
| 332 | + |
| 333 | + expect(writeText).toHaveBeenCalledWith(value); |
| 334 | + expect(button.classList.contains("copied")).toBe(true); |
| 335 | + expect(panel.querySelector('[role="alert"]')).toBeNull(); |
| 336 | + |
| 337 | + timers.run(1_500); |
| 338 | + await panel.updateComplete; |
| 339 | + |
| 340 | + expect(button.getAttribute("aria-label")).toBe(label); |
| 341 | + expect(button.classList.contains("copied")).toBe(false); |
| 342 | + }, |
| 343 | + ); |
| 344 | + |
| 345 | + it.each(copyActions)( |
| 346 | + "ignores an older successful $label attempt after a failed retry", |
| 347 | + async ({ label }) => { |
| 348 | + const { writeText } = denyClipboard(); |
| 349 | + let finishFirstCopy = () => {}; |
| 350 | + writeText.mockReturnValueOnce( |
| 351 | + new Promise<void>((resolve) => { |
| 352 | + finishFirstCopy = resolve; |
| 353 | + }), |
| 354 | + ); |
| 355 | + const panel = await mountFilePanel(); |
| 356 | + const button = findCopyButton(panel, label); |
| 357 | + |
| 358 | + button.click(); |
| 359 | + button.click(); |
| 360 | + await vi.waitFor(() => expect(button.getAttribute("aria-label")).toBe("Copy failed")); |
| 361 | + finishFirstCopy(); |
| 362 | + await Promise.resolve(); |
| 363 | + await Promise.resolve(); |
| 364 | + await panel.updateComplete; |
| 365 | + |
| 366 | + expect(writeText).toHaveBeenCalledTimes(2); |
| 367 | + expect(button.getAttribute("aria-label")).toBe("Copy failed"); |
| 368 | + expect(panel.querySelector('[role="alert"]')?.textContent).toContain("Copy failed"); |
| 369 | + }, |
| 370 | + ); |
| 371 | + |
| 372 | + it("keeps path and contents feedback reset timers independent", async () => { |
| 373 | + denyClipboard(); |
| 374 | + const panel = await mountFilePanel(); |
| 375 | + const pathButton = findCopyButton(panel, "Copy path"); |
| 376 | + const contentsButton = findCopyButton(panel, "Copy file contents"); |
| 377 | + const timers = captureFeedbackTimers(); |
| 378 | + |
| 379 | + pathButton.click(); |
| 380 | + contentsButton.click(); |
| 381 | + await vi.waitFor(() => { |
| 382 | + expect(pathButton.getAttribute("aria-label")).toBe("Copy failed"); |
| 383 | + expect(contentsButton.getAttribute("aria-label")).toBe("Copy failed"); |
| 384 | + }); |
| 385 | + |
| 386 | + timers.run(2_000); |
| 387 | + await panel.updateComplete; |
| 388 | + expect(pathButton.getAttribute("aria-label")).toBe("Copy path"); |
| 389 | + expect(contentsButton.getAttribute("aria-label")).toBe("Copy failed"); |
| 390 | + expect(panel.querySelector('[role="alert"]')?.textContent).toContain("Copy failed"); |
| 391 | + |
| 392 | + timers.run(2_000, 1); |
| 393 | + await panel.updateComplete; |
| 394 | + expect(contentsButton.getAttribute("aria-label")).toBe("Copy file contents"); |
| 395 | + expect(panel.querySelector('[role="alert"]')).toBeNull(); |
| 396 | + }); |
| 397 | + |
| 398 | + it.each(["file selection", "disconnection"])( |
| 399 | + "ignores a delayed successful copy after %s changes its owner", |
| 400 | + async (change) => { |
| 401 | + let finishCopy = () => {}; |
| 402 | + const writeText = vi.fn( |
| 403 | + () => |
| 404 | + new Promise<void>((resolve) => { |
| 405 | + finishCopy = resolve; |
| 406 | + }), |
| 407 | + ); |
| 408 | + vi.stubGlobal("navigator", { clipboard: { writeText } }); |
| 409 | + const panel = await mountFilePanel(); |
| 410 | + const button = findCopyButton(panel, "Copy file contents"); |
| 411 | + const timers = captureFeedbackTimers(); |
| 412 | + |
| 413 | + button.click(); |
| 414 | + if (change === "file selection") { |
| 415 | + panel.content = { |
| 416 | + kind: "file", |
| 417 | + path: "src/next.ts", |
| 418 | + name: "next.ts", |
| 419 | + content: "const next = true;", |
| 420 | + }; |
| 421 | + await panel.updateComplete; |
| 422 | + } else { |
| 423 | + panel.remove(); |
| 424 | + } |
| 425 | + finishCopy(); |
| 426 | + await Promise.resolve(); |
| 427 | + await Promise.resolve(); |
| 428 | + await panel.updateComplete; |
| 429 | + |
| 430 | + expect(timers.schedule.mock.calls.some(([, delay]) => delay === 1_500)).toBe(false); |
| 431 | + expect(button.getAttribute("aria-label")).toBe("Copy file contents"); |
| 432 | + expect(panel.querySelector('[role="alert"]')).toBeNull(); |
| 433 | + }, |
| 434 | + ); |
| 435 | + |
| 436 | + it.each([ |
| 437 | + { label: "Copy path", failed: true }, |
| 438 | + { label: "Copy file contents", failed: false }, |
| 439 | + ])( |
| 440 | + "restores idle $label feedback when the same sidebar reconnects", |
| 441 | + async ({ label, failed }) => { |
| 442 | + if (failed) { |
| 443 | + denyClipboard(); |
| 444 | + } else { |
| 445 | + vi.stubGlobal("navigator", { |
| 446 | + clipboard: { writeText: vi.fn().mockResolvedValue(undefined) }, |
| 447 | + }); |
| 448 | + } |
| 449 | + const panel = await mountFilePanel(); |
| 450 | + const button = findCopyButton(panel, label); |
| 451 | + |
| 452 | + button.click(); |
| 453 | + await vi.waitFor(() => |
| 454 | + expect(button.getAttribute("aria-label")).toBe(failed ? "Copy failed" : "Copied!"), |
| 455 | + ); |
| 456 | + |
| 457 | + panel.remove(); |
| 458 | + document.body.append(panel); |
| 459 | + await panel.updateComplete; |
| 460 | + |
| 461 | + expect(findCopyButton(panel, label)).toBe(button); |
| 462 | + expect(button.classList.contains("copied")).toBe(false); |
| 463 | + expect(panel.querySelector('[role="alert"]')).toBeNull(); |
| 464 | + }, |
| 465 | + ); |
| 466 | + |
| 467 | + it.each(copyActions)( |
| 468 | + "ignores an older $label completion after sidebar reconnection", |
| 469 | + async ({ label }) => { |
| 470 | + let finishCopy = () => {}; |
| 471 | + const writeText = vi.fn( |
| 472 | + () => |
| 473 | + new Promise<void>((resolve) => { |
| 474 | + finishCopy = resolve; |
| 475 | + }), |
| 476 | + ); |
| 477 | + vi.stubGlobal("navigator", { clipboard: { writeText } }); |
| 478 | + const panel = await mountFilePanel(); |
| 479 | + const button = findCopyButton(panel, label); |
| 480 | + const timers = captureFeedbackTimers(); |
| 481 | + |
| 482 | + button.click(); |
| 483 | + panel.remove(); |
| 484 | + document.body.append(panel); |
| 485 | + await panel.updateComplete; |
| 486 | + finishCopy(); |
| 487 | + await Promise.resolve(); |
| 488 | + await Promise.resolve(); |
| 489 | + await panel.updateComplete; |
| 490 | + |
| 491 | + expect(button.getAttribute("aria-label")).toBe(label); |
| 492 | + expect(timers.schedule.mock.calls.some(([, delay]) => delay === 1_500)).toBe(false); |
| 493 | + expect(panel.querySelector('[role="alert"]')).toBeNull(); |
| 494 | + }, |
| 495 | + ); |
| 496 | +}); |
0 commit comments