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 type { AnyRouter } from "@tanstack/react-router"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { isMacPlatform } from "../lib/utils"; |
| 5 | import { useThreadSelectionStore } from "../threadSelectionStore"; |
| 6 | import { installChatRouteCommands } from "./chatRouteCommands"; |
| 7 | |
| 8 | type Match = { readonly routeId: string; readonly pathname: string }; |
| 9 | |
| 10 | function routerAt(matches: Match[]) { |
| 11 | const state = { matches: matches.map((match) => ({ ...match, params: {} })) }; |
| 12 | return { |
| 13 | router: { state } as unknown as AnyRouter, |
| 14 | navigate: (next: Match[]) => { |
| 15 | state.matches = next.map((match) => ({ ...match, params: {} })); |
| 16 | }, |
| 17 | }; |
| 18 | } |
| 19 | |
| 20 | const CHAT: Match[] = [ |
| 21 | { routeId: "__root__", pathname: "/" }, |
| 22 | { routeId: "/_chat", pathname: "/" }, |
| 23 | ]; |
| 24 | |
| 25 | function pressEscape(): KeyboardEvent { |
| 26 | const event = new KeyboardEvent("keydown", { key: "Escape", cancelable: true, bubbles: true }); |
| 27 | window.dispatchEvent(event); |
| 28 | return event; |
| 29 | } |
| 30 | |
| 31 | function pressMod(key: string): KeyboardEvent { |
| 32 | const mac = isMacPlatform(navigator.platform); |
| 33 | const event = new KeyboardEvent("keydown", { |
| 34 | key, |
| 35 | metaKey: mac, |
| 36 | ctrlKey: !mac, |
| 37 | cancelable: true, |
| 38 | bubbles: true, |
| 39 | }); |
| 40 | document.activeElement?.dispatchEvent(event); |
| 41 | return event; |
| 42 | } |
| 43 | |
| 44 | function selectThread(threadKey: string) { |
| 45 | useThreadSelectionStore.getState().clearSelection(); |
| 46 | useThreadSelectionStore.getState().toggleThread(threadKey); |
| 47 | } |
| 48 | |
| 49 | describe("chat route commands", () => { |
| 50 | it("clears a selection made after install, reading the store at keypress time", () => { |
| 51 | const { router } = routerAt(CHAT); |
| 52 | using stack = new DisposableStack(); |
| 53 | stack.defer(installChatRouteCommands(router)); |
| 54 | stack.defer(() => useThreadSelectionStore.getState().clearSelection()); |
| 55 | |
| 56 | selectThread("env:thread-1"); |
| 57 | const event = pressEscape(); |
| 58 | |
| 59 | expect(event.defaultPrevented).toBe(true); |
| 60 | expect(useThreadSelectionStore.getState().selectedThreadKeys.size).toBe(0); |
| 61 | // With nothing selected, Escape is left for other handlers. |
| 62 | expect(pressEscape().defaultPrevented).toBe(false); |
| 63 | }); |
| 64 | |
| 65 | it("stays out of the way off the chat route and under modal pages", () => { |
| 66 | const { router, navigate } = routerAt([{ routeId: "__root__", pathname: "/pair" }]); |
| 67 | using stack = new DisposableStack(); |
| 68 | stack.defer(installChatRouteCommands(router)); |
| 69 | stack.defer(() => useThreadSelectionStore.getState().clearSelection()); |
| 70 | selectThread("env:thread-1"); |
| 71 | |
| 72 | expect(pressEscape().defaultPrevented).toBe(false); |
| 73 | |
| 74 | navigate([...CHAT, { routeId: "/_chat/settings", pathname: "/settings" }]); |
| 75 | expect(pressEscape().defaultPrevented).toBe(false); |
| 76 | expect(useThreadSelectionStore.getState().selectedThreadKeys.size).toBe(1); |
| 77 | |
| 78 | navigate(CHAT); |
| 79 | expect(pressEscape().defaultPrevented).toBe(true); |
| 80 | }); |
| 81 | |
| 82 | it("leaves zoom keys to the app unless the focused right panel shows a browser", () => { |
| 83 | const { router } = routerAt(CHAT); |
| 84 | using stack = new DisposableStack(); |
| 85 | stack.defer(installChatRouteCommands(router)); |
| 86 | const panel = document.createElement("div"); |
| 87 | panel.setAttribute("data-preview-panel-mode", "inline"); |
| 88 | const pullRequestTab = document.createElement("button"); |
| 89 | panel.append(pullRequestTab); |
| 90 | document.body.append(panel); |
| 91 | stack.defer(() => panel.remove()); |
| 92 | pullRequestTab.focus(); |
| 93 | |
| 94 | for (const key of ["=", "+", "-", "0"]) expect(pressMod(key).defaultPrevented).toBe(false); |
| 95 | |
| 96 | const browser = document.createElement("div"); |
| 97 | browser.setAttribute("data-preview-view", ""); |
| 98 | panel.append(browser); |
| 99 | for (const key of ["=", "+", "-", "0"]) expect(pressMod(key).defaultPrevented).toBe(true); |
| 100 | }); |
| 101 | }); |