The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import "~/index.css"; |
| 2 | |
| 3 | import { EnvironmentId } from "@t3tools/contracts"; |
| 4 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 5 | import { userEvent } from "vite-plus/test/browser"; |
| 6 | |
| 7 | import { InputGroup, InputGroupAddon, InputGroupInput } from "~/components/ui/input-group"; |
| 8 | import type { RightPanelSurface } from "~/rightPanelStore"; |
| 9 | import { render, stubGlobal } from "../../test/render"; |
| 10 | import { HistorySwipeNavigation } from "./HistorySwipeNavigation"; |
| 11 | import { RightPanelTabs, type RightPanelTabsProps } from "./RightPanelTabs"; |
| 12 | import { subscribePreviewAction } from "./preview/previewActionBus"; |
| 13 | |
| 14 | const navigateHistory = vi.hoisted(() => vi.fn()); |
| 15 | vi.mock("../navigationHistory", () => ({ canNavigateHistory: () => true, navigateHistory })); |
| 16 | |
| 17 | // Chromium starts a gesture scroll, and Electron reports its begin, only for a |
| 18 | // wheel event the page did not preventDefault; it then scrolls the latched scroller. |
| 19 | function trackpad() { |
| 20 | const begin = new Set<() => void>(); |
| 21 | const end = new Set<() => void>(); |
| 22 | const subscribe = (listeners: Set<() => void>) => (listener: () => void) => { |
| 23 | listeners.add(listener); |
| 24 | return () => listeners.delete(listener); |
| 25 | }; |
| 26 | let scrolling = false; |
| 27 | return { |
| 28 | bridge: { |
| 29 | onTrackpadScrollBegin: subscribe(begin), |
| 30 | onTrackpadScrollEnd: subscribe(end), |
| 31 | onMenuAction: () => () => {}, |
| 32 | }, |
| 33 | scroll(target: Element, deltas: readonly number[]) { |
| 34 | let scroller: Element | null = target; |
| 35 | while ( |
| 36 | scroller && |
| 37 | (scroller.scrollWidth <= scroller.clientWidth || |
| 38 | !/auto|scroll/.test(getComputedStyle(scroller).overflowX)) |
| 39 | ) { |
| 40 | scroller = scroller.parentElement; |
| 41 | } |
| 42 | for (const deltaX of deltas) { |
| 43 | const event = new WheelEvent("wheel", { |
| 44 | deltaX, |
| 45 | bubbles: true, |
| 46 | cancelable: true, |
| 47 | composed: true, |
| 48 | }); |
| 49 | target.dispatchEvent(event); |
| 50 | if (event.defaultPrevented) continue; |
| 51 | if (scroller) scroller.scrollLeft += deltaX; |
| 52 | if (scrolling) continue; |
| 53 | scrolling = true; |
| 54 | for (const listener of begin) listener(); |
| 55 | } |
| 56 | if (scrolling) for (const listener of end) listener(); |
| 57 | scrolling = false; |
| 58 | }, |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | const surfaces: RightPanelSurface[] = Array.from({ length: 16 }, (_, index) => ({ |
| 63 | id: `terminal:${index}`, |
| 64 | kind: "terminal", |
| 65 | resourceId: `${index}`, |
| 66 | terminalIds: [`${index}`], |
| 67 | activeTerminalId: `${index}`, |
| 68 | })); |
| 69 | |
| 70 | describe("RightPanelTabs", () => { |
| 71 | it("a flick that scrolls the tab strip to its left edge keeps the thread, a swipe from the edge goes back", async () => { |
| 72 | navigateHistory.mockReset(); |
| 73 | const { bridge, scroll } = trackpad(); |
| 74 | using _bridge = stubGlobal("desktopBridge", bridge); |
| 75 | using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel"); |
| 76 | const panel = document.createElement("div"); |
| 77 | panel.style.cssText = "width: 320px; height: 200px; display: flex"; |
| 78 | const noop = () => {}; |
| 79 | await using screen = await render( |
| 80 | <> |
| 81 | <HistorySwipeNavigation /> |
| 82 | <RightPanelTabs |
| 83 | mode="embedded" |
| 84 | keybindings={[]} |
| 85 | getShortcutContext={() => ({ |
| 86 | terminalFocus: false, |
| 87 | terminalOpen: false, |
| 88 | previewFocus: false, |
| 89 | previewOpen: false, |
| 90 | isWeb: true, |
| 91 | isDesktop: false, |
| 92 | })} |
| 93 | surfaces={surfaces} |
| 94 | environmentId={null} |
| 95 | activeSurfaceId={null} |
| 96 | pendingSurfaceIds={new Set()} |
| 97 | previewSessions={{}} |
| 98 | desktopByTabId={{}} |
| 99 | terminalLabelsById={new Map(surfaces.map((surface) => [surface.id, "Terminal"]))} |
| 100 | onActivate={noop} |
| 101 | onCloseSurface={noop} |
| 102 | onCloseOtherSurfaces={noop} |
| 103 | onCloseSurfacesToRight={noop} |
| 104 | onCloseAllSurfaces={noop} |
| 105 | onCopyFilePath={noop} |
| 106 | onAddBrowser={noop} |
| 107 | onAddBrowserInProfile={noop} |
| 108 | onAddTerminal={noop} |
| 109 | onAddDiff={noop} |
| 110 | onAddFiles={noop} |
| 111 | onAddPullRequest={noop} |
| 112 | onAddPullRequests={noop} |
| 113 | onAddDevice={noop} |
| 114 | browserAvailable={false} |
| 115 | terminalAvailable |
| 116 | diffAvailable={false} |
| 117 | filesAvailable={false} |
| 118 | pullRequestAvailable={false} |
| 119 | pullRequestsAvailable={false} |
| 120 | deviceAvailable={false} |
| 121 | > |
| 122 | {null} |
| 123 | </RightPanelTabs> |
| 124 | </>, |
| 125 | { container: panel }, |
| 126 | ); |
| 127 | const viewport = screen.container.querySelector<HTMLElement>( |
| 128 | '[data-right-panel-tab-list] [data-slot="scroll-area-viewport"]', |
| 129 | )!; |
| 130 | expect(viewport.scrollWidth - viewport.clientWidth).toBeGreaterThan(120); |
| 131 | viewport.scrollLeft = 120; |
| 132 | |
| 133 | const momentum = Array.from({ length: 24 }, (_, index) => -60 * 0.85 ** index); |
| 134 | scroll(viewport.querySelector("[data-active-tab]")!, [-10, -20, -30, ...momentum]); |
| 135 | |
| 136 | expect(viewport.scrollLeft).toBe(0); |
| 137 | expect(navigateHistory).not.toHaveBeenCalled(); |
| 138 | |
| 139 | viewport.scrollLeft = 0; |
| 140 | scroll(viewport.querySelector("[data-active-tab]")!, [-40, -40, -40, -40, -40]); |
| 141 | |
| 142 | expect(navigateHistory).toHaveBeenCalledExactlyOnceWith("back"); |
| 143 | }); |
| 144 | }); |
| 145 | |
| 146 | const BROWSER_SURFACE: RightPanelSurface = { |
| 147 | id: "browser:tab-1", |
| 148 | kind: "preview", |
| 149 | resourceId: "tab-1", |
| 150 | }; |
| 151 | const DIFF_SURFACE: RightPanelSurface = { id: "diff", kind: "diff" }; |
| 152 | |
| 153 | function panel(activeSurface: RightPanelSurface) { |
| 154 | const noop = () => {}; |
| 155 | const props: RightPanelTabsProps = { |
| 156 | mode: "inline", |
| 157 | keybindings: [], |
| 158 | getShortcutContext: () => ({ |
| 159 | terminalFocus: false, |
| 160 | terminalOpen: false, |
| 161 | previewFocus: false, |
| 162 | previewOpen: true, |
| 163 | isWeb: true, |
| 164 | isDesktop: false, |
| 165 | }), |
| 166 | surfaces: [BROWSER_SURFACE, DIFF_SURFACE], |
| 167 | environmentId: EnvironmentId.make("environment-local"), |
| 168 | activeSurfaceId: activeSurface.id, |
| 169 | pendingSurfaceIds: new Set(), |
| 170 | previewSessions: {}, |
| 171 | desktopByTabId: {}, |
| 172 | terminalLabelsById: new Map(), |
| 173 | onActivate: noop, |
| 174 | onCloseSurface: noop, |
| 175 | onCloseOtherSurfaces: noop, |
| 176 | onCloseSurfacesToRight: noop, |
| 177 | onCloseAllSurfaces: noop, |
| 178 | onCopyFilePath: noop, |
| 179 | onAddBrowser: noop, |
| 180 | onAddBrowserInProfile: noop, |
| 181 | onAddTerminal: noop, |
| 182 | onAddDiff: noop, |
| 183 | onAddFiles: noop, |
| 184 | onAddPullRequest: noop, |
| 185 | onAddPullRequests: noop, |
| 186 | onAddDevice: noop, |
| 187 | browserAvailable: true, |
| 188 | terminalAvailable: true, |
| 189 | diffAvailable: true, |
| 190 | filesAvailable: true, |
| 191 | pullRequestAvailable: true, |
| 192 | pullRequestsAvailable: true, |
| 193 | deviceAvailable: true, |
| 194 | children: ( |
| 195 | <div data-testid="surface-content"> |
| 196 | {/* Stands in for ServerBrowserSurface, whose page canvas handles thumb buttons itself. */} |
| 197 | <canvas data-testid="page-canvas" onMouseUp={(event) => event.preventDefault()} /> |
| 198 | <InputGroup> |
| 199 | <InputGroupInput aria-label="Address" /> |
| 200 | <InputGroupAddon data-testid="address-addon" align="inline-end" /> |
| 201 | </InputGroup> |
| 202 | </div> |
| 203 | ), |
| 204 | }; |
| 205 | return <RightPanelTabs {...props} />; |
| 206 | } |
| 207 | |
| 208 | // Chromium focuses on an uncancelled mousedown and runs history back/forward on an |
| 209 | // uncancelled button 3/4 mouseup. |
| 210 | function releaseThumbButton( |
| 211 | target: Element, |
| 212 | button: 3 | 4, |
| 213 | ): { focusMoved: boolean; routerMoved: boolean } { |
| 214 | const focused = document.activeElement; |
| 215 | const down = new MouseEvent("mousedown", { bubbles: true, cancelable: true, button }); |
| 216 | const up = new MouseEvent("mouseup", { bubbles: true, cancelable: true, button }); |
| 217 | target.dispatchEvent(down); |
| 218 | target.dispatchEvent(up); |
| 219 | return { |
| 220 | focusMoved: !down.defaultPrevented || document.activeElement !== focused, |
| 221 | routerMoved: !up.defaultPrevented, |
| 222 | }; |
| 223 | } |
| 224 | |
| 225 | const STAYS_PUT = { focusMoved: false, routerMoved: false }; |
| 226 | |
| 227 | describe("right panel thumb buttons", () => { |
| 228 | it("keep the app router still over a surface that is not a browser", async () => { |
| 229 | await using screen = await render(panel(DIFF_SURFACE)); |
| 230 | const tabStrip = screen.container.querySelector("[data-right-panel-tabbar]")!; |
| 231 | const content = screen.getByTestId("surface-content").element(); |
| 232 | |
| 233 | expect(releaseThumbButton(tabStrip, 3)).toEqual(STAYS_PUT); |
| 234 | expect(releaseThumbButton(tabStrip, 4)).toEqual(STAYS_PUT); |
| 235 | expect(releaseThumbButton(content, 3)).toEqual(STAYS_PUT); |
| 236 | expect(releaseThumbButton(content, 4)).toEqual(STAYS_PUT); |
| 237 | }); |
| 238 | |
| 239 | it("go back and forward in the active browser tab instead of the app router", async () => { |
| 240 | const actions: string[] = []; |
| 241 | using _subscription = { [Symbol.dispose]: subscribePreviewAction((a) => actions.push(a)) }; |
| 242 | await using screen = await render(panel(BROWSER_SURFACE)); |
| 243 | const tabStrip = screen.container.querySelector("[data-right-panel-tabbar]")!; |
| 244 | const content = screen.getByTestId("surface-content").element(); |
| 245 | |
| 246 | expect(releaseThumbButton(tabStrip, 3)).toEqual(STAYS_PUT); |
| 247 | expect(releaseThumbButton(content, 4)).toEqual(STAYS_PUT); |
| 248 | // The address addon focuses its input on press; a thumb press must leave focus alone. |
| 249 | expect(releaseThumbButton(screen.getByTestId("address-addon").element(), 3)).toEqual(STAYS_PUT); |
| 250 | expect(actions).toEqual(["back", "forward", "back"]); |
| 251 | |
| 252 | // The page canvas already went back in the tab; the panel must not go back a second time. |
| 253 | releaseThumbButton(screen.getByTestId("page-canvas").element(), 3); |
| 254 | expect(actions).toEqual(["back", "forward", "back"]); |
| 255 | }); |
| 256 | }); |
| 257 | |
| 258 | const DEVICE_SURFACE: RightPanelSurface = { |
| 259 | id: "device:host:phone", |
| 260 | kind: "device", |
| 261 | target: { hostId: "host", deviceId: "phone", platform: "ios", name: "Phone" }, |
| 262 | }; |
| 263 | |
| 264 | describe("device tab rename", () => { |
| 265 | it("keeps the editor open on an Enter that confirms an IME candidate", async () => { |
| 266 | const onRenameDevice = vi.fn(); |
| 267 | const props = panel(DEVICE_SURFACE).props as RightPanelTabsProps; |
| 268 | await using screen = await render( |
| 269 | <RightPanelTabs {...props} surfaces={[DEVICE_SURFACE]} onRenameDevice={onRenameDevice}> |
| 270 | {null} |
| 271 | </RightPanelTabs>, |
| 272 | ); |
| 273 | await userEvent.dblClick(screen.getByRole("button", { name: "Phone", exact: true })); |
| 274 | const input = screen.getByRole("textbox", { name: "Device tab name" }); |
| 275 | await userEvent.fill(input, "にほん"); |
| 276 | |
| 277 | for (const shape of [ |
| 278 | { isComposing: true, keyCode: 13 }, |
| 279 | { isComposing: false, keyCode: 229 }, |
| 280 | ]) { |
| 281 | input |
| 282 | .element() |
| 283 | .dispatchEvent( |
| 284 | new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true, ...shape }), |
| 285 | ); |
| 286 | } |
| 287 | |
| 288 | await expect.element(input).toHaveFocus(); |
| 289 | expect(onRenameDevice).not.toHaveBeenCalled(); |
| 290 | await userEvent.fill(input, "日本語"); |
| 291 | await userEvent.keyboard("{Enter}"); |
| 292 | expect(onRenameDevice).toHaveBeenCalledExactlyOnceWith(DEVICE_SURFACE.id, "日本語"); |
| 293 | }); |
| 294 | }); |