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

android-workspace-navigation.test.ts

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