Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { describe, expect, it } from "@effect/vitest";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Option from "effect/Option";
import * as Ref from "effect/Ref";
import * as Stream from "effect/Stream";
import * as SubscriptionRef from "effect/SubscriptionRef";
import { ConnectionTransientError } from "./model.ts";
import * as Persistence from "../platform/persistence.ts";
import * as EnvironmentRegistry from "./registry.ts";
import {
TARGET,
SECOND_TARGET,
BEARER_TARGET,
BEARER_PROFILE,
CACHED_SNAPSHOT,
makeHarness,
awaitConnectionState,
} from "./registry.test-support.ts";
describe("EnvironmentRegistry", () => {
it.effect("publishes network status changes independently of connection state", () =>
Effect.gen(function* () {
const harness = yield* makeHarness([]);
yield* Effect.gen(function* () {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
const offline = yield* Effect.forkChild(
SubscriptionRef.changes(registry.networkStatus).pipe(
Stream.filter((status) => status === "offline"),
Stream.runHead,
Effect.map(Option.getOrThrow),
),
);
yield* SubscriptionRef.set(harness.networkStatus, "offline");
expect(yield* Fiber.join(offline)).toBe("offline");
expect(yield* SubscriptionRef.get(registry.networkStatus)).toBe("offline");
}).pipe(Effect.provide(harness.layer), Effect.scoped);
}),
);
it.effect("starts persisted environments independently", () =>
Effect.gen(function* () {
const bothLoadsStarted = yield* Deferred.make<void>();
const releaseLoads = yield* Deferred.make<void>();
const loadCount = yield* Ref.make(0);
const harness = yield* makeHarness([TARGET, SECOND_TARGET], [], [], {
beforeSessionConnect: () =>
Ref.updateAndGet(loadCount, (count) => count + 1).pipe(
Effect.tap((count) =>
count === 2 ? Deferred.succeed(bothLoadsStarted, undefined) : Effect.void,
),
Effect.andThen(Deferred.await(releaseLoads)),
),
});
yield* Effect.gen(function* () {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
const start = yield* Effect.forkChild(registry.start);
yield* Deferred.await(bothLoadsStarted).pipe(Effect.timeout("1 second"));
yield* Deferred.succeed(releaseLoads, undefined);
yield* Fiber.join(start);
expect(yield* Ref.get(loadCount)).toBe(2);
}).pipe(Effect.provide(harness.layer), Effect.scoped);
}),
);
it.effect("preserves cached data on connection failure and clears it on explicit removal", () =>
Effect.gen(function* () {
const harness = yield* makeHarness([TARGET]);
yield* Effect.gen(function* () {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
yield* registry.start;
yield* awaitConnectionState(
registry,
TARGET.environmentId,
(state) => state.phase === "connected",
);
const controls = yield* Ref.get(harness.sessions);
expect(controls).toHaveLength(1);
const active = controls[0];
expect(active).toBeDefined();
expect((yield* Ref.get(harness.shellCache)).get(TARGET.environmentId)).toEqual(
CACHED_SNAPSHOT,
);
const retryFiber = yield* Effect.forkChild(
awaitConnectionState(
registry,
TARGET.environmentId,
(state) => state.phase === "backoff",
),
);
yield* Effect.yieldNow;
yield* Deferred.fail(
active!.closed,
new ConnectionTransientError({
reason: "transport",
detail: "Disconnected.",
}),
);
yield* Fiber.join(retryFiber);
expect((yield* Ref.get(harness.shellCache)).get(TARGET.environmentId)).toEqual(
CACHED_SNAPSHOT,
);
yield* registry.remove(TARGET.environmentId);
expect((yield* Ref.get(harness.storedTargets)).has(TARGET.environmentId)).toBe(false);
expect((yield* Ref.get(harness.shellCache)).has(TARGET.environmentId)).toBe(false);
expect(yield* Ref.get(harness.cacheClears)).toEqual([TARGET.environmentId]);
expect((yield* SubscriptionRef.get(registry.entries)).has(TARGET.environmentId)).toBe(
false,
);
}).pipe(Effect.provide(harness.layer));
}),
);
it.effect("keeps the runtime registered when durable removal fails", () =>
Effect.gen(function* () {
const harness = yield* makeHarness([BEARER_TARGET], [BEARER_PROFILE], [], {
beforeRegistrationRemove: () =>
Effect.fail(
new Persistence.ConnectionPersistenceError({
operation: "remove-connection",
message: "Storage is unavailable.",
}),
),
});
yield* Effect.gen(function* () {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
yield* registry.start;
yield* awaitConnectionState(
registry,
BEARER_TARGET.environmentId,
(state) => state.phase === "connected",
);
const error = yield* Effect.flip(registry.remove(BEARER_TARGET.environmentId));
expect(error._tag).toBe("ConnectionPersistenceError");
expect(yield* Ref.get(harness.releasedSessions)).toBe(0);
expect(
(yield* SubscriptionRef.get(registry.entries)).has(BEARER_TARGET.environmentId),
).toBe(true);
expect((yield* Ref.get(harness.storedTargets)).has(BEARER_TARGET.environmentId)).toBe(true);
expect(yield* Ref.get(harness.cacheClears)).toEqual([]);
expect(yield* Ref.get(harness.ownedDataClears)).toEqual([]);
}).pipe(Effect.provide(harness.layer), Effect.scoped);
}),
);
it.effect("does not reacquire a runtime while its registration is being removed", () =>
Effect.gen(function* () {
const removalStarted = yield* Deferred.make<void>();
const continueRemoval = yield* Deferred.make<void>();
const harness = yield* makeHarness([TARGET], [], [], {
beforeRegistrationRemove: () =>
Deferred.succeed(removalStarted, undefined).pipe(
Effect.andThen(Deferred.await(continueRemoval)),
),
});
yield* Effect.gen(function* () {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
yield* registry.start;
yield* awaitConnectionState(
registry,
TARGET.environmentId,
(state) => state.phase === "connected",
);
const removal = yield* Effect.forkChild(registry.remove(TARGET.environmentId));
yield* Deferred.await(removalStarted);
const stateLookup = yield* Effect.forkChild(
Effect.flip(registry.state(TARGET.environmentId)),
);
yield* Effect.yieldNow;
expect(yield* Ref.get(harness.sessions)).toHaveLength(1);
yield* Deferred.succeed(continueRemoval, undefined);
yield* Fiber.join(removal);
const error = yield* Fiber.join(stateLookup);
expect(error._tag).toBe("EnvironmentNotRegisteredError");
}).pipe(Effect.provide(harness.layer), Effect.scoped);
}),
);
});