The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | import { page, userEvent } from "vite-plus/test/browser"; |
| 3 | |
| 4 | import type { ReactNode } from "react"; |
| 5 | |
| 6 | import { act, render } from "../../../test/render"; |
| 7 | import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog"; |
| 8 | import { ThemeEditorPanel } from "./ThemeEditorPanel"; |
| 9 | |
| 10 | function dragHeader(panel: HTMLElement, toX: number, toY: number) { |
| 11 | const header = panel.querySelector<HTMLElement>("h2")!.closest<HTMLElement>(".cursor-grab")!; |
| 12 | const rect = panel.getBoundingClientRect(); |
| 13 | const start = { clientX: rect.left + 40, clientY: rect.top + 10 }; |
| 14 | const init = { bubbles: true, button: 0, pointerId: 1 }; |
| 15 | header.dispatchEvent(new PointerEvent("pointerdown", { ...init, ...start })); |
| 16 | header.dispatchEvent( |
| 17 | new PointerEvent("pointermove", { ...init, clientX: toX + 40, clientY: toY + 10 }), |
| 18 | ); |
| 19 | header.dispatchEvent(new PointerEvent("pointerup", init)); |
| 20 | } |
| 21 | |
| 22 | function dragResizeHandle(panel: HTMLElement, byX: number) { |
| 23 | const handle = panel.querySelector<HTMLElement>(".cursor-se-resize")!; |
| 24 | const rect = handle.getBoundingClientRect(); |
| 25 | const init = { bubbles: true, button: 0, pointerId: 2 }; |
| 26 | const start = { clientX: rect.left + 4, clientY: rect.top + 4 }; |
| 27 | handle.dispatchEvent(new PointerEvent("pointerdown", { ...init, ...start })); |
| 28 | handle.dispatchEvent( |
| 29 | new PointerEvent("pointermove", { |
| 30 | ...init, |
| 31 | clientX: start.clientX + byX, |
| 32 | clientY: start.clientY, |
| 33 | }), |
| 34 | ); |
| 35 | handle.dispatchEvent(new PointerEvent("pointerup", init)); |
| 36 | } |
| 37 | |
| 38 | function pointerAt(target: HTMLElement, type: string, xFraction: number) { |
| 39 | const rect = target.getBoundingClientRect(); |
| 40 | target.dispatchEvent( |
| 41 | new PointerEvent(type, { |
| 42 | bubbles: true, |
| 43 | button: 0, |
| 44 | pointerId: 3, |
| 45 | clientX: rect.left + rect.width * xFraction, |
| 46 | clientY: rect.top + rect.height / 2, |
| 47 | }), |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | async function renderPanel(appContent?: ReactNode) { |
| 52 | await page.viewport(1280, 900); |
| 53 | await using stack = new AsyncDisposableStack(); |
| 54 | // Synthetic pointer ids are not active pointers, so the browser would reject capture. |
| 55 | stack.use(vi.spyOn(HTMLElement.prototype, "setPointerCapture").mockImplementation(() => {})); |
| 56 | // Tailwind is not loaded here; give the panel its fixed, card-sized box. |
| 57 | const style = document.head.appendChild(document.createElement("style")); |
| 58 | style.textContent = "[data-theme-editor-panel]{position:fixed;width:400px;height:300px}"; |
| 59 | stack.defer(() => style.remove()); |
| 60 | const screen = stack.use( |
| 61 | await render( |
| 62 | <> |
| 63 | {appContent} |
| 64 | <ThemeEditorPanel |
| 65 | open |
| 66 | onOpenChange={() => {}} |
| 67 | onSaved={() => true} |
| 68 | editingTheme={null} |
| 69 | initialAppearance="dark" |
| 70 | restoreTheme={() => {}} |
| 71 | /> |
| 72 | </>, |
| 73 | ), |
| 74 | ); |
| 75 | const panel = screen.getByRole("dialog", { name: "Create theme" }); |
| 76 | await expect.element(panel).toBeVisible(); |
| 77 | const owned = stack.move(); |
| 78 | return { |
| 79 | panel: panel.element() as HTMLElement, |
| 80 | [Symbol.asyncDispose]: () => owned.disposeAsync(), |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | // The editor floats beside the Settings dialog, whose Esc handler sits on the document. |
| 85 | async function renderBesideSettings() { |
| 86 | const onSettingsOpenChange = vi.fn(); |
| 87 | const onEditorOpenChange = vi.fn(); |
| 88 | await using stack = new AsyncDisposableStack(); |
| 89 | stack.use( |
| 90 | await render( |
| 91 | <> |
| 92 | <Dialog onOpenChange={onSettingsOpenChange} open> |
| 93 | <DialogPopup> |
| 94 | <DialogTitle>Settings</DialogTitle> |
| 95 | </DialogPopup> |
| 96 | </Dialog> |
| 97 | <ThemeEditorPanel |
| 98 | open |
| 99 | onOpenChange={onEditorOpenChange} |
| 100 | onSaved={() => true} |
| 101 | editingTheme={null} |
| 102 | initialAppearance="light" |
| 103 | restoreTheme={() => {}} |
| 104 | /> |
| 105 | </>, |
| 106 | ), |
| 107 | ); |
| 108 | // Settings moves focus in on a later frame; landing after the test focuses the editor, it steals it. |
| 109 | await expect.element(page.getByRole("button", { name: "Close" })).toHaveFocus(); |
| 110 | const screen = stack.move(); |
| 111 | return { |
| 112 | onSettingsOpenChange, |
| 113 | onEditorOpenChange, |
| 114 | [Symbol.asyncDispose]: () => screen.disposeAsync(), |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | // The modal Settings dialog marks the editor inert, hiding it from role queries and pointer input. |
| 119 | async function editorElement(selector: string) { |
| 120 | const query = () => document.querySelector<HTMLElement>(`[data-theme-editor-panel] ${selector}`); |
| 121 | await expect.poll(query).toBeTruthy(); |
| 122 | return query()!; |
| 123 | } |
| 124 | |
| 125 | describe("ThemeEditorPanel", () => { |
| 126 | it("keeps its left edge when resized after the window shrank", async () => { |
| 127 | await using view = await renderPanel(); |
| 128 | dragHeader(view.panel, 700, 40); |
| 129 | await expect.poll(() => view.panel.style.left).toBe("700px"); |
| 130 | |
| 131 | await page.viewport(600, 900); |
| 132 | await expect.poll(() => Number.parseFloat(view.panel.style.left)).toBeLessThan(600); |
| 133 | const clampedLeft = view.panel.style.left; |
| 134 | |
| 135 | dragResizeHandle(view.panel, -100); |
| 136 | await expect.poll(() => view.panel.style.width).not.toBe(""); |
| 137 | expect(view.panel.style.left).toBe(clampedLeft); |
| 138 | }); |
| 139 | |
| 140 | it("returns to where the user left it when the window shrinks and grows back", async () => { |
| 141 | await page.viewport(1280, 900); |
| 142 | // Synthetic pointer ids are not active pointers, so the browser would reject capture. |
| 143 | using _capture = vi |
| 144 | .spyOn(HTMLElement.prototype, "setPointerCapture") |
| 145 | .mockImplementation(() => {}); |
| 146 | // Tailwind is not loaded here; give the panel its fixed, card-sized box. |
| 147 | const style = document.head.appendChild(document.createElement("style")); |
| 148 | style.textContent = "[data-theme-editor-panel]{position:fixed;width:400px;height:300px}"; |
| 149 | using _style = { [Symbol.dispose]: () => style.remove() }; |
| 150 | await using screen = await render( |
| 151 | <ThemeEditorPanel |
| 152 | open |
| 153 | onOpenChange={() => {}} |
| 154 | onSaved={() => true} |
| 155 | editingTheme={null} |
| 156 | initialAppearance="dark" |
| 157 | restoreTheme={() => {}} |
| 158 | />, |
| 159 | ); |
| 160 | const panel = screen.getByRole("dialog", { name: "Create theme" }); |
| 161 | await expect.element(panel).toBeVisible(); |
| 162 | |
| 163 | dragHeader(panel.element() as HTMLElement, 700, 40); |
| 164 | await expect.poll(() => (panel.element() as HTMLElement).style.left).toBe("700px"); |
| 165 | |
| 166 | await page.viewport(600, 900); |
| 167 | await expect |
| 168 | .poll(() => Number.parseFloat((panel.element() as HTMLElement).style.left)) |
| 169 | .toBeLessThan(600); |
| 170 | |
| 171 | await page.viewport(1280, 900); |
| 172 | await expect.poll(() => (panel.element() as HTMLElement).style.left).toBe("700px"); |
| 173 | }); |
| 174 | |
| 175 | it("restores the colors from before the picker opened when Escape closes it", async () => { |
| 176 | await using _view = await renderPanel(); |
| 177 | const hex = page.getByRole("textbox", { name: "Accent hex value" }); |
| 178 | const original = (hex.element() as HTMLInputElement).value; |
| 179 | await userEvent.click(page.getByRole("button", { name: "Choose Accent color" })); |
| 180 | const hue = page.getByRole("slider", { name: "Accent hue" }); |
| 181 | await expect.element(hue).toBeInTheDocument(); |
| 182 | const hueSlider = hue.element() as HTMLElement; |
| 183 | |
| 184 | await act(async () => { |
| 185 | pointerAt(hueSlider, "pointerdown", 0.5); |
| 186 | pointerAt(hueSlider, "pointerup", 0.5); |
| 187 | }); |
| 188 | await expect.element(hex).not.toHaveValue(original); |
| 189 | |
| 190 | await act(async () => { |
| 191 | pointerAt(hueSlider, "pointerdown", 0.25); |
| 192 | hueSlider.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true, key: "Escape" })); |
| 193 | // The drag keeps going while the picker animates out. |
| 194 | pointerAt(hueSlider, "pointermove", 0.75); |
| 195 | pointerAt(hueSlider, "pointerup", 0.75); |
| 196 | }); |
| 197 | expect((hex.element() as HTMLInputElement).value).toBe(original); |
| 198 | }); |
| 199 | |
| 200 | it("keeps the typed text while backspacing the background hex past valid shorthand", async () => { |
| 201 | await using _view = await renderPanel(); |
| 202 | const hex = page.getByRole("textbox", { name: "Background hex value" }); |
| 203 | await userEvent.fill(hex, "#222222"); |
| 204 | await expect.element(hex).toHaveValue("#222222"); |
| 205 | |
| 206 | // Four- and three-digit prefixes are valid shorthand; the guided palette must not rewrite them. |
| 207 | for (const expected of ["#22222", "#2222", "#222", "#22", "#2", "#", ""]) { |
| 208 | await userEvent.keyboard("{Backspace}"); |
| 209 | await expect.element(hex).toHaveValue(expected); |
| 210 | } |
| 211 | |
| 212 | await userEvent.keyboard("#3366cc"); |
| 213 | await expect.element(hex).toHaveValue("#3366cc"); |
| 214 | await act(async () => (hex.element() as HTMLInputElement).blur()); |
| 215 | await expect.element(hex).toHaveValue("#3366cc"); |
| 216 | }); |
| 217 | |
| 218 | it("commits a typed shorthand hex when inspecting an advanced color switches modes", async () => { |
| 219 | await using _view = await renderPanel(<div className="bg-sidebar" data-testid="app-sidebar" />); |
| 220 | await userEvent.click(page.getByRole("button", { name: "Inspect app colors" })); |
| 221 | await userEvent.fill(page.getByRole("textbox", { name: "Background hex value" }), "#abc"); |
| 222 | |
| 223 | // Inspect mode cancels the pointerdown, so the hex field keeps focus while it unmounts. |
| 224 | const sidebar = page.getByTestId("app-sidebar").element() as HTMLElement; |
| 225 | await act(async () => pointerAt(sidebar, "pointerdown", 0.5)); |
| 226 | |
| 227 | await expect |
| 228 | .element(page.getByRole("switch", { name: "Use advanced theme colors" })) |
| 229 | .toBeChecked(); |
| 230 | await expect |
| 231 | .element(page.getByRole("textbox", { name: "Background hex value" })) |
| 232 | .toHaveValue("#aabbcc"); |
| 233 | }); |
| 234 | |
| 235 | it("closes on Escape without closing Settings", async () => { |
| 236 | await using view = await renderBesideSettings(); |
| 237 | (await editorElement("input")).focus(); |
| 238 | await userEvent.keyboard("{Escape}"); |
| 239 | |
| 240 | expect(view.onEditorOpenChange).toHaveBeenCalledWith(false); |
| 241 | expect(view.onSettingsOpenChange).not.toHaveBeenCalled(); |
| 242 | }); |
| 243 | |
| 244 | it("leaves inspection on Escape without closing the editor or Settings", async () => { |
| 245 | await using view = await renderBesideSettings(); |
| 246 | (await editorElement('[aria-label="Inspect app colors"]')).click(); |
| 247 | await editorElement('[aria-label="Cancel inspecting app colors"]'); |
| 248 | await userEvent.keyboard("{Escape}"); |
| 249 | |
| 250 | await editorElement('[aria-label="Inspect app colors"]'); |
| 251 | expect(view.onEditorOpenChange).not.toHaveBeenCalled(); |
| 252 | expect(view.onSettingsOpenChange).not.toHaveBeenCalled(); |
| 253 | }); |
| 254 | }); |