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
import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import type { Bot } from "@/state/store";
const fixture = vi.hoisted(() => ({ team: true }));
vi.mock("@/state/store", async (importOriginal) => {
const original = await importOriginal<typeof import("@/state/store")>();
return { ...original, useStore: () => ({ state: { ...original.initialState, config: fixture.team ? { team: { id: "t", name: "Acme" } } : {} }, dispatch: vi.fn() }) };
});
const { MemoryPage } = await import("./pages");
const { BotKeeps } = await import("./Keeps");
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
const bot = { id: "scout", threadId: "t1", name: "Scout", title: "", description: "", notifications: true, color: "green", unread: false, messages: [],
modelSelection: { instanceId: "x", model: "x" } } as unknown as Bot;
const files: Record<string, string> = {};
const calls: Array<[string, string]> = [];
let teamLines: Array<{ id: string; text: string; at: number; yours?: true }> = [];
const reply = (status: number, body: unknown) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
let host: HTMLDivElement;
let root: Root;
beforeEach(() => {
fixture.team = true;
calls.length = 0;
teamLines = [];
Object.assign(files, { "MEMORY.md": "# Memory\n", "memory/food.md": "- Ada's favourite fruit is durian\n" });
vi.stubGlobal("fetch", vi.fn(async (input: string, init?: RequestInit) => {
const url = new URL(input, "http://x");
const method = init?.method ?? "GET";
calls.push([method, url.pathname]);
if (url.pathname.endsWith("/memory/team")) return reply(200, { entries: teamLines });
if (url.pathname.includes("/memory/team/") && method === "DELETE") {
teamLines = teamLines.filter((line) => !url.pathname.endsWith(`/${line.id}`));
return reply(200, { ok: true, removed: true });
}
if (!url.pathname.includes("/memory/mine")) return reply(403, { error: "forbidden: this session lacks the admin scope" });
const path = url.searchParams.get("path") ?? (init?.body ? JSON.parse(String(init.body)).path : "MEMORY.md");
if (url.pathname.endsWith("/mine")) return reply(200, { topics: Object.keys(files).filter((file) => file.startsWith("memory/")).map((file) => ({ path: file })) });
if (method === "PUT") files[path] = JSON.parse(String(init!.body)).text;
if (method === "DELETE") delete files[path];
const text = files[path] ?? "";
return reply(200, { path, text, hash: `h${text.length}`, exists: path in files, overview: {} });
}));
host = document.createElement("div");
document.body.append(host);
root = createRoot(host);
});
afterEach(() => {
act(() => root.unmount());
host.remove();
vi.unstubAllGlobals();
});
const settle = () => act(async () => { for (let i = 0; i < 5; i += 1) await new Promise((resolve) => setTimeout(resolve, 0)); });
const field = (scope: string) => host.querySelector<HTMLTextAreaElement>(`[data-memory-scope="${scope}"] textarea`);
it("on a Team Cloud shows the person's own notes, topics included, as text they can change", async () => {
vi.useFakeTimers({ shouldAdvanceTime: true });
try {
await act(async () => root.render(createElement(MemoryPage, { bot, derived: {} as never })));
await settle();
expect(field("memory/food.md")?.value).toBe("- Ada's favourite fruit is durian\n");
expect(host.textContent).toContain("food");
expect(host.textContent).not.toContain("Couldn't read the memory file");
expect(calls.every(([, path]) => path.includes("/memory/mine") || path.includes("/memory/team")), "never the bot's own notes file").toBe(true);
await act(() => {
const element = field("memory/food.md")!;
Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value")!.set!.call(element, "");
element.dispatchEvent(new Event("input", { bubbles: true }));
});
await act(async () => { vi.advanceTimersByTime(1_000); });
await settle();
expect(files["memory/food.md"]).toBe("");
expect(calls).toContainEqual(["PUT", "/api/bots/scout/memory/mine/file"]);
expect(calls.some(([, path]) => path.includes("/memory/thread/"))).toBe(false);
} finally {
vi.useRealTimers();
}
});
it("shows no count on the Details tab, only whether it remembers anything", async () => {
await act(async () => root.render(createElement(BotKeeps, { bot, routines: [], onOpen: () => {} })));
await settle();
expect(host.querySelector('[data-panel-page="memory"]')?.textContent).toBe("What it remembers");
files["memory/food.md"] = "";
files["MEMORY.md"] = "";
await act(async () => root.render(createElement(BotKeeps, { bot: { ...bot, threadId: "t2" }, routines: [], onOpen: () => {} })));
await settle();
expect(host.querySelector('[data-panel-page="memory"]')?.textContent).toContain("Nothing yet");
});
it("lists the lines the person saved to the team's memory beside their own notes, and takes one out through the team route", async () => {
teamLines = [{ id: "l1", text: "We ship on 3 March (saved by Rui, 2026-11-11)", at: 1, yours: true }];
await act(async () => root.render(createElement(MemoryPage, { bot, derived: {} as never })));
await settle();
const row = host.querySelector<HTMLElement>('[data-team-memory-entry="l1"]')!;
expect(row.textContent).toContain("We ship on 3 March (saved by Rui, 2026-11-11)");
expect(row.textContent).toContain("In team memory · added by you");
await act(async () => row.querySelector<HTMLButtonElement>("button")!.click());
await settle();
expect(calls).toContainEqual(["DELETE", "/api/bots/scout/memory/team/l1"]);
expect(host.querySelector('[data-team-memory-entry="l1"]')).toBeNull();
expect(calls.some(([method, path]) => method !== "GET" && path.endsWith("/memory/file"))).toBe(false);
});