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 { describe, expect, it } from "vite-plus/test"; |
| 3 | import { |
| 4 | selectWorkspaceDetail, |
| 5 | setNativelyPoppingWorkspaceRoute, |
| 6 | workspaceDetailLinkAction, |
| 7 | workspaceDetailRouter, |
| 8 | type WorkspaceDetail, |
| 9 | } from "./workspace-detail-selection"; |
| 10 | |
| 11 | const require = NodeModule.createRequire(import.meta.url); |
| 12 | const requireFromNative = NodeModule.createRequire( |
| 13 | require.resolve("@react-navigation/native/package.json"), |
| 14 | ); |
| 15 | const requireFromCore = NodeModule.createRequire( |
| 16 | requireFromNative.resolve("@react-navigation/core/package.json"), |
| 17 | ); |
| 18 | const { StackActions, StackRouter } = requireFromCore("@react-navigation/routers") as { |
| 19 | readonly StackActions: typeof import("@react-navigation/native").StackActions; |
| 20 | readonly StackRouter: typeof import("@react-navigation/native").StackRouter; |
| 21 | }; |
| 22 | |
| 23 | const base = StackRouter({}); |
| 24 | const router = { ...base, ...workspaceDetailRouter(base) }; |
| 25 | const options = { |
| 26 | routeNames: [ |
| 27 | "Home", |
| 28 | "Thread", |
| 29 | "ThreadFiles", |
| 30 | "PullRequest", |
| 31 | "PullRequestReviewers", |
| 32 | "Settings", |
| 33 | "SettingsAppearance", |
| 34 | "GitOverview", |
| 35 | ], |
| 36 | routeParamList: {}, |
| 37 | routeGetIdList: {}, |
| 38 | }; |
| 39 | type State = ReturnType<typeof base.getInitialState>; |
| 40 | type RouterAction = Parameters<typeof base.getStateForAction>[1]; |
| 41 | |
| 42 | const threadA = { name: "Thread", params: { environmentId: "env", threadId: "a" } } as const; |
| 43 | const threadB = { name: "Thread", params: { environmentId: "env", threadId: "b" } } as const; |
| 44 | const pullRequest = { |
| 45 | name: "PullRequest", |
| 46 | params: { environmentId: "env", projectId: "p", repository: "o/r", number: 1, host: "h" }, |
| 47 | } as const; |
| 48 | |
| 49 | function apply(state: State, action: Parameters<typeof base.getStateForAction>[1]) { |
| 50 | const next = router.getStateForAction(state, action, options); |
| 51 | if (!next) throw new Error(`Router rejected ${action.type}`); |
| 52 | // React Navigation rehydrates every state a router returns, as a stale RESET needs. |
| 53 | return base.getRehydratedState(next as State, options); |
| 54 | } |
| 55 | |
| 56 | function select( |
| 57 | state: State, |
| 58 | detail: WorkspaceDetail, |
| 59 | sheet?: Parameters<typeof selectWorkspaceDetail>[1], |
| 60 | ) { |
| 61 | return apply(state, selectWorkspaceDetail(detail, sheet) as unknown as RouterAction); |
| 62 | } |
| 63 | |
| 64 | function push(state: State, name: string, params?: object) { |
| 65 | return apply(state, StackActions.push(name, params)); |
| 66 | } |
| 67 | |
| 68 | const home = () => base.getInitialState(options); |
| 69 | const page = (state: State) => ({ |
| 70 | names: state.routes.map((route) => route.name), |
| 71 | index: state.index, |
| 72 | }); |
| 73 | |
| 74 | describe("selectWorkspaceDetail", () => { |
| 75 | it("swaps the open thread in place instead of pushing another", () => { |
| 76 | const first = select(home(), threadA); |
| 77 | const second = select(first, threadB); |
| 78 | expect(page(second)).toEqual({ names: ["Home", "Thread"], index: 1 }); |
| 79 | expect(second.routes[1]?.key).toBe(first.routes[1]?.key); |
| 80 | expect(second.routes[1]?.params).toEqual(threadB.params); |
| 81 | }); |
| 82 | |
| 83 | it.each([ |
| 84 | ["a page drilled into from the thread", (state: State) => push(state, "ThreadFiles")], |
| 85 | ["a linked thread", (state: State) => push(state, "Thread", threadB.params)], |
| 86 | ["a pull request opened from the thread", (state: State) => push(state, "PullRequest")], |
| 87 | ["a sheet", (state: State) => push(state, "GitOverview")], |
| 88 | ])("leaves only the selection when %s is on top", (_, drillIn) => { |
| 89 | const thread = select(home(), threadA); |
| 90 | const selected = select(drillIn(thread), threadA); |
| 91 | expect(page(selected)).toEqual({ names: ["Home", "Thread"], index: 1 }); |
| 92 | expect(selected.routes[1]?.key).toBe(thread.routes[1]?.key); |
| 93 | }); |
| 94 | |
| 95 | it("replaces another tab's page and Settings with the selection", () => { |
| 96 | const settings = push(push(select(home(), threadA), "Settings"), "SettingsAppearance"); |
| 97 | const selected = select(settings, pullRequest); |
| 98 | expect(page(selected)).toEqual({ names: ["Home", "PullRequest"], index: 1 }); |
| 99 | expect(selected.routes[0]?.key).toBe(settings.routes[0]?.key); |
| 100 | }); |
| 101 | |
| 102 | it("puts Home under a cold-linked page and a sheet over the selection", () => { |
| 103 | const cold = apply(home(), { type: "RESET", payload: { index: 0, routes: [pullRequest] } }); |
| 104 | const selected = select(cold, pullRequest, { |
| 105 | name: "PullRequestReviewers", |
| 106 | params: pullRequest.params, |
| 107 | }); |
| 108 | expect(page(selected)).toEqual({ |
| 109 | names: ["Home", "PullRequest", "PullRequestReviewers"], |
| 110 | index: 2, |
| 111 | }); |
| 112 | }); |
| 113 | |
| 114 | it("never reuses the page native Back is popping", () => { |
| 115 | const thread = select(home(), threadA); |
| 116 | setNativelyPoppingWorkspaceRoute(thread.routes[1]!.key); |
| 117 | try { |
| 118 | const selected = select(thread, threadA); |
| 119 | expect(page(selected)).toEqual({ names: ["Home", "Thread"], index: 1 }); |
| 120 | expect(selected.routes[1]?.key).not.toBe(thread.routes[1]?.key); |
| 121 | } finally { |
| 122 | setNativelyPoppingWorkspaceRoute(null); |
| 123 | } |
| 124 | }); |
| 125 | }); |
| 126 | |
| 127 | describe("workspaceDetailLinkAction", () => { |
| 128 | it("selects a linked thread or pull request, and leaves other links alone", () => { |
| 129 | expect(workspaceDetailLinkAction({ routes: [{ name: "Home" }, threadA] })).toEqual( |
| 130 | selectWorkspaceDetail(threadA), |
| 131 | ); |
| 132 | expect(workspaceDetailLinkAction({ routes: [pullRequest] })).toEqual( |
| 133 | selectWorkspaceDetail(pullRequest), |
| 134 | ); |
| 135 | expect( |
| 136 | workspaceDetailLinkAction({ |
| 137 | routes: [{ name: "Home" }, { name: "Settings" }, { name: "SettingsAppearance" }], |
| 138 | }), |
| 139 | ).toBeUndefined(); |
| 140 | }); |
| 141 | }); |