diff --git a/apps/mobile/src/state/use-remote-environment-registry.test.tsx b/apps/mobile/src/state/use-remote-environment-registry.test.tsx new file mode 100644 index 0000000000000..84276393944e2 --- /dev/null +++ b/apps/mobile/src/state/use-remote-environment-registry.test.tsx @@ -0,0 +1,129 @@ +import { RegistryContext } from "@effect/atom-react"; +import { EnvironmentId } from "@t3tools/contracts"; +import { + EnvironmentRegistry, + EnvironmentSupervisor, + AVAILABLE_CONNECTION_STATE, + PrimaryConnectionTarget, + type SupervisorConnectionState, + type PreparedConnection, + type ConnectionCatalogEntry, + type NetworkStatus, +} from "@t3tools/client-runtime/connection"; +import type { RpcSession } from "@t3tools/client-runtime/rpc"; +import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; +import * as Stream from "effect/Stream"; +import * as SubscriptionRef from "effect/SubscriptionRef"; +import { AtomRegistry } from "effect/reactivity"; +import { act } from "react"; +import { createRoot } from "react-dom/client"; +import { expect, it } from "@effect/vitest"; +import { vi } from "vite-plus/test"; + +const fixture = vi.hoisted(() => ({ + environment: undefined as EnvironmentRegistry.EnvironmentRegistry["Service"] | undefined, +})); + +vi.mock("react-native", () => ({})); +vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({}) })); +vi.mock("../features/cloud/publicConfig", () => ({ hasCloudPublicConfig: () => false })); +vi.mock("../connection/runtime", async () => { + const { Atom } = await import("effect/reactivity"); + const Layer = await import("effect/Layer"); + const { EnvironmentRegistry } = await import("@t3tools/client-runtime/connection"); + return { + connectionAtomRuntime: Atom.runtime( + Layer.suspend(() => + Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, fixture.environment!), + ), + ), + }; +}); + +import { useRemoteConnections } from "./use-remote-environment-registry"; + +const environmentId = EnvironmentId.make("switched-off-environment"); + +it.effect("Reconnect switches a switched-off environment back on", () => + Effect.gen(function* () { + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + const target = new PrimaryConnectionTarget({ + environmentId, + label: "Switched off", + httpBaseUrl: "https://off.test", + wsBaseUrl: "wss://off.test", + }); + const entries = yield* SubscriptionRef.make>( + new Map([[environmentId, { target, profile: Option.none(), enabled: false }]]), + ); + const state = yield* SubscriptionRef.make( + AVAILABLE_CONNECTION_STATE, + ); + const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ + target, + state, + session: yield* SubscriptionRef.make(Option.none()), + prepared: yield* SubscriptionRef.make(Option.none()), + connect: Effect.void, + disconnect: Effect.void, + retryNow: Effect.void, + }); + const environment = { + entries, + networkStatus: yield* SubscriptionRef.make("online"), + followStream: (_id, stream) => + Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor), + // Like the real supervisor, a retry does nothing while the environment is switched off. + retryNow: () => Effect.void, + setEnabled: (id, enabled) => + Effect.gen(function* () { + yield* SubscriptionRef.update(entries, (current) => + new Map(current).set(id, { ...current.get(id)!, enabled }), + ); + yield* SubscriptionRef.update(state, (current): SupervisorConnectionState => ({ + ...current, + desired: enabled, + phase: enabled ? "connecting" : "available", + })); + }), + } satisfies Pick< + EnvironmentRegistry.EnvironmentRegistry["Service"], + "entries" | "networkStatus" | "followStream" | "retryNow" | "setEnabled" + >; + fixture.environment = + environment as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]; + + const registry = AtomRegistry.make(); + const root = createRoot(document.createElement("div")); + using _mounted = { + [Symbol.dispose]() { + act(() => root.unmount()); + registry.dispose(); + }, + }; + let connections!: ReturnType; + function Probe() { + connections = useRemoteConnections(); + return null; + } + yield* Effect.promise(async () => { + await act(async () => + root.render( + + + , + ), + ); + expect(connections.connectedEnvironments).toMatchObject([ + { environmentId, isEnabled: false, connectionState: "available" }, + ]); + await act(async () => { + await connections.onReconnectEnvironment(environmentId); + }); + expect(connections.connectedEnvironments).toMatchObject([ + { environmentId, isEnabled: true, connectionState: "connecting" }, + ]); + }); + }), +); diff --git a/apps/mobile/src/state/use-remote-environment-registry.ts b/apps/mobile/src/state/use-remote-environment-registry.ts index c75456461bf64..53d7a94117e6b 100644 --- a/apps/mobile/src/state/use-remote-environment-registry.ts +++ b/apps/mobile/src/state/use-remote-environment-registry.ts @@ -166,8 +166,15 @@ export function useRemoteConnections() { const onConnectPress = (pairingUrl?: string, expectedEnvironmentId?: EnvironmentId) => pairEnvironment(pairingUrl ?? connectionPairingUrl, expectedEnvironmentId); - const onReconnectEnvironment = (environmentId: EnvironmentId) => - controller.retryEnvironment(environmentId); + const onReconnectEnvironment = (environmentId: EnvironmentId) => { + const environment = connectedEnvironments.find( + (candidate) => candidate.environmentId === environmentId, + ); + // A retry does nothing while the environment is switched off. + return environment?.isEnabled === false + ? controller.setEnvironmentEnabled(environmentId, true) + : controller.retryEnvironment(environmentId); + }; const onSetEnvironmentEnabled = (environmentId: EnvironmentId, enabled: boolean) => controller.setEnvironmentEnabled(environmentId, enabled); const onUpdateEnvironment = ( diff --git a/apps/mobile/vite.config.ts b/apps/mobile/vite.config.ts index 6e24636671608..894ea0ed338ab 100644 --- a/apps/mobile/vite.config.ts +++ b/apps/mobile/vite.config.ts @@ -6,6 +6,7 @@ import "vite-plus/test/config"; const reactTests = [ "src/features/workspace/workspace-tabs-state.test.ts", "src/state/use-thread-pr.demand.test.tsx", + "src/state/use-remote-environment-registry.test.tsx", "src/features/threads/git/GitBranchesSheet.occupancy.test.tsx", "src/features/files/useFileTreeEntries.test.tsx", "src/features/projects/AddProjectScreen.test.ts",