Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions apps/mobile/src/state/use-remote-environment-registry.test.tsx
Original file line number Diff line number Diff line change
@@ -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<ReadonlyMap<EnvironmentId, ConnectionCatalogEntry>>(
new Map([[environmentId, { target, profile: Option.none(), enabled: false }]]),
);
const state = yield* SubscriptionRef.make<SupervisorConnectionState>(
AVAILABLE_CONNECTION_STATE,
);
const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
target,
state,
session: yield* SubscriptionRef.make(Option.none<RpcSession>()),
prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
connect: Effect.void,
disconnect: Effect.void,
retryNow: Effect.void,
});
const environment = {
entries,
networkStatus: yield* SubscriptionRef.make<NetworkStatus>("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<typeof useRemoteConnections>;
function Probe() {
connections = useRemoteConnections();
return null;
}
yield* Effect.promise(async () => {
await act(async () =>
root.render(
<RegistryContext value={registry}>
<Probe />
</RegistryContext>,
),
);
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" },
]);
});
}),
);
11 changes: 9 additions & 2 deletions apps/mobile/src/state/use-remote-environment-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
1 change: 1 addition & 0 deletions apps/mobile/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading