Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
// @vitest-environment happy-dom
// ThreadRefsProvider selects its threads by their signature: what a #Title link reads must follow every change to a
// title, a name or the open bot, and nothing else may hand the message rows a new value.
import { createElement } from "react";
import { flushSync } from "react-dom";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, describe, expect, it, vi } from "vitest";
import { createAppStore } from "@/state/app-store";
import { initialState, reducer, type Action, type AppState, type Bot, type Group } from "@/state/store";
import { StoreSourceContext, type StoreSource } from "@/state/store-context";
import { ThreadRefsProvider, useThreadRefs, type ThreadRefsValue } from "./ThreadRefs";
const bot = (id: string, name: string, tasks: Bot["tasks"]): Bot => ({
id, threadId: tasks?.[0]?.threadId ?? id, name, title: "", description: "", notifications: true,
color: "green", unread: false, messages: [], modelSelection: { instanceId: "fake", model: "test" }, tasks,
});
const scout = bot("scout", "Scout", [{ threadId: "scout-main", title: "Main", createdAt: 1 }, { threadId: "qa", title: "QA PR 245", createdAt: 2 }]);
const ada = bot("ada", "Ada", [{ threadId: "ada-main", title: "Main", createdAt: 3 }]);
const room: Group = {
id: "standup", threadId: "standup-1", name: "Standup", memberIds: ["scout"], defaultResponder: { kind: "everyone" },
bulletin: "", unread: false, createdAt: 1, messages: [], tasks: [{ threadId: "standup-1", title: "Monday plan", createdAt: 4 }],
};
let root: Root | null = null;
afterEach(() => { flushSync(() => root?.unmount()); root = null; });
function mounted() {
const store = createAppStore<AppState, Action, never>(reducer, { ...initialState, bots: [scout, ada], groups: [room], selectedId: "scout" },
(flush) => { flush(); return () => {}; });
const source: StoreSource = { getState: store.getState, subscribe: store.subscribe, dispatch: store.dispatch,
flushBotPatches: vi.fn(), refreshInstances: vi.fn(), refreshModels: vi.fn() };
const seen: ThreadRefsValue[] = [];
function Reader() {
seen.push(useThreadRefs());
return null;
}
const container = document.createElement("div");
document.body.append(container);
root = createRoot(container);
flushSync(() => root!.render(createElement(StoreSourceContext, { value: source }, createElement(ThreadRefsProvider, null, createElement(Reader)))));
const latest = () => seen.at(-1)!;
const titles = () => latest().threads.map((ref) => `${ref.botName}: ${ref.title}`);
return { store, seen, latest, titles, act: (action: Action) => flushSync(() => store.dispatch(action)) };
}
describe("ThreadRefsProvider", () => {
it("follows a renamed thread, a renamed bot or room, a new thread and the open bot", () => {
const view = mounted();
expect(view.titles()).toEqual(["Scout: Main", "Scout: QA PR 245", "Ada: Main", "Standup: Monday plan"]);
expect(view.latest().currentBotId).toBe("scout");
view.act({ type: "botPatched", bot: { ...scout, tasks: [scout.tasks![0]!, { threadId: "qa", title: "QA PR 246", createdAt: 2 }] } });
expect(view.titles()).toContain("Scout: QA PR 246");
view.act({ type: "botPatched", bot: { ...ada, name: "Ada Lovelace" } });
expect(view.titles()).toContain("Ada Lovelace: Main");
view.act({ type: "botPatched", bot: { ...ada, name: "Ada Lovelace", tasks: [...ada.tasks!, { threadId: "ada-new", title: "Fresh", createdAt: 9 }] } });
expect(view.titles()).toContain("Ada Lovelace: Fresh");
view.act({ type: "groupPatched", group: { ...room, name: "Daily", tasks: [{ threadId: "standup-1", title: "Tuesday plan", createdAt: 4 }] } });
expect(view.titles()).toContain("Daily: Tuesday plan");
view.act({ type: "select", id: "ada" });
expect(view.latest().currentBotId).toBe("ada");
});
it("hands the rows the same value for a change no link reads", () => {
const view = mounted();
const before = view.latest();
const renders = view.seen.length;
view.act({ type: "botPatched", bot: { ...scout, unread: true, newThreadModel: { instanceId: "fake", model: "other" } } });
view.act({ type: "connected", value: false });
expect(view.seen.length).toBe(renders);
expect(view.latest()).toBe(before);
});
});