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("dismisses nested content and overlays when the sidebar selects a peer", () => {
114 let state = legacyHistory();
115 state = apply(
116 state,
117 StackActions.push("PullRequestFile", { ...pullRequest.params, path: "src/main.ts" }),
118 );
119 state = apply(state, StackActions.push("SettingsSheet"));
120 const selected = select(state, thread);
121 expect(names(selected)).toEqual(["Home", "Thread"]);
122 expect(selected.routes[1]?.params).toEqual(thread.params);
123 });
124
125 it("creates Home beneath cold-linked details and gives direct list escape", () => {
126 const initial = router.getInitialState(options);
127 const cold = apply(initial, CommonActions.reset({ index: 0, routes: [pullRequest] }));
128 expect(names(select(cold, pullRequest))).toEqual(["Home", "PullRequest"]);
129 expect(names(select(cold))).toEqual(["Home"]);
130 });
131
132 it("keeps a request-review flow above the chosen PR instead of an old chat", () => {
133 const state = select(legacyHistory(), pullRequest, pullRequest.params);
134 expect(names(state)).toEqual(["Home", "PullRequest", "PullRequestReviewers"]);
135 const back = apply(state, StackActions.pop());
136 expect(names(back)).toEqual(["Home", "PullRequest"]);
137 expect(back.routes[back.index]?.params).toEqual(pullRequest.params);
138 });
139
140 it("preserves normal nested PR Back behavior", () => {
141 const detail = select(legacyHistory(), pullRequest);
142 const nested = apply(
143 detail,
144 StackActions.push("PullRequestFile", { ...pullRequest.params, path: "file.ts" }),
145 );
146 const back = apply(nested, StackActions.pop());
147 expect(back.routes[back.index]?.key).toBe(detail.routes[detail.index]?.key);
148 expect(names(back)).toEqual(["Home", "PullRequest"]);
149 });
150
151 it.each(["ThreadFiles", "ThreadTerminal", "GitOverview"])(
152 "returns to chat when its sidebar row is selected from %s",
153 (route) => {
154 const detail = select(router.getInitialState(options), thread);
155 const nested = apply(detail, StackActions.push(route, thread.params));
156 const selected = select(nested, thread);
157 expect(names(selected)).toEqual(["Home", "Thread"]);
158 expect(selected.routes[1]?.key).toBe(detail.routes[1]?.key);
159 expect(names(apply(selected, StackActions.pop()))).toEqual(["Home"]);
160 },
161 );
162
163 it.each([
164 { environmentId: "environment-b" },
165 { projectId: "project-b" },
166 { repository: "owner/other" },
167 { host: "git.example.test" },
168 { number: 43 },
169 ])("does not reuse another PR's key when its scope differs: %o", (change) => {
170 const previous = select(router.getInitialState(options), pullRequest);
171 const state = select(previous, {
172 name: "PullRequest",
173 params: { ...pullRequest.params, ...change },
174 });
175 expect(state.routes[1]?.key).not.toBe(previous.routes[1]?.key);
176 expect(state.routes[1]?.params).toEqual({ ...pullRequest.params, ...change });
177 });
178
179 it("does not reuse a thread key from another environment", () => {
180 const previous = select(router.getInitialState(options), thread);
181 const state = select(previous, {
182 name: "Thread",
183 params: { ...thread.params, environmentId: "environment-b" },
184 });
185 expect(state.routes[1]?.key).not.toBe(previous.routes[1]?.key);
186 });
187
188 it("returns to a distinct previous thread, otherwise to the workspace list", () => {
189 const parent = select(router.getInitialState(options), thread);
190 let state = apply(
191 parent,
192 StackActions.push("Thread", { ...thread.params, threadId: "child-a" }),
193 );
194 let listSelected = false;
195 const navigation = {
196 dispatch: (action: AndroidWorkspaceResetAction) => {
197 state = apply(state, action);
198 },
199 getParent: () => undefined,
200 getState: () => state,
201 goBack: () => {
202 state = apply(state, StackActions.pop());
203 },
204 };
205 const back = () =>
206 returnToAndroidWorkspaceList(navigation, () => {
207 listSelected = true;
208 });
209
210 back();
211 expect(state.routes).toEqual(parent.routes);
212 expect(state.index).toBe(parent.index);
213 expect(listSelected).toBe(false);
214
215 back();
216 expect(state.routes).toEqual([parent.routes[0]]);
217 expect(state.index).toBe(0);
218 expect(listSelected).toBe(true);
219
220 state = apply(
221 parent,
222 StackActions.push("Thread", { ...thread.params, environmentId: "environment-b" }),
223 );
224 listSelected = false;
225 back();
226 expect(state.routes).toEqual(parent.routes);
227 expect(state.index).toBe(parent.index);
228 expect(listSelected).toBe(false);
229
230 // Confirming a Git action from the inline inspector can duplicate the current thread.
231 state = apply(parent, StackActions.push("Thread", thread.params));
232 back();
233 expect({ routes: names(state), index: state.index, listSelected }).toEqual({
234 routes: ["Home"],
235 index: 0,
236 listSelected: true,
237 });
238 });
239
240 it("recognizes a parent only when its environment and thread are immediately below", () => {
241 const parent = select(router.getInitialState(options), thread);
242 const child = apply(
243 parent,
244 StackActions.push("Thread", { ...thread.params, threadId: "child-a" }),
245 );
246
247 expect(parentThreadIsBelow(child, thread.params)).toBe(true);
248 expect(parentThreadIsBelow(child, { ...thread.params, environmentId: "environment-b" })).toBe(
249 false,
250 );
251 expect(parentThreadIsBelow(child, { ...thread.params, threadId: "other" })).toBe(false);
252 expect(parentThreadIsBelow(parent, thread.params)).toBe(false);
253 expect(
254 parentThreadIsBelow(
255 apply(child, StackActions.push("Thread", { ...thread.params, threadId: "grandchild-a" })),
256 thread.params,
257 ),
258 ).toBe(false);
259 });
260
261 it("drops stale detail history on a tab switch while retaining Home", () => {
262 const previous = legacyHistory();
263 const state = select(previous);
264 expect(names(state)).toEqual(["Home"]);
265 expect(state.routes[0]?.key).toBe(previous.routes[0]?.key);
266 expect(state.index).toBe(0);
267 });
268
269 it("resets the root stack when a nested Settings stack has focus", () => {
270 const root = apply(router.getInitialState(options), StackActions.push("SettingsSheet"));
271 const settings = router.getInitialState({
272 ...options,
273 routeNames: ["SettingsContent", "SettingsEnvironments"],
274 });
275 const actions: AndroidWorkspaceResetAction[] = [];
276 const rootNavigation = {
277 dispatch: () => undefined,
278 getParent: () => undefined,
279 getState: () => root,
280 };
281 resetAndroidWorkspace(
282 {
283 dispatch: (action) => actions.push(action),
284 getParent: () => rootNavigation,
285 getState: () => settings,
286 },
287 thread,
288 );
289 const [action] = actions;
290 expect(action?.target).toBe(root.key);
291 expect(names(apply(root, action!))).toEqual(["Home", "Thread"]);
292 });
293
294 it("resets from the navigation container before any navigation action", () => {
295 const root = router.getInitialState(options);
296 const actions: AndroidWorkspaceResetAction[] = [];
297 // The container only learns the root state after the first action.
298 resetAndroidWorkspace(
299 {
300 dispatch: (action) => actions.push(action),
301 getParent: () => undefined,
302 getState: () => undefined,
303 getRootState: () => root,
304 },
305 thread,
306 );
307 const [action] = actions;
308 expect(action?.target).toBe(root.key);
309 expect(names(apply(root, action!))).toEqual(["Home", "Thread"]);
310 });
311});
312
313describe("Android Settings tab", () => {
314 type State = ReturnType<typeof router.getInitialState>;
315 type Tab = AndroidWorkspaceNavigationTab;
316 const openTab = (state: State, tab: Tab) =>
317 apply(state, CommonActions.reset(androidWorkspaceTabNavigationState(state, tab)));
318 const selectedTab = (state: State, previous: Tab) =>
319 androidWorkspaceTabFromNavigationState(state, previous);
320
321 it("opens over Home alone, dropping chat and PR history, and Back returns to Home", () => {
322 const previous = legacyHistory();
323 const settings = openTab(previous, "settings");
324 expect(names(settings)).toEqual(["Home", "SettingsSheet"]);
325 expect(settings.routes[0]?.key).toBe(previous.routes[0]?.key);
326 expect(selectedTab(settings, "threads")).toBe("settings");
327 const back = apply(settings, StackActions.pop());
328 expect(names(back)).toEqual(["Home"]);
329 expect(back.routes[0]?.key).toBe(previous.routes[0]?.key);
330 expect(selectedTab(back, "settings")).not.toBe("settings");
331 });
332
333 it("stays selected under a nested settings page until a list tab returns to Home", () => {
334 const previous = legacyHistory();
335 const settings = openTab(previous, "settings");
336 // Usage pushes inside the nested settings navigator, which the root router never sees.
337 const usage: State = {
338 ...settings,
339 routes: settings.routes.map((route) =>
340 route.name === "SettingsSheet"
341 ? {
342 ...route,
343 state: {
344 routes: [
345 {
346 name: "SettingsContent",
347 state: { index: 1, routes: [{ name: "Settings" }, { name: "SettingsUsage" }] },
348 },
349 ],
350 },
351 }
352 : route,
353 ),
354 };
355 expect(selectedTab(usage, "threads")).toBe("settings");
356 const list = openTab(usage, "pullRequests");
357 expect(names(list)).toEqual(["Home"]);
358 expect(list.routes[0]?.key).toBe(previous.routes[0]?.key);
359 expect(selectedTab(list, "pullRequests")).toBe("pullRequests");
360 });
361});