Open History to see the last commit
1import * as NodeModule from "node:module";
2import { describe, expect, it } from "vite-plus/test";
3import {
4 androidWorkspaceTabFromNavigationState,
5 androidWorkspaceTabNavigationState,
6 type AndroidWorkspaceNavigationTab,
7} from "../features/workspace/android-workspace-tabs";
8import {
9 androidWorkspaceNavigationState,
10 parentThreadIsBelow,
11 resetAndroidWorkspace,
12 returnToAndroidWorkspaceList,
13 type AndroidWorkspaceDestination,
14 type AndroidWorkspaceResetAction,
15} from "./android-workspace-navigation";
16
17const require = NodeModule.createRequire(import.meta.url);
18const nativePackage = require.resolve("@react-navigation/native/package.json");
19const requireFromNative = NodeModule.createRequire(nativePackage);
20const corePackage = requireFromNative.resolve("@react-navigation/core/package.json");
21const requireFromCore = NodeModule.createRequire(corePackage);
22const { CommonActions, StackActions, StackRouter } = requireFromCore(
23 "@react-navigation/routers",
24) as {
25 readonly CommonActions: typeof import("@react-navigation/native").CommonActions;
26 readonly StackActions: typeof import("@react-navigation/native").StackActions;
27 readonly StackRouter: typeof import("@react-navigation/native").StackRouter;
28};
29const router = StackRouter({});
30const options = {
31 routeNames: [
32 "Home",
33 "Thread",
34 "ThreadFiles",
35 "ThreadTerminal",
36 "GitOverview",
37 "PullRequest",
38 "PullRequestFile",
39 "PullRequestReviewers",
40 "SettingsSheet",
41 ],
42 routeParamList: {},
43 routeGetIdList: {},
44};
45const thread = {
46 name: "Thread",
47 params: { environmentId: "environment-a", threadId: "thread-a" },
48} as const;
49const pullRequest = {
50 name: "PullRequest",
51 params: {
52 environmentId: "environment-a",
53 projectId: "project-a",
54 repository: "owner/repository",
55 number: 42,
56 host: "github.com",
57 },
58} as const;
59
60function apply(
61 state: ReturnType<typeof router.getInitialState>,
62 action: Parameters<typeof router.getStateForAction>[1],
63) {
64 const result = router.getStateForAction(state, action, options);
65 if (!result) throw new Error(`Router rejected ${action.type}`);
66 return router.getRehydratedState(result, options);
67}
68
69function select(
70 state: ReturnType<typeof router.getInitialState>,
71 destination?: AndroidWorkspaceDestination,
72 reviewers?: ReactNavigation.RootParamList["PullRequestReviewers"],
73) {
74 return apply(
75 state,
76 CommonActions.reset(androidWorkspaceNavigationState(state, destination, reviewers)),
77 );
78}
79
80function legacyHistory() {
81 let state = router.getInitialState(options);
82 state = apply(state, StackActions.push(thread.name, thread.params));
83 state = apply(state, StackActions.push(pullRequest.name, pullRequest.params));
84 return state;
85}
86
87function names(state: ReturnType<typeof router.getInitialState>) {
88 return state.routes.map((route) => route.name);
89}
90
91describe("Android workspace navigation", () => {
92 it("replaces cross-tab history with one detail and returns directly to its list", () => {
93 const previous = legacyHistory();
94 const state = select(previous, pullRequest);
95 expect(names(state)).toEqual(["Home", "PullRequest"]);
96 expect(state.routes[0]?.key).toBe(previous.routes[0]?.key);
97 expect(state.routes[1]?.key).toBe(previous.routes[2]?.key);
98 expect(names(apply(state, StackActions.pop()))).toEqual(["Home"]);
99 });
100
101 it("replaces peer thread selections without leaving the previous thread underneath", () => {
102 const first = select(router.getInitialState(options), thread);
103 const second = select(first, {
104 name: "Thread",
105 params: { ...thread.params, threadId: "thread-b" },
106 });
107 expect(names(second)).toEqual(["Home", "Thread"]);
108 expect(second.routes[1]?.params).toEqual({ ...thread.params, threadId: "thread-b" });
109 expect(second.routes[1]?.key).not.toBe(first.routes[1]?.key);
110 expect(names(apply(second, StackActions.pop()))).toEqual(["Home"]);
111 });
112
113 it("creates Home beneath cold-linked details and gives direct list escape", () => {
114 const initial = router.getInitialState(options);
115 const cold = apply(initial, CommonActions.reset({ index: 0, routes: [pullRequest] }));
116 expect(names(select(cold, pullRequest))).toEqual(["Home", "PullRequest"]);
117 expect(names(select(cold))).toEqual(["Home"]);
118 });
119
120 it("keeps a request-review flow above the chosen PR instead of an old chat", () => {
121 const state = select(legacyHistory(), pullRequest, pullRequest.params);
122 expect(names(state)).toEqual(["Home", "PullRequest", "PullRequestReviewers"]);
123 const back = apply(state, StackActions.pop());
124 expect(names(back)).toEqual(["Home", "PullRequest"]);
125 expect(back.routes[back.index]?.params).toEqual(pullRequest.params);
126 });
127
128 it.each(["ThreadFiles", "ThreadTerminal", "GitOverview"])(
129 "returns to chat when its sidebar row is selected from %s",
130 (route) => {
131 const detail = select(router.getInitialState(options), thread);
132 const nested = apply(detail, StackActions.push(route, thread.params));
133 const selected = select(nested, thread);
134 expect(names(selected)).toEqual(["Home", "Thread"]);
135 expect(selected.routes[1]?.key).toBe(detail.routes[1]?.key);
136 expect(names(apply(selected, StackActions.pop()))).toEqual(["Home"]);
137 },
138 );
139
140 it.each([{ environmentId: "environment-b" }, { repository: "owner/other" }])(
141 "does not reuse another PR's key when its scope differs: %o",
142 (change) => {
143 const previous = select(router.getInitialState(options), pullRequest);
144 const state = select(previous, {
145 name: "PullRequest",
146 params: { ...pullRequest.params, ...change },
147 });
148 expect(state.routes[1]?.key).not.toBe(previous.routes[1]?.key);
149 expect(state.routes[1]?.params).toEqual({ ...pullRequest.params, ...change });
150 },
151 );
152
153 it("returns to a distinct previous thread, otherwise to the workspace list", () => {
154 const parent = select(router.getInitialState(options), thread);
155 let state = apply(
156 parent,
157 StackActions.push("Thread", { ...thread.params, threadId: "child-a" }),
158 );
159 let listSelected = false;
160 const navigation = {
161 dispatch: (action: AndroidWorkspaceResetAction) => {
162 state = apply(state, action);
163 },
164 getParent: () => undefined,
165 getState: () => state,
166 goBack: () => {
167 state = apply(state, StackActions.pop());
168 },
169 };
170 const back = () =>
171 returnToAndroidWorkspaceList(navigation, () => {
172 listSelected = true;
173 });
174
175 back();
176 expect(state.routes).toEqual(parent.routes);
177 expect(state.index).toBe(parent.index);
178 expect(listSelected).toBe(false);
179
180 back();
181 expect(state.routes).toEqual([parent.routes[0]]);
182 expect(state.index).toBe(0);
183 expect(listSelected).toBe(true);
184
185 state = apply(
186 parent,
187 StackActions.push("Thread", { ...thread.params, environmentId: "environment-b" }),
188 );
189 listSelected = false;
190 back();
191 expect(state.routes).toEqual(parent.routes);
192 expect(state.index).toBe(parent.index);
193 expect(listSelected).toBe(false);
194
195 // Confirming a Git action from the inline inspector can duplicate the current thread.
196 state = apply(parent, StackActions.push("Thread", thread.params));
197 back();
198 expect({ routes: names(state), index: state.index, listSelected }).toEqual({
199 routes: ["Home"],
200 index: 0,
201 listSelected: true,
202 });
203 });
204
205 it("recognizes a parent only when its environment and thread are immediately below", () => {
206 const parent = select(router.getInitialState(options), thread);
207 const child = apply(
208 parent,
209 StackActions.push("Thread", { ...thread.params, threadId: "child-a" }),
210 );
211
212 expect(parentThreadIsBelow(child, thread.params)).toBe(true);
213 expect(parentThreadIsBelow(child, { ...thread.params, environmentId: "environment-b" })).toBe(
214 false,
215 );
216 expect(parentThreadIsBelow(child, { ...thread.params, threadId: "other" })).toBe(false);
217 expect(parentThreadIsBelow(parent, thread.params)).toBe(false);
218 expect(
219 parentThreadIsBelow(
220 apply(child, StackActions.push("Thread", { ...thread.params, threadId: "grandchild-a" })),
221 thread.params,
222 ),
223 ).toBe(false);
224 });
225
226 it("resets the root stack when a nested Settings stack has focus", () => {
227 const root = apply(router.getInitialState(options), StackActions.push("SettingsSheet"));
228 const settings = router.getInitialState({
229 ...options,
230 routeNames: ["SettingsContent", "SettingsEnvironments"],
231 });
232 const actions: AndroidWorkspaceResetAction[] = [];
233 const rootNavigation = {
234 dispatch: () => undefined,
235 getParent: () => undefined,
236 getState: () => root,
237 };
238 resetAndroidWorkspace(
239 {
240 dispatch: (action) => actions.push(action),
241 getParent: () => rootNavigation,
242 getState: () => settings,
243 },
244 thread,
245 );
246 const [action] = actions;
247 expect(action?.target).toBe(root.key);
248 expect(names(apply(root, action!))).toEqual(["Home", "Thread"]);
249 });
250
251 it("resets from the navigation container before any navigation action", () => {
252 const root = router.getInitialState(options);
253 const actions: AndroidWorkspaceResetAction[] = [];
254 // The container only learns the root state after the first action.
255 resetAndroidWorkspace(
256 {
257 dispatch: (action) => actions.push(action),
258 getParent: () => undefined,
259 getState: () => undefined,
260 getRootState: () => root,
261 },
262 thread,
263 );
264 const [action] = actions;
265 expect(action?.target).toBe(root.key);
266 expect(names(apply(root, action!))).toEqual(["Home", "Thread"]);
267 });
268});
269
270describe("Android Settings tab", () => {
271 type State = ReturnType<typeof router.getInitialState>;
272 type Tab = AndroidWorkspaceNavigationTab;
273 const openTab = (state: State, tab: Tab) =>
274 apply(state, CommonActions.reset(androidWorkspaceTabNavigationState(state, tab)));
275 const selectedTab = (state: State, previous: Tab) =>
276 androidWorkspaceTabFromNavigationState(state, previous);
277
278 it("opens over Home alone, dropping chat and PR history, and Back returns to Home", () => {
279 const previous = legacyHistory();
280 const settings = openTab(previous, "settings");
281 expect(names(settings)).toEqual(["Home", "SettingsSheet"]);
282 expect(settings.routes[0]?.key).toBe(previous.routes[0]?.key);
283 expect(selectedTab(settings, "threads")).toBe("settings");
284 const back = apply(settings, StackActions.pop());
285 expect(names(back)).toEqual(["Home"]);
286 expect(back.routes[0]?.key).toBe(previous.routes[0]?.key);
287 expect(selectedTab(back, "settings")).not.toBe("settings");
288 });
289
290 it("stays selected under a nested settings page until a list tab returns to Home", () => {
291 const previous = legacyHistory();
292 const settings = openTab(previous, "settings");
293 // Usage pushes inside the nested settings navigator, which the root router never sees.
294 const usage: State = {
295 ...settings,
296 routes: settings.routes.map((route) =>
297 route.name === "SettingsSheet"
298 ? {
299 ...route,
300 state: {
301 routes: [
302 {
303 name: "SettingsContent",
304 state: { index: 1, routes: [{ name: "Settings" }, { name: "SettingsUsage" }] },
305 },
306 ],
307 },
308 }
309 : route,
310 ),
311 };
312 expect(selectedTab(usage, "threads")).toBe("settings");
313 const list = openTab(usage, "pullRequests");
314 expect(names(list)).toEqual(["Home"]);
315 expect(list.routes[0]?.key).toBe(previous.routes[0]?.key);
316 expect(selectedTab(list, "pullRequests")).toBe("pullRequests");
317 });
318});