The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeModule from "node:module"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { restoreSettingsLinkState, settingsLinkAction } from "../../settings/settings-navigation"; |
| 5 | import { |
| 6 | workspaceSelectionsIn, |
| 7 | workspaceTabColumns, |
| 8 | workspaceTabNavigationState, |
| 9 | workspaceTabPage, |
| 10 | } from "./workspace-tab-navigation"; |
| 11 | |
| 12 | const home = { key: "home", name: "Home" }; |
| 13 | const thread = { |
| 14 | key: "thread", |
| 15 | name: "Thread", |
| 16 | params: { environmentId: "env", threadId: "thread-a" }, |
| 17 | }; |
| 18 | const pullRequest = { |
| 19 | key: "pr", |
| 20 | name: "PullRequest", |
| 21 | params: { environmentId: "env", projectId: "project", repository: "o/r", number: 7 }, |
| 22 | }; |
| 23 | const settings = { key: "settings", name: "Settings" }; |
| 24 | const requireFromNative = NodeModule.createRequire( |
| 25 | NodeModule.createRequire(import.meta.url).resolve("@react-navigation/native/package.json"), |
| 26 | ); |
| 27 | const { getStateFromPath } = requireFromNative("@react-navigation/core") as { |
| 28 | readonly getStateFromPath: typeof import("@react-navigation/native").getStateFromPath; |
| 29 | }; |
| 30 | const { StackRouter } = NodeModule.createRequire( |
| 31 | requireFromNative.resolve("@react-navigation/core/package.json"), |
| 32 | )("@react-navigation/routers") as { |
| 33 | readonly StackRouter: typeof import("@react-navigation/native").StackRouter; |
| 34 | }; |
| 35 | const available = { thread: () => true, pullRequest: () => true, issue: () => true }; |
| 36 | const names = (state: { routes: readonly { name: string }[] }) => |
| 37 | state.routes.map((route) => route.name); |
| 38 | |
| 39 | describe("workspace tab selections", () => { |
| 40 | it("remembers the chat, pull request and Settings page each tab showed", () => { |
| 41 | const afterThread = workspaceSelectionsIn([home, thread], {}); |
| 42 | const afterPullRequest = workspaceSelectionsIn([home, pullRequest], afterThread); |
| 43 | expect(afterPullRequest).toMatchObject({ |
| 44 | threads: thread.params, |
| 45 | pullRequests: pullRequest.params, |
| 46 | }); |
| 47 | const afterSettings = workspaceSelectionsIn( |
| 48 | [home, settings, { key: "appearance", name: "SettingsAppearance" }], |
| 49 | afterPullRequest, |
| 50 | ); |
| 51 | expect(afterSettings.settings).toBe("SettingsAppearance"); |
| 52 | expect(workspaceSelectionsIn([home, thread], afterSettings)).toBe(afterSettings); |
| 53 | }); |
| 54 | |
| 55 | it("doesn't remember pages a Settings row doesn't open", () => { |
| 56 | for (const name of ["SettingsEnvironmentNew", "SettingsProjectOverview", "SettingsAuth"]) { |
| 57 | expect(workspaceSelectionsIn([home, settings, { key: name, name }], {}).settings).toBe( |
| 58 | undefined, |
| 59 | ); |
| 60 | } |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | describe("opening a workspace tab", () => { |
| 65 | const columns = { columns: true, check: available }; |
| 66 | |
| 67 | it("opens Android Settings beside its default page only in a wide window", () => { |
| 68 | const open = (wide: boolean) => |
| 69 | workspaceTabNavigationState( |
| 70 | { routes: [home, thread] }, |
| 71 | "settings", |
| 72 | {}, |
| 73 | { |
| 74 | columns: workspaceTabColumns("android", "settings", wide), |
| 75 | check: available, |
| 76 | }, |
| 77 | ); |
| 78 | expect(names(open(false))).toEqual(["Home", "Settings"]); |
| 79 | expect(names(open(true))).toEqual(["Home", "Settings", "SettingsEnvironments"]); |
| 80 | }); |
| 81 | |
| 82 | it("keeps Android list tabs and iOS remembered-detail navigation unchanged", () => { |
| 83 | const open = (platform: string) => |
| 84 | workspaceTabNavigationState( |
| 85 | { routes: [home, thread] }, |
| 86 | "threads", |
| 87 | { threads: thread.params }, |
| 88 | { |
| 89 | columns: workspaceTabColumns(platform, "threads", true), |
| 90 | check: available, |
| 91 | }, |
| 92 | ); |
| 93 | expect(names(open("android"))).toEqual(["Home"]); |
| 94 | expect(names(open("ios"))).toEqual(["Home", "Thread"]); |
| 95 | }); |
| 96 | |
| 97 | it("opens the remembered page beside the list with columns, and the list without", () => { |
| 98 | const state = { routes: [home, thread] }; |
| 99 | const selections = { threads: thread.params, pullRequests: pullRequest.params }; |
| 100 | expect(names(workspaceTabNavigationState(state, "pullRequests", selections, columns))).toEqual([ |
| 101 | "Home", |
| 102 | "PullRequest", |
| 103 | ]); |
| 104 | expect( |
| 105 | names( |
| 106 | workspaceTabNavigationState(state, "pullRequests", selections, { |
| 107 | columns: false, |
| 108 | check: available, |
| 109 | }), |
| 110 | ), |
| 111 | ).toEqual(["Home"]); |
| 112 | expect(names(workspaceTabNavigationState(state, "settings", {}, columns))).toEqual([ |
| 113 | "Home", |
| 114 | "Settings", |
| 115 | "SettingsEnvironments", |
| 116 | ]); |
| 117 | expect( |
| 118 | names( |
| 119 | workspaceTabNavigationState(state, "settings", { settings: "SettingsMicrophone" }, columns), |
| 120 | ), |
| 121 | ).toEqual(["Home", "Settings", "SettingsVoice"]); |
| 122 | // A hidden tab renders its page under the key that opening the tab later uses. |
| 123 | for (const tab of ["pullRequests", "settings"] as const) { |
| 124 | expect(workspaceTabNavigationState(state, tab, selections, columns).routes.at(-1)?.key).toBe( |
| 125 | workspaceTabPage(state, tab, selections, columns)?.key, |
| 126 | ); |
| 127 | } |
| 128 | }); |
| 129 | |
| 130 | it("keeps the route that is already open, so re-selecting a tab doesn't rebuild it", () => { |
| 131 | const next = workspaceTabNavigationState( |
| 132 | { routes: [home, thread] }, |
| 133 | "threads", |
| 134 | { threads: thread.params }, |
| 135 | columns, |
| 136 | ); |
| 137 | expect(next.routes.map((route) => route.key)).toEqual(["home", "thread"]); |
| 138 | }); |
| 139 | |
| 140 | it("reopens a tab's page under the key it had, so its kept screen is reused", () => { |
| 141 | const columnsAvailable = { columns: true, check: available }; |
| 142 | let selections = workspaceSelectionsIn([home, thread], {}); |
| 143 | const away = workspaceTabNavigationState( |
| 144 | { routes: [home, thread] }, |
| 145 | "pullRequests", |
| 146 | selections, |
| 147 | { |
| 148 | columns: false, |
| 149 | check: available, |
| 150 | }, |
| 151 | ); |
| 152 | selections = workspaceSelectionsIn(away.routes, selections); |
| 153 | const back = workspaceTabNavigationState(away, "threads", selections, columnsAvailable); |
| 154 | expect(back.routes.at(-1)).toMatchObject({ key: "thread", params: thread.params }); |
| 155 | }); |
| 156 | |
| 157 | it("reopens a draft the tab showed after its last chat, until another chat opens", () => { |
| 158 | const columns = { columns: true, check: available }; |
| 159 | const draft = { key: "draft", name: "Thread", params: { draft: { requestId: "r" } } }; |
| 160 | const withDraft = workspaceSelectionsIn( |
| 161 | [home, draft], |
| 162 | workspaceSelectionsIn([home, thread], {}), |
| 163 | ); |
| 164 | const back = workspaceTabNavigationState( |
| 165 | { routes: [home, settings] }, |
| 166 | "threads", |
| 167 | withDraft, |
| 168 | columns, |
| 169 | ); |
| 170 | expect(back.routes.at(-1)).toEqual(draft); |
| 171 | const afterChat = workspaceSelectionsIn([home, thread], withDraft); |
| 172 | const again = workspaceTabNavigationState( |
| 173 | { routes: [home, settings] }, |
| 174 | "threads", |
| 175 | afterChat, |
| 176 | columns, |
| 177 | ); |
| 178 | expect(again.routes.at(-1)).toMatchObject({ key: "thread", params: thread.params }); |
| 179 | }); |
| 180 | |
| 181 | it("falls back to the list for a chat that no longer exists, or an unknown Settings page", () => { |
| 182 | const missing = { thread: () => false, pullRequest: () => true, issue: () => true }; |
| 183 | expect( |
| 184 | names( |
| 185 | workspaceTabNavigationState( |
| 186 | { routes: [home] }, |
| 187 | "threads", |
| 188 | { threads: thread.params }, |
| 189 | { |
| 190 | columns: true, |
| 191 | check: missing, |
| 192 | }, |
| 193 | ), |
| 194 | ), |
| 195 | ).toEqual(["Home"]); |
| 196 | expect( |
| 197 | names( |
| 198 | workspaceTabNavigationState( |
| 199 | { routes: [home] }, |
| 200 | "settings", |
| 201 | { settings: "SettingsGone" }, |
| 202 | columns, |
| 203 | ), |
| 204 | ), |
| 205 | ).toEqual(["Home", "Settings", "SettingsEnvironments"]); |
| 206 | }); |
| 207 | |
| 208 | it("keeps an open sheet on top", () => { |
| 209 | const sheet = { key: "sheet", name: "ConnectOnboarding" }; |
| 210 | const next = workspaceTabNavigationState( |
| 211 | { routes: [home] }, |
| 212 | "threads", |
| 213 | { threads: thread.params }, |
| 214 | { |
| 215 | ...columns, |
| 216 | overlays: [sheet], |
| 217 | }, |
| 218 | ); |
| 219 | expect(names(next)).toEqual(["Home", "Thread", "ConnectOnboarding"]); |
| 220 | expect(next.index).toBe(2); |
| 221 | }); |
| 222 | }); |
| 223 | |
| 224 | describe("Settings links", () => { |
| 225 | it("puts the Settings list under a linked page, and a page beside a bare link with columns", () => { |
| 226 | const linked = restoreSettingsLinkState( |
| 227 | { routes: [{ name: "Home" }, { name: "SettingsKeyboard" }] }, |
| 228 | undefined, |
| 229 | ); |
| 230 | expect(names(linked!)).toEqual(["Home", "Settings", "SettingsKeyboard"]); |
| 231 | const bare = { routes: [{ name: "Home" }, { name: "Settings" }] }; |
| 232 | expect(names(restoreSettingsLinkState(bare, "SettingsAppearance")!)).toEqual([ |
| 233 | "Home", |
| 234 | "Settings", |
| 235 | "SettingsAppearance", |
| 236 | ]); |
| 237 | expect(restoreSettingsLinkState(bare, undefined)).toBe(bare); |
| 238 | }); |
| 239 | |
| 240 | it("opens a link sent to the running app on the Settings tab, as its button does", () => { |
| 241 | const screens = { |
| 242 | Home: "", |
| 243 | Settings: "settings", |
| 244 | SettingsAbout: "settings/about", |
| 245 | SettingsAppearance: "settings/appearance", |
| 246 | }; |
| 247 | const router = StackRouter({}); |
| 248 | const options = { routeNames: Object.keys(screens), routeParamList: {}, routeGetIdList: {} }; |
| 249 | const open = router.getRehydratedState( |
| 250 | { index: 2, routes: [{ name: "Home" }, { name: "Settings" }, { name: "SettingsAbout" }] }, |
| 251 | options, |
| 252 | ); |
| 253 | const follow = (path: string) => { |
| 254 | const link = getStateFromPath(path, { initialRouteName: "Home", screens })!; |
| 255 | const action = settingsLinkAction(link, open)!; |
| 256 | return router.getStateForAction(open, action, options)!; |
| 257 | }; |
| 258 | |
| 259 | expect(names(follow("settings"))).toEqual(["Home", "Settings"]); |
| 260 | const appearance = follow("settings/appearance"); |
| 261 | expect(names(appearance)).toEqual(["Home", "Settings", "SettingsAppearance"]); |
| 262 | expect(appearance.routes[1]!.key).toBe(open.routes[1]!.key); |
| 263 | }); |
| 264 | |
| 265 | it("opens the sign-in link as a modal over the workspace", () => { |
| 266 | const state = { routes: [{ name: "Home" }, { name: "SettingsAuth" }] }; |
| 267 | expect(restoreSettingsLinkState(state, "SettingsAppearance")).toBe(state); |
| 268 | }); |
| 269 | }); |