The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ReactNode } from "react"; |
| 2 | import { act } from "react"; |
| 3 | import { createRoot } from "react-dom/client"; |
| 4 | import { expect, it, vi } from "vite-plus/test"; |
| 5 | |
| 6 | const { Children, harness } = vi.hoisted(() => ({ |
| 7 | Children: ({ children }: { children?: ReactNode }) => <>{children}</>, |
| 8 | harness: { |
| 9 | // The app-wide error slot other features (composer sends, branch refreshes) write to. |
| 10 | pendingConnectionError: null as string | null, |
| 11 | connectPress: vi.fn(), |
| 12 | }, |
| 13 | })); |
| 14 | |
| 15 | vi.mock("react-native", () => ({ |
| 16 | View: Children, |
| 17 | Pressable: Children, |
| 18 | Linking: { openSettings: vi.fn() }, |
| 19 | StyleSheet: { absoluteFill: {} }, |
| 20 | })); |
| 21 | vi.mock("react-native-svg", () => ({ |
| 22 | default: Children, |
| 23 | Defs: Children, |
| 24 | RadialGradient: Children, |
| 25 | Rect: () => null, |
| 26 | Stop: () => null, |
| 27 | })); |
| 28 | vi.mock("react-native-safe-area-context", () => ({ useSafeAreaInsets: () => ({ bottom: 0 }) })); |
| 29 | vi.mock("@react-navigation/native", () => ({ |
| 30 | StackActions: { replace: vi.fn() }, |
| 31 | useIsFocused: () => true, |
| 32 | useNavigation: () => ({ canGoBack: () => true, goBack: vi.fn(), dispatch: vi.fn() }), |
| 33 | useRoute: () => ({ name: "ConnectionsNew" }), |
| 34 | })); |
| 35 | vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null })); |
| 36 | vi.mock("@clerk/expo", () => ({ useAuth: () => ({ isLoaded: false, isSignedIn: false }) })); |
| 37 | vi.mock("expo-camera", () => ({ |
| 38 | CameraView: () => null, |
| 39 | useCameraPermissions: () => [null, vi.fn()], |
| 40 | })); |
| 41 | vi.mock("expo-haptics", () => ({ |
| 42 | notificationAsync: vi.fn(async () => {}), |
| 43 | NotificationFeedbackType: { Success: "success", Error: "error" }, |
| 44 | })); |
| 45 | vi.mock("../../components/ui/ListRow", () => ({ ListRow: () => null })); |
| 46 | vi.mock("../../components/ui/Dialog", () => ({ alert: vi.fn() })); |
| 47 | vi.mock("../../components/ui/ScreenScrollView", () => ({ ScreenScrollView: Children })); |
| 48 | vi.mock("../../components/ui/AppText", () => ({ |
| 49 | AppText: ({ children }: { children?: ReactNode }) => <span>{children}</span>, |
| 50 | AppTextInput: ({ |
| 51 | accessibilityLabel, |
| 52 | value, |
| 53 | onChangeText, |
| 54 | }: { |
| 55 | accessibilityLabel: string; |
| 56 | value: string; |
| 57 | onChangeText: (value: string) => void; |
| 58 | }) => ( |
| 59 | <input |
| 60 | aria-label={accessibilityLabel} |
| 61 | value={value} |
| 62 | onChange={(event) => onChangeText(event.currentTarget.value)} |
| 63 | /> |
| 64 | ), |
| 65 | })); |
| 66 | vi.mock("../../components/ui/FrostedCutout", () => ({ FrostedCutout: () => null })); |
| 67 | vi.mock("../../components/ui/AppSymbol", () => ({ SymbolView: () => null })); |
| 68 | vi.mock("../../lib/useUniwindTheme", () => ({ useUniwindTheme: () => ({}) })); |
| 69 | vi.mock("../settings/appearance/AppearancePreferencesProvider", () => ({ |
| 70 | useAppearancePreferences: () => ({ themeAppearance: "dark" }), |
| 71 | })); |
| 72 | vi.mock("../settings/components/SettingsScreen", () => ({ SettingsScreen: Children })); |
| 73 | vi.mock("../settings/settings-navigation", () => ({ |
| 74 | leaveSettingsPage: vi.fn(), |
| 75 | openSettings: vi.fn(), |
| 76 | })); |
| 77 | vi.mock("../layout/workspace/workspace-selection-store", () => ({ |
| 78 | workspaceHasColumns: () => false, |
| 79 | })); |
| 80 | vi.mock("../cloud/publicConfig", () => ({ hasCloudPublicConfig: () => false })); |
| 81 | vi.mock("./CloudEnvironmentRows", () => ({ CloudEnvironmentRows: () => null })); |
| 82 | vi.mock("./environmentSections", () => ({ |
| 83 | splitEnvironmentSections: () => ({ connectedCloudEnvironments: [] }), |
| 84 | })); |
| 85 | vi.mock("./devAutoConnect", () => ({ |
| 86 | autoConnectReport: () => ({ connecting: false, error: null }), |
| 87 | devAutoConnectAttemptAtom: {}, |
| 88 | })); |
| 89 | vi.mock("../../state/use-remote-environment-registry", () => ({ |
| 90 | setPendingConnectionError: (message: string | null) => { |
| 91 | harness.pendingConnectionError = message; |
| 92 | }, |
| 93 | useRemoteConnections: () => ({ |
| 94 | connectionPairingUrl: "", |
| 95 | connectedEnvironments: [], |
| 96 | pairingConnectionError: harness.pendingConnectionError, |
| 97 | onChangeConnectionPairingUrl: vi.fn(), |
| 98 | onConnectPress: harness.connectPress, |
| 99 | onSetEnvironmentEnabled: vi.fn(), |
| 100 | onRemoveEnvironmentPress: vi.fn(), |
| 101 | }), |
| 102 | })); |
| 103 | |
| 104 | import { ConnectionsNewRouteScreen } from "./ConnectionsNewRouteScreen"; |
| 105 | |
| 106 | it("does not show another feature's error as a pairing failure", () => { |
| 107 | (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 108 | vi.stubGlobal("__DEV__", false); |
| 109 | harness.pendingConnectionError = "Could not send the message."; |
| 110 | const container = document.createElement("div"); |
| 111 | const root = createRoot(container); |
| 112 | act(() => root.render(<ConnectionsNewRouteScreen route={{ params: undefined }} />)); |
| 113 | |
| 114 | expect(container.textContent).not.toContain("Could not send the message."); |
| 115 | expect(container.textContent).toContain("For machines on your local network or tailnet."); |
| 116 | |
| 117 | act(() => root.unmount()); |
| 118 | // Leaving the form must not wipe an error that belongs to another screen. |
| 119 | expect(harness.pendingConnectionError).toBe("Could not send the message."); |
| 120 | }); |
| 121 | |
| 122 | it("splits a pasted pairing link and preserves the code during address edits", () => { |
| 123 | (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 124 | vi.stubGlobal("__DEV__", false); |
| 125 | harness.pendingConnectionError = null; |
| 126 | const container = document.createElement("div"); |
| 127 | const root = createRoot(container); |
| 128 | |
| 129 | try { |
| 130 | act(() => root.render(<ConnectionsNewRouteScreen route={{ params: undefined }} />)); |
| 131 | const address = container.querySelector<HTMLInputElement>('input[aria-label="Address"]')!; |
| 132 | const code = container.querySelector<HTMLInputElement>('input[aria-label="Code"]')!; |
| 133 | const enter = (input: HTMLInputElement, value: string) => { |
| 134 | act(() => { |
| 135 | Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call( |
| 136 | input, |
| 137 | value, |
| 138 | ); |
| 139 | input.dispatchEvent(new Event("input", { bubbles: true })); |
| 140 | }); |
| 141 | }; |
| 142 | |
| 143 | enter(code, "manual-code"); |
| 144 | enter(address, " http://100.82.53.91:3773/pair#token=test-pairing-code\n"); |
| 145 | expect(address.value).toBe("http://100.82.53.91:3773"); |
| 146 | expect(code.value).toBe("test-pairing-code"); |
| 147 | |
| 148 | enter(code, "replacement-code"); |
| 149 | for (const value of ["", "100.82.53.92:3773", "http://100.82.53.92:3773/"]) { |
| 150 | enter(address, value); |
| 151 | expect(address.value).toBe(value); |
| 152 | expect(code.value).toBe("replacement-code"); |
| 153 | } |
| 154 | } finally { |
| 155 | act(() => root.unmount()); |
| 156 | } |
| 157 | }); |