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 { testRpcClient } from "../test-support/services.ts";
import {
BotId,
CommandId,
DelegationId,
EnvironmentId,
GroupId,
McpServerId,
MessageId,
ORCHESTRATION_WS_METHODS,
ProjectId,
ThreadId,
type ClientOrchestrationCommand,
} from "@akeru/contracts";
import { describe, expect, it } from "@effect/vitest";
import * as Crypto from "effect/Crypto";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as SubscriptionRef from "effect/SubscriptionRef";
import {
AVAILABLE_CONNECTION_STATE,
PrimaryConnectionTarget,
type PreparedConnection,
} from "../connection/model.ts";
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
import * as RpcSession from "../rpc/session.ts";
import {
archiveThread,
assignGroupMember,
cancelDelegation,
connectChannel,
createMcpServer,
createProject,
detachChannel,
disconnectChannel,
reconnectChannel,
changeChannelProject,
resumeThreadTurn,
sendChannelMessage,
setThreadMessageReaction,
settleThread,
setGroupBoss,
startThreadTurn,
stopThreadSession,
unassignGroupMember,
unsettleThread,
updateMcpServer,
} from "./commands.ts";
const TEST_CRYPTO_LAYER = Layer.succeed(
Crypto.Crypto,
Crypto.make({
randomBytes: (size) => new Uint8Array(size),
digest: (_algorithm, data) => Effect.succeed(data),
}),
);
const TARGET = new PrimaryConnectionTarget({
environmentId: EnvironmentId.make("environment-1"),
label: "Test environment",
httpBaseUrl: "https://environment.example.test",
wsBaseUrl: "wss://environment.example.test",
});
const makeSupervisor = Effect.fn("TestEnvironmentCommands.makeSupervisor")(function* (
dispatched: ClientOrchestrationCommand[],
) {
const client = testRpcClient({
[ORCHESTRATION_WS_METHODS.dispatchCommand]: (command: ClientOrchestrationCommand) =>
Effect.sync(() => {
dispatched.push(command);
return { sequence: dispatched.length };
}),
});
const session: RpcSession.RpcSession = {
client,
initialConfig: Effect.never,
ready: Effect.void,
probe: Effect.void,
closed: Effect.never,
};
return EnvironmentSupervisor.EnvironmentSupervisor.of({
target: TARGET,
state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
session: yield* SubscriptionRef.make(Option.some(session)),
prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
connect: Effect.void,
disconnect: Effect.void,
retryNow: Effect.void,
retryIfDesired: Effect.void,
} satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
});
describe("environment commands", () => {
it.effect("adds generated command metadata", () =>
Effect.gen(function* () {
const dispatched: ClientOrchestrationCommand[] = [];
const supervisor = yield* makeSupervisor(dispatched);
const result = yield* createProject({
projectId: ProjectId.make("project-1"),
title: "Project",
workspaceRoot: "/workspace/project",
createdAt: "2026-06-06T00:00:00.000Z",
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(result).toEqual({ sequence: 1 });
expect(dispatched).toEqual([
{
type: "project.create",
commandId: "00000000-0000-4000-8000-000000000000",
projectId: "project-1",
title: "Project",
workspaceRoot: "/workspace/project",
createdAt: "2026-06-06T00:00:00.000Z",
},
]);
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("dispatches both MCP transport configurations", () =>
Effect.gen(function* () {
const dispatched: ClientOrchestrationCommand[] = [];
const supervisor = yield* makeSupervisor(dispatched);
const mcpServerId = McpServerId.make("mcp-server-1");
yield* createMcpServer({
commandId: CommandId.make("mcp-create"),
mcpServerId,
name: "Filesystem",
transport: "stdio",
command: "bunx",
args: ["server-filesystem"],
createdAt: "2026-06-06T00:00:00.000Z",
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
yield* updateMcpServer({
commandId: CommandId.make("mcp-update"),
mcpServerId,
name: "Remote",
transport: "url",
url: "https://mcp.example.com",
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(dispatched).toEqual([
{
type: "mcp-server.create",
commandId: "mcp-create",
mcpServerId: "mcp-server-1",
name: "Filesystem",
transport: "stdio",
command: "bunx",
args: ["server-filesystem"],
createdAt: "2026-06-06T00:00:00.000Z",
},
{
type: "mcp-server.update",
commandId: "mcp-update",
mcpServerId: "mcp-server-1",
name: "Remote",
transport: "url",
url: "https://mcp.example.com",
},
]);
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("dispatches reversible group membership commands", () =>
Effect.gen(function* () {
const dispatched: ClientOrchestrationCommand[] = [];
const supervisor = yield* makeSupervisor(dispatched);
const groupId = GroupId.make("group-1");
const botId = BotId.make("bot-1");
yield* assignGroupMember({
commandId: CommandId.make("assign-member"),
groupId,
botId,
role: "specialist",
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
yield* unassignGroupMember({
commandId: CommandId.make("unassign-member"),
groupId,
botId,
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
yield* setGroupBoss({
commandId: CommandId.make("set-boss"),
groupId,
bossBotId: botId,
unassignPreviousBoss: true,
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(dispatched.map((command) => command.type)).toEqual([
"group.member.assign",
"group.member.unassign",
"group.boss.set",
]);
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("dispatches channel lifecycle and send commands", () =>
Effect.gen(function* () {
const dispatched: ClientOrchestrationCommand[] = [];
const supervisor = yield* makeSupervisor(dispatched);
const botId = BotId.make("bot-1");
yield* connectChannel({
botId,
targetProjectId: ProjectId.make("project-1"),
provider: "telegram",
token: "token",
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
yield* reconnectChannel({ botId, provider: "telegram" }).pipe(
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
);
yield* sendChannelMessage({
botId,
threadId: ThreadId.make("thread-1"),
messageId: MessageId.make("message-1"),
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
yield* disconnectChannel({ botId, provider: "telegram" }).pipe(
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
);
yield* detachChannel({ botId, provider: "telegram" }).pipe(
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
);
yield* changeChannelProject({
botId,
provider: "telegram",
projectId: ProjectId.make("project-2"),
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(dispatched.map((command) => command.type)).toEqual([
"channel.connect",
"channel.reconnect",
"channel.send",
"channel.disconnect",
"channel.detach",
"channel.change-project",
]);
expect(dispatched.at(-1)).toMatchObject({ projectId: "project-2", provider: "telegram" });
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("preserves caller metadata for idempotent queued commands", () =>
Effect.gen(function* () {
const dispatched: ClientOrchestrationCommand[] = [];
const supervisor = yield* makeSupervisor(dispatched);
yield* stopThreadSession({
commandId: CommandId.make("queued-command"),
threadId: ThreadId.make("thread-1"),
createdAt: "2026-06-06T00:01:00.000Z",
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(dispatched).toEqual([
{
type: "thread.session.stop",
commandId: "queued-command",
threadId: "thread-1",
createdAt: "2026-06-06T00:01:00.000Z",
},
]);
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("dispatches delegation cancellation with the keep choice", () =>
Effect.gen(function* () {
const dispatched: ClientOrchestrationCommand[] = [];
const supervisor = yield* makeSupervisor(dispatched);
yield* cancelDelegation({
commandId: CommandId.make("cancel-delegation"),
delegationId: DelegationId.make("delegation-1"),
keep: false,
createdAt: "2026-08-31T00:01:00.000Z",
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(dispatched).toEqual([
{
type: "delegation.cancel",
commandId: "cancel-delegation",
delegationId: "delegation-1",
keep: false,
createdAt: "2026-08-31T00:01:00.000Z",
},
]);
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("stamps turn starts with the client timezone", () =>
Effect.gen(function* () {
const dispatched: ClientOrchestrationCommand[] = [];
const supervisor = yield* makeSupervisor(dispatched);
yield* startThreadTurn({
commandId: CommandId.make("start-turn"),
threadId: ThreadId.make("thread-1"),
message: {
messageId: MessageId.make("message-1"),
role: "user",
text: "Create a morning routine.",
attachments: [],
},
runtimeMode: "approval-required",
interactionMode: "default",
createdAt: "2026-06-06T00:01:00.000Z",
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(dispatched).toHaveLength(1);
expect(dispatched[0]).toMatchObject({
type: "thread.turn.start",
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC",
});
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("dispatches an invisible turn resume command", () =>
Effect.gen(function* () {
const dispatched: ClientOrchestrationCommand[] = [];
const supervisor = yield* makeSupervisor(dispatched);
yield* resumeThreadTurn({
commandId: CommandId.make("resume-turn"),
threadId: ThreadId.make("thread-1"),
createdAt: "2026-06-06T00:02:00.000Z",
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(dispatched).toEqual([
{
type: "thread.turn.resume",
commandId: "resume-turn",
threadId: "thread-1",
createdAt: "2026-06-06T00:02:00.000Z",
},
]);
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("does not add timestamps to commands without createdAt", () =>
Effect.gen(function* () {
const dispatched: ClientOrchestrationCommand[] = [];
const supervisor = yield* makeSupervisor(dispatched);
yield* archiveThread({
commandId: CommandId.make("archive-command"),
threadId: ThreadId.make("thread-1"),
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(dispatched).toEqual([
{
type: "thread.archive",
commandId: "archive-command",
threadId: "thread-1",
},
]);
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("dispatches settle and unsettle commands without timestamps", () =>
Effect.gen(function* () {
const dispatched: ClientOrchestrationCommand[] = [];
const supervisor = yield* makeSupervisor(dispatched);
yield* settleThread({
commandId: CommandId.make("settle-command"),
threadId: ThreadId.make("thread-1"),
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
yield* unsettleThread({
commandId: CommandId.make("unsettle-command"),
threadId: ThreadId.make("thread-1"),
reason: "user",
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(dispatched).toEqual([
{
type: "thread.settle",
commandId: "settle-command",
threadId: "thread-1",
},
{
type: "thread.unsettle",
commandId: "unsettle-command",
threadId: "thread-1",
reason: "user",
},
]);
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("dispatches message reactions", () =>
Effect.gen(function* () {
const dispatched: ClientOrchestrationCommand[] = [];
const supervisor = yield* makeSupervisor(dispatched);
yield* setThreadMessageReaction({
commandId: CommandId.make("reaction-command"),
threadId: ThreadId.make("thread-1"),
messageId: MessageId.make("message-1"),
emoji: "👍",
present: true,
updatedAt: "2026-09-01T00:00:00.000Z",
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(dispatched).toEqual([
{
type: "thread.message.reaction.set",
commandId: "reaction-command",
threadId: "thread-1",
messageId: "message-1",
emoji: "👍",
present: true,
updatedAt: "2026-09-01T00:00:00.000Z",
},
]);
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
});