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 { RuntimeMode } from "@t3tools/contracts"; |
| 2 | import { type ReactNode, useRef, useState } from "react"; |
| 3 | import { expect, it, vi } from "vite-plus/test"; |
| 4 | import { page, userEvent } from "vite-plus/test/browser"; |
| 5 | |
| 6 | import { render } from "../../../test/render"; |
| 7 | import { ComposerHandleContext, type ComposerHandleRef } from "../../composerHandleContext"; |
| 8 | import { ComposerContextPermissions } from "../ComposerContextPermissions"; |
| 9 | import { Menu, MenuPopup, MenuTrigger } from "../ui/menu"; |
| 10 | import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover"; |
| 11 | import { |
| 12 | focusRequestedPermission, |
| 13 | markOpenToPermissions, |
| 14 | useComposerMenuProps, |
| 15 | } from "./composerEventScope"; |
| 16 | import { ComposerFooterModeControls, ComposerPermissionsSection } from "./ComposerFooterControls"; |
| 17 | import { ComposerPickerFooter } from "./ComposerModelEffort"; |
| 18 | import { resolveRuntimeModeOption, runtimeModeOptions } from "./runtimeModeConfig"; |
| 19 | |
| 20 | const options = runtimeModeOptions.map((mode) => resolveRuntimeModeOption(mode)); |
| 21 | |
| 22 | // Mirrors the overflow menu, which holds the Permissions section. |
| 23 | function OverflowMenu() { |
| 24 | const [mode, setMode] = useState<RuntimeMode>("full-access"); |
| 25 | const composerMenuProps = useComposerMenuProps(); |
| 26 | const triggerRef = useRef<HTMLButtonElement>(null); |
| 27 | return ( |
| 28 | <> |
| 29 | <Menu onOpenChangeComplete={(open) => focusRequestedPermission(open, triggerRef.current)}> |
| 30 | <MenuTrigger ref={triggerRef}>More</MenuTrigger> |
| 31 | <MenuPopup {...composerMenuProps}> |
| 32 | <ComposerPermissionsSection |
| 33 | runtimeMode={mode} |
| 34 | runtimeModeOptions={options} |
| 35 | onRuntimeModeChange={setMode} |
| 36 | /> |
| 37 | </MenuPopup> |
| 38 | </Menu> |
| 39 | <p role="status">{mode}</p> |
| 40 | </> |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | it("reaches Permissions with arrow keys, previews, and picks with Enter", async () => { |
| 45 | await using screen = await render(<OverflowMenu />, { router: false }); // no routes involved |
| 46 | const trigger = screen.getByRole("button", { name: "More" }); |
| 47 | await userEvent.tab(); |
| 48 | await expect.element(trigger).toHaveFocus(); |
| 49 | await userEvent.keyboard("{ArrowDown}"); |
| 50 | const supervised = page.getByRole("menuitemradio", { name: "Supervised" }); |
| 51 | await expect.element(supervised).toHaveFocus(); |
| 52 | await expect |
| 53 | .element(supervised) |
| 54 | .toHaveAccessibleDescription("Ask before commands and file changes."); |
| 55 | |
| 56 | await userEvent.keyboard("{ArrowRight}"); |
| 57 | await expect |
| 58 | .element(page.getByRole("menuitemradio", { name: "Auto-accept edits" })) |
| 59 | .toHaveFocus(); |
| 60 | await userEvent.keyboard("{ArrowLeft}{ArrowLeft}"); |
| 61 | await expect.element(page.getByRole("menuitemradio", { name: "Supervised" })).toHaveFocus(); |
| 62 | |
| 63 | await userEvent.keyboard("{ArrowDown}{Enter}"); |
| 64 | await expect.element(screen.getByRole("status")).toHaveTextContent("auto-accept-edits"); |
| 65 | await expect |
| 66 | .element(page.getByRole("menuitemradio", { name: "Supervised" })) |
| 67 | .not.toBeInTheDocument(); |
| 68 | await expect.element(trigger).toHaveFocus(); |
| 69 | }); |
| 70 | |
| 71 | it("marks the current mode as checked and closes on Escape", async () => { |
| 72 | await using screen = await render(<OverflowMenu />, { router: false }); // no routes involved |
| 73 | await userEvent.tab(); |
| 74 | await userEvent.keyboard("{ArrowDown}"); |
| 75 | const fullAccess = page.getByRole("menuitemradio", { name: "Full access" }); |
| 76 | await expect.element(fullAccess).toHaveAttribute("aria-checked", "true"); |
| 77 | await userEvent.keyboard("{Escape}"); |
| 78 | await expect.element(fullAccess).not.toBeInTheDocument(); |
| 79 | await expect.element(screen.getByRole("button", { name: "More" })).toHaveFocus(); |
| 80 | }); |
| 81 | |
| 82 | // The real composer menu focus rule (useComposerMenuProps), with a stub composer handle. |
| 83 | function withComposer(children: ReactNode) { |
| 84 | const handle = { focusAtEnd: vi.fn() }; |
| 85 | const ref = { current: handle } as unknown as ComposerHandleRef; |
| 86 | return { handle, ui: <ComposerHandleContext value={ref}>{children}</ComposerHandleContext> }; |
| 87 | } |
| 88 | |
| 89 | it("returns focus to the editor after a pick", async () => { |
| 90 | const { handle, ui } = withComposer(<OverflowMenu />); |
| 91 | await using screen = await render(ui, { router: false }); // no routes involved |
| 92 | await userEvent.tab(); |
| 93 | await userEvent.keyboard("{ArrowDown}"); |
| 94 | await expect.element(page.getByRole("menuitemradio", { name: "Supervised" })).toHaveFocus(); |
| 95 | await userEvent.keyboard("{Enter}"); |
| 96 | await expect.element(screen.getByRole("status")).toHaveTextContent("approval-required"); |
| 97 | await vi.waitFor(() => expect(handle.focusAtEnd).toHaveBeenCalled()); |
| 98 | }); |
| 99 | |
| 100 | // Mirrors the model picker: a composer popover whose footer nests the Permissions menu. |
| 101 | function PickerWithPermissions() { |
| 102 | const [mode, setMode] = useState<RuntimeMode>("full-access"); |
| 103 | const composerMenuProps = useComposerMenuProps(); |
| 104 | return ( |
| 105 | <> |
| 106 | <p>Timeline</p> |
| 107 | <Popover> |
| 108 | <PopoverTrigger>Model</PopoverTrigger> |
| 109 | <PopoverPopup {...composerMenuProps}> |
| 110 | <ComposerPickerFooter |
| 111 | traits={null} |
| 112 | runtimeMode={mode} |
| 113 | runtimeModeOptions={options} |
| 114 | onRuntimeModeChange={setMode} |
| 115 | /> |
| 116 | </PopoverPopup> |
| 117 | </Popover> |
| 118 | </> |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | it("on a touch tablet, tapping outside the Permissions menu leaves the editor unfocused", async () => { |
| 123 | const realMatchMedia = window.matchMedia.bind(window); |
| 124 | using _touch = vi |
| 125 | .spyOn(window, "matchMedia") |
| 126 | .mockImplementation((query) => realMatchMedia(query === "(pointer: coarse)" ? "all" : query)); |
| 127 | const { handle, ui } = withComposer(<PickerWithPermissions />); |
| 128 | await using screen = await render(ui, { router: false }); // no routes involved |
| 129 | await userEvent.click(screen.getByRole("button", { name: "Model" })); |
| 130 | await userEvent.click(page.getByRole("button", { name: "Permissions: Full access" })); |
| 131 | const fullAccess = page.getByRole("menuitemradio", { name: "Full access" }); |
| 132 | await expect.element(fullAccess).toBeInTheDocument(); |
| 133 | const permissionsTrigger = page.getByRole("button", { name: "Permissions: Full access" }); |
| 134 | // The first outside tap closes the Permissions menu, the second the picker around it. |
| 135 | await userEvent.click(document.body, { position: { x: 1, y: 1 } }); |
| 136 | await expect.element(fullAccess).not.toBeInTheDocument(); |
| 137 | await userEvent.click(screen.getByText("Timeline")); |
| 138 | await expect.element(permissionsTrigger).not.toBeInTheDocument(); |
| 139 | // Focusing the editor after a tap opens the on-screen keyboard on Android tablets. |
| 140 | expect(handle.focusAtEnd).not.toHaveBeenCalled(); |
| 141 | }); |
| 142 | |
| 143 | it("opens on the checked permission when composer.mode marked the trigger", async () => { |
| 144 | await using screen = await render(<OverflowMenu />, { router: false }); // no routes involved |
| 145 | const trigger = screen.getByRole("button", { name: "More" }); |
| 146 | markOpenToPermissions(trigger.element() as HTMLElement); |
| 147 | await userEvent.click(trigger); |
| 148 | await expect.element(page.getByRole("menuitemradio", { name: "Full access" })).toHaveFocus(); |
| 149 | |
| 150 | // The mark is used once: a plain reopen lands on the menu's usual first focus. |
| 151 | await userEvent.keyboard("{Escape}"); |
| 152 | await userEvent.click(trigger); |
| 153 | await expect.element(page.getByRole("menuitemradio", { name: "Full access" })).not.toHaveFocus(); |
| 154 | }); |
| 155 | |
| 156 | // Mirrors ComposerControlsBar: the chip shows only while the mode differs from the default. |
| 157 | function PermissionsChip() { |
| 158 | const [mode, setMode] = useState<RuntimeMode>("approval-required"); |
| 159 | return ( |
| 160 | <div data-slot="composer-shell"> |
| 161 | <button type="button" data-composer-shortcut="composer.effort composer.mode"> |
| 162 | Model |
| 163 | </button> |
| 164 | {mode === "full-access" ? null : ( |
| 165 | <ComposerFooterModeControls |
| 166 | runtimeMode={mode} |
| 167 | runtimeModeOptions={options} |
| 168 | accessDisplay="label" |
| 169 | onRuntimeModeChange={setMode} |
| 170 | /> |
| 171 | )} |
| 172 | </div> |
| 173 | ); |
| 174 | } |
| 175 | |
| 176 | it("returns focus to the editor when picking the default hides the chip", async () => { |
| 177 | const { handle, ui } = withComposer(<PermissionsChip />); |
| 178 | await using screen = await render(ui, { router: false }); // no routes involved |
| 179 | await userEvent.click(screen.getByRole("button", { name: "Permissions: Supervised" })); |
| 180 | await userEvent.click(page.getByRole("menuitemradio", { name: "Full access" })); |
| 181 | await vi.waitFor(() => expect(handle.focusAtEnd).toHaveBeenCalled()); |
| 182 | await expect.element(screen.getByRole("button", { name: "Model" })).not.toHaveFocus(); |
| 183 | }); |
| 184 | |
| 185 | function ContextPermissions() { |
| 186 | const [mode, setMode] = useState<RuntimeMode>("full-access"); |
| 187 | return ( |
| 188 | <ComposerContextPermissions |
| 189 | environmentId={null} |
| 190 | runtimeMode={mode} |
| 191 | supportedRuntimeModes={["approval-required", "full-access"]} |
| 192 | onRuntimeModeChange={setMode} |
| 193 | /> |
| 194 | ); |
| 195 | } |
| 196 | |
| 197 | it("opens permissions directly from the context strip and applies a supported mode", async () => { |
| 198 | await using screen = await render(<ContextPermissions />, { router: false }); // no routes involved |
| 199 | await userEvent.click(screen.getByRole("button", { name: "Permissions: Full access" })); |
| 200 | await expect |
| 201 | .element(page.getByRole("menuitemradio", { name: "Full access" })) |
| 202 | .toHaveAttribute("aria-checked", "true"); |
| 203 | await expect |
| 204 | .element(page.getByRole("menuitemradio", { name: "Auto", exact: true })) |
| 205 | .not.toBeInTheDocument(); |
| 206 | await userEvent.click(page.getByRole("menuitemradio", { name: "Supervised" })); |
| 207 | const trigger = screen.getByRole("button", { name: "Permissions: Supervised" }); |
| 208 | await expect.element(trigger).toBeVisible(); |
| 209 | await expect |
| 210 | .element(page.getByRole("menuitemradio", { name: "Supervised" })) |
| 211 | .not.toBeInTheDocument(); |
| 212 | await userEvent.click(trigger); |
| 213 | await expect |
| 214 | .element(page.getByRole("menuitemradio", { name: "Supervised" })) |
| 215 | .toHaveAttribute("aria-checked", "true"); |
| 216 | await userEvent.keyboard("{Escape}"); |
| 217 | await expect.element(trigger).toHaveFocus(); |
| 218 | }); |