|
| 1 | +/* @vitest-environment jsdom */ |
| 2 | + |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { |
| 5 | + enhanceMarkdownTables, |
| 6 | + handleMarkdownTableInteraction, |
| 7 | + releaseMarkdownTables, |
| 8 | +} from "./markdown-tables.ts"; |
| 9 | +import { toSanitizedMarkdownHtml } from "./markdown.ts"; |
| 10 | + |
| 11 | +const { copyToClipboard } = vi.hoisted(() => ({ |
| 12 | + copyToClipboard: vi.fn(async () => true), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("../lib/clipboard.ts", () => ({ copyToClipboard })); |
| 16 | + |
| 17 | +const markdown = `Open agent:main:dashboard:table |
| 18 | +
|
| 19 | +<progress value="3" max="7"></progress> |
| 20 | +
|
| 21 | +| Name | Value | |
| 22 | +| --- | --- | |
| 23 | +| Alpha | One |`; |
| 24 | + |
| 25 | +class TestMutationObserver { |
| 26 | + static instances: TestMutationObserver[] = []; |
| 27 | + readonly disconnect = vi.fn(); |
| 28 | + readonly observe = vi.fn(); |
| 29 | + |
| 30 | + constructor(readonly callback: MutationCallback) { |
| 31 | + TestMutationObserver.instances.push(this); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +class TestResizeObserver { |
| 36 | + static instances: TestResizeObserver[] = []; |
| 37 | + readonly disconnect = vi.fn(); |
| 38 | + readonly observe = vi.fn(); |
| 39 | + readonly unobserve = vi.fn(); |
| 40 | + |
| 41 | + constructor(readonly callback: ResizeObserverCallback) { |
| 42 | + TestResizeObserver.instances.push(this); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function interactiveOwner(): { |
| 47 | + owner: HTMLElement; |
| 48 | + shell: HTMLElement; |
| 49 | + viewport: HTMLElement; |
| 50 | +} { |
| 51 | + const owner = document.createElement("div"); |
| 52 | + owner.className = "chat-thread"; |
| 53 | + owner.innerHTML = `<div class="chat-text">${toSanitizedMarkdownHtml(markdown, { |
| 54 | + progressBars: true, |
| 55 | + sessionLinks: true, |
| 56 | + tableInteractions: "enabled", |
| 57 | + })}</div>`; |
| 58 | + document.body.append(owner); |
| 59 | + const shell = owner.querySelector<HTMLElement>(".markdown-table")!; |
| 60 | + const viewport = owner.querySelector<HTMLElement>(".markdown-table__viewport")!; |
| 61 | + Object.defineProperties(viewport, { |
| 62 | + clientWidth: { configurable: true, value: 100 }, |
| 63 | + scrollLeft: { configurable: true, value: 0, writable: true }, |
| 64 | + scrollWidth: { configurable: true, value: 300 }, |
| 65 | + }); |
| 66 | + owner.addEventListener("click", handleMarkdownTableInteraction); |
| 67 | + enhanceMarkdownTables(owner); |
| 68 | + return { owner, shell, viewport }; |
| 69 | +} |
| 70 | + |
| 71 | +describe("Markdown table interactions", () => { |
| 72 | + beforeEach(() => { |
| 73 | + TestMutationObserver.instances = []; |
| 74 | + TestResizeObserver.instances = []; |
| 75 | + vi.stubGlobal("MutationObserver", TestMutationObserver); |
| 76 | + vi.stubGlobal("ResizeObserver", TestResizeObserver); |
| 77 | + copyToClipboard.mockClear(); |
| 78 | + copyToClipboard.mockResolvedValue(true); |
| 79 | + Object.defineProperty(HTMLDialogElement.prototype, "showModal", { |
| 80 | + configurable: true, |
| 81 | + value: vi.fn(function (this: HTMLDialogElement) { |
| 82 | + this.setAttribute("open", ""); |
| 83 | + }), |
| 84 | + }); |
| 85 | + Object.defineProperty(HTMLDialogElement.prototype, "close", { |
| 86 | + configurable: true, |
| 87 | + value: vi.fn(function (this: HTMLDialogElement) { |
| 88 | + this.removeAttribute("open"); |
| 89 | + this.dispatchEvent(new Event("close")); |
| 90 | + }), |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + afterEach(() => { |
| 95 | + vi.useRealTimers(); |
| 96 | + vi.unstubAllGlobals(); |
| 97 | + document.body.replaceChildren(); |
| 98 | + }); |
| 99 | + |
| 100 | + it("composes table chrome with session links and progress markup", () => { |
| 101 | + const disabled = toSanitizedMarkdownHtml(markdown, { |
| 102 | + progressBars: true, |
| 103 | + sessionLinks: true, |
| 104 | + }); |
| 105 | + const enabled = toSanitizedMarkdownHtml(markdown, { |
| 106 | + progressBars: true, |
| 107 | + sessionLinks: true, |
| 108 | + tableInteractions: "enabled", |
| 109 | + }); |
| 110 | + |
| 111 | + expect(disabled).not.toContain("data-table-interactions"); |
| 112 | + expect(enabled).toContain("data-table-interactions"); |
| 113 | + expect(enabled).toContain('data-session-key="agent:main:dashboard:table"'); |
| 114 | + expect(enabled).toContain('<progress value="3" max="7"></progress>'); |
| 115 | + }); |
| 116 | + |
| 117 | + it("tracks hidden columns in both scroll directions", () => { |
| 118 | + const { shell, viewport } = interactiveOwner(); |
| 119 | + |
| 120 | + expect(shell.classList.contains("markdown-table--can-scroll-left")).toBe(false); |
| 121 | + expect(shell.classList.contains("markdown-table--can-scroll-right")).toBe(true); |
| 122 | + |
| 123 | + viewport.scrollLeft = 100; |
| 124 | + viewport.dispatchEvent(new Event("scroll")); |
| 125 | + expect(shell.classList.contains("markdown-table--can-scroll-left")).toBe(true); |
| 126 | + expect(shell.classList.contains("markdown-table--can-scroll-right")).toBe(true); |
| 127 | + |
| 128 | + viewport.scrollLeft = 200; |
| 129 | + viewport.dispatchEvent(new Event("scroll")); |
| 130 | + expect(shell.classList.contains("markdown-table--can-scroll-left")).toBe(true); |
| 131 | + expect(shell.classList.contains("markdown-table--can-scroll-right")).toBe(false); |
| 132 | + }); |
| 133 | + |
| 134 | + it("copies TSV and restores focus after the table dialog closes", async () => { |
| 135 | + vi.useFakeTimers(); |
| 136 | + const { owner } = interactiveOwner(); |
| 137 | + const copy = owner.querySelector<HTMLButtonElement>(".markdown-table__copy")!; |
| 138 | + copy.click(); |
| 139 | + await vi.waitFor(() => { |
| 140 | + expect(copyToClipboard).toHaveBeenCalledWith("Name\tValue\nAlpha\tOne"); |
| 141 | + }); |
| 142 | + expect(copy.getAttribute("aria-label")).toBe("Copied!"); |
| 143 | + |
| 144 | + const expand = owner.querySelector<HTMLButtonElement>(".markdown-table__expand")!; |
| 145 | + expand.focus(); |
| 146 | + expand.click(); |
| 147 | + const dialog = document.querySelector<HTMLDialogElement>(".markdown-table-dialog")!; |
| 148 | + expect(dialog.hasAttribute("open")).toBe(true); |
| 149 | + expect(dialog.querySelector("table")?.textContent).toContain("Alpha"); |
| 150 | + |
| 151 | + dialog.querySelector<HTMLButtonElement>(".markdown-table-dialog__close")!.click(); |
| 152 | + expect(document.querySelector(".markdown-table-dialog")).toBeNull(); |
| 153 | + expect(document.activeElement).toBe(expand); |
| 154 | + }); |
| 155 | + |
| 156 | + it("disconnects observers when the transcript owner is released", () => { |
| 157 | + const { owner } = interactiveOwner(); |
| 158 | + const mutation = TestMutationObserver.instances.at(-1)!; |
| 159 | + const resize = TestResizeObserver.instances.at(-1)!; |
| 160 | + |
| 161 | + releaseMarkdownTables(owner); |
| 162 | + |
| 163 | + expect(mutation.disconnect).toHaveBeenCalledOnce(); |
| 164 | + expect(resize.disconnect).toHaveBeenCalledOnce(); |
| 165 | + }); |
| 166 | +}); |
0 commit comments