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

main
ts 337 lines 14.5 KB
Raw
1 import { describe, expect, it, vi } from "vite-plus/test";
2 import { act, createElement, createContext, useState, type ReactNode } from "react";
3 import { createRoot } from "react-dom/client";
4 import { WorkspaceRootScreen } from "./WorkspaceRootScreen.android";
5 import {
6 initialWorkspaceTabsState,
7 workspaceTabsReducer,
8 shouldRestoreNativeSearch,
9 workspaceTabForNativeSlot,
10 resolveWorkspaceTabOrder,
11 workspaceSearchBottomClearance,
12 workspaceDockBottomClearance,
13 workspaceUsesColumns,
14 type WorkspaceTab,
15 type WorkspaceTabsState,
16 } from "./workspace-tabs-state";
17
18 describe("compact list clearance", () => {
19 it.each([
20 { bottomInset: 34, keyboardHeight: 300, clearance: 380 },
21 { bottomInset: 34, keyboardHeight: 0, clearance: 114 },
22 { bottomInset: 34, keyboardHeight: 20, clearance: 114 },
23 ])("keeps the last result above the keyboard and dock: %j", ({ clearance, ...input }) => {
24 expect(workspaceDockBottomClearance(input)).toBe(clearance);
25 });
26 });
27
28 describe("Duo list clearance", () => {
29 it.each([18, 60])(
30 "scrolls the last row above search with a %s-point native bar inset",
31 (bottomBarInset) => {
32 const clearance = workspaceSearchBottomClearance({
33 bottomInset: 34,
34 bottomBarInset,
35 keyboardHeight: 0,
36 });
37 expect(clearance).toBe(bottomBarInset + 48 + 16);
38 },
39 );
40
41 it("keeps the last row above search when the keyboard lifts the field", () => {
42 expect(
43 workspaceSearchBottomClearance({ bottomInset: 34, bottomBarInset: 18, keyboardHeight: 300 }),
44 ).toBe(300 + 8 + 48 + 16);
45 });
46
47 it("falls back to the field's safe-area spacing before native bar metrics arrive", () => {
48 expect(workspaceSearchBottomClearance({ bottomInset: 34, keyboardHeight: 0 })).toBe(
49 34 + 48 + 16,
50 );
51 expect(workspaceSearchBottomClearance({ bottomInset: 0, keyboardHeight: 0 })).toBe(
52 12 + 48 + 16,
53 );
54 });
55 });
56
57 describe("workspace root navigation", () => {
58 it("pairs Settings, chats and pull requests with a detail column, and keeps Usage one page", () => {
59 expect(workspaceUsesColumns("usage")).toBe(false);
60 expect(workspaceUsesColumns("usage", true)).toBe(true);
61 expect(workspaceUsesColumns("threads")).toBe(true);
62 expect(workspaceUsesColumns("pullRequests")).toBe(true);
63 });
64
65 it("keeps the registered defaults while customization is unavailable", () => {
66 expect(resolveWorkspaceTabOrder(undefined)).toEqual([
67 "threads",
68 "pullRequests",
69 "issues",
70 "usage",
71 ]);
72 expect(resolveWorkspaceTabOrder(["home", "usage", "issues", "threads"])).toEqual(
73 resolveWorkspaceTabOrder(undefined),
74 );
75 });
76
77 it("retains separate queries and positions across tabs, and cancels only the current search", () => {
78 let state = initialWorkspaceTabsState();
79 state = workspaceTabsReducer(state, { type: "scroll", tab: "threads", offset: 640 });
80 state = workspaceTabsReducer(state, { type: "search" });
81 state = workspaceTabsReducer(state, { type: "query", query: "keyboard" });
82 state = workspaceTabsReducer(state, { type: "scroll", tab: "threads", offset: 160 });
83 state = workspaceTabsReducer(state, { type: "expand" });
84 state = workspaceTabsReducer(state, { type: "select", tab: "pullRequests" });
85 expect(state.tabs.pullRequests.query).toBe("");
86 state = workspaceTabsReducer(state, { type: "scroll", tab: "pullRequests", offset: 240 });
87 state = workspaceTabsReducer(state, { type: "search" });
88 state = workspaceTabsReducer(state, { type: "query", query: "navigation" });
89 state = workspaceTabsReducer(state, { type: "select", tab: "threads" });
90 expect(state.tabs.threads).toMatchObject({
91 query: "keyboard",
92 scrollOffset: 160,
93 searchMode: "expanded",
94 });
95 state = workspaceTabsReducer(state, { type: "cancel" });
96 expect(state.tabs.threads).toMatchObject({
97 query: "",
98 scrollOffset: 640,
99 searchMode: "resting",
100 });
101 expect(state.tabs.pullRequests).toMatchObject({
102 query: "navigation",
103 scrollOffset: 240,
104 searchMode: "active",
105 });
106 });
107
108 it("clear keeps search active without losing the position cancel restores", () => {
109 let state = initialWorkspaceTabsState();
110 state = workspaceTabsReducer(state, { type: "scroll", tab: "threads", offset: 500 });
111 state = workspaceTabsReducer(state, { type: "search" });
112 state = workspaceTabsReducer(state, { type: "query", query: "diff" });
113 state = workspaceTabsReducer(state, { type: "scroll", tab: "threads", offset: 80 });
114 state = workspaceTabsReducer(state, { type: "query", query: "" });
115 state = workspaceTabsReducer(state, { type: "search" });
116 expect(state.tabs.threads).toMatchObject({
117 query: "",
118 searchMode: "active",
119 searchEntryOffset: 500,
120 });
121 expect(workspaceTabsReducer(state, { type: "cancel" }).tabs.threads.scrollOffset).toBe(500);
122 });
123
124 it("keeps the Settings search apart from the tab Settings opened over", () => {
125 let state = workspaceTabsReducer(initialWorkspaceTabsState(), {
126 type: "query",
127 query: "diff",
128 tab: "threads",
129 });
130 state = workspaceTabsReducer(state, { type: "search", tab: "settings" });
131 state = workspaceTabsReducer(state, { type: "query", query: "theme", tab: "settings" });
132 expect(state.tabs.settings).toMatchObject({ query: "theme", searchMode: "active" });
133 state = workspaceTabsReducer(state, { type: "cancel", tab: "settings" });
134 expect(state.tabs.settings).toMatchObject({ query: "", searchMode: "resting" });
135 expect(state.tabs.threads.query).toBe("diff");
136 });
137
138 it("applying an empty-query view resets the position that search cancellation restores", () => {
139 let state = initialWorkspaceTabsState();
140 state = workspaceTabsReducer(state, { type: "scroll", tab: "pullRequests", offset: 640 });
141 state = workspaceTabsReducer(state, { type: "search", tab: "pullRequests" });
142 state = workspaceTabsReducer(state, { type: "query", tab: "pullRequests", query: "old query" });
143 state = workspaceTabsReducer(state, { type: "scroll", tab: "pullRequests", offset: 80 });
144 state = workspaceTabsReducer(state, { type: "cancel", tab: "pullRequests", scrollToTop: true });
145 expect(state.tabs.pullRequests).toEqual({
146 query: "",
147 searchMode: "resting",
148 scrollOffset: 0,
149 searchEntryOffset: 0,
150 });
151 });
152
153 it("a late native blur cannot reopen a cancelled search", () => {
154 let state = workspaceTabsReducer(initialWorkspaceTabsState(), { type: "search" });
155 state = workspaceTabsReducer(state, { type: "cancel" });
156 expect(workspaceTabsReducer(state, { type: "expand" })).toBe(state);
157 });
158
159 it("a blur from the previous native tab cannot open search in the selected tab", () => {
160 let state = workspaceTabsReducer(initialWorkspaceTabsState(), { type: "search" });
161 state = workspaceTabsReducer(state, { type: "query", query: "keyboard" });
162 state = workspaceTabsReducer(state, { type: "select", tab: "pullRequests" });
163 state = workspaceTabsReducer(state, { type: "expand", tab: "threads" });
164 expect(state.tabs.threads).toMatchObject({ query: "keyboard", searchMode: "expanded" });
165 expect(state.tabs.pullRequests.searchMode).toBe("resting");
166 expect(workspaceTabsReducer(state, { type: "select", tab: "pullRequests" })).toBe(state);
167 });
168
169 it("keeps an empty search closed after iOS closes it with only a blur", () => {
170 const opened = workspaceTabsReducer(initialWorkspaceTabsState(), { type: "search" });
171 const closed = workspaceTabsReducer(opened, { type: "expand" });
172 const filtered = workspaceTabsReducer(closed, { type: "query", query: "skia" });
173
174 expect(shouldRestoreNativeSearch(closed.tabs.threads, false)).toBe(false);
175 expect(shouldRestoreNativeSearch(closed.tabs.threads, true)).toBe(true);
176 expect(shouldRestoreNativeSearch(filtered.tabs.threads, false)).toBe(true);
177 expect(shouldRestoreNativeSearch(opened.tabs.threads, false)).toBe(true);
178 expect(shouldRestoreNativeSearch(initialWorkspaceTabsState().tabs.threads, true)).toBe(false);
179 });
180
181 it("returns from Usage without losing list state", () => {
182 let state = workspaceTabsReducer(initialWorkspaceTabsState(), {
183 type: "query",
184 query: "retained",
185 tab: "threads",
186 });
187 for (const tab of ["usage"] as const) {
188 state = workspaceTabsReducer(state, { type: "select", tab });
189 expect(state.selectedTab).toBe(tab);
190 expect(workspaceTabsReducer(state, { type: "search" })).toBe(state);
191 }
192 state = workspaceTabsReducer(state, { type: "select", tab: "threads" });
193 expect(state.tabs.threads.query).toBe("retained");
194 });
195 it("opens Settings on native reselection instead of selecting its overflow section", () => {
196 const order = ["threads", "pullRequests", "usage"] as const;
197 expect(workspaceTabForNativeSlot(order, "settings")).toBe("settings");
198 expect(workspaceTabForNativeSlot(order, "first")).toBe("threads");
199 expect(workspaceTabForNativeSlot(order, "third")).toBe("usage");
200 expect(workspaceTabForNativeSlot(order, "removed")).toBeNull();
201 });
202 });
203
204 const mocks = vi.hoisted(() => ({
205 split: false,
206 floating: false,
207 state: undefined as WorkspaceTabsState | undefined,
208 openWorkspaceTab: (_tab: string) => {},
209 backListeners: new Set<() => boolean>(),
210 }));
211
212 vi.mock("../../settings/appearance/AppearancePreferencesProvider", () => ({
213 useAndroidFloatingTabs: () => mocks.floating,
214 }));
215 vi.mock("react-native", () => ({
216 BackHandler: {
217 addEventListener: (_event: string, listener: () => boolean) => {
218 mocks.backListeners.add(listener);
219 return { remove: () => mocks.backListeners.delete(listener) };
220 },
221 },
222 Keyboard: { dismiss: () => undefined },
223 useWindowDimensions: () => ({ width: 400, height: 800 }),
224 View: ({ children }: { children: ReactNode }) => createElement("div", null, children),
225 }));
226 vi.mock("react-native-safe-area-context", () => ({
227 SafeAreaInsetsContext: createContext({ top: 0, right: 0, bottom: 24, left: 0 }),
228 useSafeAreaInsets: () => ({ top: 0, right: 0, bottom: 24, left: 0 }),
229 }));
230 vi.mock("../AdaptiveWorkspaceLayout", () => ({
231 useAdaptiveWorkspaceLayout: () => ({
232 layout: { usesSplitView: mocks.split },
233 startNewTask: () => undefined,
234 }),
235 }));
236 vi.mock("@react-navigation/native", () => ({ useIsFocused: () => true }));
237 vi.mock("../adaptive-workspace-context", () => ({
238 useAdaptiveWorkspaceLayout: () => ({ openWorkspaceTab: mocks.openWorkspaceTab }),
239 }));
240 vi.mock("./navigation-tabs", () => ({
241 useEnabledNavigationTabs: () => ["threads", "pullRequests", "usage"],
242 }));
243 vi.mock("./WorkspaceTabsContext", () => ({
244 WorkspaceTabsContext: createContext(null),
245 useWorkspaceTabs: () => ({ state: mocks.state, dispatch: () => undefined }),
246 }));
247 vi.mock("./WorkspaceExtraTab", () => ({
248 WorkspaceExtraTab: function UsageProbe() {
249 const [tab, setTab] = useState("Limits");
250 return createElement("button", { onClick: () => setTab("Usage") }, tab);
251 },
252 }));
253 vi.mock("../../../native/StackHeader", () => ({ NativeStackScreenOptions: () => null }));
254 vi.mock("../../../components/ui/AppBar", () => ({ AppBar: () => null }));
255 vi.mock("../WorkspaceEmptyDetail", () => ({ WorkspaceEmptyDetail: () => null }));
256 vi.mock("../../../components/ui/ScreenContent", () => ({
257 ScreenContent: ({ children }: { children: ReactNode }) => createElement("div", null, children),
258 }));
259 vi.mock("../../../components/ui/useAndroidControlSizing", () => ({
260 useAndroidControlSizing: () => ({ fabClearance: 64, scale: 1, fabSize: 56 }),
261 }));
262 vi.mock("../../home/HomeRouteScreen", () => ({ HomeRouteScreen: () => null }));
263 vi.mock("../../pull-requests/PullRequestFilterMenuContext", () => ({
264 PullRequestFilterMenuProvider: ({ children }: { children: ReactNode }) =>
265 createElement("div", null, children),
266 }));
267 vi.mock("../../pull-requests/PullRequestsScreen", () => ({ PullRequestsScreen: () => null }));
268 vi.mock("../../issues/IssuesScreen", () => ({ IssuesScreen: () => null }));
269 vi.mock("./ListSearchToolbar.android", () => ({ ListSearchToolbar: () => null }));
270 vi.mock("./AndroidWorkspaceNavigation", () => ({
271 AndroidWorkspaceNavigation: () => null,
272 FloatingWorkspaceTabs: () => null,
273 }));
274 vi.mock("./WorkspaceNoTabs", () => ({ WorkspaceNoTabs: () => null }));
275 vi.mock("../../../components/ui/Pane", () => ({ usePaneRole: () => "screen" }));
276 vi.mock("./AndroidWorkspaceSidebar.android", () => ({ AndroidWorkspaceSidebar: () => null }));
277 vi.mock("../../../components/ui/WorkspaceAppBar.android", () => ({
278 WorkspaceAppBarScope: ({ children }: { children: ReactNode }) => children,
279 }));
280
281 function setup(split: boolean, floating: boolean, state = selected("usage")) {
282 mocks.split = split;
283 mocks.floating = floating;
284 mocks.state = state;
285 mocks.openWorkspaceTab = () => undefined;
286 mocks.backListeners.clear();
287 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
288 return { [Symbol.dispose]: () => vi.unstubAllGlobals() };
289 }
290
291 function selected(tab: WorkspaceTab) {
292 return workspaceTabsReducer(initialWorkspaceTabsState(), { type: "select", tab });
293 }
294
295 /** Android calls the newest listener first and stops at the first that handles Back. */
296 function pressBack() {
297 return [...mocks.backListeners].reverse().some((listener) => listener());
298 }
299
300 it("Back from the Pulls tab returns to Threads instead of leaving the app", async () => {
301 using _actEnvironment = setup(false, false, selected("pullRequests"));
302 const opened: string[] = [];
303 mocks.openWorkspaceTab = (tab) => opened.push(tab);
304 const root = createRoot(document.createElement("div"));
305 using _root = { [Symbol.dispose]: () => act(() => root.unmount()) };
306 await act(async () => root.render(createElement(WorkspaceRootScreen)));
307
308 expect(pressBack()).toBe(true);
309 expect(opened).toEqual(["threads"]);
310 });
311
312 it.each([
313 [false, false],
314 [true, false],
315 [false, true],
316 [true, true],
317 ])(
318 "keeps the Usage page mounted through layout transitions (split: %s, floating: %s)",
319 async (split, floating) => {
320 using _actEnvironment = setup(split, floating);
321 const container = document.createElement("div");
322 const root = createRoot(container);
323 using _root = { [Symbol.dispose]: () => act(() => root.unmount()) };
324 await act(async () => root.render(createElement(WorkspaceRootScreen)));
325
326 const button = container.querySelector("button")!;
327 await act(async () => button.click());
328 expect(button.textContent).toBe("Usage");
329
330 for (const nextSplit of [!split, split, !split, split]) {
331 mocks.split = nextSplit;
332 await act(async () => root.render(createElement(WorkspaceRootScreen)));
333 expect(container.querySelector("button")?.textContent).toBe("Usage");
334 expect(container.querySelector("button")).toBe(button);
335 }
336 },
337 );