Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { describe, expect, it } from "vitest";
import {
addressesEveryone,
applyHoldDirective,
classifyHoldDirective,
isStopCommand,
MAX_HELD_MESSAGES,
rememberHeldMessage,
stopWordPlacement,
} from "./room-holds.ts";
const members = ["scout", "pixel", "bee"];
describe("room member holds", () => {
it("holds a member when a stop word sits next to its mention", () => {
expect(classifyHoldDirective("stop @Scout", ["scout"])).toEqual({ hold: ["scout"], holdAll: false, release: [], releaseAll: false });
expect(classifyHoldDirective("@Scout please halt", ["scout"]).hold).toEqual(["scout"]);
expect(classifyHoldDirective("pause @New Bot", ["newbot"]).hold).toEqual(["newbot"]);
expect(classifyHoldDirective("STOP @Scout and @Pixel", ["scout", "pixel"]).hold).toEqual(["scout", "pixel"]);
});
it("reads a stop word with an object as a task, not a hold", () => {
expect(classifyHoldDirective("@Engineer stop this bridge and uninstall it", ["engineer"])).toEqual({ hold: [], holdAll: false, release: ["engineer"], releaseAll: false });
expect(classifyHoldDirective("@Scout pause the render at 10s", ["scout"]).hold).toEqual([]);
expect(classifyHoldDirective("@Scout stop that", ["scout"]).hold).toEqual(["scout"]);
expect(classifyHoldDirective("@Scout stop it now", ["scout"]).hold).toEqual(["scout"]);
expect(classifyHoldDirective("stop the @Scout", ["scout"]).hold).toEqual(["scout"]);
expect(classifyHoldDirective("@Scout stop that now", ["scout"]).hold).toEqual(["scout"]);
expect(classifyHoldDirective("@Scout stop this right now", ["scout"]).hold).toEqual(["scout"]);
expect(classifyHoldDirective("@Scout pause your work", ["scout"]).hold).toEqual(["scout"]);
});
it("releases a member addressed directly without a stop word", () => {
expect(classifyHoldDirective("@Scout resume", ["scout"])).toEqual({ hold: [], holdAll: false, release: ["scout"], releaseAll: false });
expect(classifyHoldDirective("@Scout what did you find?", ["scout"]).release).toEqual(["scout"]);
});
it("treats a stop word far from every mention as neither a hold nor a release", () => {
expect(stopWordPlacement("@Scout go, das ist halt ein Test")).toBe("distant");
expect(classifyHoldDirective("@Scout go, das ist halt ein Test", ["scout"])).toEqual({ hold: [], holdAll: false, release: [], releaseAll: false });
});
it("ignores stop words the person quotes or pastes", () => {
expect(stopWordPlacement("@Scout what does `stop` do?")).toBeNull();
expect(stopWordPlacement('@Scout the log said "stop"')).toBeNull();
expect(stopWordPlacement("@Scout look:\n```\nstop\n```")).toBeNull();
expect(stopWordPlacement("> stop @Scout\nwhat did they mean?")).toBeNull();
expect(classifyHoldDirective("> stop @Scout\nwhat did they mean?", ["scout"]).release).toEqual(["scout"]);
});
it("holds and resumes everyone through @everyone / @all", () => {
expect(addressesEveryone("@all stop")).toBe(true);
expect(addressesEveryone("@everyone resume")).toBe(true);
expect(addressesEveryone("@allison hi")).toBe(false);
expect(addressesEveryone("mail [email protected]")).toBe(false);
expect(classifyHoldDirective("@all stop", []).holdAll).toBe(true);
expect(classifyHoldDirective("@everyone resume", []).releaseAll).toBe(true);
expect(classifyHoldDirective("resume", []).releaseAll).toBe(true);
expect(classifyHoldDirective("Resume all.", []).releaseAll).toBe(true);
expect(classifyHoldDirective("resume the deploy tomorrow", []).releaseAll).toBe(false);
});
it("takes the Portuguese word the pt-BR held banner asks for", () => {
expect(classifyHoldDirective("retomar", []).releaseAll).toBe(true);
expect(classifyHoldDirective("Retomar todos.", []).releaseAll).toBe(true);
expect(classifyHoldDirective("retome, por favor", []).releaseAll).toBe(true);
expect(classifyHoldDirective("retomar o deploy amanhã", []).releaseAll).toBe(false);
expect(classifyHoldDirective("para @Scout", ["scout"]).hold).toEqual([]);
});
it("applies directives to the room's holds and reports what changed", () => {
const first = applyHoldDirective(undefined, classifyHoldDirective("stop @Scout", ["scout"]), members, 10);
expect(first).toEqual({ holds: { scout: { at: 10 } }, held: ["scout"], released: [] });
const again = applyHoldDirective(first.holds, classifyHoldDirective("stop @Scout", ["scout"]), members, 20);
expect(again.holds).toBe(first.holds);
expect(again.held).toEqual([]);
const all = applyHoldDirective(first.holds, classifyHoldDirective("@all stop", []), members, 30);
expect(all.holds).toEqual({ scout: { at: 10 }, pixel: { at: 30 }, bee: { at: 30 } });
expect(all.held).toEqual(["pixel", "bee"]);
const one = applyHoldDirective(all.holds, classifyHoldDirective("@Pixel carry on", ["pixel"]), members, 40);
expect(Object.keys(one.holds)).toEqual(["scout", "bee"]);
expect(one.released).toEqual(["pixel"]);
const none = applyHoldDirective(one.holds, classifyHoldDirective("@all resume", []), members, 50);
expect(none.holds).toEqual({});
expect(none.released).toEqual(["scout", "bee"]);
});
it("only holds room members", () => {
expect(applyHoldDirective({}, { hold: ["stranger"], holdAll: false, release: [], releaseAll: false }, members, 1).held).toEqual([]);
});
it("keeps a bounded, de-duplicated list of messages for a held member", () => {
let held = rememberHeldMessage(undefined, "scout", { threadId: "t", messageId: "m1" });
held = rememberHeldMessage(held, "scout", { threadId: "t", messageId: "m1" });
expect(held.scout).toHaveLength(1);
for (let i = 0; i < MAX_HELD_MESSAGES + 5; i += 1) held = rememberHeldMessage(held, "scout", { threadId: "t", messageId: `x${i}` });
expect(held.scout).toHaveLength(MAX_HELD_MESSAGES);
expect(held.scout.at(-1)?.messageId).toBe(`x${MAX_HELD_MESSAGES + 4}`);
});
});
describe("isStopCommand", () => {
it("reads a message made only of stop words and fillers as a stop", () => {
for (const text of ["stop", "Stop now!", "please stop", "STOP STOP", "cancel that", "abort", "halt it", "stop everything"]) {
expect(isStopCommand(text), text).toBe(true);
}
});
it("reads the app's own word for Stop in each of its languages as a stop", () => {
for (const text of ["stopp", "alles stoppen", "Bitte stoppen!", "STOPP", "detener", "detener todo", "arrêter", "arrêter tout", "रोकें", "interrompi", "ferma tutto", "停止",
"중지", "멈추기", "zatrzymaj", "zatrzymaj wszystko", "parar", "parar agora", "стоп", "остановить всё", "durdur", "lütfen durdur", "зупинити", "dừng", "D\u1EEB\u006E\u0067", "Du\u031B\u0300ng"]) {
expect(isStopCommand(text), text).toBe(true);
}
});
it("reads a real sentence that merely has a stop word in it as a message, in every language", () => {
for (const text of ["stoppen wir das Projekt?", "bitte nicht stoppen", "Stopp-Schild bestellen", "wann soll ich stoppen", "no detener", "detener el servidor", "arrêter de fumer", "ne pas arrêter",
"रोकें नहीं", "non interrompi", "ferma il server", "停止服务", "不要停止", "停止してください", "중지 말고 계속", "nie zatrzymaj", "parar de compartilhar", "não parar", "не остановить", "стоп-лосс на 5%",
"durdur ve yeniden başlat", "не зупинити", "dừng lại sau khi xong", "alles", "bitte", "todo", "`stopp`", "\"alles stoppen\"", "> alles stoppen"]) {
expect(isStopCommand(text), text).toBe(false);
}
});
it("takes @everyone and the bot's own name as a tag, and any other tag as a message about them", () => {
expect(isStopCommand("@everyone stop")).toBe(true);
expect(isStopCommand("@everyone alles stoppen")).toBe(true);
expect(isStopCommand("@Chris Assistent stop", "Chris Assistent")).toBe(true);
expect(isStopCommand("@Ben stop", "Ada")).toBe(false);
expect(isStopCommand("@Adam stop", "Ada")).toBe(false);
expect(isStopCommand("@everyone-else stop")).toBe(false);
});
it.each([
"stop @Todo",
"stop @Now",
"stop @Please",
"stop @Stop",
"@Adaplease stop",
"@Ada.stop stop",
"@Ada+Todo stop",
"@all+Please stop",
"@everyoneplease stop",
"/@Ada stop",
"@everyone stop @Todo",
])("keeps another name or a non-mention out of a typed stop: %s", (text) => {
expect(isStopCommand(text, "Ada")).toBe(false);
});
it.each([
["@aDa stop", "Ada"],
["(@Ada) STOP!", "Ada"],
["**@Ada** stop!", "Ada"],
["@Ada. stop", "Ada"],
["@Ada-Bot stop", "Ada-Bot"],
["@Ada.Bot stop", "Ada.Bot"],
["@A+Bot stop", "A+Bot"],
["@Ada+Todo stop", "Ada+Todo"],
["@Chris Assistent stop", "Chris Assistent"],
["@Rene\u0301 stop", "René"],
["【@小助手】停止", "小助手"],
["@🐝 Bee stop", "🐝 Bee"],
["@Todo stop", "Todo"],
["@Everyone Please stop", "Everyone Please"],
["@everyone stop", "Ada"],
["@all stop", "Ada"],
])("keeps a complete allowed name usable: %s", (text, name) => {
expect(isStopCommand(text, name)).toBe(true);
});
it("reads any other message as a message, including a quoted or coded stop", () => {
for (const text of ["", "all right", "stop using tabs", "don't stop", "pause", "stop the build and run tests", "`stop`", "\"stop\"", "> stop",
"why did it stop?", "it stopped", "never stop", "non-stop", "stop-loss at 5%", "cancel my 3pm", "please stop the server and restart it", "stop\nthen deploy"]) {
expect(isStopCommand(text), text).toBe(false);
}
});
});