Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Sonnet 5.5 <[email protected]> Co-authored-by: SAPHID <[email protected]>
2108 lines
81 KiB
TypeScript
2108 lines
81 KiB
TypeScript
import { AuthStandardClientScopes, EnvironmentId, type ServerConfig } from "@t3tools/contracts";
|
|
import { RelayClientTracer } from "@t3tools/shared/relayTracing";
|
|
import { describe, expect, it } from "@effect/vitest";
|
|
import * as Deferred from "effect/Deferred";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Layer from "effect/Layer";
|
|
import * as Option from "effect/Option";
|
|
import * as Random from "effect/Random";
|
|
import * as Ref from "effect/Ref";
|
|
import * as Stream from "effect/Stream";
|
|
import * as SubscriptionRef from "effect/SubscriptionRef";
|
|
import * as TestClock from "effect/testing/TestClock";
|
|
import * as Tracer from "effect/Tracer";
|
|
|
|
import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
|
|
import * as TokenStore from "../authorization/tokenStore.ts";
|
|
import * as ClientCapabilities from "../platform/capabilities.ts";
|
|
import * as ManagedRelay from "../relay/managedRelay.ts";
|
|
import * as RpcHttp from "../rpc/http.ts";
|
|
import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
|
|
import { fetchEnvironmentSessionState } from "../state/session.ts";
|
|
import type { ConnectionCatalogEntry, ConnectionRoute } from "./catalog.ts";
|
|
import * as Connectivity from "./connectivity.ts";
|
|
import * as ConnectionDriver from "./driver.ts";
|
|
import { BearerConnectionProfile } from "./catalog.ts";
|
|
import {
|
|
BearerConnectionTarget,
|
|
ConnectionBlockedError,
|
|
ConnectionTransientError,
|
|
PrimaryConnectionTarget,
|
|
RelayConnectionTarget,
|
|
type ConnectionAttemptError,
|
|
type ConnectionTarget,
|
|
type NetworkStatus,
|
|
type PreparedConnection,
|
|
type SupervisorConnectionState,
|
|
} from "./model.ts";
|
|
import * as RpcSession from "../rpc/session.ts";
|
|
import * as EnvironmentSupervisor from "./supervisor.ts";
|
|
import * as ConnectionWakeups from "./wakeups.ts";
|
|
import { connectionRouteId, entryWithRoutes, isLearned, mergeLearnedRoutes } from "./routes.ts";
|
|
import { NETWORK_BLOCKING_HINT } from "../errors/network.ts";
|
|
|
|
const TARGET = new PrimaryConnectionTarget({
|
|
environmentId: EnvironmentId.make("environment-1"),
|
|
label: "Test environment",
|
|
httpBaseUrl: "https://environment.example.test",
|
|
wsBaseUrl: "wss://environment.example.test",
|
|
});
|
|
|
|
const RELAY_TARGET = new RelayConnectionTarget({
|
|
environmentId: TARGET.environmentId,
|
|
label: TARGET.label,
|
|
});
|
|
|
|
const TARGET_ENTRY: ConnectionCatalogEntry = {
|
|
target: TARGET,
|
|
profile: Option.none(),
|
|
enabled: true,
|
|
};
|
|
|
|
const RELAY_ENTRY: ConnectionCatalogEntry = {
|
|
target: RELAY_TARGET,
|
|
profile: Option.none(),
|
|
enabled: true,
|
|
};
|
|
|
|
const PREPARED_CONNECTION: PreparedConnection = {
|
|
environmentId: TARGET.environmentId,
|
|
label: TARGET.label,
|
|
httpBaseUrl: TARGET.httpBaseUrl,
|
|
socketUrl: "wss://environment.example.test/ws",
|
|
httpAuthorization: null,
|
|
target: TARGET,
|
|
};
|
|
|
|
const TEST_RPC_CLIENT = {} as WsRpcProtocolClient;
|
|
|
|
const LAN_TARGET = new BearerConnectionTarget({
|
|
environmentId: TARGET.environmentId,
|
|
label: TARGET.label,
|
|
connectionId: "bearer:lan",
|
|
});
|
|
const LAN_ROUTE: ConnectionRoute = {
|
|
target: LAN_TARGET,
|
|
profile: Option.some(
|
|
new BearerConnectionProfile({
|
|
connectionId: LAN_TARGET.connectionId,
|
|
environmentId: TARGET.environmentId,
|
|
label: TARGET.label,
|
|
httpBaseUrl: "http://192.168.1.10:3773/",
|
|
wsBaseUrl: "ws://192.168.1.10:3773/",
|
|
}),
|
|
),
|
|
};
|
|
// LAN first, T3 Connect as the fallback.
|
|
const LAN_THEN_RELAY_ENTRY: ConnectionCatalogEntry = {
|
|
target: LAN_ROUTE.target,
|
|
profile: LAN_ROUTE.profile,
|
|
alternateRoutes: [{ target: RELAY_TARGET, profile: Option.none() }],
|
|
enabled: true,
|
|
};
|
|
|
|
function preparedFor(target: ConnectionTarget): PreparedConnection {
|
|
return { ...PREPARED_CONNECTION, target };
|
|
}
|
|
|
|
function transient(message = "Connection failed.") {
|
|
return new ConnectionTransientError({
|
|
reason: "transport",
|
|
detail: message,
|
|
});
|
|
}
|
|
|
|
function blocked(message = "Authentication required.") {
|
|
return new ConnectionBlockedError({
|
|
reason: "authentication",
|
|
detail: message,
|
|
});
|
|
}
|
|
|
|
function awaitState(
|
|
state: SubscriptionRef.SubscriptionRef<SupervisorConnectionState>,
|
|
predicate: (value: SupervisorConnectionState) => boolean,
|
|
) {
|
|
return SubscriptionRef.changes(state).pipe(
|
|
Stream.filter(predicate),
|
|
Stream.runHead,
|
|
Effect.map(Option.getOrThrow),
|
|
);
|
|
}
|
|
|
|
const eventuallyState = Effect.fn("TestConnectionHarness.eventuallyState")(function* (
|
|
state: SubscriptionRef.SubscriptionRef<SupervisorConnectionState>,
|
|
predicate: (value: SupervisorConnectionState) => boolean,
|
|
) {
|
|
let lastState = yield* SubscriptionRef.get(state);
|
|
for (let iteration = 0; iteration < 100; iteration += 1) {
|
|
lastState = yield* SubscriptionRef.get(state);
|
|
if (predicate(lastState)) {
|
|
return lastState;
|
|
}
|
|
yield* Effect.yieldNow;
|
|
}
|
|
return yield* Effect.die(
|
|
new Error(
|
|
`Expected supervisor state was not observed. Last state: phase=${lastState.phase}, stage=${lastState.stage ?? "none"}, attempt=${lastState.attempt}, generation=${lastState.generation}`,
|
|
),
|
|
);
|
|
});
|
|
|
|
const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?: {
|
|
readonly networkStatus?: NetworkStatus;
|
|
readonly prepare?: (
|
|
attempt: number,
|
|
target: ConnectionTarget,
|
|
) => Effect.Effect<PreparedConnection, ConnectionAttemptError>;
|
|
readonly ready?: (attempt: number) => Effect.Effect<void, ConnectionAttemptError>;
|
|
readonly probe?: (attempt: number) => Effect.Effect<void, ConnectionAttemptError>;
|
|
readonly initialConfig?: (attempt: number) => Effect.Effect<ServerConfig, ConnectionAttemptError>;
|
|
readonly checkRoute?: (route: ConnectionRoute) => Effect.Effect<ConnectionDriver.RouteCheck>;
|
|
}) {
|
|
const networkStatus = yield* SubscriptionRef.make<NetworkStatus>(
|
|
options?.networkStatus ?? "online",
|
|
);
|
|
const prepareCount = yield* Ref.make(0);
|
|
const sessionCount = yield* Ref.make(0);
|
|
const releaseCount = yield* Ref.make(0);
|
|
const wakeups = yield* SubscriptionRef.make<{
|
|
readonly sequence: number;
|
|
readonly reason: ConnectionWakeups.ConnectionWakeup;
|
|
}>({
|
|
sequence: 0,
|
|
reason: "application-active",
|
|
});
|
|
const closedSessions = yield* Ref.make<
|
|
ReadonlyArray<Deferred.Deferred<never, ConnectionTransientError>>
|
|
>([]);
|
|
|
|
const connectivity = Connectivity.Connectivity.of({
|
|
status: SubscriptionRef.get(networkStatus),
|
|
changes: SubscriptionRef.changes(networkStatus),
|
|
});
|
|
|
|
const prepare = Effect.fn("TestConnectionDriver.prepare")(function* (target: ConnectionTarget) {
|
|
const attempt = yield* Ref.updateAndGet(prepareCount, (count) => count + 1);
|
|
if (options?.prepare) {
|
|
return yield* options.prepare(attempt, target);
|
|
}
|
|
return PREPARED_CONNECTION;
|
|
});
|
|
|
|
const checkRoute = (route: ConnectionRoute) =>
|
|
options?.checkRoute?.(route) ?? Effect.succeed<ConnectionDriver.RouteCheck>("unchecked");
|
|
|
|
const connectRoute = Effect.fn("TestConnectionDriver.connectRoute")(function* (
|
|
target: ConnectionTarget,
|
|
reportProgress: (progress: ConnectionDriver.ConnectionDriverProgress) => Effect.Effect<void>,
|
|
) {
|
|
const prepared = yield* prepare(target);
|
|
yield* reportProgress({ stage: "opening", prepared });
|
|
|
|
const attempt = yield* Ref.updateAndGet(sessionCount, (count) => count + 1);
|
|
const closed = yield* Deferred.make<never, ConnectionTransientError>();
|
|
yield* Ref.update(closedSessions, (sessions) => [...sessions, closed]);
|
|
|
|
const session = yield* Effect.acquireRelease(
|
|
Effect.succeed({
|
|
client: TEST_RPC_CLIENT,
|
|
initialConfig:
|
|
options?.initialConfig?.(attempt) ??
|
|
Effect.die(new Error("Initial config is not used by supervisor tests.")),
|
|
subscribeServerConfig: (input) => TEST_RPC_CLIENT.subscribeServerConfig(input),
|
|
ready: options?.ready?.(attempt) ?? Effect.void,
|
|
probe: options?.probe?.(attempt) ?? Effect.void,
|
|
closed: Deferred.await(closed),
|
|
} satisfies RpcSession.RpcSession),
|
|
() => Ref.update(releaseCount, (count) => count + 1),
|
|
);
|
|
|
|
yield* reportProgress({ stage: "synchronizing", prepared });
|
|
yield* session.ready;
|
|
return { prepared, session } satisfies ConnectionDriver.EnvironmentConnectionLease;
|
|
});
|
|
|
|
const connect = Effect.fn("TestConnectionDriver.connect")(function* (
|
|
entry: ConnectionCatalogEntry,
|
|
reportProgress: (progress: ConnectionDriver.ConnectionDriverProgress) => Effect.Effect<void>,
|
|
) {
|
|
yield* reportProgress({ stage: "preparing" });
|
|
return yield* ConnectionDriver.connectOverRoutes(entry, checkRoute, (route) =>
|
|
connectRoute(route.target, reportProgress),
|
|
);
|
|
});
|
|
|
|
const layerDependencies = Layer.mergeAll(
|
|
// Jitter at its maximum, so each retry waits exactly its ceiling: 2s, 4s, 8s...
|
|
Layer.succeed(Random.Random, {
|
|
nextDoubleUnsafe: () => 1 - Number.EPSILON,
|
|
nextIntUnsafe: () => 0,
|
|
}),
|
|
Layer.succeed(Connectivity.Connectivity, connectivity),
|
|
Layer.succeed(
|
|
ConnectionWakeups.ConnectionWakeups,
|
|
ConnectionWakeups.ConnectionWakeups.of({
|
|
changes: SubscriptionRef.changes(wakeups).pipe(
|
|
Stream.drop(1),
|
|
Stream.map((event) => event.reason),
|
|
),
|
|
}),
|
|
),
|
|
Layer.succeed(
|
|
ConnectionDriver.ConnectionDriver,
|
|
ConnectionDriver.ConnectionDriver.of({
|
|
connect,
|
|
checkRoute: (_entry, route) => checkRoute(route),
|
|
preflight: (_entry, route) =>
|
|
checkRoute(route).pipe(Effect.map((check) => check === "answered")),
|
|
}),
|
|
),
|
|
);
|
|
|
|
return {
|
|
dependencies: layerDependencies,
|
|
prepareCount,
|
|
sessionCount,
|
|
releaseCount,
|
|
setNetworkStatus: (status: NetworkStatus) => SubscriptionRef.set(networkStatus, status),
|
|
wake: (reason: ConnectionWakeups.ConnectionWakeup) =>
|
|
SubscriptionRef.update(wakeups, (event) => ({
|
|
sequence: event.sequence + 1,
|
|
reason,
|
|
})),
|
|
closeLatestSession: Effect.fn("TestConnectionHarness.closeLatestSession")(function* (
|
|
error = transient("Session closed."),
|
|
) {
|
|
const sessions = yield* Ref.get(closedSessions);
|
|
const latest = sessions.at(-1);
|
|
if (latest) {
|
|
yield* Deferred.fail(latest, error);
|
|
}
|
|
}),
|
|
};
|
|
});
|
|
|
|
describe("retryDelayMs", () => {
|
|
it("doubles from 2 seconds to a 5 minute cap, jittered within the upper half of each step", () => {
|
|
const ceilings = [2_000, 4_000, 8_000, 16_000, 32_000, 64_000, 128_000, 256_000, 300_000];
|
|
for (const [failureCount, ceiling] of [...ceilings, 300_000].entries()) {
|
|
expect(EnvironmentSupervisor.retryDelayMs(failureCount, 0)).toBe(ceiling / 2);
|
|
expect(EnvironmentSupervisor.retryDelayMs(failureCount, 0.5)).toBe((ceiling * 3) / 4);
|
|
expect(EnvironmentSupervisor.retryDelayMs(failureCount, 1 - Number.EPSILON)).toBe(ceiling);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("EnvironmentSupervisor", () => {
|
|
it.effect("exports each relay setup as a standalone linked trace that ends at readiness", () =>
|
|
Effect.gen(function* () {
|
|
const spans: Array<Tracer.NativeSpan> = [];
|
|
const tracer = Tracer.make({
|
|
span: (options) => {
|
|
const span = new Tracer.NativeSpan(options);
|
|
const end = span.end.bind(span);
|
|
span.end = (endTime, exit) => {
|
|
end(endTime, exit);
|
|
spans.push(span);
|
|
};
|
|
return span;
|
|
},
|
|
});
|
|
const harness = yield* makeHarness({
|
|
prepare: (attempt) =>
|
|
attempt === 1 ? Effect.fail(transient()) : Effect.succeed(PREPARED_CONNECTION),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(
|
|
Effect.provide(harness.dependencies),
|
|
Effect.provideService(RelayClientTracer, Option.some(tracer)),
|
|
);
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
const firstAttempt = spans.find((span) => span.name === "relay.connection.attempt");
|
|
expect(firstAttempt).toBeDefined();
|
|
|
|
yield* TestClock.adjust("3 seconds");
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
|
|
const attempts = spans.filter((span) => span.name === "relay.connection.attempt");
|
|
expect(attempts).toHaveLength(2);
|
|
expect(attempts[0]?.traceId).not.toBe(attempts[1]?.traceId);
|
|
expect(attempts[1]?.links.map((link) => link.span.spanId)).toContain(attempts[0]?.spanId);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("does not attempt a connection until it is desired", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness();
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY).pipe(
|
|
Effect.provide(harness.dependencies),
|
|
);
|
|
|
|
expect((yield* SubscriptionRef.get(supervisor.state)).phase).toBe("available");
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(0);
|
|
}),
|
|
);
|
|
|
|
it.effect("does not let the initial connect signal cancel the first attempt", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness();
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY).pipe(
|
|
Effect.provide(harness.dependencies),
|
|
);
|
|
|
|
yield* supervisor.connect;
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
|
|
}),
|
|
);
|
|
|
|
it.effect("waits while offline and connects immediately when the network returns", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({ networkStatus: "offline" });
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "offline");
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(0);
|
|
|
|
yield* harness.setNetworkStatus("online");
|
|
const ready = yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
|
|
expect(ready).toMatchObject({
|
|
desired: true,
|
|
network: "online",
|
|
phase: "connected",
|
|
attempt: 1,
|
|
generation: 1,
|
|
lastFailure: null,
|
|
});
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(1);
|
|
}),
|
|
);
|
|
|
|
it.effect("resets retries when activation arrives before the network returns", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness();
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.closeLatestSession();
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
yield* harness.setNetworkStatus("offline");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "offline" && state.attempt === 2,
|
|
);
|
|
|
|
yield* harness.wake("application-active-reconnect");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "offline" && state.attempt === 1,
|
|
);
|
|
yield* harness.setNetworkStatus("online");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2 && state.attempt === 1,
|
|
);
|
|
}),
|
|
);
|
|
|
|
it.effect("retries forever with exponential backoff capped at five minutes", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
prepare: () => Effect.fail(transient()),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(1);
|
|
|
|
const delays = [
|
|
2_000, 4_000, 8_000, 16_000, 32_000, 64_000, 128_000, 256_000, 300_000, 300_000,
|
|
];
|
|
for (const [index, delay] of delays.entries()) {
|
|
yield* TestClock.adjust(delay - 1);
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(index + 1);
|
|
yield* TestClock.adjust(1);
|
|
yield* eventuallyState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === index + 2,
|
|
);
|
|
}
|
|
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(delays.length + 1);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("keeps the latest failure visible throughout the next connection attempt", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
prepare: (attempt) =>
|
|
attempt === 1 ? Effect.fail(transient("Relay connection timed out.")) : Effect.never,
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
yield* TestClock.adjust("3 seconds");
|
|
|
|
const retrying = yield* awaitState(
|
|
supervisor.state,
|
|
(state) =>
|
|
state.phase === "connecting" && state.stage === "preparing" && state.attempt === 2,
|
|
);
|
|
expect(retrying).toMatchObject({
|
|
phase: "connecting",
|
|
stage: "preparing",
|
|
attempt: 2,
|
|
lastFailure: {
|
|
_tag: "ConnectionTransientError",
|
|
reason: "transport",
|
|
message: "Relay connection timed out.",
|
|
},
|
|
});
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("keeps a working socket open while a slow server config snapshot arrives", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
ready: () => Effect.sleep("40 seconds"),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connecting" && state.stage === "synchronizing",
|
|
);
|
|
yield* TestClock.adjust("39 seconds");
|
|
expect((yield* SubscriptionRef.get(supervisor.state)).phase).toBe("connecting");
|
|
|
|
yield* TestClock.adjust("1 second");
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(1);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("retries when a session never becomes ready", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
ready: () => Effect.never,
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connecting" && state.stage === "synchronizing",
|
|
);
|
|
// 15 seconds of setup plus 75 for the snapshot; setup finished instantly.
|
|
yield* TestClock.adjust("89 seconds");
|
|
expect((yield* SubscriptionRef.get(supervisor.state)).stage).toBe("synchronizing");
|
|
|
|
yield* TestClock.adjust("1 second");
|
|
const retrying = yield* awaitState(supervisor.state, (state) => state.phase === "backoff");
|
|
|
|
expect(retrying).toMatchObject({
|
|
phase: "backoff",
|
|
lastFailure: {
|
|
_tag: "ConnectionTransientError",
|
|
reason: "timeout",
|
|
message: "Test environment did not respond during connection setup.",
|
|
},
|
|
});
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
|
|
expect(Option.isNone(yield* SubscriptionRef.get(supervisor.prepared))).toBe(true);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("interrupts and releases a connection attempt when setup times out", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
prepare: () => Effect.never,
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connecting" && state.stage === "preparing",
|
|
);
|
|
yield* TestClock.adjust("15 seconds");
|
|
const retrying = yield* eventuallyState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
|
|
expect(retrying).toMatchObject({
|
|
lastFailure: {
|
|
_tag: "ConnectionTransientError",
|
|
reason: "timeout",
|
|
message: "Test environment did not respond during connection setup.",
|
|
},
|
|
});
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect(
|
|
"shows a network hint for a stalled relay connection and clears it after recovery",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
prepare: (attempt) =>
|
|
attempt === 1 ? Effect.never : Effect.succeed(PREPARED_CONNECTION),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connecting");
|
|
yield* TestClock.adjust("15 seconds");
|
|
const failed = yield* awaitState(supervisor.state, (state) => state.phase === "backoff");
|
|
expect(failed.lastFailure?.message).toBe(
|
|
`Test environment did not respond during connection setup. ${NETWORK_BLOCKING_HINT}`,
|
|
);
|
|
|
|
yield* TestClock.adjust("3 seconds");
|
|
const recovered = yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected",
|
|
);
|
|
expect(recovered.lastFailure).toBeNull();
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(2);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("converts unexpected driver defects into retryable failures", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
prepare: (attempt) =>
|
|
attempt === 1
|
|
? Effect.die(new Error("Native transport defect."))
|
|
: Effect.succeed(PREPARED_CONNECTION),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
const failed = yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
expect(failed).toMatchObject({
|
|
lastFailure: {
|
|
_tag: "ConnectionTransientError",
|
|
reason: "transport",
|
|
message: "Test environment connection failed unexpectedly.",
|
|
},
|
|
});
|
|
|
|
yield* TestClock.adjust("3 seconds");
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(2);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("explicit retry interrupts the current backoff", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
prepare: (attempt) =>
|
|
attempt === 1 ? Effect.fail(transient()) : Effect.succeed(PREPARED_CONNECTION),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "backoff");
|
|
yield* supervisor.retryNow;
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(2);
|
|
}),
|
|
);
|
|
|
|
it.effect("explicit retry starts a fresh backoff sequence", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
prepare: () => Effect.fail(transient()),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
yield* TestClock.adjust("3 seconds");
|
|
yield* eventuallyState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 2,
|
|
);
|
|
|
|
yield* supervisor.retryNow;
|
|
yield* eventuallyState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(3);
|
|
|
|
yield* TestClock.adjust("1999 millis");
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(3);
|
|
yield* TestClock.adjust("1 milli");
|
|
yield* eventuallyState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 2,
|
|
);
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(4);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("keeps blocked failures idle until an external signal requests another attempt", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
prepare: (attempt) =>
|
|
attempt === 1 ? Effect.fail(blocked()) : Effect.succeed(PREPARED_CONNECTION),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "blocked");
|
|
yield* TestClock.adjust("1 hour");
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(1);
|
|
|
|
yield* supervisor.retryNow;
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(2);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("resets retries when activation wakes a blocked connection", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
prepare: (attempt) =>
|
|
attempt === 1
|
|
? Effect.fail(transient())
|
|
: attempt === 2
|
|
? Effect.fail(blocked())
|
|
: Effect.succeed(PREPARED_CONNECTION),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
yield* TestClock.adjust("3 seconds");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "blocked" && state.attempt === 2,
|
|
);
|
|
|
|
yield* harness.wake("application-active-reconnect");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.attempt === 1,
|
|
);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("keeps a session that still answers when the network reports offline", () =>
|
|
Effect.gen(function* () {
|
|
const probeCount = yield* Ref.make(0);
|
|
const harness = yield* makeHarness({
|
|
probe: () => Ref.update(probeCount, (count) => count + 1),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 1,
|
|
);
|
|
// A loopback server, or a flap shorter than the probe, keeps working.
|
|
yield* harness.setNetworkStatus("offline");
|
|
for (let attempt = 0; attempt < 100; attempt += 1) {
|
|
if ((yield* Ref.get(probeCount)) > 0) break;
|
|
yield* Effect.yieldNow;
|
|
}
|
|
yield* harness.setNetworkStatus("online");
|
|
yield* Effect.yieldNow;
|
|
|
|
expect(yield* Ref.get(probeCount)).toBe(1);
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
|
|
expect(yield* SubscriptionRef.get(supervisor.state)).toMatchObject({
|
|
phase: "connected",
|
|
generation: 1,
|
|
});
|
|
}),
|
|
);
|
|
|
|
it.effect("replaces the session on a long resume while the network reports offline", () =>
|
|
Effect.gen(function* () {
|
|
const probeCount = yield* Ref.make(0);
|
|
const harness = yield* makeHarness({
|
|
probe: () => Ref.update(probeCount, (count) => count + 1),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 1,
|
|
);
|
|
// A wrong offline report: the probe answers, so the session stays.
|
|
yield* harness.setNetworkStatus("offline");
|
|
for (let attempt = 0; attempt < 100; attempt += 1) {
|
|
if ((yield* Ref.get(probeCount)) > 0) break;
|
|
yield* Effect.yieldNow;
|
|
}
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
|
|
|
|
// The replacement connects although the network still reports offline.
|
|
yield* harness.wake("application-active-reconnect");
|
|
const replaced = yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
|
|
expect(replaced.attempt).toBe(1);
|
|
expect(yield* Ref.get(probeCount)).toBe(1);
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
|
|
}),
|
|
);
|
|
|
|
it.effect(
|
|
"releases a session that stops answering while offline and reconnects when online",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
probe: (attempt) => (attempt === 1 ? Effect.never : Effect.void),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 1,
|
|
);
|
|
yield* harness.setNetworkStatus("offline");
|
|
yield* TestClock.adjust("3 seconds");
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "offline");
|
|
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
|
|
expect(Option.isNone(yield* SubscriptionRef.get(supervisor.session))).toBe(true);
|
|
|
|
yield* harness.setNetworkStatus("online");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("probes instead of replacing a healthy session on an explicit retry", () =>
|
|
Effect.gen(function* () {
|
|
const probeCount = yield* Ref.make(0);
|
|
const harness = yield* makeHarness({
|
|
probe: () => Ref.update(probeCount, (count) => count + 1),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* supervisor.retryNow;
|
|
for (let attempt = 0; attempt < 100; attempt += 1) {
|
|
if ((yield* Ref.get(probeCount)) > 0) break;
|
|
yield* Effect.yieldNow;
|
|
}
|
|
|
|
expect(yield* Ref.get(probeCount)).toBe(1);
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
|
|
}),
|
|
);
|
|
|
|
it.effect("keeps the backoff ladder after an explicit retry finds a healthy session", () =>
|
|
Effect.gen(function* () {
|
|
const probeCount = yield* Ref.make(0);
|
|
const harness = yield* makeHarness({
|
|
probe: () => Ref.update(probeCount, (count) => count + 1),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.closeLatestSession();
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
yield* TestClock.adjust("2 seconds");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
|
|
yield* supervisor.retryNow;
|
|
for (let attempt = 0; attempt < 100; attempt += 1) {
|
|
if ((yield* Ref.get(probeCount)) > 0) break;
|
|
yield* Effect.yieldNow;
|
|
}
|
|
expect(yield* Ref.get(probeCount)).toBe(1);
|
|
|
|
// The flapping session keeps climbing the ladder: the answered retry does
|
|
// not reset it after this unrelated close.
|
|
yield* harness.closeLatestSession();
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 2,
|
|
);
|
|
yield* TestClock.adjust("4 seconds");
|
|
const reconnected = yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 3,
|
|
);
|
|
expect(reconnected.attempt).toBe(3);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("retries a blocked connection when platform credentials change", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
prepare: (attempt) =>
|
|
attempt === 1 ? Effect.fail(blocked()) : Effect.succeed(PREPARED_CONNECTION),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "blocked");
|
|
yield* harness.wake("credentials-changed");
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(2);
|
|
}),
|
|
);
|
|
|
|
it.effect("does not let platform wakeups reset an in-flight attempt", () =>
|
|
Effect.gen(function* () {
|
|
const firstAttemptStarted = yield* Deferred.make<void>();
|
|
const harness = yield* makeHarness({
|
|
prepare: () =>
|
|
Deferred.succeed(firstAttemptStarted, undefined).pipe(Effect.andThen(Effect.never)),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* Deferred.await(firstAttemptStarted);
|
|
yield* Effect.all(
|
|
[
|
|
harness.wake("credentials-changed"),
|
|
harness.wake("application-active"),
|
|
harness.wake("credentials-changed"),
|
|
],
|
|
{ concurrency: "unbounded" },
|
|
);
|
|
yield* Effect.yieldNow;
|
|
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(1);
|
|
|
|
yield* TestClock.adjust("15 seconds");
|
|
const retrying = yield* eventuallyState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
|
|
expect(retrying).toMatchObject({
|
|
lastFailure: {
|
|
_tag: "ConnectionTransientError",
|
|
reason: "timeout",
|
|
message: "Test environment did not respond during connection setup.",
|
|
},
|
|
});
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(1);
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(0);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("treats an involuntary session close as transient and reconnects", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness();
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.closeLatestSession();
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
expect(Option.isNone(yield* SubscriptionRef.get(supervisor.prepared))).toBe(true);
|
|
|
|
yield* TestClock.adjust("3 seconds");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
|
|
expect(Option.isSome(yield* SubscriptionRef.get(supervisor.prepared))).toBe(true);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("keeps escalating backoff when a newly opened session flaps", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness();
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.closeLatestSession();
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
|
|
yield* TestClock.adjust("3 seconds");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
yield* harness.closeLatestSession();
|
|
const secondFailure = yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 2,
|
|
);
|
|
|
|
expect(secondFailure.retryAt).not.toBeNull();
|
|
|
|
yield* TestClock.adjust("3 seconds");
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
|
|
|
|
yield* TestClock.adjust("1 second");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 3,
|
|
);
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(3);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("restarts the retry ladder when mobile returns to the foreground", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness();
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.closeLatestSession();
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
yield* TestClock.adjust("3 seconds");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
yield* harness.closeLatestSession();
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 2,
|
|
);
|
|
|
|
yield* harness.wake("application-active-reconnect");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 3 && state.attempt === 1,
|
|
);
|
|
yield* harness.closeLatestSession();
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(3);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("restarts the retry ladder when a long resume replaces a connected session", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness();
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.closeLatestSession();
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
yield* TestClock.adjust("2 seconds");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2 && state.attempt === 2,
|
|
);
|
|
|
|
yield* harness.wake("application-active-reconnect");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 3 && state.attempt === 1,
|
|
);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("restarts the retry ladder when a long resume interrupts connection setup", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
prepare: (attempt) => (attempt === 2 ? Effect.never : Effect.succeed(PREPARED_CONNECTION)),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.closeLatestSession();
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
yield* TestClock.adjust("3 seconds");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connecting" && state.attempt === 2,
|
|
);
|
|
|
|
yield* harness.wake("application-active-reconnect");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2 && state.attempt === 1,
|
|
);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("probes the active session without reconnecting on application activation", () =>
|
|
Effect.gen(function* () {
|
|
const probeCount = yield* Ref.make(0);
|
|
const probeCalled = yield* Deferred.make<void>();
|
|
const harness = yield* makeHarness({
|
|
probe: () =>
|
|
Ref.update(probeCount, (count) => count + 1).pipe(
|
|
Effect.andThen(Deferred.succeed(probeCalled, undefined)),
|
|
),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.wake("application-active");
|
|
yield* Deferred.await(probeCalled);
|
|
|
|
expect(yield* Ref.get(probeCount)).toBe(1);
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
|
|
expect((yield* SubscriptionRef.get(supervisor.state)).phase).toBe("connected");
|
|
}),
|
|
);
|
|
|
|
it.effect("immediately replaces a mobile session after a long background resume", () =>
|
|
Effect.gen(function* () {
|
|
const probeCount = yield* Ref.make(0);
|
|
const harness = yield* makeHarness({
|
|
probe: () => Ref.update(probeCount, (count) => count + 1),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 1,
|
|
);
|
|
yield* harness.wake("application-active-reconnect");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
|
|
expect(yield* Ref.get(probeCount)).toBe(0);
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
|
|
}),
|
|
);
|
|
|
|
it.effect("replaces a mobile session when a long resume interrupts an active probe", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
probe: (attempt) => (attempt === 1 ? Effect.never : Effect.void),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 1,
|
|
);
|
|
yield* harness.wake("application-active-probe");
|
|
yield* Effect.yieldNow;
|
|
yield* harness.wake("application-active-reconnect");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
|
|
}),
|
|
);
|
|
|
|
it.effect("reconnects immediately when the session closes during a resume probe", () =>
|
|
Effect.gen(function* () {
|
|
const probeStarted = yield* Deferred.make<void>();
|
|
const harness = yield* makeHarness({
|
|
probe: (attempt) =>
|
|
attempt === 1
|
|
? Deferred.succeed(probeStarted, undefined).pipe(Effect.andThen(Effect.never))
|
|
: Effect.void,
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.wake("application-active-probe");
|
|
yield* Deferred.await(probeStarted);
|
|
// The OS reports the suspended socket's close before the probe answers.
|
|
yield* harness.closeLatestSession();
|
|
|
|
// No TestClock advance: the unanswered probe skips the first backoff rung.
|
|
const reconnected = yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
expect(reconnected.attempt).toBe(1);
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("reconnects immediately when the foreground liveness probe fails", () =>
|
|
Effect.gen(function* () {
|
|
const allowReconnect = yield* Deferred.make<void>();
|
|
const harness = yield* makeHarness({
|
|
prepare: (attempt) =>
|
|
attempt === 2
|
|
? Deferred.await(allowReconnect).pipe(Effect.as(PREPARED_CONNECTION))
|
|
: Effect.succeed(PREPARED_CONNECTION),
|
|
probe: (attempt) =>
|
|
attempt === 1 ? Effect.fail(transient("The live session is stale.")) : Effect.void,
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.wake("application-active");
|
|
const reconnecting = yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connecting",
|
|
);
|
|
expect(reconnecting.attempt).toBe(1);
|
|
expect(Option.isNone(yield* SubscriptionRef.get(supervisor.session))).toBe(true);
|
|
|
|
// No TestClock advance: a failed wake probe skips the first backoff rung.
|
|
yield* Deferred.succeed(allowReconnect, undefined);
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2 && state.attempt === 1,
|
|
);
|
|
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("keeps normal backoff when a reconnect after a failed wake probe also fails", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
prepare: (attempt) =>
|
|
attempt === 2 ? Effect.fail(transient()) : Effect.succeed(PREPARED_CONNECTION),
|
|
probe: (attempt) =>
|
|
attempt === 1 ? Effect.fail(transient("The live session is stale.")) : Effect.void,
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.wake("application-active");
|
|
// The immediate follow-up attempt fails: only the first attempt after
|
|
// the wake probe skips the ladder, so this failure backs off normally.
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "backoff" && state.attempt === 1,
|
|
);
|
|
yield* TestClock.adjust("1999 millis");
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(2);
|
|
yield* TestClock.adjust("1 milli");
|
|
yield* eventuallyState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(3);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("uses the full tolerance window for a stalled desktop foreground probe", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
probe: (attempt) => (attempt === 1 ? Effect.never : Effect.void),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.wake("application-active");
|
|
yield* TestClock.adjust("14999 millis");
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
|
|
yield* TestClock.adjust("1 milli");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2 && state.attempt === 1,
|
|
);
|
|
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("an explicit retry shortens a stalled desktop foreground probe", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
probe: (attempt) => (attempt === 1 ? Effect.never : Effect.void),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.wake("application-active");
|
|
yield* TestClock.adjust("5 seconds");
|
|
yield* supervisor.retryNow;
|
|
// The retry's 3 second limit applies, not the 10 seconds left of the 15.
|
|
yield* TestClock.adjust("2999 millis");
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
|
|
yield* TestClock.adjust("1 milli");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2 && state.attempt === 1,
|
|
);
|
|
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("quickly times out a stalled mobile foreground liveness probe", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
probe: (attempt) => (attempt === 1 ? Effect.never : Effect.void),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.wake("application-active-probe");
|
|
yield* TestClock.adjust("3 seconds");
|
|
// The timed-out wake probe reconnects immediately without a backoff
|
|
// sleep: no further clock advance is needed.
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2 && state.attempt === 1,
|
|
);
|
|
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("honors an explicit disconnect while a foreground probe is stalled", () =>
|
|
Effect.gen(function* () {
|
|
const probeStarted = yield* Deferred.make<void>();
|
|
const harness = yield* makeHarness({
|
|
probe: () => Deferred.succeed(probeStarted, undefined).pipe(Effect.andThen(Effect.never)),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.wake("application-active");
|
|
yield* Deferred.await(probeStarted);
|
|
yield* supervisor.disconnect;
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "available");
|
|
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
|
|
}),
|
|
);
|
|
|
|
it.effect("does not churn a healthy session when credentials change", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness();
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.wake("credentials-changed");
|
|
yield* Effect.yieldNow;
|
|
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
|
|
expect((yield* SubscriptionRef.get(supervisor.state)).phase).toBe("connected");
|
|
}),
|
|
);
|
|
|
|
it.effect("releases and reconnects a relay session when credentials change", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
prepare: () => Effect.succeed(preparedFor(RELAY_TARGET)),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.wake("credentials-changed");
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
|
|
}),
|
|
);
|
|
|
|
it.effect("keeps a healthy relay session when its HTTP access token expires", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
prepare: (attempt) =>
|
|
Effect.succeed({
|
|
...PREPARED_CONNECTION,
|
|
target: RELAY_TARGET,
|
|
httpAuthorization: {
|
|
_tag: "Dpop",
|
|
accessToken: `access-token-${attempt}`,
|
|
expiresAtEpochMs: 3_600_000 * attempt,
|
|
},
|
|
}),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
const session = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.session));
|
|
|
|
yield* TestClock.adjust("2 hours");
|
|
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
|
|
expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.session))).toBe(session);
|
|
expect(yield* SubscriptionRef.get(supervisor.state)).toMatchObject({
|
|
phase: "connected",
|
|
generation: 1,
|
|
});
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("refreshes HTTP authorization without replacing the active relay session", () =>
|
|
Effect.gen(function* () {
|
|
const endpoint = {
|
|
httpBaseUrl: TARGET.httpBaseUrl,
|
|
wsBaseUrl: TARGET.wsBaseUrl,
|
|
providerKind: "cloudflare_tunnel" as const,
|
|
};
|
|
const token = yield* Ref.make(
|
|
Option.some(
|
|
new TokenStore.RemoteDpopAccessToken({
|
|
environmentId: TARGET.environmentId,
|
|
accountId: "test-account",
|
|
label: TARGET.label,
|
|
endpoint,
|
|
accessToken: "access-token-1",
|
|
expiresAtEpochMs: 3_600_000,
|
|
dpopThumbprint: "test-thumbprint",
|
|
}),
|
|
),
|
|
);
|
|
const bootstrapFails = yield* Ref.make(false);
|
|
const bootstrapCalls = yield* Ref.make(0);
|
|
const httpPaths: Array<string> = [];
|
|
const sessionAuthorizations: Array<string | null> = [];
|
|
const fetchFn = ((input, init) => {
|
|
const request = new Request(input, init);
|
|
const pathname = new URL(request.url).pathname;
|
|
httpPaths.push(pathname);
|
|
switch (pathname) {
|
|
case "/.well-known/t3/environment":
|
|
return Promise.resolve(
|
|
Response.json({
|
|
environmentId: TARGET.environmentId,
|
|
label: TARGET.label,
|
|
platform: { os: "linux", arch: "x64" },
|
|
serverVersion: "0.0.0-test",
|
|
capabilities: { repositoryIdentity: true },
|
|
}),
|
|
);
|
|
case "/oauth/token":
|
|
return Promise.resolve(
|
|
Response.json({
|
|
access_token: "access-token-2",
|
|
issued_token_type: "urn:ietf:params:oauth:token-type:access_token",
|
|
token_type: "DPoP",
|
|
expires_in: 3_600,
|
|
scope: AuthStandardClientScopes.join(" "),
|
|
}),
|
|
);
|
|
case "/api/auth/websocket-ticket":
|
|
return Promise.resolve(
|
|
Response.json({
|
|
ticket: "ws-ticket",
|
|
expiresAt: "2026-09-04T01:00:00.000Z",
|
|
}),
|
|
);
|
|
case "/api/auth/session": {
|
|
const authorization = request.headers.get("authorization");
|
|
sessionAuthorizations.push(authorization);
|
|
return Promise.resolve(
|
|
Response.json({
|
|
authenticated: authorization === "DPoP access-token-2",
|
|
auth: {
|
|
policy: "loopback-browser",
|
|
bootstrapMethods: ["one-time-token"],
|
|
sessionMethods: ["dpop-access-token"],
|
|
sessionCookieName: "t3_session_test",
|
|
},
|
|
scopes: AuthStandardClientScopes,
|
|
}),
|
|
);
|
|
}
|
|
default:
|
|
return Promise.reject(new Error(`Unexpected HTTP request to ${request.url}`));
|
|
}
|
|
}) satisfies typeof fetch;
|
|
const signer = ManagedRelay.ManagedRelayDpopSigner.of({
|
|
thumbprint: Effect.succeed("test-thumbprint"),
|
|
createProof: () => Effect.succeed("test-proof"),
|
|
});
|
|
const unused = () => Effect.die("Unexpected relay operation.");
|
|
const relay = ManagedRelay.ManagedRelayClient.of({
|
|
relayUrl: "https://relay.example.test",
|
|
listEnvironments: unused,
|
|
listDevices: unused,
|
|
createEnvironmentLinkChallenge: unused,
|
|
linkEnvironment: unused,
|
|
unlinkEnvironment: unused,
|
|
getEnvironmentStatus: unused,
|
|
connectEnvironment: Effect.fn("TestConnectionHttp.connectEnvironment")(function* () {
|
|
yield* Ref.update(bootstrapCalls, (count) => count + 1);
|
|
if (yield* Ref.get(bootstrapFails)) {
|
|
return yield* new ManagedRelay.ManagedRelayRequestTimeoutError({
|
|
activity: "Relay environment connection",
|
|
timeoutMs: 6_000,
|
|
traceId: null,
|
|
});
|
|
}
|
|
return {
|
|
environmentId: TARGET.environmentId,
|
|
endpoint,
|
|
credential: "relay-bootstrap",
|
|
expiresAt: "2026-09-04T01:00:00.000Z",
|
|
};
|
|
}),
|
|
registerDevice: unused,
|
|
unregisterDevice: unused,
|
|
registerLiveActivity: unused,
|
|
getAgentActivitySnapshot: unused,
|
|
resetTokenCache: Effect.void,
|
|
});
|
|
const layerHttp = RpcHttp.layerRemoteHttpClient(fetchFn);
|
|
const remoteAuthorization = yield* RemoteEnvironmentAuthorization.make.pipe(
|
|
Effect.provide(
|
|
Layer.mergeAll(
|
|
layerHttp,
|
|
Layer.succeed(ManagedRelay.ManagedRelayDpopSigner, signer),
|
|
Layer.succeed(ManagedRelay.ManagedRelayClient, relay),
|
|
Layer.succeed(ClientCapabilities.CloudSession, {
|
|
identity: Effect.succeedSome({ accountId: "test-account" }),
|
|
clerkToken: Effect.succeed("clerk-token"),
|
|
}),
|
|
Layer.succeed(ClientCapabilities.RelayDeviceIdentity, {
|
|
deviceId: Effect.succeedNone,
|
|
}),
|
|
TokenStore.layer({
|
|
get: () => Ref.get(token),
|
|
put: (value) => Ref.set(token, Option.some(value)),
|
|
remove: () => Ref.set(token, Option.none()),
|
|
}),
|
|
Layer.succeed(ClientCapabilities.ClientPresentation, {
|
|
metadata: { label: "Test client", deviceType: "desktop" },
|
|
}),
|
|
),
|
|
),
|
|
);
|
|
const harness = yield* makeHarness({
|
|
prepare: () =>
|
|
remoteAuthorization
|
|
.authorizeDpop({ expectedEnvironmentId: TARGET.environmentId })
|
|
.pipe(Effect.map((prepared) => ({ ...prepared, target: RELAY_TARGET }))),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
const session = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.session));
|
|
const prepared = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared));
|
|
const readSession = fetchEnvironmentSessionState({
|
|
prepared,
|
|
signer: Option.some(signer),
|
|
remoteAuthorization: Option.some(remoteAuthorization),
|
|
}).pipe(Effect.provide(layerHttp));
|
|
|
|
yield* TestClock.adjust("2 hours");
|
|
expect((yield* readSession).authenticated).toBe(true);
|
|
expect(sessionAuthorizations).toEqual(["DPoP access-token-2"]);
|
|
expect(yield* Ref.get(bootstrapCalls)).toBe(1);
|
|
expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.session))).toBe(session);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
|
|
expect(yield* SubscriptionRef.get(supervisor.state)).toMatchObject({
|
|
phase: "connected",
|
|
generation: 1,
|
|
});
|
|
|
|
yield* TestClock.adjust("2 hours");
|
|
yield* Ref.set(bootstrapFails, true);
|
|
const failure = yield* readSession.pipe(Effect.flip);
|
|
expect(failure._tag).toBe("RemoteEnvironmentAuthFetchError");
|
|
expect(yield* Ref.get(bootstrapCalls)).toBe(2);
|
|
expect(sessionAuthorizations).toEqual(["DPoP access-token-2"]);
|
|
expect(httpPaths.filter((path) => path === "/api/auth/websocket-ticket")).toHaveLength(1);
|
|
expect(httpPaths.filter((path) => path === "/oauth/token")).toHaveLength(1);
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
|
|
expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.session))).toBe(session);
|
|
expect(yield* SubscriptionRef.get(supervisor.state)).toMatchObject({
|
|
phase: "connected",
|
|
generation: 1,
|
|
});
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("interrupts relay setup when credentials change", () =>
|
|
Effect.gen(function* () {
|
|
const firstAttemptStarted = yield* Deferred.make<void>();
|
|
const harness = yield* makeHarness({
|
|
prepare: (attempt) =>
|
|
attempt === 1
|
|
? Deferred.succeed(firstAttemptStarted, undefined).pipe(Effect.andThen(Effect.never))
|
|
: Effect.succeed(PREPARED_CONNECTION),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* Deferred.await(firstAttemptStarted);
|
|
yield* harness.wake("credentials-changed");
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(2);
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
|
|
}),
|
|
);
|
|
|
|
it.effect("explicit disconnect releases the session and returns to available", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness();
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* supervisor.disconnect;
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "available");
|
|
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
|
|
expect(Option.isNone(yield* SubscriptionRef.get(supervisor.session))).toBe(true);
|
|
expect(Option.isNone(yield* SubscriptionRef.get(supervisor.prepared))).toBe(true);
|
|
}),
|
|
);
|
|
|
|
it.effect("does not lose an explicit disconnect among concurrent wakeup signals", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness();
|
|
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* Effect.all(
|
|
[
|
|
supervisor.disconnect,
|
|
harness.wake("credentials-changed"),
|
|
harness.wake("application-active"),
|
|
harness.wake("credentials-changed"),
|
|
],
|
|
{ concurrency: "unbounded" },
|
|
);
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "available");
|
|
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
|
|
expect(Option.isNone(yield* SubscriptionRef.get(supervisor.session))).toBe(true);
|
|
}),
|
|
);
|
|
});
|
|
|
|
describe("EnvironmentSupervisor routes", () => {
|
|
it.effect("skips a silent LAN route and connects over T3 Connect", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
checkRoute: (route) =>
|
|
Effect.succeed(route.target._tag === "BearerConnectionTarget" ? "silent" : "unchecked"),
|
|
prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
const prepared = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared));
|
|
expect(prepared.target._tag).toBe("RelayConnectionTarget");
|
|
// The silent route never reached the resolver.
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(1);
|
|
}),
|
|
);
|
|
|
|
it.effect("moves to a blocked route's fallback instead of stopping", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
checkRoute: () => Effect.succeed("answered"),
|
|
prepare: (_attempt, target) =>
|
|
target._tag === "BearerConnectionTarget"
|
|
? Effect.fail(blocked("The environment credential is invalid."))
|
|
: Effect.succeed(preparedFor(target)),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
const prepared = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared));
|
|
expect(prepared.target._tag).toBe("RelayConnectionTarget");
|
|
}),
|
|
);
|
|
|
|
it.effect("tries every route when none answers the reachability check", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
checkRoute: () => Effect.succeed("silent"),
|
|
prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
const prepared = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared));
|
|
expect(prepared.target._tag).toBe("BearerConnectionTarget");
|
|
}),
|
|
);
|
|
|
|
it.effect(
|
|
"gives the next route its own snapshot budget after a route fails while synchronizing",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
checkRoute: () => Effect.succeed("answered"),
|
|
prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
|
|
// The LAN session fails 50 seconds into its snapshot; T3 Connect needs 40.
|
|
ready: (attempt) =>
|
|
attempt === 1
|
|
? Effect.sleep("50 seconds").pipe(Effect.andThen(Effect.fail(transient())))
|
|
: Effect.sleep("40 seconds"),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connecting" && state.stage === "synchronizing",
|
|
);
|
|
yield* TestClock.adjust("50 seconds");
|
|
yield* TestClock.adjust("40 seconds");
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared)).target._tag).toBe(
|
|
"RelayConnectionTarget",
|
|
);
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(2);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
|
|
}).pipe(Effect.provide(TestClock.layer())),
|
|
);
|
|
|
|
it.effect("still tries a silent LAN route after the T3 Connect route fails", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
checkRoute: (route) =>
|
|
Effect.succeed(route.target._tag === "BearerConnectionTarget" ? "silent" : "unchecked"),
|
|
// Signed out of T3 Connect; the LAN is up but its check timed out.
|
|
prepare: (_attempt, target) =>
|
|
target._tag === "RelayConnectionTarget"
|
|
? Effect.fail(blocked("Sign in to T3 Connect."))
|
|
: Effect.succeed(preparedFor(target)),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared)).target._tag).toBe(
|
|
"BearerConnectionTarget",
|
|
);
|
|
}),
|
|
);
|
|
|
|
it.effect("keeps retrying when one route is blocked and another failed transiently", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
checkRoute: () => Effect.succeed("answered"),
|
|
prepare: (_attempt, target) =>
|
|
target._tag === "RelayConnectionTarget"
|
|
? Effect.fail(blocked("Sign in to T3 Connect."))
|
|
: Effect.fail(transient("LAN socket refused.")),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
// A blocked state would stop retrying; the LAN may come back.
|
|
const state = yield* awaitState(supervisor.state, (value) => value.phase === "backoff");
|
|
expect(state.lastFailure?._tag).toBe("ConnectionTransientError");
|
|
}),
|
|
);
|
|
|
|
it.effect("moves back to the LAN route when the network changes and it answers again", () =>
|
|
Effect.gen(function* () {
|
|
const lanReachable = yield* Ref.make(false);
|
|
const harness = yield* makeHarness({
|
|
checkRoute: (route) =>
|
|
route.target._tag === "BearerConnectionTarget"
|
|
? Ref.get(lanReachable).pipe(
|
|
Effect.map((reachable) => (reachable ? "answered" : "silent")),
|
|
)
|
|
: Effect.succeed("unchecked"),
|
|
prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 1,
|
|
);
|
|
expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared)).target._tag).toBe(
|
|
"RelayConnectionTarget",
|
|
);
|
|
|
|
// Back home: the LAN answers, and a network change asks for a better route.
|
|
yield* Ref.set(lanReachable, true);
|
|
yield* harness.setNetworkStatus("unknown");
|
|
yield* harness.setNetworkStatus("online");
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared)).target._tag).toBe(
|
|
"BearerConnectionTarget",
|
|
);
|
|
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
|
|
}),
|
|
);
|
|
|
|
it.effect("checks for a better route periodically while on a fallback", () =>
|
|
Effect.gen(function* () {
|
|
const lanReachable = yield* Ref.make(false);
|
|
const harness = yield* makeHarness({
|
|
checkRoute: (route) =>
|
|
route.target._tag === "BearerConnectionTarget"
|
|
? Ref.get(lanReachable).pipe(
|
|
Effect.map((reachable) => (reachable ? "answered" : "silent")),
|
|
)
|
|
: Effect.succeed("unchecked"),
|
|
prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 1,
|
|
);
|
|
yield* Ref.set(lanReachable, true);
|
|
yield* TestClock.adjust("60 seconds");
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared)).target._tag).toBe(
|
|
"BearerConnectionTarget",
|
|
);
|
|
}),
|
|
);
|
|
|
|
it.effect("does not retry a better route that answered but failed to connect", () =>
|
|
Effect.gen(function* () {
|
|
const lanAnswers = yield* Ref.make(false);
|
|
const harness = yield* makeHarness({
|
|
checkRoute: (route) =>
|
|
route.target._tag === "BearerConnectionTarget"
|
|
? Ref.get(lanAnswers).pipe(Effect.map((answers) => (answers ? "answered" : "silent")))
|
|
: Effect.succeed("unchecked"),
|
|
// The LAN answers its check but the socket never opens.
|
|
prepare: (_attempt, target) =>
|
|
target._tag === "BearerConnectionTarget"
|
|
? Effect.fail(transient("Socket refused."))
|
|
: Effect.succeed(preparedFor(target)),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 1,
|
|
);
|
|
yield* Ref.set(lanAnswers, true);
|
|
yield* TestClock.adjust("60 seconds");
|
|
// The switch lands back on T3 Connect.
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
const preparesAfterSwitch = yield* Ref.get(harness.prepareCount);
|
|
|
|
// The failed LAN is cooling down, so the next periodic check leaves the
|
|
// session alone.
|
|
yield* TestClock.adjust("60 seconds");
|
|
yield* Effect.yieldNow;
|
|
expect(yield* Ref.get(harness.prepareCount)).toBe(preparesAfterSwitch);
|
|
expect(yield* SubscriptionRef.get(supervisor.state)).toMatchObject({
|
|
phase: "connected",
|
|
generation: 2,
|
|
});
|
|
}),
|
|
);
|
|
|
|
it.effect("stays on the preferred route without checking other routes", () =>
|
|
Effect.gen(function* () {
|
|
const checks = yield* Ref.make(0);
|
|
const harness = yield* makeHarness({
|
|
checkRoute: () => Ref.update(checks, (count) => count + 1).pipe(Effect.as("answered")),
|
|
prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
const checksAtConnect = yield* Ref.get(checks);
|
|
yield* harness.setNetworkStatus("unknown");
|
|
yield* harness.setNetworkStatus("online");
|
|
yield* TestClock.adjust("5 minutes");
|
|
yield* Effect.yieldNow;
|
|
|
|
expect(yield* Ref.get(checks)).toBe(checksAtConnect);
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
|
|
}),
|
|
);
|
|
|
|
it.effect("moves off a dead LAN socket when the phone switches to cellular", () =>
|
|
Effect.gen(function* () {
|
|
const lanReachable = yield* Ref.make(true);
|
|
const harness = yield* makeHarness({
|
|
checkRoute: (route) =>
|
|
route.target._tag === "BearerConnectionTarget"
|
|
? Ref.get(lanReachable).pipe(
|
|
Effect.map((reachable) => (reachable ? "answered" : "silent")),
|
|
)
|
|
: Effect.succeed("unchecked"),
|
|
prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
|
|
// The LAN socket never answers once the phone has left the network.
|
|
probe: () =>
|
|
Ref.get(lanReachable).pipe(
|
|
Effect.flatMap((reachable) => (reachable ? Effect.void : Effect.never)),
|
|
),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 1,
|
|
);
|
|
expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared)).target._tag).toBe(
|
|
"BearerConnectionTarget",
|
|
);
|
|
|
|
yield* Ref.set(lanReachable, false);
|
|
yield* harness.wake("network-changed");
|
|
yield* TestClock.adjust("3 seconds");
|
|
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared)).target._tag).toBe(
|
|
"RelayConnectionTarget",
|
|
);
|
|
}),
|
|
);
|
|
|
|
it.effect("keeps a LAN session when the T3 Connect account changes", () =>
|
|
Effect.gen(function* () {
|
|
const harness = yield* makeHarness({
|
|
checkRoute: () => Effect.succeed("answered"),
|
|
prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
|
|
});
|
|
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
|
|
initiallyDesired: true,
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
|
yield* harness.wake("credentials-changed");
|
|
yield* Effect.yieldNow;
|
|
|
|
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
|
|
expect(yield* SubscriptionRef.get(supervisor.state)).toMatchObject({
|
|
phase: "connected",
|
|
generation: 1,
|
|
});
|
|
}),
|
|
);
|
|
|
|
it.effect("learns the LAN address over T3 Connect and moves to it", () =>
|
|
Effect.gen(function* () {
|
|
const relayEntry: ConnectionCatalogEntry = {
|
|
target: RELAY_TARGET,
|
|
profile: Option.none(),
|
|
enabled: true,
|
|
};
|
|
const lanAddress = yield* Ref.make("http://192.168.1.10:3773/");
|
|
const learned = yield* Ref.make<ReadonlyArray<string>>([]);
|
|
const harness = yield* makeHarness({
|
|
checkRoute: (route) =>
|
|
route.target._tag === "BearerConnectionTarget"
|
|
? Effect.succeed("answered")
|
|
: Effect.succeed("unchecked"),
|
|
prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
|
|
// Only the reported endpoints matter to the supervisor.
|
|
initialConfig: () =>
|
|
Ref.get(lanAddress).pipe(
|
|
Effect.map((httpBaseUrl): Pick<ServerConfig, "directEndpoints"> => ({
|
|
directEndpoints: [{ kind: "lan", httpBaseUrl }],
|
|
})),
|
|
Effect.map((config) => config as ServerConfig),
|
|
),
|
|
});
|
|
const current = yield* Ref.make(relayEntry);
|
|
const supervisor = yield* EnvironmentSupervisor.make(relayEntry, {
|
|
initiallyDesired: true,
|
|
learnRoutes: ({ activeRoute, reported }) =>
|
|
Effect.gen(function* () {
|
|
const entry = yield* Ref.get(current);
|
|
const routes = mergeLearnedRoutes({
|
|
entry,
|
|
activeRoute,
|
|
reported,
|
|
allowInsecure: true,
|
|
});
|
|
if (routes === null) return Option.none();
|
|
const next = entryWithRoutes(entry, routes);
|
|
yield* Ref.set(current, next);
|
|
yield* Ref.update(learned, (all) => [
|
|
...all,
|
|
...routes.filter(isLearned).map((route) => connectionRouteId(route.target)),
|
|
]);
|
|
return Option.some(next);
|
|
}),
|
|
}).pipe(Effect.provide(harness.dependencies));
|
|
|
|
// Connected over T3 Connect, the server reports its LAN address; the
|
|
// learned route ranks first, answers, and the session moves to it.
|
|
yield* awaitState(
|
|
supervisor.state,
|
|
(state) => state.phase === "connected" && state.generation === 2,
|
|
);
|
|
expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared)).target._tag).toBe(
|
|
"BearerConnectionTarget",
|
|
);
|
|
expect(yield* Ref.get(learned)).toEqual([
|
|
`learned:${TARGET.environmentId}:http://192.168.1.10:3773`,
|
|
]);
|
|
}),
|
|
);
|
|
});
|