The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings"; |
| 2 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 3 | import { userEvent } from "vite-plus/test/browser"; |
| 4 | |
| 5 | import { render } from "../../test/render"; |
| 6 | |
| 7 | const state = vi.hoisted(() => ({ navigate: vi.fn() })); |
| 8 | |
| 9 | // One key toggles Usage: it opens Usage elsewhere and goes back from it. |
| 10 | const keybindings = compileResolvedKeybindingsConfig([ |
| 11 | { key: "mod+u", command: "navigation.back", when: "usagePageOpen" }, |
| 12 | { key: "mod+u", command: "usage.open", when: "!usagePageOpen" }, |
| 13 | ]); |
| 14 | |
| 15 | vi.mock(import("@effect/atom-react"), async (original) => ({ |
| 16 | ...(await original()), |
| 17 | useAtomValue: (() => keybindings) as never, |
| 18 | })); |
| 19 | vi.mock(import("../state/server"), async (original) => { |
| 20 | const actual = await original(); |
| 21 | const { Atom } = await import("effect/reactivity"); |
| 22 | const { compileResolvedKeybindingsConfig: compile } = await import("@t3tools/shared/keybindings"); |
| 23 | // Factories are hoisted above the module's own constants. |
| 24 | const compiled = compile([ |
| 25 | { key: "mod+u", command: "navigation.back", when: "usagePageOpen" }, |
| 26 | { key: "mod+u", command: "usage.open", when: "!usagePageOpen" }, |
| 27 | ]); |
| 28 | return { ...actual, primaryServerKeybindingsAtom: Atom.make(compiled) }; |
| 29 | }); |
| 30 | vi.mock(import("@tanstack/react-router"), async (original) => ({ |
| 31 | ...(await original()), |
| 32 | useNavigate: (() => state.navigate) as never, |
| 33 | })); |
| 34 | |
| 35 | import { useCommandPaletteMount } from "../commandPaletteStore"; |
| 36 | import { installCommandPaletteCommands } from "../shortcuts/commandPaletteCommands"; |
| 37 | import { registerNavigationHistoryCommands } from "./AppSidebarLayout"; |
| 38 | |
| 39 | function MountedPalette() { |
| 40 | useCommandPaletteMount(); |
| 41 | return null; |
| 42 | } |
| 43 | |
| 44 | // regression: #21357 |
| 45 | describe("usage toggle shortcut on Usage", () => { |
| 46 | it("goes back", async () => { |
| 47 | vi.clearAllMocks(); |
| 48 | using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64"); |
| 49 | await using screen = await render(null, { router: "/usage" }); |
| 50 | using _commands = { [Symbol.dispose]: registerNavigationHistoryCommands(screen.router!) }; |
| 51 | |
| 52 | using back = vi.spyOn(window.history, "back").mockImplementation(() => undefined); |
| 53 | await userEvent.keyboard("{Control>}u{/Control}"); |
| 54 | |
| 55 | expect(back).toHaveBeenCalledOnce(); |
| 56 | }); |
| 57 | |
| 58 | // Both listeners sit on window, so the palette must not claim the key whatever their order. |
| 59 | it("does not reopen Usage from the command palette", async () => { |
| 60 | vi.clearAllMocks(); |
| 61 | using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64"); |
| 62 | await using screen = await render(<MountedPalette />, { router: "/usage" }); |
| 63 | using navigate = vi.spyOn(screen.router!, "navigate"); |
| 64 | using commands = new DisposableStack(); |
| 65 | commands.defer( |
| 66 | installCommandPaletteCommands(screen.router!, { keybindings: () => keybindings }), |
| 67 | ); |
| 68 | |
| 69 | await userEvent.keyboard("{Control>}u{/Control}"); |
| 70 | |
| 71 | expect(navigate).not.toHaveBeenCalled(); |
| 72 | }); |
| 73 | }); |