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

android-workspace-navigation.test.ts

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