The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodePath from "node:path"; |
| 2 | import * as NodeModule from "node:module"; |
| 3 | import * as React from "react"; |
| 4 | import { act, useLayoutEffect } from "react"; |
| 5 | import { createRoot } from "react-dom/client"; |
| 6 | import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 7 | import { expect, it, vi } from "vite-plus/test"; |
| 8 | |
| 9 | const harness = vi.hoisted(() => ({ |
| 10 | dispatch: (_action: unknown) => {}, |
| 11 | getState: (): unknown => undefined, |
| 12 | getRoute: (): unknown => undefined, |
| 13 | inspectorSupported: true, |
| 14 | })); |
| 15 | |
| 16 | vi.mock("react-native", () => ({ Platform: { OS: "ios" } })); |
| 17 | vi.mock("../layout/AdaptiveWorkspaceLayout", () => ({ |
| 18 | useWorkspacePaneState: () => ({ fileInspector: { supported: harness.inspectorSupported } }), |
| 19 | })); |
| 20 | vi.mock("@react-navigation/native", async () => { |
| 21 | const { createRequire } = await import("node:module"); |
| 22 | const requireNavigation = createRequire( |
| 23 | createRequire(import.meta.url).resolve("@react-navigation/native/package.json"), |
| 24 | ); |
| 25 | return { |
| 26 | StackActions: requireNavigation("@react-navigation/routers").StackActions, |
| 27 | useNavigation: () => ({ dispatch: harness.dispatch, getState: harness.getState }), |
| 28 | useRoute: () => harness.getRoute(), |
| 29 | }; |
| 30 | }); |
| 31 | |
| 32 | import { useOpenThreadInspector } from "./thread-inspector-navigation"; |
| 33 | import { useOpenLinkedThread, useOpenLinkedThreadRead } from "./useOpenLinkedThread"; |
| 34 | |
| 35 | const requireNavigation = NodeModule.createRequire( |
| 36 | NodeModule.createRequire(import.meta.url).resolve("@react-navigation/native/package.json"), |
| 37 | ); |
| 38 | const { StackRouter, StackActions } = requireNavigation("@react-navigation/routers") as { |
| 39 | StackRouter: typeof import("@react-navigation/native").StackRouter; |
| 40 | StackActions: typeof import("@react-navigation/native").StackActions; |
| 41 | }; |
| 42 | |
| 43 | function setup(fromAgents: boolean, fullscreen = false, inspectorSupported = true) { |
| 44 | harness.inspectorSupported = inspectorSupported; |
| 45 | (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 46 | const router = StackRouter({}); |
| 47 | const options = { |
| 48 | routeNames: ["Home", "Thread", "ThreadAgents", "ThreadInspectorFullscreen", "LinkedThread"], |
| 49 | routeParamList: {}, |
| 50 | routeGetIdList: {}, |
| 51 | }; |
| 52 | let state = router.getInitialState(options); |
| 53 | const dispatch = (action: Parameters<typeof router.getStateForAction>[1]) => { |
| 54 | const next = router.getStateForAction(state, action, options); |
| 55 | if (!next) throw new Error(`Rejected ${action.type}`); |
| 56 | state = router.getRehydratedState(next, options); |
| 57 | }; |
| 58 | harness.dispatch = (action) => dispatch(action as Parameters<typeof dispatch>[0]); |
| 59 | harness.getState = () => state; |
| 60 | harness.getRoute = () => state.routes.at(-1); |
| 61 | const source = { environmentId: "remote-environment", threadId: "parent" }; |
| 62 | dispatch(StackActions.push("Thread", { ...source, prompt: "retained prompt" })); |
| 63 | const parentKey = state.routes.at(-1)!.key; |
| 64 | if (fullscreen) |
| 65 | dispatch(StackActions.push("ThreadInspectorFullscreen", { ...source, threadId: "child" })); |
| 66 | if (fromAgents) |
| 67 | dispatch( |
| 68 | StackActions.push("ThreadAgents", fullscreen ? { ...source, threadId: "child" } : source), |
| 69 | ); |
| 70 | let openInspector: ReturnType<typeof useOpenThreadInspector>; |
| 71 | let openLinkedThread: ReturnType<typeof useOpenLinkedThread>; |
| 72 | let openLinkedThreadRead: ReturnType<typeof useOpenLinkedThreadRead>; |
| 73 | function Probe() { |
| 74 | const open = useOpenThreadInspector(); |
| 75 | const openLinked = useOpenLinkedThread(); |
| 76 | const openLinkedRead = useOpenLinkedThreadRead(); |
| 77 | useLayoutEffect(() => { |
| 78 | openInspector = open; |
| 79 | openLinkedThread = openLinked; |
| 80 | openLinkedThreadRead = openLinkedRead; |
| 81 | }); |
| 82 | return null; |
| 83 | } |
| 84 | const root = createRoot(document.createElement("div")); |
| 85 | act(() => root.render(<Probe />)); |
| 86 | return { |
| 87 | source, |
| 88 | parentKey, |
| 89 | dispatch, |
| 90 | get state() { |
| 91 | return state; |
| 92 | }, |
| 93 | open(target: Parameters<typeof openInspector>[1], from = source) { |
| 94 | let opened = false; |
| 95 | act(() => { |
| 96 | opened = openInspector(from, target); |
| 97 | }); |
| 98 | return opened; |
| 99 | }, |
| 100 | openLinked(threadId: string) { |
| 101 | act(() => openLinkedThread({ environmentId: source.environmentId, threadId })); |
| 102 | }, |
| 103 | openLinkedRead(...input: Parameters<typeof openLinkedThreadRead>) { |
| 104 | act(() => openLinkedThreadRead(...input)); |
| 105 | }, |
| 106 | [Symbol.dispose]() { |
| 107 | act(() => root.unmount()); |
| 108 | }, |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | it("opens a subagent from the Agents sheet without replacing the parent thread", () => { |
| 113 | using screen = setup(true); |
| 114 | const inspector = { |
| 115 | type: "thread", |
| 116 | environmentId: screen.source.environmentId, |
| 117 | threadId: "child", |
| 118 | } as const; |
| 119 | screen.open(inspector); |
| 120 | expect(screen.state.routes.map((route) => route.name)).toEqual(["Home", "Thread"]); |
| 121 | expect(screen.state.routes.at(-1)).toMatchObject({ |
| 122 | key: screen.parentKey, |
| 123 | params: { ...screen.source, prompt: "retained prompt", inspector }, |
| 124 | }); |
| 125 | screen.dispatch(StackActions.push("ThreadInspectorFullscreen", inspector)); |
| 126 | screen.dispatch(StackActions.pop()); |
| 127 | expect(screen.state.routes.at(-1)).toMatchObject({ |
| 128 | key: screen.parentKey, |
| 129 | params: { ...screen.source, inspector }, |
| 130 | }); |
| 131 | }); |
| 132 | |
| 133 | it("opens a completion link in the parent inspector", () => { |
| 134 | using screen = setup(false); |
| 135 | screen.openLinked("child"); |
| 136 | expect(screen.state.routes).toHaveLength(2); |
| 137 | expect(screen.state.routes.at(-1)).toMatchObject({ |
| 138 | key: screen.parentKey, |
| 139 | params: { |
| 140 | ...screen.source, |
| 141 | inspector: { type: "thread", environmentId: screen.source.environmentId, threadId: "child" }, |
| 142 | }, |
| 143 | }); |
| 144 | }); |
| 145 | |
| 146 | it.each([ |
| 147 | { inspectorSupported: true, fromAgents: false }, |
| 148 | { inspectorSupported: true, fromAgents: true }, |
| 149 | { inspectorSupported: false, fromAgents: false }, |
| 150 | { inspectorSupported: false, fromAgents: true }, |
| 151 | ])( |
| 152 | "opens a linked environment's thread in the inspector where there is one ($inspectorSupported, from Agents $fromAgents)", |
| 153 | ({ inspectorSupported, fromAgents }) => { |
| 154 | using screen = setup(fromAgents, false, inspectorSupported); |
| 155 | const thread = { |
| 156 | environmentId: EnvironmentId.make("linked-vm"), |
| 157 | threadId: ThreadId.make("there"), |
| 158 | }; |
| 159 | const linked = { viaEnvironmentId: EnvironmentId.make("remote-environment"), label: "fs-17" }; |
| 160 | screen.openLinkedRead(thread, linked, "Fix the build", { |
| 161 | source: screen.source, |
| 162 | replace: fromAgents, |
| 163 | }); |
| 164 | if (inspectorSupported) { |
| 165 | // Beside the parent, like a subagent or a side chat; the Agents sheet gives way. |
| 166 | expect(screen.state.routes.map((route) => route.name)).toEqual(["Home", "Thread"]); |
| 167 | expect(screen.state.routes.at(-1)).toMatchObject({ |
| 168 | key: screen.parentKey, |
| 169 | params: { |
| 170 | inspector: { |
| 171 | type: "linkedThread", |
| 172 | ...thread, |
| 173 | linked: { ...linked, title: "Fix the build" }, |
| 174 | }, |
| 175 | }, |
| 176 | }); |
| 177 | return; |
| 178 | } |
| 179 | // A phone has no inspector: the read-only thread takes the screen, replacing the sheet. |
| 180 | expect(screen.state.routes.map((route) => route.name)).toEqual([ |
| 181 | "Home", |
| 182 | "Thread", |
| 183 | "LinkedThread", |
| 184 | ]); |
| 185 | expect(screen.state.routes.at(-1)?.params).toEqual({ |
| 186 | ...thread, |
| 187 | ...linked, |
| 188 | title: "Fix the build", |
| 189 | }); |
| 190 | }, |
| 191 | ); |
| 192 | |
| 193 | it("switches between subagents and devices in the same parent route", () => { |
| 194 | using screen = setup(false); |
| 195 | screen.open({ type: "thread", environmentId: screen.source.environmentId, threadId: "child" }); |
| 196 | const inspector = { type: "device", ...screen.source } as const; |
| 197 | screen.open(inspector); |
| 198 | expect(screen.state.routes).toHaveLength(2); |
| 199 | expect(screen.state.routes.at(-1)).toMatchObject({ |
| 200 | key: screen.parentKey, |
| 201 | params: { ...screen.source, inspector }, |
| 202 | }); |
| 203 | }); |
| 204 | |
| 205 | it.each(["thread", "device"] as const)( |
| 206 | "keeps the parent when a maximized child opens a %s preview", |
| 207 | (type) => { |
| 208 | using screen = setup(false, true); |
| 209 | const child = { ...screen.source, threadId: "child" }; |
| 210 | const originalState = screen.state; |
| 211 | expect(screen.open({ type, ...child, threadId: "grandchild" }, child)).toBe(false); |
| 212 | expect(screen.state).toEqual(originalState); |
| 213 | screen.dispatch(StackActions.pop()); |
| 214 | expect(screen.state.routes.at(-1)).toMatchObject({ |
| 215 | key: screen.parentKey, |
| 216 | params: screen.source, |
| 217 | }); |
| 218 | }, |
| 219 | ); |
| 220 | |
| 221 | it("keeps the parent when the maximized child's Agents sheet opens another child", () => { |
| 222 | using screen = setup(true, true); |
| 223 | const child = { ...screen.source, threadId: "child" }; |
| 224 | const originalState = screen.state; |
| 225 | expect(screen.open({ type: "thread", ...child, threadId: "grandchild" }, child)).toBe(false); |
| 226 | expect(screen.state).toEqual(originalState); |
| 227 | }); |
| 228 | |
| 229 | it("keeps a focused inspector registered across renders and updates its content", () => { |
| 230 | (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 231 | const requireMobile = NodeModule.createRequire(import.meta.url); |
| 232 | const listeners = { focus: new Set<() => void>(), blur: new Set<() => void>() }; |
| 233 | let focused = true; |
| 234 | let activeRender: (() => React.ReactNode) | undefined; |
| 235 | const deactivate = vi.fn(() => { |
| 236 | activeRender = undefined; |
| 237 | }); |
| 238 | const registerWorkspaceInspector = vi.fn( |
| 239 | (content: { readonly panes: ReadonlyArray<{ readonly render: () => React.ReactNode }> }) => { |
| 240 | activeRender = content.panes[0]?.render; |
| 241 | return deactivate; |
| 242 | }, |
| 243 | ); |
| 244 | const navigation = { |
| 245 | isFocused: () => focused, |
| 246 | addListener(event: "focus" | "blur", listener: () => void) { |
| 247 | listeners[event].add(listener); |
| 248 | return () => { |
| 249 | listeners[event].delete(listener); |
| 250 | }; |
| 251 | }, |
| 252 | }; |
| 253 | const navigationContext = React.createContext(navigation); |
| 254 | const routeContext = React.createContext({ key: "file" }); |
| 255 | const babel = requireMobile("@babel/core") as { |
| 256 | transformFileSync(file: string, options: Record<string, unknown>): { code: string }; |
| 257 | }; |
| 258 | const compiled = babel.transformFileSync( |
| 259 | NodePath.resolve(__dirname, "../layout/AdaptiveWorkspaceLayout.tsx"), |
| 260 | { |
| 261 | babelrc: false, |
| 262 | configFile: false, |
| 263 | presets: [ |
| 264 | [ |
| 265 | requireMobile.resolve("babel-preset-expo"), |
| 266 | { unstable_transformImportMeta: true, enableBabelRuntime: false }, |
| 267 | ], |
| 268 | ], |
| 269 | caller: { |
| 270 | name: "metro", |
| 271 | bundler: "metro", |
| 272 | platform: "ios", |
| 273 | isDev: false, |
| 274 | isServer: false, |
| 275 | isNodeModule: false, |
| 276 | supportsStaticESM: false, |
| 277 | supportsReactCompiler: true, |
| 278 | }, |
| 279 | }, |
| 280 | ); |
| 281 | const exports: { |
| 282 | Inspector?: (props: { |
| 283 | readonly content: { |
| 284 | readonly active: string; |
| 285 | readonly panes: ReadonlyArray<{ readonly key: string; readonly render: () => string }>; |
| 286 | }; |
| 287 | }) => null; |
| 288 | } = {}; |
| 289 | const load = (name: string) => { |
| 290 | if (name === "react" || name.startsWith("react/")) return requireMobile(name); |
| 291 | if (name === "./adaptive-workspace-context") |
| 292 | return { |
| 293 | useAdaptiveWorkspaceLayout: () => ({ registerWorkspaceInspector }), |
| 294 | }; |
| 295 | if (name === "@react-navigation/native") |
| 296 | return { |
| 297 | NavigationContext: navigationContext, |
| 298 | NavigationRouteContext: routeContext, |
| 299 | }; |
| 300 | return {}; |
| 301 | }; |
| 302 | new Function("require", "exports", compiled.code)(load, exports); |
| 303 | const Inspector = exports.Inspector!; |
| 304 | const contents = new Map<string, Parameters<typeof Inspector>[0]["content"]>(); |
| 305 | function Screen({ content }: { content: string | undefined }) { |
| 306 | if (content === undefined) return null; |
| 307 | let panes = contents.get(content); |
| 308 | if (!panes) { |
| 309 | panes = { active: "main", panes: [{ key: "main", render: () => content }] }; |
| 310 | contents.set(content, panes); |
| 311 | } |
| 312 | return <Inspector content={panes} />; |
| 313 | } |
| 314 | const root = createRoot(document.createElement("div")); |
| 315 | using _root = { [Symbol.dispose]: () => act(() => root.unmount()) }; |
| 316 | const render = (content: string | undefined) => |
| 317 | act(() => root.render(<Screen content={content} />)); |
| 318 | const first = "first"; |
| 319 | render(first); |
| 320 | registerWorkspaceInspector.mockClear(); |
| 321 | deactivate.mockClear(); |
| 322 | render(first); |
| 323 | render(first); |
| 324 | expect(registerWorkspaceInspector).not.toHaveBeenCalled(); |
| 325 | expect(deactivate).not.toHaveBeenCalled(); |
| 326 | const second = "second"; |
| 327 | render(second); |
| 328 | expect(registerWorkspaceInspector).toHaveBeenCalledTimes(1); |
| 329 | const paneContainer = document.createElement("div"); |
| 330 | const paneRoot = createRoot(paneContainer); |
| 331 | using _pane = { [Symbol.dispose]: () => act(() => paneRoot.unmount()) }; |
| 332 | act(() => paneRoot.render(activeRender?.())); |
| 333 | expect(paneContainer.textContent).toBe("second"); |
| 334 | const emit = (event: "focus" | "blur") => |
| 335 | act(() => { |
| 336 | focused = event === "focus"; |
| 337 | for (const listener of listeners[event]) listener(); |
| 338 | }); |
| 339 | emit("blur"); |
| 340 | expect(deactivate).toHaveBeenCalledTimes(1); |
| 341 | registerWorkspaceInspector.mockClear(); |
| 342 | render(first); |
| 343 | expect(registerWorkspaceInspector).not.toHaveBeenCalled(); |
| 344 | emit("focus"); |
| 345 | expect(registerWorkspaceInspector).toHaveBeenCalledTimes(1); |
| 346 | emit("focus"); |
| 347 | expect(registerWorkspaceInspector).toHaveBeenCalledTimes(1); |
| 348 | render(undefined); |
| 349 | expect(activeRender).toBeUndefined(); |
| 350 | render(second); |
| 351 | expect(activeRender).toBeDefined(); |
| 352 | act(() => root.unmount()); |
| 353 | expect(activeRender).toBeUndefined(); |
| 354 | expect(listeners.focus.size + listeners.blur.size).toBe(0); |
| 355 | }); |