The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import * as NodeModule from "node:module"; |
| 2 | import type { ParamListBase, StackNavigationState } from "@react-navigation/native"; |
| 3 | import { describe, expect, it } from "vite-plus/test"; |
| 4 | |
| 5 | import { |
| 6 | collapsedSplitTransition, |
| 7 | nativeStackPopAction, |
| 8 | nativeWorkspacePopAction, |
| 9 | nativeWorkspacePopCount, |
| 10 | projectStackPresentation, |
| 11 | projectWorkspaceStack, |
| 12 | partitionStackPresentations, |
| 13 | reconcileStackScreens, |
| 14 | type CollapsedSplitColumn, |
| 15 | } from "./workspace-stack-projection"; |
| 16 | |
| 17 | const requireNavigation = NodeModule.createRequire( |
| 18 | NodeModule.createRequire(import.meta.url).resolve("@react-navigation/native/package.json"), |
| 19 | ); |
| 20 | const { StackRouter } = requireNavigation("@react-navigation/routers") as { |
| 21 | StackRouter: typeof import("@react-navigation/native").StackRouter; |
| 22 | }; |
| 23 | |
| 24 | const home = { key: "home", name: "Home" }; |
| 25 | const thread = { key: "thread", name: "Thread", params: { threadId: "draft-thread" } }; |
| 26 | const files = { key: "files", name: "ThreadFiles", params: thread.params }; |
| 27 | const settings = { key: "settings", name: "Settings" }; |
| 28 | const legal = { key: "legal", name: "SettingsLegal" }; |
| 29 | |
| 30 | function history( |
| 31 | routes: StackNavigationState<ParamListBase>["routes"], |
| 32 | ): StackNavigationState<ParamListBase> { |
| 33 | return { |
| 34 | key: "router", |
| 35 | type: "stack", |
| 36 | stale: false, |
| 37 | index: routes.length - 1, |
| 38 | routeNames: ["Home", "Thread", "ThreadFiles", "Settings", "SettingsLegal"], |
| 39 | routes, |
| 40 | preloadedRoutes: [], |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | describe("workspace presentation envelope", () => { |
| 45 | const isOverlay = (route: { name: string }) => route.name === "ReviewSheet"; |
| 46 | |
| 47 | it("keeps card settings in the detail history and restores it after system Back closes a sheet", () => { |
| 48 | const router = StackRouter({}); |
| 49 | const state = history([home, thread, settings]); |
| 50 | const routerOptions = { |
| 51 | routeNames: [...state.routeNames, "ReviewSheet", "ReviewDetails"], |
| 52 | routeParamList: {}, |
| 53 | routeGetIdList: {}, |
| 54 | }; |
| 55 | const opened = router.getStateForAction( |
| 56 | { ...state, routeNames: routerOptions.routeNames }, |
| 57 | { type: "PUSH", payload: { name: "ReviewSheet" } }, |
| 58 | routerOptions, |
| 59 | ); |
| 60 | if (!opened || opened.stale !== false) throw new Error("Expected an opened sheet history"); |
| 61 | const projection = projectStackPresentation(opened, isOverlay); |
| 62 | expect(projection.workspace).toEqual(state.routes); |
| 63 | expect(projection.workspace.at(-1)).toBe(settings); |
| 64 | expect(projection.overlays.map((route) => route.name)).toEqual(["ReviewSheet"]); |
| 65 | const closed = router.getStateForAction(opened, { type: "GO_BACK" }, routerOptions); |
| 66 | if (!closed || closed.stale !== false) throw new Error("Expected a restored workspace history"); |
| 67 | expect(projectStackPresentation(closed, isOverlay)).toEqual({ |
| 68 | workspace: state.routes, |
| 69 | overlays: [], |
| 70 | }); |
| 71 | }); |
| 72 | |
| 73 | it("keeps pushed sheet pages above the workspace and dismisses the entire sheet group", () => { |
| 74 | const sheet = { key: "review", name: "ReviewSheet" }; |
| 75 | const page = { key: "review-details", name: "ReviewDetails" }; |
| 76 | const state = history([home, thread, sheet, page]); |
| 77 | expect(projectStackPresentation(state, isOverlay)).toEqual({ |
| 78 | workspace: [home, thread], |
| 79 | overlays: [sheet, page], |
| 80 | }); |
| 81 | const closed = StackRouter({}).getStateForAction( |
| 82 | state, |
| 83 | nativeWorkspacePopAction(state, sheet.key)!, |
| 84 | { routeNames: state.routeNames, routeParamList: {}, routeGetIdList: {} }, |
| 85 | ); |
| 86 | if (!closed || closed.stale !== false) throw new Error("Expected a dismissed sheet history"); |
| 87 | expect(projectStackPresentation(closed, isOverlay)).toEqual({ |
| 88 | workspace: [home, thread], |
| 89 | overlays: [], |
| 90 | }); |
| 91 | expect(closed.routes[1]).toBe(thread); |
| 92 | }); |
| 93 | |
| 94 | it("preserves a cold-linked detail history without requiring a Home route", () => { |
| 95 | const sheet = { key: "review", name: "ReviewSheet" }; |
| 96 | expect(projectStackPresentation(history([thread, files, sheet]), isOverlay)).toEqual({ |
| 97 | workspace: [thread, files], |
| 98 | overlays: [sheet], |
| 99 | }); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | describe("workspace router projection", () => { |
| 104 | it("keeps workspace flows beside Home and restores them after a modal closes", () => { |
| 105 | const draft = { key: "draft", name: "Thread", params: { draft: {} } }; |
| 106 | const modal = { key: "connect", name: "ConnectOnboarding" }; |
| 107 | const routes = [home, thread, draft, legal]; |
| 108 | const isOverlay = (route: { name: string }) => route.name === "ConnectOnboarding"; |
| 109 | expect(projectWorkspaceStack(history([...routes, modal]), isOverlay)).toEqual({ |
| 110 | primary: home, |
| 111 | detail: [thread, draft, legal], |
| 112 | overlays: [modal], |
| 113 | }); |
| 114 | expect(projectWorkspaceStack(history(routes), isOverlay)).toEqual({ |
| 115 | primary: home, |
| 116 | detail: [thread, draft, legal], |
| 117 | overlays: [], |
| 118 | }); |
| 119 | }); |
| 120 | |
| 121 | it("shows the Settings list as the primary content and its pages as the detail", () => { |
| 122 | const environments = { key: "environments", name: "SettingsEnvironments" }; |
| 123 | expect(projectWorkspaceStack(history([home, settings, environments]), () => false)).toEqual({ |
| 124 | primary: home, |
| 125 | detail: [environments], |
| 126 | overlays: [], |
| 127 | }); |
| 128 | }); |
| 129 | |
| 130 | it("returns from a Settings page to the Settings list that Home is showing", () => { |
| 131 | const environments = { key: "environments", name: "SettingsEnvironments" }; |
| 132 | expect( |
| 133 | nativeStackPopAction(history([home, settings, environments]), environments.key, home.key), |
| 134 | ).toMatchObject({ payload: { count: 1 } }); |
| 135 | }); |
| 136 | |
| 137 | it("retains a cold-linked detail route without changing the router history", () => { |
| 138 | const state = history([thread, files]); |
| 139 | expect(projectWorkspaceStack(state, () => false)).toEqual({ |
| 140 | primary: undefined, |
| 141 | detail: [thread, files], |
| 142 | overlays: [], |
| 143 | }); |
| 144 | expect(state.routes[0]).toBe(thread); |
| 145 | }); |
| 146 | }); |
| 147 | |
| 148 | describe("native workspace dismissal", () => { |
| 149 | it("dismisses a sheet and its pushed pages without removing the underlying draft", () => { |
| 150 | const state = history([home, thread, settings, legal]); |
| 151 | const action = nativeWorkspacePopAction(state, settings.key)!; |
| 152 | const next = StackRouter({}).getStateForAction(state, action, { |
| 153 | routeNames: state.routeNames, |
| 154 | routeParamList: {}, |
| 155 | routeGetIdList: {}, |
| 156 | }); |
| 157 | expect(next?.routes).toEqual([home, thread]); |
| 158 | expect(next?.routes[1]).toBe(thread); |
| 159 | expect(next?.index).toBe(1); |
| 160 | }); |
| 161 | |
| 162 | it("does not dismiss retained sheets beyond the active index", () => { |
| 163 | const state = { ...history([home, thread, settings, legal]), index: 1 }; |
| 164 | expect(nativeWorkspacePopAction(state, settings.key)).toBeNull(); |
| 165 | expect(nativeWorkspacePopAction(state, legal.key)).toBeNull(); |
| 166 | }); |
| 167 | |
| 168 | it("removes the descendants when UIKit dismisses their parent conversation", () => { |
| 169 | expect(nativeWorkspacePopCount(history([home, thread, files]), thread.key)).toBe(2); |
| 170 | }); |
| 171 | |
| 172 | it("ignores delayed callbacks from JS removal and replacement", () => { |
| 173 | expect(nativeWorkspacePopCount(history([home, thread]), files.key)).toBe(0); |
| 174 | expect( |
| 175 | nativeWorkspacePopCount(history([home, { ...thread, key: "replacement" }]), thread.key), |
| 176 | ).toBe(0); |
| 177 | }); |
| 178 | |
| 179 | it("never removes the root or a route ahead of the active index", () => { |
| 180 | const state = history([home, thread, files]); |
| 181 | expect(nativeWorkspacePopCount(state, home.key)).toBe(0); |
| 182 | expect(nativeWorkspacePopCount({ ...state, index: 1 }, files.key)).toBe(0); |
| 183 | }); |
| 184 | }); |
| 185 | |
| 186 | describe("native card pop handoff", () => { |
| 187 | const routerOptions = { |
| 188 | routeNames: history([home]).routeNames, |
| 189 | routeParamList: {}, |
| 190 | routeGetIdList: {}, |
| 191 | }; |
| 192 | it("keeps a newly opened screen when an earlier swipe dismissal arrives late", () => { |
| 193 | const state = history([home, thread, files, settings]); |
| 194 | const next = StackRouter({}).getStateForAction( |
| 195 | state, |
| 196 | nativeStackPopAction(state, files.key)!, |
| 197 | routerOptions, |
| 198 | ); |
| 199 | expect(next?.routes).toEqual([home, thread, settings]); |
| 200 | expect(next?.index).toBe(2); |
| 201 | if (!next || next.stale !== false) throw new Error("Expected a rehydrated router history"); |
| 202 | expect(nativeStackPopAction(next, files.key)).toBeNull(); |
| 203 | }); |
| 204 | it("acknowledges a native pop to an earlier page without consuming a newer push", () => { |
| 205 | const state = history([home, thread, files, settings]); |
| 206 | const next = StackRouter({}).getStateForAction( |
| 207 | state, |
| 208 | nativeStackPopAction(state, files.key, home.key)!, |
| 209 | routerOptions, |
| 210 | ); |
| 211 | expect(next?.routes).toEqual([home, settings]); |
| 212 | expect(next?.index).toBe(1); |
| 213 | }); |
| 214 | it("allows reopening the same page after its destination finishes appearing", () => { |
| 215 | const state = history([home, thread, files]); |
| 216 | const router = StackRouter({}); |
| 217 | const popped = router.getStateForAction( |
| 218 | state, |
| 219 | nativeStackPopAction(state, files.key, thread.key)!, |
| 220 | routerOptions, |
| 221 | ); |
| 222 | if (!popped || popped.stale !== false) throw new Error("Expected a rehydrated router history"); |
| 223 | const reopened = router.getStateForAction( |
| 224 | popped, |
| 225 | { type: "NAVIGATE", payload: { name: files.name, params: files.params } }, |
| 226 | routerOptions, |
| 227 | ); |
| 228 | expect(reopened?.routes.map((route) => route.name)).toEqual(["Home", "Thread", "ThreadFiles"]); |
| 229 | expect(reopened?.routes.at(-1)?.key).not.toBe(files.key); |
| 230 | if (!reopened || reopened.stale !== false) |
| 231 | throw new Error("Expected a rehydrated router history"); |
| 232 | expect(nativeStackPopAction(reopened, files.key)).toBeNull(); |
| 233 | }); |
| 234 | it("leaves history intact when a swipe is cancelled or its source is already gone", () => { |
| 235 | const state = history([home, thread, files]); |
| 236 | expect(nativeStackPopAction(state, files.key, files.key)).toBeNull(); |
| 237 | expect(nativeStackPopAction(state, files.key, settings.key)).toBeNull(); |
| 238 | expect(nativeStackPopAction(state, home.key)).toBeNull(); |
| 239 | expect(nativeStackPopAction({ ...state, index: 1 }, files.key)).toBeNull(); |
| 240 | }); |
| 241 | }); |
| 242 | |
| 243 | describe("collapsed split Back", () => { |
| 244 | const routerOptions = { |
| 245 | routeNames: history([home]).routeNames, |
| 246 | routeParamList: {}, |
| 247 | routeGetIdList: {}, |
| 248 | }; |
| 249 | type Appearance = "willAppear" | "didAppear" | "willDisappear" | "didDisappear"; |
| 250 | // UIKit's appearance callbacks for one collapsed transition, against the router at each step. |
| 251 | function replay( |
| 252 | steps: ReadonlyArray<readonly [Appearance, string, StackNavigationState<ParamListBase>]>, |
| 253 | ) { |
| 254 | let column: CollapsedSplitColumn = { visible: "secondary", leaving: null }; |
| 255 | let pop: ReturnType<typeof nativeStackPopAction> = null; |
| 256 | for (const [type, key, state] of steps) { |
| 257 | const transition = collapsedSplitTransition(column, { type, key }, state, home.key); |
| 258 | column = transition.column; |
| 259 | pop ??= transition.pop; |
| 260 | } |
| 261 | return { visible: column.visible, pop }; |
| 262 | } |
| 263 | |
| 264 | it("returns the router to the list so the next selection opens a new detail", () => { |
| 265 | const state = history([home, thread]); |
| 266 | const back = replay([ |
| 267 | ["willDisappear", thread.key, state], |
| 268 | ["willAppear", home.key, state], |
| 269 | ["didAppear", home.key, state], |
| 270 | ]); |
| 271 | expect(back.visible).toBe("primary"); |
| 272 | const router = StackRouter({}); |
| 273 | const listed = router.getStateForAction(state, back.pop!, routerOptions); |
| 274 | expect(listed?.routes).toEqual([home]); |
| 275 | if (!listed || listed.stale !== false) throw new Error("Expected a rehydrated router history"); |
| 276 | const select = { |
| 277 | type: "NAVIGATE", |
| 278 | payload: { name: "Thread", params: { threadId: "next" } }, |
| 279 | } as const; |
| 280 | expect(router.getStateForAction(listed, select, routerOptions)?.routes[1]?.key).not.toBe( |
| 281 | thread.key, |
| 282 | ); |
| 283 | // Left on the hidden detail, the selection would only rename it and never show the column. |
| 284 | expect(router.getStateForAction(state, select, routerOptions)?.routes[1]?.key).toBe(thread.key); |
| 285 | }); |
| 286 | |
| 287 | it("clears every detail while keeping a presentation opened during the Back animation", () => { |
| 288 | const leaving = history([home, thread, files]); |
| 289 | const presented = history([home, thread, files, settings]); |
| 290 | const { pop } = replay([ |
| 291 | ["willDisappear", files.key, leaving], |
| 292 | ["didAppear", home.key, presented], |
| 293 | ]); |
| 294 | expect(StackRouter({}).getStateForAction(presented, pop!, routerOptions)?.routes).toEqual([ |
| 295 | home, |
| 296 | settings, |
| 297 | ]); |
| 298 | }); |
| 299 | |
| 300 | it("keeps the detail when it appears again before the list finishes appearing", () => { |
| 301 | const state = history([home, thread]); |
| 302 | const cancelledSwipe = replay([ |
| 303 | ["willDisappear", thread.key, state], |
| 304 | ["willAppear", home.key, state], |
| 305 | ["willAppear", thread.key, state], |
| 306 | ["didAppear", thread.key, state], |
| 307 | ]); |
| 308 | expect(cancelledSwipe).toEqual({ visible: "secondary", pop: null }); |
| 309 | expect( |
| 310 | replay([ |
| 311 | ["willDisappear", thread.key, state], |
| 312 | ["willAppear", thread.key, state], |
| 313 | ["didAppear", home.key, state], |
| 314 | ]).pop, |
| 315 | ).toBeNull(); |
| 316 | }); |
| 317 | |
| 318 | it("forgets the leaving detail when a full-screen presentation covers it", () => { |
| 319 | const state = history([home, thread]); |
| 320 | const leavingAfter = (steps: ReadonlyArray<readonly [Appearance, string]>) => |
| 321 | steps.reduce<CollapsedSplitColumn>( |
| 322 | (column, [type, key]) => |
| 323 | collapsedSplitTransition(column, { type, key }, state, home.key).column, |
| 324 | { visible: "secondary", leaving: null }, |
| 325 | ).leaving; |
| 326 | expect( |
| 327 | leavingAfter([ |
| 328 | ["willDisappear", thread.key], |
| 329 | ["didDisappear", thread.key], |
| 330 | ]), |
| 331 | ).toBeNull(); |
| 332 | // Native Back reveals the list before the detail finishes disappearing. |
| 333 | expect( |
| 334 | leavingAfter([ |
| 335 | ["willDisappear", thread.key], |
| 336 | ["willAppear", home.key], |
| 337 | ["didDisappear", thread.key], |
| 338 | ]), |
| 339 | ).toBe(thread.key); |
| 340 | }); |
| 341 | |
| 342 | it("does not mistake JS navigation or a cold link for native Back", () => { |
| 343 | const pushed = history([home, thread, files]); |
| 344 | expect( |
| 345 | replay([ |
| 346 | ["willDisappear", thread.key, pushed], |
| 347 | ["didAppear", files.key, pushed], |
| 348 | ]), |
| 349 | ).toEqual({ visible: "secondary", pop: null }); |
| 350 | const presented = history([home, thread, settings]); |
| 351 | expect( |
| 352 | replay([ |
| 353 | ["willDisappear", thread.key, presented], |
| 354 | ["didAppear", home.key, presented], |
| 355 | ]).pop, |
| 356 | ).toBeNull(); |
| 357 | const popped = history([home]); |
| 358 | expect( |
| 359 | replay([ |
| 360 | ["willDisappear", thread.key, popped], |
| 361 | ["didAppear", home.key, popped], |
| 362 | ]).pop, |
| 363 | ).toBeNull(); |
| 364 | // A linked thread whose list appears first still needs its detail column shown. |
| 365 | expect(replay([["didAppear", home.key, history([home, thread])]])).toEqual({ |
| 366 | visible: "primary", |
| 367 | pop: null, |
| 368 | }); |
| 369 | }); |
| 370 | }); |
| 371 | |
| 372 | describe("v5 stack handoff", () => { |
| 373 | it("releases a completed native pop when the router acknowledges it", () => { |
| 374 | const completed = new Set([files.key]); |
| 375 | expect(reconcileStackScreens([home, thread, files], [home, thread, files], completed)).toEqual([ |
| 376 | home, |
| 377 | thread, |
| 378 | files, |
| 379 | ]); |
| 380 | expect(reconcileStackScreens([home, thread, files], [home, thread], completed)).toEqual([ |
| 381 | home, |
| 382 | thread, |
| 383 | ]); |
| 384 | }); |
| 385 | it("still retains an unfinished JS pop beside a completed native pop", () => { |
| 386 | expect(reconcileStackScreens([home, thread, files], [home], new Set([files.key]))).toEqual([ |
| 387 | home, |
| 388 | thread, |
| 389 | ]); |
| 390 | }); |
| 391 | it("retains removed native screens through a pop followed immediately by a push", () => { |
| 392 | const popped = reconcileStackScreens([home, thread, files], [home, thread]); |
| 393 | const next = { ...files, key: "new-files" }; |
| 394 | const screens = reconcileStackScreens(popped, [home, thread, next]); |
| 395 | expect(screens.map((route) => route.key)).toEqual(["home", "thread", "files", "new-files"]); |
| 396 | expect(screens.filter((route) => [home, thread, next].includes(route))).toEqual([ |
| 397 | home, |
| 398 | thread, |
| 399 | next, |
| 400 | ]); |
| 401 | expect(screens[2]).toBe(files); |
| 402 | }); |
| 403 | it("keeps the root and outgoing settings page in place until the JS pop finishes", () => { |
| 404 | const environments = { ...files, key: "environments", name: "SettingsEnvironments" }; |
| 405 | const popped = reconcileStackScreens([settings, environments], [settings]); |
| 406 | expect(popped).toEqual([settings, environments]); |
| 407 | expect(reconcileStackScreens(popped, [settings])).toEqual(popped); |
| 408 | const next = { ...environments, key: "next-environments" }; |
| 409 | expect(reconcileStackScreens(popped, [settings, next])).toEqual([settings, environments, next]); |
| 410 | expect(reconcileStackScreens(popped, [settings], new Set([environments.key]))).toEqual([ |
| 411 | settings, |
| 412 | ]); |
| 413 | }); |
| 414 | // regression: #17231 keep native screens ordered during stack pops |
| 415 | it("retains a multi-page pop in order without changing the active router order", () => { |
| 416 | const replacement = { ...thread, key: "replacement" }; |
| 417 | const screens = reconcileStackScreens([home, thread, files], [home, replacement]); |
| 418 | expect(screens).toEqual([home, thread, files, replacement]); |
| 419 | expect(screens.filter((route) => route === home || route === replacement)).toEqual([ |
| 420 | home, |
| 421 | replacement, |
| 422 | ]); |
| 423 | }); |
| 424 | it("updates params without retaining duplicate copies of the same screen", () => { |
| 425 | const updated = { ...thread, params: { threadId: "another-thread" } }; |
| 426 | expect(reconcileStackScreens([home, thread], [home, updated])).toEqual([home, updated]); |
| 427 | }); |
| 428 | it("keeps card pushes inside the modal they belong to", () => { |
| 429 | const secondModal = { ...settings, key: "another-settings" }; |
| 430 | expect( |
| 431 | partitionStackPresentations( |
| 432 | [home, thread, settings, legal, secondModal], |
| 433 | (route) => route.name === "Settings", |
| 434 | ), |
| 435 | ).toEqual([[home, thread], [settings, legal], [secondModal]]); |
| 436 | }); |
| 437 | }); |