The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 133 lines 4.67 KB
Raw
1 import * as NodeModule from "node:module";
2 import { act, useLayoutEffect } from "react";
3 import { createRoot } from "react-dom/client";
4 import { expect, it, vi } from "vite-plus/test";
5
6 const harness = vi.hoisted(() => ({ navigation: undefined as unknown }));
7
8 vi.mock("@react-navigation/native", async () => {
9 const { createRequire } = await import("node:module");
10 const requireNavigation = createRequire(
11 createRequire(import.meta.url).resolve("@react-navigation/native/package.json"),
12 );
13 return {
14 StackActions: requireNavigation("@react-navigation/routers").StackActions,
15 useNavigation: () => harness.navigation,
16 };
17 });
18
19 import { useTerminalExitNavigation } from "./useTerminalExitNavigation";
20
21 const requireNavigation = NodeModule.createRequire(
22 NodeModule.createRequire(import.meta.url).resolve("@react-navigation/native/package.json"),
23 );
24 const { StackRouter, StackActions, CommonActions } = requireNavigation(
25 "@react-navigation/routers",
26 ) as {
27 StackRouter: typeof import("@react-navigation/native").StackRouter;
28 StackActions: typeof import("@react-navigation/native").StackActions;
29 CommonActions: typeof import("@react-navigation/native").CommonActions;
30 };
31
32 type Action = Parameters<ReturnType<typeof StackRouter>["getStateForAction"]>[1];
33
34 function setup(fallbackTerminalId: string | null) {
35 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
36 const router = StackRouter({});
37 const options = {
38 routeNames: ["Thread", "ThreadTerminal", "ThreadFiles"],
39 routeParamList: {},
40 routeGetIdList: {},
41 };
42 let state = router.getInitialState(options);
43 const apply = (action: Action) => {
44 const next = router.getStateForAction(state, action, options);
45 if (!next) throw new Error(`Rejected ${action.type}`);
46 state = router.getRehydratedState(next, options);
47 };
48 const thread = { environmentId: "environment", threadId: "thread" };
49 apply(StackActions.replace("Thread", thread));
50 apply(StackActions.push("ThreadTerminal", { ...thread, terminalId: "second" }));
51 const terminalKey = state.routes.at(-1)!.key;
52 const focusListeners = new Set<() => void>();
53 // A screen's navigation object stamps its route as the source but leaves the
54 // target unset, as React Navigation's screen navigation does.
55 const dispatch = (action: Action) => apply({ source: terminalKey, ...action } as Action);
56 harness.navigation = {
57 dispatch,
58 goBack: () => dispatch(CommonActions.goBack()),
59 canGoBack: () => state.routes.length > 1,
60 isFocused: () => state.routes.at(-1)?.key === terminalKey,
61 addListener: (_event: "focus", listener: () => void) => {
62 focusListeners.add(listener);
63 return () => focusListeners.delete(listener);
64 },
65 };
66 let exitNavigation!: ReturnType<typeof useTerminalExitNavigation>;
67 function Probe() {
68 const value = useTerminalExitNavigation({
69 ...thread,
70 readFallbackTerminalId: () => fallbackTerminalId,
71 });
72 useLayoutEffect(() => {
73 exitNavigation = value;
74 });
75 return null;
76 }
77 const root = createRoot(document.createElement("div"));
78 act(() => root.render(<Probe />));
79 return {
80 exitNavigation,
81 routes: () => state.routes.map((route) => [route.name, route.params]),
82 openFiles: () => apply(StackActions.push("ThreadFiles", thread)),
83 returnToTerminal: () => {
84 apply(StackActions.pop());
85 for (const listener of focusListeners) listener();
86 },
87 [Symbol.dispose]: () => act(() => root.unmount()),
88 };
89 }
90
91 const thread = { environmentId: "environment", threadId: "thread" };
92
93 it("leaves the screen on top alone when a terminal behind it exits, then goes back on return", () => {
94 using screen = setup(null);
95 screen.openFiles();
96
97 screen.exitNavigation.exited();
98 expect(screen.routes()).toEqual([
99 ["Thread", thread],
100 ["ThreadTerminal", { ...thread, terminalId: "second" }],
101 ["ThreadFiles", thread],
102 ]);
103
104 screen.returnToTerminal();
105 expect(screen.routes()).toEqual([["Thread", thread]]);
106 });
107
108 it("does not replace the screen on top with the fallback terminal", () => {
109 using screen = setup("first");
110 screen.openFiles();
111
112 screen.exitNavigation.exited();
113 expect(screen.routes().at(-1)).toEqual(["ThreadFiles", thread]);
114
115 screen.returnToTerminal();
116 expect(screen.routes()).toEqual([
117 ["Thread", thread],
118 ["ThreadTerminal", { ...thread, terminalId: "first" }],
119 ]);
120 });
121
122 it("stays on a terminal that restarted before the user returned", () => {
123 using screen = setup(null);
124 screen.openFiles();
125
126 screen.exitNavigation.exited();
127 screen.exitNavigation.restarted();
128 screen.returnToTerminal();
129 expect(screen.routes()).toEqual([
130 ["Thread", thread],
131 ["ThreadTerminal", { ...thread, terminalId: "second" }],
132 ]);
133 });