Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

android-workspace-navigation.test.ts

343 lines12.4 KB

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