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 { BearerConnectionTarget } from "./model.ts";
import * as EnvironmentRegistry from "./registry.ts";
import * as EnvironmentSupervisor from "./supervisor.ts";
import {
BEARER_TARGET,
BEARER_PROFILE,
bearerRegistration,
SSH_CONNECTION,
SSH_PROFILE,
makeHarness,
awaitConnectionState,
} from "./registry.test-support.ts";
describe("EnvironmentRegistry", () => {
it.effect("hydrates connection profiles into catalog entries", () =>
Effect.gen(function* () {
const harness = yield* makeHarness([SSH_CONNECTION], [SSH_PROFILE]);
yield* Effect.gen(function* () {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
const entry = (yield* SubscriptionRef.get(registry.entries)).get(
SSH_CONNECTION.environmentId,
);
expect(entry?.target).toEqual(SSH_CONNECTION);
expect(Option.getOrThrow(entry?.profile ?? Option.none())).toEqual(SSH_PROFILE);
}).pipe(Effect.provide(harness.layer), Effect.scoped);
}),
);
it.effect("persists and starts a newly registered environment", () =>
Effect.gen(function* () {
const harness = yield* makeHarness([]);
yield* Effect.gen(function* () {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
yield* registry.register(bearerRegistration());
yield* awaitConnectionState(
registry,
BEARER_TARGET.environmentId,
(state) => state.phase === "connected",
);
expect((yield* Ref.get(harness.storedTargets)).get(BEARER_TARGET.environmentId)).toEqual(
BEARER_TARGET,
);
expect(yield* Ref.get(harness.sessions)).toHaveLength(1);
}).pipe(Effect.provide(harness.layer));
}),
);
it.effect("moves durable streams to a replacement supervisor", () =>
Effect.gen(function* () {
const replacement = new BearerConnectionTarget({
environmentId: BEARER_TARGET.environmentId,
label: "Replacement bearer environment",
connectionId: "replacement-bearer-connection",
});
const harness = yield* makeHarness([BEARER_TARGET], [BEARER_PROFILE]);
yield* Effect.gen(function* () {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
const firstObserved = yield* Deferred.make<void>();
const secondObserved = yield* Deferred.make<void>();
const labels = yield* Ref.make<ReadonlyArray<string>>([]);
yield* registry.start;
yield* awaitConnectionState(
registry,
BEARER_TARGET.environmentId,
(state) => state.phase === "connected",
);
const subscription = yield* Effect.forkChild(
registry
.followStream(
BEARER_TARGET.environmentId,
Stream.unwrap(
EnvironmentSupervisor.EnvironmentSupervisor.pipe(
Effect.map((supervisor) =>
Stream.concat(Stream.succeed(supervisor.target.label), Stream.never),
),
),
),
)
.pipe(
Stream.tap((label) =>
Ref.updateAndGet(labels, (current) => [...current, label]).pipe(
Effect.flatMap((current) =>
current.length === 1
? Deferred.succeed(firstObserved, undefined)
: Deferred.succeed(secondObserved, undefined),
),
),
),
Stream.runDrain,
),
);
yield* Deferred.await(firstObserved).pipe(Effect.timeout("1 second"));
yield* registry.register(bearerRegistration(replacement));
yield* Deferred.await(secondObserved).pipe(Effect.timeout("1 second"));
yield* Fiber.interrupt(subscription);
expect(yield* Ref.get(labels)).toEqual([BEARER_TARGET.label, replacement.label]);
}).pipe(Effect.provide(harness.layer), Effect.scoped);
}),
);
});