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 { createElement } from "react";
import { flushSync } from "react-dom";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, expect, it, vi } from "vitest";
import { initialState, reducer, type Action, type AppState, type Bot, type Group } from "@/state/store";
const store = vi.hoisted(() => ({ state: null as unknown as AppState, dispatch: (_action: unknown) => {} }));
vi.mock("@/state/store", async (original) => ({ ...(await original<typeof import("@/state/store")>()), useStore: () => store }));
const { useReadShownChatOnFocus } = await import("./use-read-shown-chat");
const { holdMarkedUnread } = await import("@/state/read-visible");
const bot = (unread: boolean, updatedAt = 10): Bot => ({
id: "reviewer", threadId: "shown", name: "Reviewer", title: "", description: "", notifications: true,
color: "green", unread, messages: [], modelSelection: { instanceId: "fake", model: "test" },
tasks: [{ threadId: "shown", title: "Shown", createdAt: 1, updatedAt, unread }],
});
function Shell() {
useReadShownChatOnFocus();
return null;
}
let root: Root | null = null;
afterEach(() => {
flushSync(() => root?.unmount());
root = null;
});
it("reads the shown chat when the window gets focus back, and only while it is unread", () => {
const dispatched: Action[] = [];
store.dispatch = (action) => dispatched.push(action as Action);
store.state = { ...initialState, activeView: "chat", selectedId: "reviewer", bots: [bot(true)] };
root = createRoot(document.createElement("div"));
flushSync(() => root!.render(createElement(Shell)));
window.dispatchEvent(new Event("focus"));
expect(dispatched).toEqual([{ type: "select", id: "reviewer", keepPanel: true }]);
store.state = { ...store.state, bots: [bot(false)] };
flushSync(() => root!.render(createElement(Shell)));
window.dispatchEvent(new Event("focus"));
expect(dispatched).toHaveLength(1);
});
it("leaves a chat the person marked unread alone when the window gets focus back, until a newer reply is in it", () => {
const dispatched: Action[] = [];
store.dispatch = (action) => dispatched.push(action as Action);
store.state = { ...initialState, activeView: "chat", selectedId: "reviewer", bots: [bot(false)] };
holdMarkedUnread({ type: "markUnread", botId: "reviewer" }, store.state);
store.state = { ...store.state, bots: [bot(true)] };
root = createRoot(document.createElement("div"));
flushSync(() => root!.render(createElement(Shell)));
window.dispatchEvent(new Event("blur"));
window.dispatchEvent(new Event("focus"));
window.dispatchEvent(new Event("focus"));
expect(dispatched).toEqual([]);
store.state = { ...store.state, bots: [bot(true, 11)] };
flushSync(() => root!.render(createElement(Shell)));
window.dispatchEvent(new Event("focus"));
expect(dispatched).toEqual([{ type: "select", id: "reviewer", keepPanel: true }]);
holdMarkedUnread({ type: "select", id: "reviewer" }, store.state);
});
it("keeps a room thread open beside the room when the window gets focus back and reads the room", () => {
const room: Group = {
id: "brand", threadId: "main", name: "Brand", memberIds: [], defaultResponder: { kind: "everyone" }, bulletin: "", unread: true,
createdAt: 1, messages: [], tasks: [{ threadId: "main", title: "Main", createdAt: 1, unread: true }, { threadId: "open", title: "Open", createdAt: 2 }],
};
const panel = { kind: "group" as const, ownerId: "brand", threadId: "open", messages: [], activeLeafId: null, loading: false, loadId: "load-1", loadFailed: false };
store.state = { ...initialState, activeView: "chat", selectedId: "brand", groups: [room], forkPanel: panel };
store.dispatch = (action) => { store.state = reducer(store.state, action as Action); };
root = createRoot(document.createElement("div"));
flushSync(() => root!.render(createElement(Shell)));
window.dispatchEvent(new Event("focus"));
expect(store.state.forkPanel?.threadId).toBe("open");
expect(store.state.groups[0].tasks?.find((task) => task.threadId === "main")?.unread).toBe(false);
store.dispatch({ type: "select", id: "brand" });
expect(store.state.forkPanel).toBeNull();
});