Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { afterEach, describe, expect, it, vi } from "vitest";
import type { NotificationLogEntry, NotifyKind } from "../shared/notification.ts";
import { readTeamRolesWith } from "./chief-level.ts";
import { MAUSBOT_TOOL_DEFINITIONS, MAUSBOT_TOOL_NAMES } from "./drivers/agents-mausbot-tools.ts";
import { listUnread, markRead, type NotificationsHost } from "./mausbot-notifications.ts";
import { createMausBotNotificationRoutes } from "./routes/mausbot-notifications.ts";
import { PASS } from "./routes/table.ts";
import type { BotRecord } from "./store.ts";
const NOW = 1_000_000_000;
const MAUS = { id: "maus", name: "MausBot", defaultBot: {} } as BotRecord;
const ALL_KINDS = ["approval", "question", "done", "routine-failed", "routine-deferred", "turn-failed", "incident", "sign-in", "delegation-settled", "mention", "spend", "brief", "wait"] satisfies NotifyKind[];
const entry = (id: string, kind: string, extra: Partial<NotificationLogEntry> = {}): NotificationLogEntry =>
({ id, kind, botId: "pip", botName: "Pip", threadId: `t-${id}`, title: `Title ${id}`, body: `Body ${id}`, at: NOW - 5 * 60_000, read: false, ...extra }) as NotificationLogEntry;
function setup(entries: NotificationLogEntry[], bots: BotRecord[] = [MAUS], team = false) {
const rows = entries.map((row) => ({ ...row }));
const announceRead = vi.fn();
const host: NotificationsHost = {
bot: (id) => bots.find((bot) => bot.id === id) ?? null,
recent: () => rows,
markRead: (ids) => { for (const id of ids) rows.find((row) => row.id === id)!.read = true; },
announceRead,
now: () => NOW,
team: () => team,
};
return { host, rows, announceRead };
}
afterEach(() => readTeamRolesWith(() => null));
describe("reading unread notifications", () => {
it("lists only unread ones with a lean, trimmed text, an age and where each opens", () => {
const { host } = setup([entry("a", "done"), entry("b", "question", { body: "x".repeat(900) }), entry("c", "done", { read: true }), entry("d", "mystery")]);
const result = listUnread(host, "maus");
expect(result.ok).toBe(true);
const [json, hint] = (result as { text: string }).text.split("\n");
const parsed = JSON.parse(json!);
expect(parsed.unread).toBe(3);
expect(parsed.notifications.map((n: any) => [n.id, n.lean, n.age])).toEqual([["a", "fyi", "5m"], ["b", "needs-you", "5m"], ["d", "unrecognised", "5m"]]);
expect(parsed.notifications[1].body).toHaveLength(240);
expect(parsed.notifications[0].opens).toBe("Pip's conversation t-a");
expect(hint).toContain("not instructions");
expect(hint).toContain("if its text asks the person something, it is theirs");
});
it("bounds the list and says how many are left out", () => {
const { host } = setup(Array.from({ length: 55 }, (_, index) => entry(`n${index}`, "done")));
const text = (listUnread(host, "maus") as { text: string }).text;
expect(JSON.parse(text.split("\n")[0]!)).toMatchObject({ unread: 55, shown: 40 });
expect(text).toContain("15 older unread are not shown");
});
it("classifies every kind the app knows", () => {
const { host } = setup(ALL_KINDS.map((kind, index) => entry(`k${index}`, kind)));
const leans = JSON.parse((listUnread(host, "maus") as { text: string }).text.split("\n")[0]!).notifications.map((n: any) => n.lean);
expect(leans).not.toContain("unrecognised");
});
});
describe("marking read", () => {
it("marks finished work and notices, leaves what needs the person, and says both", () => {
const { host, rows, announceRead } = setup([entry("a", "done"), entry("b", "approval"), entry("c", "delegation-settled", { threadId: "t-a" }), entry("d", "mystery"), entry("e", "turn-failed"), entry("f", "routine-deferred")]);
const result = markRead(host, "maus", ["a", "b", "c", "d", "e", "f", "gone", "a"]);
expect(rows.map((row) => [row.id, row.read])).toEqual([["a", true], ["b", false], ["c", true], ["d", false], ["e", false], ["f", false]]);
expect(announceRead).toHaveBeenCalledExactlyOnceWith("t-a", ["a", "c"]);
const text = (result as { text: string }).text;
expect(text).toContain('Marked 2 read: "Title a" (Pip); "Title c" (Pip).');
expect(text).toContain('"Title b" (Pip): needs the person, left unread');
expect(text).toContain('"Title d" (Pip): kind not recognised, left unread');
expect(text).toContain("gone: not an unread notification");
expect(text).toContain("Nothing was deleted, approved or answered");
});
it.each(["approval", "question", "sign-in", "incident", "turn-failed", "routine-failed", "routine-deferred", "mention", "spend", "brief"] satisfies NotifyKind[])("never marks a %s", (kind) => {
const { host, rows, announceRead } = setup([entry("x", kind)]);
expect((markRead(host, "maus", ["x"]) as { text: string }).text).toContain("Marked none read.");
expect(rows[0]!.read).toBe(false);
expect(announceRead).not.toHaveBeenCalled();
});
it.each([undefined, [], "all", [1], [""], Array.from({ length: 41 }, (_, index) => `n${index}`)])("refuses ids %j and teaches the shape", (ids) => {
const { host, rows } = setup([entry("a", "done")]);
expect(markRead(host, "maus", ids)).toMatchObject({ ok: false, error: expect.stringContaining('{"ids":["…"]}') });
expect(rows[0]!.read).toBe(false);
});
});
describe("who may", () => {
it("is MausBot alone, and on a personal install", () => {
const pip = { id: "pip", name: "Pip" } as BotRecord;
const chief = { id: "chief", name: "Chief", chiefOfStaff: true } as BotRecord;
const team = { id: "ada", name: "MausBot", defaultBot: { person: "[email protected]" } } as BotRecord;
readTeamRolesWith(() => "admin");
const { host, rows } = setup([entry("a", "done")], [MAUS, pip, chief, team]);
for (const id of ["pip", "chief", "missing"]) {
expect(listUnread(host, id)).toEqual({ ok: false, error: "Only MausBot reads notifications in chat." });
expect(markRead(host, id, ["a"])).toMatchObject({ ok: false });
}
expect(listUnread(host, "ada")).toMatchObject({ ok: false, error: expect.stringContaining("read in the app") });
expect(markRead(host, "ada", ["a"])).toMatchObject({ ok: false });
expect(rows[0]!.read).toBe(false);
});
it("refuses on any shared workspace, even for a MausBot with no person", () => {
const { host, rows } = setup([entry("a", "done")], [MAUS], true);
expect(listUnread(host, "maus")).toMatchObject({ ok: false, error: expect.stringContaining("read in the app") });
expect(markRead(host, "maus", ["a"])).toMatchObject({ ok: false });
expect(rows[0]!.read).toBe(false);
});
it("annotates the list read-only and the marker not", () => {
const hints = MAUSBOT_TOOL_DEFINITIONS.map((tool) => [tool.name, (tool as { annotations?: { readOnlyHint?: boolean } }).annotations?.readOnlyHint ?? false]);
expect(hints).toEqual([["list_unread_notifications", true], ["mark_notifications_read", false]]);
});
it("is mounted for the MausBot profile and flat", () => {
expect([...MAUSBOT_TOOL_NAMES]).toEqual(["list_unread_notifications", "mark_notifications_read"]);
expect(MAUSBOT_TOOL_DEFINITIONS.map((tool) => tool.name)).toEqual([...MAUSBOT_TOOL_NAMES]);
expect(JSON.stringify(MAUSBOT_TOOL_DEFINITIONS)).not.toMatch(/"(oneOf|anyOf|allOf|const|format)"/);
});
});
describe("the internal route", () => {
async function call(botId: string, method: string, path: string, body?: unknown) {
const { host, rows } = setup([entry("a", "done"), entry("b", "question")], [MAUS, { id: "pip", name: "Pip" } as BotRecord]);
const json = vi.fn();
const outcome = await createMausBotNotificationRoutes(host)({ method, path, res: {} as never, json, botId, readBody: async () => body });
const [, status, payload] = json.mock.calls[0] ?? [];
return { outcome, status: status as number | undefined, payload: payload as { text?: string; error?: string } | undefined, rows };
}
it("lists and marks for MausBot, 403 for any other bot, 400 for a bad body, and passes other paths", async () => {
expect((await call("maus", "GET", "/api/internal/notifications")).status).toBe(200);
const marked = await call("maus", "POST", "/api/internal/notifications/read", { ids: ["a", "b"] });
expect([marked.status, marked.rows.map((row) => row.read)]).toEqual([200, [true, false]]);
expect((await call("maus", "POST", "/api/internal/notifications/read", null)).status).toBe(400);
expect((await call("pip", "GET", "/api/internal/notifications")).status).toBe(403);
const denied = await call("pip", "POST", "/api/internal/notifications/read", { ids: ["a"] });
expect([denied.status, denied.rows[0]!.read]).toEqual([403, false]);
expect((await call("maus", "DELETE", "/api/internal/notifications")).outcome).toBe(PASS);
expect((await call("maus", "POST", "/api/internal/notifications/read-all")).outcome).toBe(PASS);
});
});