The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | AuthOrchestrationOperateScope, |
| 3 | EnvironmentId, |
| 4 | ProjectId, |
| 5 | ThreadId, |
| 6 | type AuthEnvironmentScope, |
| 7 | type EditorId, |
| 8 | type ThreadPullRequestLink, |
| 9 | } from "@t3tools/contracts"; |
| 10 | import { AsyncResult } from "effect/reactivity"; |
| 11 | import { expect, it, vi } from "vite-plus/test"; |
| 12 | import { userEvent } from "vite-plus/test/browser"; |
| 13 | |
| 14 | import { act, render } from "../../test/render"; |
| 15 | import { useRightPanelStore } from "../rightPanelStore"; |
| 16 | |
| 17 | const state = vi.hoisted(() => ({ |
| 18 | allowed: true, |
| 19 | listeners: new Set<() => void>(), |
| 20 | openEditor: vi.fn(), |
| 21 | link: vi.fn(), |
| 22 | unlink: vi.fn(), |
| 23 | search: vi.fn(), |
| 24 | choose: vi.fn(), |
| 25 | openFile: vi.fn(), |
| 26 | openExternal: vi.fn(), |
| 27 | copy: vi.fn(), |
| 28 | pullRequests: [] as ThreadPullRequestLink[], |
| 29 | })); |
| 30 | |
| 31 | vi.mock(import("../state/use-atom-query-runner"), async (original) => ({ |
| 32 | ...(await original()), |
| 33 | useAtomQueryRunner: (() => state.search) as never, |
| 34 | })); |
| 35 | vi.mock(import("../state/use-atom-command"), async (original) => { |
| 36 | const { threadEnvironment } = await import("../state/threads"); |
| 37 | return { |
| 38 | ...(await original()), |
| 39 | useAtomCommand: ((command: unknown) => |
| 40 | command === threadEnvironment.linkPullRequest |
| 41 | ? state.link |
| 42 | : command === threadEnvironment.unlinkPullRequest |
| 43 | ? state.unlink |
| 44 | : state.openEditor) as never, |
| 45 | }; |
| 46 | }); |
| 47 | vi.mock(import("../state/session"), async (original) => { |
| 48 | const { useSyncExternalStore } = await import("react"); |
| 49 | const readEnvironmentScope = (id: EnvironmentId | null, scope: AuthEnvironmentScope) => |
| 50 | id !== null && |
| 51 | (scope !== AuthOrchestrationOperateScope || id !== threadRef.environmentId || state.allowed); |
| 52 | return { |
| 53 | ...(await original()), |
| 54 | readEnvironmentScope, |
| 55 | useEnvironmentScope: (id: EnvironmentId | null, scope: AuthEnvironmentScope) => |
| 56 | useSyncExternalStore( |
| 57 | (listener) => { |
| 58 | state.listeners.add(listener); |
| 59 | return () => state.listeners.delete(listener); |
| 60 | }, |
| 61 | () => readEnvironmentScope(id, scope), |
| 62 | ), |
| 63 | }; |
| 64 | }); |
| 65 | vi.mock(import("../state/server"), async (original) => { |
| 66 | const actual = await original(); |
| 67 | const { Atom } = await import("effect/reactivity"); |
| 68 | const config = Atom.make(() => serverConfig); |
| 69 | return { |
| 70 | ...actual, |
| 71 | serverEnvironment: { ...actual.serverEnvironment, configValueAtom: (() => config) as never }, |
| 72 | environmentServerConfigsAtom: Atom.make( |
| 73 | () => new Map([[threadRef.environmentId, serverConfig]]), |
| 74 | ) as never, |
| 75 | }; |
| 76 | }); |
| 77 | vi.mock(import("../state/entities"), async (original) => ({ |
| 78 | ...(await original()), |
| 79 | readThreadShell: (() => ({ pullRequests: state.pullRequests })) as never, |
| 80 | useProjects: (() => [{ id: projectId, environmentId: threadRef.environmentId }]) as never, |
| 81 | })); |
| 82 | vi.mock(import("../remoteOpen"), async (original) => ({ |
| 83 | ...(await original()), |
| 84 | useRemoteOpenResolution: () => ({ state: { mode: "local-exec" }, isResolved: true }) as never, |
| 85 | })); |
| 86 | vi.mock(import("../hooks/useLocalStorage"), async (original) => ({ |
| 87 | ...(await original()), |
| 88 | useLocalStorage: (() => ["vscode", vi.fn()]) as never, |
| 89 | getLocalStorageItem: (() => "vscode") as never, |
| 90 | })); |
| 91 | vi.mock(import("../localApi"), async (original) => ({ |
| 92 | ...(await original()), |
| 93 | readLocalApi: () => |
| 94 | ({ |
| 95 | contextMenu: { show: state.choose }, |
| 96 | shell: { openExternal: state.openExternal }, |
| 97 | }) as never, |
| 98 | })); |
| 99 | vi.mock(import("@t3tools/client-runtime/change-request-links"), async (original) => ({ |
| 100 | ...(await original()), |
| 101 | resolvePullRequestPreviewTarget: () => null, |
| 102 | findProjectForChangeRequest: ((projects: readonly { id: ProjectId }[]) => projects[0]) as never, |
| 103 | })); |
| 104 | |
| 105 | import ChatMarkdown from "./ChatMarkdown"; |
| 106 | import { MarkdownFileLink } from "./chat/MarkdownFileLink"; |
| 107 | |
| 108 | const threadRef = { |
| 109 | environmentId: EnvironmentId.make("selected"), |
| 110 | threadId: ThreadId.make("thread"), |
| 111 | }; |
| 112 | const projectId = ProjectId.make("project"); |
| 113 | const linkedPullRequest: ThreadPullRequestLink = { |
| 114 | host: "github.com", |
| 115 | repository: "example/repo", |
| 116 | number: 42, |
| 117 | url: "https://github.com/example/repo/pull/42", |
| 118 | source: "manual", |
| 119 | linkedAt: "2026-09-08T00:00:00.000Z", |
| 120 | snapshot: null, |
| 121 | stack: null, |
| 122 | }; |
| 123 | const serverConfig = { |
| 124 | availableEditors: ["vscode", "file-manager"] as readonly EditorId[], |
| 125 | shellRevealInFileManager: true, |
| 126 | shellRevealInFileManagerKind: "xdg-open", |
| 127 | environment: { platform: { os: "linux" }, capabilities: {} }, |
| 128 | }; |
| 129 | |
| 130 | function setup() { |
| 131 | using stack = new DisposableStack(); |
| 132 | state.allowed = true; |
| 133 | state.listeners.clear(); |
| 134 | state.pullRequests = []; |
| 135 | state.openEditor.mockReset().mockResolvedValue(AsyncResult.success(undefined)); |
| 136 | state.link.mockReset().mockResolvedValue(AsyncResult.success(undefined)); |
| 137 | state.unlink.mockReset().mockResolvedValue(AsyncResult.success(undefined)); |
| 138 | state.search.mockReset().mockResolvedValue(AsyncResult.success({ entries: [] })); |
| 139 | state.choose.mockReset().mockResolvedValue(null); |
| 140 | state.openFile.mockReset(); |
| 141 | state.openExternal.mockReset().mockResolvedValue(undefined); |
| 142 | state.copy.mockReset().mockResolvedValue(undefined); |
| 143 | const { openFile } = useRightPanelStore.getState(); |
| 144 | useRightPanelStore.setState({ openFile: state.openFile }); |
| 145 | stack.defer(() => useRightPanelStore.setState({ openFile })); |
| 146 | stack.use(vi.spyOn(navigator.clipboard, "writeText").mockImplementation(state.copy)); |
| 147 | stack.use( |
| 148 | vi.spyOn(navigator.clipboard, "write").mockImplementation(async (items) => { |
| 149 | const item = items[0]!; |
| 150 | await state.copy(await (await item.getType("text/plain")).text()); |
| 151 | }), |
| 152 | ); |
| 153 | return stack.move(); |
| 154 | } |
| 155 | |
| 156 | async function renderMarkdown(text: string) { |
| 157 | const screen = await render(<ChatMarkdown cwd="/work" threadRef={threadRef} text={text} />); |
| 158 | const anchor = () => screen.container.querySelector("a")!; |
| 159 | return Object.assign(screen, { |
| 160 | anchor, |
| 161 | openContextMenu: () => |
| 162 | act(async () => { |
| 163 | anchor().dispatchEvent( |
| 164 | new MouseEvent("contextmenu", { |
| 165 | bubbles: true, |
| 166 | cancelable: true, |
| 167 | clientX: 10, |
| 168 | clientY: 10, |
| 169 | }), |
| 170 | ); |
| 171 | }), |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | function offeredActions() { |
| 176 | return state.choose.mock.lastCall![0].map((item: { id: string }) => item.id); |
| 177 | } |
| 178 | |
| 179 | async function setAllowed(allowed: boolean) { |
| 180 | await act(async () => { |
| 181 | state.allowed = allowed; |
| 182 | for (const listener of state.listeners) listener(); |
| 183 | }); |
| 184 | } |
| 185 | |
| 186 | it("removes host actions after revocation while keeping file preview and copying", async () => { |
| 187 | using _setup = setup(); |
| 188 | await using screen = await renderMarkdown("[Readme](docs/readme.md)"); |
| 189 | await screen.openContextMenu(); |
| 190 | expect(offeredActions()).toEqual(["open", "reveal", "copy-relative", "copy-full"]); |
| 191 | await setAllowed(false); |
| 192 | state.choose.mockResolvedValue("copy-full"); |
| 193 | await screen.openContextMenu(); |
| 194 | expect(offeredActions()).toEqual(["copy-relative", "copy-full"]); |
| 195 | await vi.waitFor(() => expect(state.copy).toHaveBeenCalledWith("/work/docs/readme.md")); |
| 196 | await act(async () => screen.anchor().click()); |
| 197 | expect(state.openFile).toHaveBeenCalledWith(threadRef, "docs/readme.md", undefined); |
| 198 | expect(state.openEditor).not.toHaveBeenCalled(); |
| 199 | }); |
| 200 | |
| 201 | it.each(["[Docs](https://example.com/docs)", "[Readme](docs/readme.md)"])( |
| 202 | "tells the native menu for %s whether the keyboard opened it", |
| 203 | async (text) => { |
| 204 | using _setup = setup(); |
| 205 | await using screen = await renderMarkdown(text); |
| 206 | screen.anchor().focus(); |
| 207 | await userEvent.keyboard("{Shift>}{F10}{/Shift}"); |
| 208 | await vi.waitFor(() => expect(state.choose.mock.lastCall?.[2]).toBe("keyboard")); |
| 209 | await userEvent.click(screen.anchor(), { button: "right" }); |
| 210 | await vi.waitFor(() => expect(state.choose.mock.lastCall?.[2]).toBe("mouse")); |
| 211 | }, |
| 212 | ); |
| 213 | |
| 214 | it("does not launch an editor after revocation during a native context menu", async () => { |
| 215 | using _setup = setup(); |
| 216 | let choose: (action: string) => void = () => { |
| 217 | throw new Error("Menu not opened"); |
| 218 | }; |
| 219 | state.choose.mockImplementationOnce( |
| 220 | () => |
| 221 | new Promise<string>((resolve) => { |
| 222 | choose = resolve; |
| 223 | }), |
| 224 | ); |
| 225 | await using screen = await renderMarkdown("[Readme](docs/readme.md)"); |
| 226 | await screen.openContextMenu(); |
| 227 | expect(offeredActions()).toContain("open"); |
| 228 | await act(async () => { |
| 229 | state.allowed = false; |
| 230 | choose("open"); |
| 231 | }); |
| 232 | expect(state.openEditor).not.toHaveBeenCalled(); |
| 233 | }); |
| 234 | |
| 235 | it("does not reveal a file after revocation during its workspace lookup", async () => { |
| 236 | using _setup = setup(); |
| 237 | let finishLookup: () => void = () => { |
| 238 | throw new Error("Lookup not started"); |
| 239 | }; |
| 240 | state.search.mockImplementationOnce( |
| 241 | () => |
| 242 | new Promise((resolve) => { |
| 243 | finishLookup = () => resolve(AsyncResult.success({ entries: [] })); |
| 244 | }), |
| 245 | ); |
| 246 | state.choose.mockResolvedValue("reveal"); |
| 247 | await using screen = await renderMarkdown("[Readme](readme.md)"); |
| 248 | await screen.openContextMenu(); |
| 249 | expect(state.search).toHaveBeenCalledOnce(); |
| 250 | await act(async () => { |
| 251 | state.allowed = false; |
| 252 | finishLookup(); |
| 253 | }); |
| 254 | expect(state.openEditor).not.toHaveBeenCalled(); |
| 255 | }); |
| 256 | |
| 257 | it.each(["link-to-thread", "unlink-from-thread"])( |
| 258 | "rechecks %s after a native context menu returns", |
| 259 | async (action) => { |
| 260 | using _setup = setup(); |
| 261 | state.pullRequests = action === "unlink-from-thread" ? [linkedPullRequest] : []; |
| 262 | const command = action === "link-to-thread" ? state.link : state.unlink; |
| 263 | let choose: (action: string) => void = () => { |
| 264 | throw new Error("Menu not opened"); |
| 265 | }; |
| 266 | state.choose.mockImplementationOnce( |
| 267 | () => |
| 268 | new Promise<string>((resolve) => { |
| 269 | choose = resolve; |
| 270 | }), |
| 271 | ); |
| 272 | await using screen = await renderMarkdown(`[Review](${linkedPullRequest.url})`); |
| 273 | await screen.openContextMenu(); |
| 274 | expect(offeredActions()).toContain(action); |
| 275 | await act(async () => { |
| 276 | state.allowed = false; |
| 277 | choose(action); |
| 278 | }); |
| 279 | expect(command).not.toHaveBeenCalled(); |
| 280 | |
| 281 | await setAllowed(true); |
| 282 | state.choose.mockResolvedValue(action); |
| 283 | await screen.openContextMenu(); |
| 284 | await vi.waitFor(() => |
| 285 | expect(command).toHaveBeenCalledExactlyOnceWith({ |
| 286 | environmentId: threadRef.environmentId, |
| 287 | input: expect.objectContaining({ |
| 288 | threadId: threadRef.threadId, |
| 289 | repository: "example/repo", |
| 290 | number: 42, |
| 291 | }), |
| 292 | }), |
| 293 | ); |
| 294 | }, |
| 295 | ); |
| 296 | |
| 297 | it("preserves keyboard activation of a file-options button", async () => { |
| 298 | using _setup = setup(); |
| 299 | await using screen = await render( |
| 300 | <MarkdownFileLink |
| 301 | environmentId={null} |
| 302 | href="readme.md" |
| 303 | targetPath="readme.md" |
| 304 | iconPath="readme.md" |
| 305 | workspaceRelativePath="readme.md" |
| 306 | panelPath={null} |
| 307 | label="Readme" |
| 308 | copyMarkdown="[Readme](readme.md)" |
| 309 | theme="dark" |
| 310 | onOpenInPanel={vi.fn()} |
| 311 | resolveDragPath={async () => "/work/readme.md"} |
| 312 | resolveCopyPaths={async () => ({ relativePath: "readme.md", fullPath: "/work/readme.md" })} |
| 313 | openInEditorMenuLabel="Open in editor" |
| 314 | />, |
| 315 | ); |
| 316 | const button = screen.container.querySelector("button")!; |
| 317 | button.focus(); |
| 318 | for (const key of ["{Enter}", " "]) { |
| 319 | await userEvent.keyboard(key); |
| 320 | await vi.waitFor(() => expect(state.choose.mock.lastCall?.[2]).toBe("keyboard")); |
| 321 | state.choose.mockClear(); |
| 322 | } |
| 323 | await userEvent.click(button); |
| 324 | await vi.waitFor(() => expect(state.choose.mock.lastCall?.[2]).toBe("mouse")); |
| 325 | }); |