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 * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
import * as NodeServices from "@effect/platform-node/NodeServices";
import {
DEFAULT_SERVER_SETTINGS,
KeybindingRule,
type PreviewEvent,
ProviderDriverKind,
ProviderInstanceId,
ResolvedKeybindingRule,
WS_METHODS,
} from "@akeru/contracts";
import { assert, it } from "@effect/vitest";
import { assertTrue } from "@effect/vitest/utils";
import * as Deferred from "effect/Deferred";
import * as Exit from "effect/Exit";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Stream from "effect/Stream";
import * as TestClock from "effect/testing/TestClock";
import { resolveAvailableEditorsForConfig, resolveFileManagerRevealKindForConfig } from "./ws.ts";
import * as PreviewManager from "./preview/Manager.ts";
import { buildAppUnderTest } from "./serverTestApp.ts";
import { getWsServerUrl, withWsRpcClient } from "./serverTestClients.ts";
import { testEnvironmentDescriptor } from "./serverTestFixtures.ts";
it.layer(NodeServices.layer)("server router seam", (it) => {
it.effect("does not block server config when editor discovery never resolves", () =>
Effect.gen(function* () {
const discoveryInterrupted = yield* Deferred.make<void>();
const responseFiber = yield* resolveAvailableEditorsForConfig(
Effect.never.pipe(
Effect.onInterrupt(() => Deferred.succeed(discoveryInterrupted, undefined)),
),
).pipe(Effect.forkChild);
yield* TestClock.adjust(Duration.seconds(5));
const availableEditors = yield* Fiber.join(responseFiber);
yield* Deferred.await(discoveryInterrupted);
assert.deepEqual(availableEditors, []);
}),
);
it.effect("does not block server config when file manager reveal discovery never resolves", () =>
Effect.gen(function* () {
const discoveryInterrupted = yield* Deferred.make<void>();
const responseFiber = yield* resolveFileManagerRevealKindForConfig(
Effect.never.pipe(
Effect.onInterrupt(() => Deferred.succeed(discoveryInterrupted, undefined)),
),
).pipe(Effect.forkChild);
yield* TestClock.adjust(Duration.seconds(5));
const revealKind = yield* Fiber.join(responseFiber);
yield* Deferred.await(discoveryInterrupted);
assert.isUndefined(revealKind);
}),
);
it.effect("routes websocket rpc server.upsertKeybinding", () =>
Effect.gen(function* () {
const rule: KeybindingRule = {
command: "terminal.toggle",
key: "ctrl+k",
};
const resolved: ResolvedKeybindingRule = {
command: "terminal.toggle",
shortcut: {
key: "k",
metaKey: false,
ctrlKey: true,
shiftKey: false,
altKey: false,
modKey: true,
},
};
yield* buildAppUnderTest({
layers: {
keybindings: {
upsertKeybindingRule: () => Effect.succeed([resolved]),
},
},
});
const wsUrl = yield* getWsServerUrl("/ws");
const response = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) => client[WS_METHODS.serverUpsertKeybinding](rule)),
);
assert.deepEqual(response.issues, []);
assert.deepEqual(response.keybindings, [resolved]);
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("routes websocket rpc server.removeKeybinding", () =>
Effect.gen(function* () {
const rule: KeybindingRule = {
command: "terminal.toggle",
key: "ctrl+k",
};
const resolved: ResolvedKeybindingRule = {
command: "terminal.toggle",
shortcut: {
key: "j",
metaKey: false,
ctrlKey: false,
shiftKey: false,
altKey: false,
modKey: true,
},
};
yield* buildAppUnderTest({
layers: {
keybindings: {
removeKeybindingRule: () => Effect.succeed([resolved]),
},
},
});
const wsUrl = yield* getWsServerUrl("/ws");
const response = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) => client[WS_METHODS.serverRemoveKeybinding](rule)),
);
assert.deepEqual(response.issues, []);
assert.deepEqual(response.keybindings, [resolved]);
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("routes websocket rpc subscribeServerConfig streams snapshot then update", () =>
Effect.gen(function* () {
const path = yield* Path.Path;
const providers = [
{
instanceId: ProviderInstanceId.make("codex"),
driver: ProviderDriverKind.make("codex"),
enabled: true,
installed: true,
version: "1.0.0",
status: "ready" as const,
auth: { status: "authenticated" as const },
checkedAt: "2026-04-11T00:00:00.000Z",
models: [],
slashCommands: [],
skills: [],
},
] as const;
const changeEvent = {
keybindings: [],
issues: [],
} as const;
yield* buildAppUnderTest({
config: {
otlpTracesUrl: "http://localhost:4318/v1/traces",
otlpMetricsUrl: "http://localhost:4318/v1/metrics",
},
layers: {
keybindings: {
loadConfigState: Effect.succeed({
keybindings: [],
issues: [],
}),
streamChanges: Stream.succeed(changeEvent),
},
providerRegistry: {
getProviders: Effect.succeed(providers),
},
},
});
const wsUrl = yield* getWsServerUrl("/ws");
const events = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[WS_METHODS.subscribeServerConfig]({}).pipe(Stream.take(2), Stream.runCollect),
),
);
const [first, second] = Array.from(events);
assert.equal(first?.type, "snapshot");
if (first?.type === "snapshot") {
assert.equal(first.version, 1);
assert.deepEqual(first.config.keybindings, []);
assert.deepEqual(first.config.issues, []);
assert.deepEqual(first.config.providers, providers);
assert.equal(path.basename(first.config.observability.logsDirectoryPath), "logs");
assert.equal(first.config.observability.localTracingEnabled, true);
assert.equal(first.config.observability.otlpTracesUrl, "http://localhost:4318/v1/traces");
assert.equal(first.config.observability.otlpTracesEnabled, true);
assert.equal(first.config.observability.otlpMetricsUrl, "http://localhost:4318/v1/metrics");
assert.equal(first.config.observability.otlpMetricsEnabled, true);
assert.deepEqual(first.config.settings, DEFAULT_SERVER_SETTINGS);
}
assert.deepEqual(second, {
version: 1,
type: "keybindingsUpdated",
payload: { keybindings: [], issues: [] },
});
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("routes websocket resource telemetry through the subscription", () =>
Effect.gen(function* () {
yield* buildAppUnderTest();
const wsUrl = yield* getWsServerUrl("/ws");
const snapshot = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[WS_METHODS.subscribeResourceTelemetry]({}).pipe(Stream.runHead),
),
);
assertTrue(Option.isSome(snapshot));
assert.equal(snapshot.value.processes.length, 0);
assert.equal(snapshot.value.groups.backend.processCount, 0);
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("routes websocket rpc subscribeServerConfig emits provider status updates", () =>
Effect.gen(function* () {
const nextProviders = [
{
instanceId: ProviderInstanceId.make("codex"),
driver: ProviderDriverKind.make("codex"),
enabled: true,
installed: true,
version: "1.0.0",
status: "ready" as const,
auth: { status: "authenticated" as const },
checkedAt: "2026-04-11T00:00:00.000Z",
models: [],
slashCommands: [],
skills: [],
},
] as const;
yield* buildAppUnderTest({
layers: {
keybindings: {
loadConfigState: Effect.succeed({
keybindings: [],
issues: [],
}),
streamChanges: Stream.empty,
},
providerRegistry: {
getProviders: Effect.succeed([]),
streamChanges: Stream.succeed(nextProviders),
},
},
});
const wsUrl = yield* getWsServerUrl("/ws");
const events = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[WS_METHODS.subscribeServerConfig]({}).pipe(Stream.take(2), Stream.runCollect),
),
);
const [first, second] = Array.from(events);
assert.equal(first?.type, "snapshot");
if (first?.type === "snapshot") {
assert.deepEqual(first.config.providers, []);
}
assert.deepEqual(second, {
version: 1,
type: "providerStatuses",
payload: { providers: nextProviders },
});
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("subscribePreviewEvents forwards the chat scope to the preview manager", () =>
Effect.gen(function* () {
const previewEvent = (threadId: string, tabId: string): PreviewEvent =>
({
type: "closed",
threadId,
tabId,
createdAt: "2026-01-01T00:00:00.000Z",
serverEpoch: "test-server",
revision: 1,
}) as PreviewEvent;
const published = [
previewEvent("thread-preview-b", "tab-1"),
previewEvent("thread-preview-a", "tab-2"),
previewEvent("thread-preview-a", "tab-3"),
];
const requestedScopes: Array<string | undefined> = [];
yield* buildAppUnderTest({
layers: {
previewManager: {
streamEvents: (input) => {
requestedScopes.push(input.threadId);
return Stream.fromIterable(published).pipe(
Stream.filter(PreviewManager.previewEventMatchesSubscription(input)),
);
},
},
},
});
const wsUrl = yield* getWsServerUrl("/ws");
const events = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[WS_METHODS.subscribePreviewEvents]({ threadId: "thread-preview-a" }).pipe(
Stream.take(2),
Stream.runCollect,
),
),
);
assert.deepEqual(requestedScopes, ["thread-preview-a"]);
assert.deepEqual(
Array.from(events, (event) => `${event.threadId}:${event.tabId}`),
["thread-preview-a:tab-2", "thread-preview-a:tab-3"],
);
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("subscribeServerConfig probes a stale available provider once across clients", () =>
Effect.gen(function* () {
const instanceId = ProviderInstanceId.make("codex");
const staleProvider = {
instanceId,
driver: ProviderDriverKind.make("codex"),
enabled: true,
installed: true,
version: "1.0.0",
status: "ready" as const,
auth: { status: "authenticated" as const },
checkedAt: "2020-01-01T00:00:00.000Z",
models: [],
slashCommands: [],
skills: [],
};
// An instance whose driver is missing has nothing to probe.
const unavailableProvider = {
...staleProvider,
instanceId: ProviderInstanceId.make("missing_driver"),
availability: "unavailable" as const,
};
const releaseProbe = yield* Deferred.make<void>();
const probeSucceeded = yield* Deferred.make<boolean>();
const probedInstanceIds: Array<string> = [];
yield* buildAppUnderTest({
layers: {
providerRegistry: {
getProviders: Effect.succeed([staleProvider, unavailableProvider]),
refreshInstance: (id) =>
Effect.sync(() => probedInstanceIds.push(id)).pipe(
Effect.andThen(Deferred.await(releaseProbe)),
Effect.as([staleProvider]),
Effect.onExit((exit) => Deferred.succeed(probeSucceeded, Exit.isSuccess(exit))),
),
},
},
});
const wsUrl = yield* getWsServerUrl("/ws");
const takeSnapshot = withWsRpcClient(wsUrl, (client) =>
client[WS_METHODS.subscribeServerConfig]({}).pipe(Stream.take(1), Stream.runCollect),
);
// Each snapshot is sent after that subscription decided whether to
// probe, and the first probe stays blocked until both have arrived.
yield* Effect.scoped(Effect.all([takeSnapshot, takeSnapshot], { concurrency: "unbounded" }));
// Both clients have disconnected. The shared probe keeps running, so a
// later client still skips it and the probe completes.
yield* Effect.scoped(takeSnapshot);
assert.deepEqual(probedInstanceIds, [instanceId]);
yield* Deferred.succeed(releaseProbe, undefined);
assert.isTrue(yield* Deferred.await(probeSucceeded));
}).pipe(Effect.provide(NodeHttpServer.layerTest), TestClock.withLive),
);
it.effect(
"routes websocket rpc subscribeServerLifecycle replays snapshot and streams updates",
() =>
Effect.gen(function* () {
const lifecycleEvents = [
{
version: 1 as const,
sequence: 1,
type: "welcome" as const,
payload: {
environment: testEnvironmentDescriptor,
cwd: "/tmp/project",
projectName: "project",
},
},
] as const;
const liveEvents = Stream.make({
version: 1 as const,
sequence: 2,
type: "ready" as const,
payload: { at: "2026-01-01T00:00:00.000Z", environment: testEnvironmentDescriptor },
});
yield* buildAppUnderTest({
layers: {
serverLifecycleEvents: {
snapshot: Effect.succeed({
sequence: 1,
events: lifecycleEvents,
}),
stream: liveEvents,
},
},
});
const wsUrl = yield* getWsServerUrl("/ws");
const events = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[WS_METHODS.subscribeServerLifecycle]({}).pipe(Stream.take(2), Stream.runCollect),
),
);
const [first, second] = Array.from(events);
assert.equal(first?.type, "welcome");
assert.equal(first?.sequence, 1);
assert.equal(second?.type, "ready");
assert.equal(second?.sequence, 2);
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
});