The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection"; |
| 2 | import type { EnvironmentProject } from "@t3tools/client-runtime/state/models"; |
| 3 | import { EnvironmentId } from "@t3tools/contracts"; |
| 4 | import { act, useEffect, useState } from "react"; |
| 5 | import { createRoot } from "react-dom/client"; |
| 6 | import { expect, it, vi } from "vite-plus/test"; |
| 7 | |
| 8 | import type { WorkspaceEnvironment } from "../../state/workspaceModel"; |
| 9 | import type { PullRequestEnvironmentTarget } from "./pullRequestEnvironmentFallback"; |
| 10 | |
| 11 | const mocks = vi.hoisted(() => ({ |
| 12 | environments: [] as ReadonlyArray<WorkspaceEnvironment>, |
| 13 | isReady: true, |
| 14 | projects: [] as ReadonlyArray<EnvironmentProject>, |
| 15 | navigation: { setParams: vi.fn<(params: Partial<PullRequestEnvironmentTarget>) => void>() }, |
| 16 | })); |
| 17 | |
| 18 | vi.mock("@react-navigation/native", async () => { |
| 19 | const { useEffect } = await import("react"); |
| 20 | return { |
| 21 | useNavigation: () => mocks.navigation, |
| 22 | // The screen under test is focused, so its focus effect runs like an effect. |
| 23 | useFocusEffect: (effect: () => void) => useEffect(effect, [effect]), |
| 24 | }; |
| 25 | }); |
| 26 | vi.mock("../../state/entities", () => ({ useProjects: () => mocks.projects })); |
| 27 | vi.mock("../../state/workspace", () => ({ |
| 28 | useWorkspaceEnvironments: () => mocks.environments, |
| 29 | useConnectionsReady: () => mocks.isReady, |
| 30 | })); |
| 31 | |
| 32 | import { usePullRequestEnvironmentFallback } from "./usePullRequestEnvironmentFallback"; |
| 33 | |
| 34 | function environment( |
| 35 | environmentId: string, |
| 36 | connectionState: EnvironmentConnectionPhase, |
| 37 | isEnabled = true, |
| 38 | ): WorkspaceEnvironment { |
| 39 | return { |
| 40 | environmentId: EnvironmentId.make(environmentId), |
| 41 | environmentLabel: environmentId, |
| 42 | displayUrl: "", |
| 43 | isRelayManaged: false, |
| 44 | isEnabled, |
| 45 | connectionState, |
| 46 | connectionError: null, |
| 47 | connectionErrorTraceId: null, |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | function setup() { |
| 52 | (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 53 | mocks.isReady = true; |
| 54 | mocks.environments = [environment("requested", "connecting"), environment("copy", "connected")]; |
| 55 | mocks.projects = [ |
| 56 | { environmentId: "requested", id: "original" }, |
| 57 | { environmentId: "copy", id: "copy-project" }, |
| 58 | ].map( |
| 59 | (project) => |
| 60 | ({ |
| 61 | ...project, |
| 62 | repositoryIdentity: { canonicalKey: "github.com/owner/repo" }, |
| 63 | }) as unknown as EnvironmentProject, |
| 64 | ); |
| 65 | const container = document.createElement("div"); |
| 66 | const root = createRoot(container); |
| 67 | mocks.navigation.setParams = vi.fn(); |
| 68 | function Probe() { |
| 69 | const [params, updateParams] = useState({ |
| 70 | environmentId: "requested", |
| 71 | projectId: "original", |
| 72 | repository: "owner/repo", |
| 73 | host: "github.com", |
| 74 | }); |
| 75 | useEffect(() => { |
| 76 | mocks.navigation.setParams.mockImplementation((next) => |
| 77 | updateParams((previous) => ({ ...previous, ...next })), |
| 78 | ); |
| 79 | }, []); |
| 80 | usePullRequestEnvironmentFallback(params); |
| 81 | return <span>{`${params.environmentId}/${params.projectId}`}</span>; |
| 82 | } |
| 83 | return { |
| 84 | render() { |
| 85 | act(() => root.render(<Probe />)); |
| 86 | }, |
| 87 | get target() { |
| 88 | return container.textContent; |
| 89 | }, |
| 90 | [Symbol.dispose]() { |
| 91 | act(() => root.unmount()); |
| 92 | }, |
| 93 | }; |
| 94 | } |
| 95 | |
| 96 | it("keeps the requested target while the catalog initializes or its enabled connection is pending", () => { |
| 97 | using screen = setup(); |
| 98 | mocks.isReady = false; |
| 99 | mocks.environments = [environment("copy", "connected")]; |
| 100 | screen.render(); |
| 101 | expect(screen.target).toBe("requested/original"); |
| 102 | expect(mocks.navigation.setParams).not.toHaveBeenCalled(); |
| 103 | |
| 104 | mocks.isReady = true; |
| 105 | for (const phase of ["available", "connecting", "reconnecting", "connected"] as const) { |
| 106 | mocks.environments = [environment("requested", phase), environment("copy", "connected")]; |
| 107 | screen.render(); |
| 108 | expect(screen.target).toBe("requested/original"); |
| 109 | expect(mocks.navigation.setParams).not.toHaveBeenCalled(); |
| 110 | } |
| 111 | }); |
| 112 | |
| 113 | it("falls back to a connected repository copy when the requested environment cannot connect", () => { |
| 114 | for (const state of ["missing", "disabled", "offline", "error", "unsupported"] as const) { |
| 115 | using screen = setup(); |
| 116 | mocks.environments = [ |
| 117 | ...(state === "missing" |
| 118 | ? [] |
| 119 | : [ |
| 120 | environment( |
| 121 | "requested", |
| 122 | state === "disabled" ? "available" : state, |
| 123 | state !== "disabled", |
| 124 | ), |
| 125 | ]), |
| 126 | environment("copy", "connected"), |
| 127 | ]; |
| 128 | screen.render(); |
| 129 | expect(screen.target).toBe("copy/copy-project"); |
| 130 | expect(mocks.navigation.setParams).toHaveBeenCalledExactlyOnceWith({ |
| 131 | environmentId: "copy", |
| 132 | projectId: "copy-project", |
| 133 | }); |
| 134 | } |
| 135 | }); |
| 136 | |
| 137 | it("does not replace a connecting target with a connected repository copy before its project hydrates", () => { |
| 138 | using screen = setup(); |
| 139 | const projects = mocks.projects; |
| 140 | mocks.projects = projects.filter((project) => project.environmentId !== "requested"); |
| 141 | screen.render(); |
| 142 | expect(screen.target).toBe("requested/original"); |
| 143 | expect(mocks.navigation.setParams).not.toHaveBeenCalled(); |
| 144 | |
| 145 | mocks.projects = projects; |
| 146 | mocks.environments = [environment("requested", "connected"), environment("copy", "connected")]; |
| 147 | screen.render(); |
| 148 | expect(screen.target).toBe("requested/original"); |
| 149 | expect(mocks.navigation.setParams).not.toHaveBeenCalled(); |
| 150 | }); |