Open History to see the last commit
1import * as NodeModule from "node:module";
2import { describe, expect, it } from "vite-plus/test";
3import {
4 androidWorkspaceNavigationState,
5 resetAndroidWorkspace,
6 type AndroidWorkspaceDestination,
7 type AndroidWorkspaceResetAction,
8} from "./android-workspace-navigation";
9
10const require = NodeModule.createRequire(import.meta.url);
11const nativePackage = require.resolve("@react-navigation/native/package.json");
12const requireFromNative = NodeModule.createRequire(nativePackage);
13const corePackage = requireFromNative.resolve("@react-navigation/core/package.json");
14const requireFromCore = NodeModule.createRequire(corePackage);
15const { CommonActions, StackActions, StackRouter } = requireFromCore(
16 "@react-navigation/routers",
17) as {
18 readonly CommonActions: typeof import("@react-navigation/native").CommonActions;
19 readonly StackActions: typeof import("@react-navigation/native").StackActions;
20 readonly StackRouter: typeof import("@react-navigation/native").StackRouter;
21};
22const router = StackRouter({});
23const options = {
24 routeNames: [
25 "Home",
26 "Thread",
27 "ThreadFiles",
28 "ThreadTerminal",
29 "GitOverview",
30 "PullRequest",
31 "PullRequestFile",
32 "PullRequestReviewers",
33 "SettingsSheet",
34 ],
35 routeParamList: {},
36 routeGetIdList: {},
37};
38const thread = {
39 name: "Thread",
40 params: { environmentId: "environment-a", threadId: "thread-a" },
41} as const;
42const pullRequest = {
43 name: "PullRequest",
44 params: {
45 environmentId: "environment-a",
46 projectId: "project-a",
47 repository: "owner/repository",
48 number: 42,
49 host: "github.com",
50 },
51} as const;
52
53function apply(
54 state: ReturnType<typeof router.getInitialState>,
55 action: Parameters<typeof router.getStateForAction>[1],
56) {
57 const result = router.getStateForAction(state, action, options);
58 if (!result) throw new Error(`Router rejected ${action.type}`);
59 return router.getRehydratedState(result, options);
60}
61
62function select(
63 state: ReturnType<typeof router.getInitialState>,
64 destination?: AndroidWorkspaceDestination,
65 reviewers?: ReactNavigation.RootParamList["PullRequestReviewers"],
66) {
67 return apply(
68 state,
69 CommonActions.reset(androidWorkspaceNavigationState(state, destination, reviewers)),
70 );
71}
72
73function legacyHistory() {
74 let state = router.getInitialState(options);
75 state = apply(state, StackActions.push(thread.name, thread.params));
76 state = apply(state, StackActions.push(pullRequest.name, pullRequest.params));
77 return state;
78}
79
80function names(state: ReturnType<typeof router.getInitialState>) {
81 return state.routes.map((route) => route.name);
82}
83
84describe("Android workspace navigation", () => {
85 it("replaces cross-tab history with one detail and returns directly to its list", () => {
86 const previous = legacyHistory();
87 const state = select(previous, pullRequest);
88 expect(names(state)).toEqual(["Home", "PullRequest"]);
89 expect(state.routes[0]?.key).toBe(previous.routes[0]?.key);
90 expect(state.routes[1]?.key).toBe(previous.routes[2]?.key);
91 expect(names(apply(state, StackActions.pop()))).toEqual(["Home"]);
92 });
93
94 it("replaces peer thread selections without leaving the previous thread underneath", () => {
95 const first = select(router.getInitialState(options), thread);
96 const second = select(first, {
97 name: "Thread",
98 params: { ...thread.params, threadId: "thread-b" },
99 });
100 expect(names(second)).toEqual(["Home", "Thread"]);
101 expect(second.routes[1]?.params).toEqual({ ...thread.params, threadId: "thread-b" });
102 expect(second.routes[1]?.key).not.toBe(first.routes[1]?.key);
103 expect(names(apply(second, StackActions.pop()))).toEqual(["Home"]);
104 });
105
106 it("dismisses nested content and overlays when the sidebar selects a peer", () => {
107 let state = legacyHistory();
108 state = apply(
109 state,
110 StackActions.push("PullRequestFile", { ...pullRequest.params, path: "src/main.ts" }),
111 );
112 state = apply(state, StackActions.push("SettingsSheet"));
113 const selected = select(state, thread);
114 expect(names(selected)).toEqual(["Home", "Thread"]);
115 expect(selected.routes[1]?.params).toEqual(thread.params);
116 });
117
118 it("creates Home beneath cold-linked details and gives direct list escape", () => {
119 const initial = router.getInitialState(options);
120 const cold = apply(initial, CommonActions.reset({ index: 0, routes: [pullRequest] }));
121 expect(names(select(cold, pullRequest))).toEqual(["Home", "PullRequest"]);
122 expect(names(select(cold))).toEqual(["Home"]);
123 });
124
125 it("keeps a request-review flow above the chosen PR instead of an old chat", () => {
126 const state = select(legacyHistory(), pullRequest, pullRequest.params);
127 expect(names(state)).toEqual(["Home", "PullRequest", "PullRequestReviewers"]);
128 const back = apply(state, StackActions.pop());
129 expect(names(back)).toEqual(["Home", "PullRequest"]);
130 expect(back.routes[back.index]?.params).toEqual(pullRequest.params);
131 });
132
133 it("preserves normal nested PR Back behavior", () => {
134 const detail = select(legacyHistory(), pullRequest);
135 const nested = apply(
136 detail,
137 StackActions.push("PullRequestFile", { ...pullRequest.params, path: "file.ts" }),
138 );
139 const back = apply(nested, StackActions.pop());
140 expect(back.routes[back.index]?.key).toBe(detail.routes[detail.index]?.key);
141 expect(names(back)).toEqual(["Home", "PullRequest"]);
142 });
143
144 it.each(["ThreadFiles", "ThreadTerminal", "GitOverview"])(
145 "returns to chat when its sidebar row is selected from %s",
146 (route) => {
147 const detail = select(router.getInitialState(options), thread);
148 const nested = apply(detail, StackActions.push(route, thread.params));
149 const selected = select(nested, thread);
150 expect(names(selected)).toEqual(["Home", "Thread"]);
151 expect(selected.routes[1]?.key).toBe(detail.routes[1]?.key);
152 expect(names(apply(selected, StackActions.pop()))).toEqual(["Home"]);
153 },
154 );
155
156 it.each([
157 { environmentId: "environment-b" },
158 { projectId: "project-b" },
159 { repository: "owner/other" },
160 { host: "git.example.test" },
161 { number: 43 },
162 ])("does not reuse another PR's key when its scope differs: %o", (change) => {
163 const previous = select(router.getInitialState(options), pullRequest);
164 const state = select(previous, {
165 name: "PullRequest",
166 params: { ...pullRequest.params, ...change },
167 });
168 expect(state.routes[1]?.key).not.toBe(previous.routes[1]?.key);
169 expect(state.routes[1]?.params).toEqual({ ...pullRequest.params, ...change });
170 });
171
172 it("does not reuse a thread key from another environment", () => {
173 const previous = select(router.getInitialState(options), thread);
174 const state = select(previous, {
175 name: "Thread",
176 params: { ...thread.params, environmentId: "environment-b" },
177 });
178 expect(state.routes[1]?.key).not.toBe(previous.routes[1]?.key);
179 });
180
181 it("drops stale detail history on a tab switch while retaining Home", () => {
182 const previous = legacyHistory();
183 const state = select(previous);
184 expect(names(state)).toEqual(["Home"]);
185 expect(state.routes[0]?.key).toBe(previous.routes[0]?.key);
186 expect(state.index).toBe(0);
187 });
188
189 it("resets the root stack when a nested Settings stack has focus", () => {
190 const root = apply(router.getInitialState(options), StackActions.push("SettingsSheet"));
191 const settings = router.getInitialState({
192 ...options,
193 routeNames: ["SettingsContent", "SettingsEnvironments"],
194 });
195 const actions: AndroidWorkspaceResetAction[] = [];
196 const rootNavigation = {
197 dispatch: () => undefined,
198 getParent: () => undefined,
199 getState: () => root,
200 };
201 resetAndroidWorkspace(
202 {
203 dispatch: (action) => actions.push(action),
204 getParent: () => rootNavigation,
205 getState: () => settings,
206 },
207 thread,
208 );
209 const [action] = actions;
210 expect(action?.target).toBe(root.key);
211 expect(names(apply(root, action!))).toEqual(["Home", "Thread"]);
212 });
213
214 it("resets from the navigation container before any navigation action", () => {
215 const root = router.getInitialState(options);
216 const actions: AndroidWorkspaceResetAction[] = [];
217 // The container only learns the root state after the first action.
218 resetAndroidWorkspace(
219 {
220 dispatch: (action) => actions.push(action),
221 getParent: () => undefined,
222 getState: () => undefined,
223 getRootState: () => root,
224 },
225 thread,
226 );
227 const [action] = actions;
228 expect(action?.target).toBe(root.key);
229 expect(names(apply(root, action!))).toEqual(["Home", "Thread"]);
230 });
231});