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, vi } from "vitest";
import type { RequestAuth } from "../request-auth.ts";
import { createPresenceRoutes } from "./presence.ts";
function call(auth: RequestAuth, body: unknown, active = vi.fn(async () => ({ returned: true })), team = false) {
const answers: Array<[number, unknown]> = [];
const heard = vi.fn((_auth: RequestAuth, _language: string) => {});
const sawChat = vi.fn((_auth: RequestAuth, _threadId: string, _typing: boolean) => {});
const route = createPresenceRoutes({ isOwner: (who) => who.scopes.includes("admin"), active, heard, sawChat, ...(team ? { member: () => ({ returned: false }) } : {}) });
const done = route({
req: {} as never, res: {} as never, url: new URL("http://x/api/presence/active"), path: "/api/presence/active", method: "POST", auth,
json: ((_res: unknown, status: number, value: unknown) => { answers.push([status, value]); }) as never, readBody: (async () => body) as never,
});
return { done, answers, active, heard, sawChat };
}
describe("POST /api/presence/active", () => {
it("records only the owner's activation, with a time zone and language it can read", async () => {
const owner = call({ kind: "loopback", scopes: ["admin", "client"] }, { source: "phone", timeZone: "Asia/Kolkata", language: "pt-BR" });
await owner.done;
expect(owner.active).toHaveBeenCalledWith("phone", "Asia/Kolkata", "pt-br");
expect(owner.answers).toEqual([[200, { ok: true, returned: true }]]);
const odd = call({ kind: "loopback", scopes: ["admin", "client"] }, { timeZone: "Not/A Zone; drop", language: "<script>" });
await odd.done;
expect(odd.active).toHaveBeenCalledWith("desktop", undefined, undefined);
});
it("answers a guest without recording anything", async () => {
const guest = call({ kind: "loopback", scopes: ["client"], trust: "service" }, { source: "desktop", language: "fr" });
await guest.done;
expect(guest.active).not.toHaveBeenCalled();
expect(guest.heard).not.toHaveBeenCalled();
expect(guest.answers).toEqual([[200, { ok: true, returned: false }]]);
});
it("hears the language the owner's app shows, and on a Team Cloud each member's, for the lines written for them", async () => {
const owner = call({ kind: "loopback", scopes: ["admin", "client"] }, { language: "de-DE" });
await owner.done;
expect(owner.heard).toHaveBeenCalledWith(expect.objectContaining({ kind: "loopback" }), "de-de");
const member = call({ kind: "loopback", scopes: ["client"] }, { language: "es" }, undefined, true);
await member.done;
expect(member.heard).toHaveBeenCalledWith(expect.anything(), "es");
const none = call({ kind: "loopback", scopes: ["admin", "client"] }, { language: "not a tag" });
await none.done;
expect(none.heard).not.toHaveBeenCalled();
});
it("passes on the conversation open in the app, and only a well-formed id", async () => {
const owner = { kind: "loopback", scopes: ["admin", "client"] } as RequestAuth;
const open = call(owner, { source: "desktop", language: "en", threadId: "3f2a-thread_1" });
await open.done;
expect(open.sawChat).toHaveBeenCalledWith(owner, "3f2a-thread_1", false);
const typing = call(owner, { source: "desktop", language: "en", threadId: "t1", typing: true });
await typing.done;
expect(typing.sawChat, "a key in its composer").toHaveBeenCalledWith(owner, "t1", true);
const truthy = call(owner, { threadId: "t1", typing: "yes" });
await truthy.done;
expect(truthy.sawChat).toHaveBeenCalledWith(owner, "t1", false);
for (const threadId of ["../etc", "x".repeat(101), 42, ""]) {
const odd = call(owner, { threadId });
await odd.done;
expect(odd.sawChat).not.toHaveBeenCalled();
}
});
it("counts the open conversation of every member of a Team Cloud, and elsewhere only the owner's", async () => {
const member = { kind: "loopback", scopes: ["client"] } as RequestAuth;
const onTeam = call(member, { threadId: "t1" }, undefined, true);
await onTeam.done;
expect(onTeam.sawChat, "a User's open chat is in front of them as an admin's is").toHaveBeenCalledWith(member, "t1", false);
const guest = call(member, { threadId: "t1" });
await guest.done;
expect(guest.sawChat, "a My Cloud guest's is not the owner's").not.toHaveBeenCalled();
});
});