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, 04:45 UTC
| 1 | import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts"; |
| 2 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 3 | import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; |
| 4 | import { AsyncResult, type Atom } from "effect/reactivity"; |
| 5 | import { expect, it, vi } from "vite-plus/test"; |
| 6 | |
| 7 | import { act, render, stubGlobal } from "../../../test/render"; |
| 8 | |
| 9 | const state = vi.hoisted(() => ({ |
| 10 | projects: [] as EnvironmentProject[], |
| 11 | routeParams: {} as Record<string, string>, |
| 12 | preview: vi.fn(), |
| 13 | openLink: vi.fn(async () => undefined), |
| 14 | navigate: vi.fn(async (): Promise<void> => undefined), |
| 15 | openScratchProject: vi.fn(async () => null), |
| 16 | confirm: vi.fn(async (_message: string) => true), |
| 17 | connectPairing: vi.fn(async (_input: { pairingUrl: string }) => ({ _tag: "Success" })), |
| 18 | localEnvironmentDisabled: false, |
| 19 | })); |
| 20 | |
| 21 | // One connected primary environment that can read pull requests. Its project list and |
| 22 | // discovery flag start empty so a test can deliver links before they arrive. |
| 23 | vi.mock(import("@tanstack/react-router"), async (original) => ({ |
| 24 | ...(await original()), |
| 25 | useNavigate: (() => state.navigate) as never, |
| 26 | useRouter: (() => ({ |
| 27 | navigate: state.navigate, |
| 28 | state: { matches: [{ params: state.routeParams }] }, |
| 29 | })) as never, |
| 30 | })); |
| 31 | vi.mock(import("../../state/environments"), async (original) => ({ |
| 32 | ...(await original()), |
| 33 | usePrimaryEnvironmentId: () => "local" as EnvironmentId, |
| 34 | usePrimaryEnvironment: (() => |
| 35 | state.localEnvironmentDisabled |
| 36 | ? null |
| 37 | : { |
| 38 | environmentId: "local", |
| 39 | connection: { phase: "connected" }, |
| 40 | serverConfig: {}, |
| 41 | }) as never, |
| 42 | })); |
| 43 | vi.mock(import("../../localEnvironment"), () => ({ |
| 44 | isLocalEnvironmentDisabled: () => state.localEnvironmentDisabled, |
| 45 | })); |
| 46 | vi.mock(import("../../state/server"), async (original) => { |
| 47 | const { Atom } = await import("effect/reactivity"); |
| 48 | const capabilities = { pullRequests: true, threadPullRequests: true }; |
| 49 | return { |
| 50 | ...(await original()), |
| 51 | environmentServerConfigsAtom: Atom.make( |
| 52 | new Map([["local", { environment: { capabilities } }]]), |
| 53 | ) as never, |
| 54 | }; |
| 55 | }); |
| 56 | vi.mock(import("../../state/shell"), async (original) => { |
| 57 | const { Atom } = await import("effect/reactivity"); |
| 58 | return { |
| 59 | ...(await original()), |
| 60 | allReachableShellsLiveAtom: Atom.make(false).pipe(Atom.keepAlive), |
| 61 | }; |
| 62 | }); |
| 63 | vi.mock(import("../../state/entities"), async (original) => ({ |
| 64 | ...(await original()), |
| 65 | readProjects: () => state.projects, |
| 66 | readThreadShell: (ref: { threadId: string }) => |
| 67 | ref.threadId === "thread" ? ({ projectId: "effect", worktreePath: null } as never) : null, |
| 68 | readProject: () => state.projects[0] ?? null, |
| 69 | })); |
| 70 | vi.mock(import("../../hooks/useScratchProject"), async (original) => ({ |
| 71 | ...(await original()), |
| 72 | useScratchProject: (() => ({ |
| 73 | scratchEnvironmentId: () => "local", |
| 74 | openScratchProject: state.openScratchProject, |
| 75 | })) as never, |
| 76 | })); |
| 77 | |
| 78 | vi.mock("../../state/use-atom-query-runner", () => ({ |
| 79 | useAtomQueryRunner: () => state.preview, |
| 80 | })); |
| 81 | vi.mock("../../browser/useOpenLink", () => ({ useOpenLink: () => state.openLink })); |
| 82 | vi.mock(import("../../confirmDialog"), async (original) => ({ |
| 83 | ...(await original()), |
| 84 | requestConfirmDialog: state.confirm, |
| 85 | })); |
| 86 | vi.mock(import("../../connection/onboarding"), async (original) => ({ |
| 87 | ...(await original()), |
| 88 | connectPairing: "connectPairing" as never, |
| 89 | })); |
| 90 | vi.mock(import("../../state/use-atom-command"), async (original) => { |
| 91 | const actual = await original(); |
| 92 | return { |
| 93 | ...actual, |
| 94 | useAtomCommand: ((command: unknown, options: never) => |
| 95 | command === "connectPairing" |
| 96 | ? state.connectPairing |
| 97 | : actual.useAtomCommand(command as never, options)) as never, |
| 98 | }; |
| 99 | }); |
| 100 | |
| 101 | import { AppAtomRegistryProvider, appAtomRegistry } from "../../rpc/atomRegistry"; |
| 102 | import { allReachableShellsLiveAtom } from "../../state/shell"; |
| 103 | import { selectThreadDiffPanelSelection, useDiffPanelStore } from "../../diffPanelStore"; |
| 104 | import { |
| 105 | selectActiveRightPanel, |
| 106 | selectActiveRightPanelSurface, |
| 107 | useRightPanelStore, |
| 108 | } from "../../rightPanelStore"; |
| 109 | import { DesktopWebLinkCoordinator } from "./DesktopWebLinkCoordinator"; |
| 110 | |
| 111 | const discoverySettled = allReachableShellsLiveAtom as Atom.Writable<boolean>; |
| 112 | const environmentId = EnvironmentId.make("local"); |
| 113 | const projectId = ProjectId.make("effect"); |
| 114 | const project = { |
| 115 | id: projectId, |
| 116 | workspaceRoot: "/workspace/effect", |
| 117 | environmentId, |
| 118 | repositoryIdentity: { |
| 119 | provider: "github", |
| 120 | displayName: "Effect-TS/effect", |
| 121 | canonicalKey: "github.com/effect-ts/effect", |
| 122 | locator: { |
| 123 | source: "git-remote", |
| 124 | remoteName: "origin", |
| 125 | remoteUrl: "https://github.com/Effect-TS/effect.git", |
| 126 | }, |
| 127 | }, |
| 128 | } as EnvironmentProject; |
| 129 | const pullRequestUrl = "https://github.com/Effect-TS/effect/pull/9339"; |
| 130 | const pullRequestView = { |
| 131 | to: "/pull-requests", |
| 132 | search: { |
| 133 | involvement: "all", |
| 134 | state: "all", |
| 135 | repository: "effect-ts/effect", |
| 136 | number: 9339, |
| 137 | selectedHost: "github.com", |
| 138 | selectedProjectId: projectId, |
| 139 | selectedEnvironmentId: environmentId, |
| 140 | }, |
| 141 | }; |
| 142 | |
| 143 | const pairingUrl = "https://vps.example.com/pair#token=ABC123"; |
| 144 | const pairLink = `t3code://pair?pairingUrl=${encodeURIComponent(pairingUrl)}`; |
| 145 | |
| 146 | function setup() { |
| 147 | vi.clearAllMocks(); |
| 148 | state.confirm.mockResolvedValue(true); |
| 149 | state.localEnvironmentDisabled = false; |
| 150 | state.projects = []; |
| 151 | state.routeParams = {}; |
| 152 | state.preview |
| 153 | .mockReset() |
| 154 | .mockResolvedValue(AsyncResult.success({ sources: [{ kind: "commit", headRef: sha }] })); |
| 155 | useDiffPanelStore.setState({ byThreadKey: {}, branchBaseRefByThreadKey: {} }); |
| 156 | useRightPanelStore.setState({ byThreadKey: {}, userActionRevisionByThreadKey: {} }); |
| 157 | appAtomRegistry.set(discoverySettled, false); |
| 158 | } |
| 159 | |
| 160 | /** Mounts the coordinator behind a desktop bridge that hands over `urls` once the renderer is ready. */ |
| 161 | async function mount(...urls: string[]) { |
| 162 | let deliver: ((url: string) => void) | undefined; |
| 163 | await using stack = new AsyncDisposableStack(); |
| 164 | stack.use( |
| 165 | stubGlobal("desktopBridge", { |
| 166 | getClientSettings: async () => null, |
| 167 | webLinks: { |
| 168 | onOpen: (listener: (url: string) => void) => { |
| 169 | deliver = listener; |
| 170 | return () => undefined; |
| 171 | }, |
| 172 | setReady: async (ready: boolean) => { |
| 173 | if (ready) for (const url of urls.splice(0)) deliver?.(url); |
| 174 | }, |
| 175 | }, |
| 176 | }), |
| 177 | ); |
| 178 | stack.use( |
| 179 | await render( |
| 180 | <AppAtomRegistryProvider> |
| 181 | <DesktopWebLinkCoordinator /> |
| 182 | </AppAtomRegistryProvider>, |
| 183 | ), |
| 184 | ); |
| 185 | return stack.move(); |
| 186 | } |
| 187 | |
| 188 | it("holds a launch PR link until project discovery settles, then opens the PR view", async () => { |
| 189 | setup(); |
| 190 | await using _coordinator = await mount(pullRequestUrl); |
| 191 | expect(state.navigate).not.toHaveBeenCalled(); |
| 192 | expect(state.openScratchProject).not.toHaveBeenCalled(); |
| 193 | |
| 194 | state.projects = [project]; |
| 195 | const navigated = new Promise<void>((resolve) => { |
| 196 | state.navigate.mockImplementationOnce(async () => resolve()); |
| 197 | }); |
| 198 | await act(async () => { |
| 199 | appAtomRegistry.set(discoverySettled, true); |
| 200 | await navigated; |
| 201 | }); |
| 202 | expect(state.navigate).toHaveBeenCalledExactlyOnceWith(pullRequestView); |
| 203 | expect(state.openScratchProject).not.toHaveBeenCalled(); |
| 204 | }); |
| 205 | |
| 206 | const sha = "a".repeat(40); |
| 207 | const commitUrl = `https://github.com/Effect-TS/effect/pull/9339/commits/${sha}`; |
| 208 | const threadRef = scopeThreadRef(environmentId, ThreadId.make("thread")); |
| 209 | |
| 210 | it.each([ |
| 211 | ["github.com", "pull/9339/commits"], |
| 212 | ["code.example.com:8443", "commit"], |
| 213 | ])("opens a %s/%s commit in the active thread's Diff pane after discovery", async (host, path) => { |
| 214 | setup(); |
| 215 | state.routeParams = { environmentId: "local", threadId: "thread" }; |
| 216 | await using _coordinator = await mount(`https://${host}/Effect-TS/effect/${path}/${sha}`); |
| 217 | expect(state.preview).not.toHaveBeenCalled(); |
| 218 | |
| 219 | state.projects = [ |
| 220 | { |
| 221 | ...project, |
| 222 | repositoryIdentity: { |
| 223 | ...project.repositoryIdentity, |
| 224 | canonicalKey: `${host}/effect-ts/effect`, |
| 225 | locator: { |
| 226 | ...project.repositoryIdentity?.locator, |
| 227 | remoteUrl: `https://${host}/Effect-TS/effect.git`, |
| 228 | }, |
| 229 | }, |
| 230 | } as EnvironmentProject, |
| 231 | ]; |
| 232 | await act(async () => appAtomRegistry.set(discoverySettled, true)); |
| 233 | await vi.waitFor(() => { |
| 234 | expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, threadRef)).toBe( |
| 235 | "diff", |
| 236 | ); |
| 237 | }); |
| 238 | expect( |
| 239 | selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, threadRef), |
| 240 | ).toMatchObject({ kind: "commit", sha }); |
| 241 | expect(state.preview).toHaveBeenCalledExactlyOnceWith({ |
| 242 | environmentId, |
| 243 | input: expect.objectContaining({ cwd: "/workspace/effect", commitSha: sha }), |
| 244 | }); |
| 245 | expect(state.navigate).not.toHaveBeenCalled(); |
| 246 | expect(state.openLink).not.toHaveBeenCalled(); |
| 247 | }); |
| 248 | |
| 249 | it("opens a PR commit the workspace lacks in the thread's PR panel", async () => { |
| 250 | setup(); |
| 251 | state.routeParams = { environmentId: "local", threadId: "thread" }; |
| 252 | state.projects = [project]; |
| 253 | state.preview.mockResolvedValueOnce(AsyncResult.success({ sources: [] })); |
| 254 | appAtomRegistry.set(discoverySettled, true); |
| 255 | await using _coordinator = await mount(commitUrl); |
| 256 | await vi.waitFor(() => |
| 257 | expect( |
| 258 | selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, threadRef), |
| 259 | ).toMatchObject({ kind: "pull-request", repository: "effect-ts/effect", number: 9339 }), |
| 260 | ); |
| 261 | expect(useDiffPanelStore.getState().byThreadKey).toEqual({}); |
| 262 | expect(state.openLink).not.toHaveBeenCalled(); |
| 263 | }); |
| 264 | |
| 265 | it("opens a plain commit the workspace lacks like any other delivered link", async () => { |
| 266 | setup(); |
| 267 | state.routeParams = { environmentId: "local", threadId: "thread" }; |
| 268 | state.projects = [project]; |
| 269 | state.preview.mockResolvedValueOnce(AsyncResult.success({ sources: [] })); |
| 270 | appAtomRegistry.set(discoverySettled, true); |
| 271 | const plainCommitUrl = `https://github.com/Effect-TS/effect/commit/${sha}`; |
| 272 | await using _coordinator = await mount(plainCommitUrl); |
| 273 | await vi.waitFor(() => expect(state.openScratchProject).toHaveBeenCalledOnce()); |
| 274 | // Handing it to the OS browser would loop when T3 Code is the default browser. |
| 275 | expect(state.openLink).not.toHaveBeenCalled(); |
| 276 | expect(useDiffPanelStore.getState().byThreadKey).toEqual({}); |
| 277 | expect(useRightPanelStore.getState().byThreadKey).toEqual({}); |
| 278 | }); |
| 279 | |
| 280 | it("adds the backend from a pairing link after the user confirms its host", async () => { |
| 281 | setup(); |
| 282 | await using _coordinator = await mount(pairLink); |
| 283 | await vi.waitFor(() => |
| 284 | expect(state.connectPairing).toHaveBeenCalledExactlyOnceWith({ pairingUrl }), |
| 285 | ); |
| 286 | expect(state.confirm).toHaveBeenCalledExactlyOnceWith( |
| 287 | "Add the backend at https://vps.example.com?", |
| 288 | ); |
| 289 | expect(state.navigate).not.toHaveBeenCalled(); |
| 290 | }); |
| 291 | |
| 292 | it("does not spend the pairing token when the user declines", async () => { |
| 293 | setup(); |
| 294 | const otherPairingUrl = "https://other.example.com/pair#token=XYZ789"; |
| 295 | state.confirm.mockResolvedValueOnce(false); |
| 296 | await using _coordinator = await mount( |
| 297 | pairLink, |
| 298 | `t3code://pair?pairingUrl=${encodeURIComponent(otherPairingUrl)}`, |
| 299 | ); |
| 300 | await vi.waitFor(() => expect(state.connectPairing).toHaveBeenCalled()); |
| 301 | expect(state.confirm).toHaveBeenCalledTimes(2); |
| 302 | expect(state.connectPairing).toHaveBeenCalledExactlyOnceWith({ pairingUrl: otherPairingUrl }); |
| 303 | }); |
| 304 | |
| 305 | it("skips a repeat of a pairing link that already paired", async () => { |
| 306 | setup(); |
| 307 | const otherPairingUrl = "https://other.example.com/pair#token=XYZ789"; |
| 308 | await using _coordinator = await mount( |
| 309 | pairLink, |
| 310 | pairLink, |
| 311 | `t3code://pair?pairingUrl=${encodeURIComponent(otherPairingUrl)}`, |
| 312 | ); |
| 313 | await vi.waitFor(() => |
| 314 | expect(state.connectPairing).toHaveBeenLastCalledWith({ pairingUrl: otherPairingUrl }), |
| 315 | ); |
| 316 | expect(state.confirm).toHaveBeenCalledTimes(2); |
| 317 | expect(state.connectPairing.mock.calls).toEqual([ |
| 318 | [{ pairingUrl }], |
| 319 | [{ pairingUrl: otherPairingUrl }], |
| 320 | ]); |
| 321 | }); |
| 322 | |
| 323 | it("pairs with Local environment disabled, where there is no primary environment to wait for", async () => { |
| 324 | setup(); |
| 325 | state.localEnvironmentDisabled = true; |
| 326 | await using _coordinator = await mount(pairLink); |
| 327 | await vi.waitFor(() => |
| 328 | expect(state.connectPairing).toHaveBeenCalledExactlyOnceWith({ pairingUrl }), |
| 329 | ); |
| 330 | }); |
| 331 | |
| 332 | it("holds a launch PR link with Local environment disabled until discovery settles", async () => { |
| 333 | setup(); |
| 334 | state.localEnvironmentDisabled = true; |
| 335 | await using _coordinator = await mount(pullRequestUrl); |
| 336 | expect(state.navigate).not.toHaveBeenCalled(); |
| 337 | expect(state.openScratchProject).not.toHaveBeenCalled(); |
| 338 | |
| 339 | state.projects = [project]; |
| 340 | const navigated = new Promise<void>((resolve) => { |
| 341 | state.navigate.mockImplementationOnce(async () => resolve()); |
| 342 | }); |
| 343 | await act(async () => { |
| 344 | appAtomRegistry.set(discoverySettled, true); |
| 345 | await navigated; |
| 346 | }); |
| 347 | expect(state.navigate).toHaveBeenCalledExactlyOnceWith(pullRequestView); |
| 348 | expect(state.openScratchProject).not.toHaveBeenCalled(); |
| 349 | }); |