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 linkedThreadStep,
11 parentThreadIsBelow,
12 resetAndroidWorkspace,
13 returnToAndroidWorkspaceList,
14} from "./android-workspace-navigation";
15import type { RootStackResetAction } from "./root-stack-reset";
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 "Settings",
41 "SettingsUsage",
42 "SettingsUsageAccount",
43 ],
44 routeParamList: {},
45 routeGetIdList: {},
46};
47const thread = {
48 name: "Thread",
49 params: { environmentId: "environment-a", threadId: "thread-a" },
50} as const;
51const pullRequest = {
52 name: "PullRequest",
53 params: {
54 environmentId: "environment-a",
55 projectId: "project-a",
56 repository: "owner/repository",
57 number: 42,
58 host: "github.com",
59 },
60} as const;
61
62function apply(
63 state: ReturnType<typeof router.getInitialState>,
64 action: Parameters<typeof router.getStateForAction>[1],
65) {
66 const result = router.getStateForAction(state, action, options);
67 if (!result) throw new Error(`Router rejected ${action.type}`);
68 return router.getRehydratedState(result, options);
69}
70
71function withThread() {
72 return apply(router.getInitialState(options), StackActions.push(thread.name, thread.params));
73}
74
75function legacyHistory() {
76 let state = router.getInitialState(options);
77 state = apply(state, StackActions.push(thread.name, thread.params));
78 state = apply(state, StackActions.push(pullRequest.name, pullRequest.params));
79 return state;
80}
81
82function names(state: ReturnType<typeof router.getInitialState>) {
83 return state.routes.map((route) => route.name);
84}
85
86describe("Android workspace navigation", () => {
87 it("returns to a distinct previous thread, otherwise to the workspace list", () => {
88 const parent = withThread();
89 let state = apply(
90 parent,
91 StackActions.push("Thread", { ...thread.params, threadId: "child-a" }),
92 );
93 let listSelected = false;
94 const navigation = {
95 dispatch: (action: RootStackResetAction) => {
96 state = apply(state, action);
97 },
98 getParent: () => undefined,
99 getState: () => state,
100 goBack: () => {
101 state = apply(state, StackActions.pop());
102 },
103 };
104 const back = () =>
105 returnToAndroidWorkspaceList(navigation, () => {
106 listSelected = true;
107 });
108
109 back();
110 expect(state.routes).toEqual(parent.routes);
111 expect(state.index).toBe(parent.index);
112 expect(listSelected).toBe(false);
113
114 back();
115 expect(state.routes).toEqual([parent.routes[0]]);
116 expect(state.index).toBe(0);
117 expect(listSelected).toBe(true);
118
119 state = apply(
120 parent,
121 StackActions.push("Thread", { ...thread.params, environmentId: "environment-b" }),
122 );
123 listSelected = false;
124 back();
125 expect(state.routes).toEqual(parent.routes);
126 expect(state.index).toBe(parent.index);
127 expect(listSelected).toBe(false);
128
129 // Confirming a Git action from the inline inspector can duplicate the current thread.
130 state = apply(parent, StackActions.push("Thread", thread.params));
131 back();
132 expect({ routes: names(state), index: state.index, listSelected }).toEqual({
133 routes: ["Home"],
134 index: 0,
135 listSelected: true,
136 });
137 });
138
139 it("opens a linked thread above the current one so Back returns to it", () => {
140 const parent = withThread();
141 const child = { ...thread.params, threadId: "child-a" };
142 const open = (
143 state: ReturnType<typeof router.getInitialState>,
144 target: ReactNavigation.RootParamList["Thread"],
145 ) => {
146 const step = linkedThreadStep(state, target);
147 if (step === "stay") return state;
148 return apply(
149 state,
150 step === "pop" ? StackActions.pop() : StackActions.push("Thread", target),
151 );
152 };
153
154 const inChild = open(parent, child);
155 expect(inChild.routes.map((route) => route.params)).toEqual([undefined, thread.params, child]);
156 expect(open(inChild, child)).toBe(inChild);
157
158 const backInParent = open(inChild, thread.params);
159 expect(backInParent.routes).toEqual(parent.routes);
160 expect(backInParent.index).toBe(parent.index);
161 });
162
163 it("recognizes a parent only when its environment and thread are immediately below", () => {
164 const parent = withThread();
165 const child = apply(
166 parent,
167 StackActions.push("Thread", { ...thread.params, threadId: "child-a" }),
168 );
169
170 expect(parentThreadIsBelow(child, thread.params)).toBe(true);
171 expect(parentThreadIsBelow(child, { ...thread.params, environmentId: "environment-b" })).toBe(
172 false,
173 );
174 expect(parentThreadIsBelow(child, { ...thread.params, threadId: "other" })).toBe(false);
175 expect(parentThreadIsBelow(parent, thread.params)).toBe(false);
176 expect(
177 parentThreadIsBelow(
178 apply(child, StackActions.push("Thread", { ...thread.params, threadId: "grandchild-a" })),
179 thread.params,
180 ),
181 ).toBe(false);
182 });
183
184 it("resets the root stack when a nested stack has focus", () => {
185 const root = apply(router.getInitialState(options), StackActions.push("Thread", thread.params));
186 const nested = router.getInitialState({ ...options, routeNames: ["Nested", "NestedChild"] });
187 const actions: RootStackResetAction[] = [];
188 const rootNavigation = {
189 dispatch: () => undefined,
190 getParent: () => undefined,
191 getState: () => root,
192 };
193 resetAndroidWorkspace({
194 dispatch: (action) => actions.push(action),
195 getParent: () => rootNavigation,
196 getState: () => nested,
197 });
198 const [action] = actions;
199 expect(action?.target).toBe(root.key);
200 expect(names(apply(root, action!))).toEqual(["Home"]);
201 });
202
203 it("resets from the navigation container before any navigation action", () => {
204 const root = router.getInitialState(options);
205 const actions: RootStackResetAction[] = [];
206 // The container only learns the root state after the first action.
207 resetAndroidWorkspace({
208 dispatch: (action) => actions.push(action),
209 getParent: () => undefined,
210 getState: () => undefined,
211 getRootState: () => root,
212 });
213 const [action] = actions;
214 expect(action?.target).toBe(root.key);
215 expect(names(apply(root, action!))).toEqual(["Home"]);
216 });
217});
218
219describe("Android Settings tab", () => {
220 type State = ReturnType<typeof router.getInitialState>;
221 type Tab = AndroidWorkspaceNavigationTab;
222 const openTab = (state: State, tab: Tab) =>
223 apply(state, CommonActions.reset(androidWorkspaceTabNavigationState(state, tab)));
224 const selectedTab = (state: State, previous: Tab) =>
225 androidWorkspaceTabFromNavigationState(state, previous);
226
227 it("opens over Home alone, dropping chat and PR history, and Back returns to Home", () => {
228 const previous = legacyHistory();
229 const settings = openTab(previous, "settings");
230 expect(names(settings)).toEqual(["Home", "Settings"]);
231 expect(settings.routes[0]?.key).toBe(previous.routes[0]?.key);
232 expect(selectedTab(settings, "threads")).toBe("settings");
233 const back = apply(settings, StackActions.pop());
234 expect(names(back)).toEqual(["Home"]);
235 expect(back.routes[0]?.key).toBe(previous.routes[0]?.key);
236 expect(selectedTab(back, "settings")).not.toBe("settings");
237 });
238
239 it("stays selected under a settings page until a list tab returns to Home", () => {
240 const previous = legacyHistory();
241 const usage = apply(openTab(previous, "settings"), StackActions.push("SettingsUsage"));
242 expect(selectedTab(usage, "threads")).toBe("settings");
243 const list = openTab(usage, "pullRequests");
244 expect(names(list)).toEqual(["Home"]);
245 expect(list.routes[0]?.key).toBe(previous.routes[0]?.key);
246 expect(selectedTab(list, "pullRequests")).toBe("pullRequests");
247 });
248
249 it("leaves a settings page opened from the Usage tab in that tab", () => {
250 const account = apply(
251 router.getInitialState(options),
252 StackActions.push("SettingsUsageAccount"),
253 );
254 expect(selectedTab(account, "usage")).toBe("usage");
255 });
256});