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 { APP_LIMITS, appBlockedText, appUsesList, appDoneOf, htmlFallbackText, htmlVisibleText, mcpAppFallbackText, parseShowHtmlInput, parseUpdateHtmlInput, utf8Bytes, wrapHtmlFragment } from "./app.ts";
describe("parseShowHtmlInput", () => {
it("keeps a whole document as written, trimmed", () => {
const html = "<!DOCTYPE html><html><body><p>Hi</p></body></html>";
expect(parseShowHtmlInput({ html: ` ${html}\n`, title: "Greeting" })).toEqual({ ok: true, value: { html, title: "Greeting" } });
});
it("wraps a fragment, and sees <html or <body in any case", () => {
expect(parseShowHtmlInput({ html: "<div>x</div>", title: "t" })).toMatchObject({
ok: true, value: { html: "<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body><div>x</div></body></html>" },
});
expect(wrapHtmlFragment("<BODY onload=x>y</BODY>")).toBe("<BODY onload=x>y</BODY>");
expect(wrapHtmlFragment("<Html lang=en>y")).toBe("<Html lang=en>y");
expect(wrapHtmlFragment("<bodyguard>y</bodyguard>")).toContain("<body><bodyguard>");
});
it("refuses an empty or oversized page, counted in UTF-8 bytes", () => {
expect(parseShowHtmlInput({ html: " ", title: "t" })).toMatchObject({ ok: false, error: expect.stringMatching(/^html: is empty/) });
expect(parseShowHtmlInput({ html: 7, title: "t" })).toMatchObject({ ok: false, error: expect.stringMatching(/^html:/) });
expect(parseShowHtmlInput({ html: "a".repeat(APP_LIMITS.htmlBytes), title: "t" }).ok).toBe(true);
expect(parseShowHtmlInput({ html: "a".repeat(APP_LIMITS.htmlBytes + 1), title: "t" })).toMatchObject({ ok: false, error: expect.stringContaining("over the 262144-byte limit") });
expect(parseShowHtmlInput({ html: "é".repeat(APP_LIMITS.htmlBytes / 2 + 1), title: "t" }).ok).toBe(false);
});
it("puts the title on one line, 1 to 80 characters", () => {
expect(parseShowHtmlInput({ html: "x", title: " Sales\n\tby month " })).toMatchObject({ ok: true, value: { title: "Sales by month" } });
expect(parseShowHtmlInput({ html: "x", title: "" })).toMatchObject({ ok: false, error: expect.stringMatching(/^title:/) });
expect(parseShowHtmlInput({ html: "x", title: "t".repeat(81) })).toMatchObject({ ok: false, error: expect.stringMatching(/^title:/) });
expect(parseShowHtmlInput({ html: "x" })).toMatchObject({ ok: false, error: expect.stringMatching(/^title:/) });
});
it("clamps the height to 80..720 and checks the fallback", () => {
expect(parseShowHtmlInput({ html: "x", title: "t", height: 20 })).toMatchObject({ ok: true, value: { height: 80 } });
expect(parseShowHtmlInput({ html: "x", title: "t", height: 5000 })).toMatchObject({ ok: true, value: { height: 720 } });
expect(parseShowHtmlInput({ html: "x", title: "t", height: 300.6 })).toMatchObject({ ok: true, value: { height: 301 } });
expect(parseShowHtmlInput({ html: "x", title: "t", height: "tall" })).toMatchObject({ ok: false, error: expect.stringMatching(/^height:/) });
expect(parseShowHtmlInput({ html: "x", title: "t", fallback_text: "f".repeat(1_501) })).toMatchObject({ ok: false, error: expect.stringMatching(/^fallback_text:/) });
expect(parseShowHtmlInput({ html: "x", title: "t", fallback_text: " Plain " })).toMatchObject({ ok: true, value: { fallbackText: "Plain" } });
expect(parseShowHtmlInput({ html: "x", title: "t", fallback_text: null, height: null })).toEqual({ ok: true, value: expect.not.objectContaining({ height: expect.anything() }) });
expect(parseShowHtmlInput("nope").ok).toBe(false);
});
});
describe("parseUpdateHtmlInput", () => {
it("needs the id show_html returned and a page; the title is optional", () => {
expect(parseUpdateHtmlInput({ app_id: "m_1", html: "<p>x</p>" })).toMatchObject({ ok: true, value: { appId: "m_1", html: expect.stringContaining("<body><p>x</p>") } });
expect(parseUpdateHtmlInput({ app_id: "m_1", html: "x", title: "New" })).toMatchObject({ ok: true, value: { title: "New" } });
expect(parseUpdateHtmlInput({ html: "x" })).toMatchObject({ ok: false, error: expect.stringMatching(/^app_id:/) });
expect(parseUpdateHtmlInput({ app_id: "../etc", html: "x" })).toMatchObject({ ok: false, error: expect.stringMatching(/^app_id:/) });
expect(parseUpdateHtmlInput({ app_id: "m_1", html: "" })).toMatchObject({ ok: false, error: expect.stringMatching(/^html:/) });
});
});
describe("fallback text", () => {
it("is the bot's fallback_text when given", () => {
expect(htmlFallbackText("Chart", "<p>ignored</p>", "Revenue rose 12%")).toBe("Revenue rose 12%");
});
it("is [App] <title> and up to 600 characters of what a person would read", () => {
const html = "<html><head><title>T</title><style>p{color:red}</style><script>var secret = 1;</script></head>" +
"<body><!-- note --><h1>Revenue</h1><p>Up 12% & rising <fast> €5 😀</p></body></html>";
expect(htmlFallbackText("Sales", html)).toBe("[App] Sales\nRevenue Up 12% & rising <fast> €5 😀");
const long = htmlFallbackText("Long", `<p>${"word ".repeat(400)}</p>`);
expect(long.split("\n")[1]!.length).toBeLessThanOrEqual(APP_LIMITS.fallbackExcerpt);
expect(long.endsWith("…")).toBe(true);
expect(htmlFallbackText("Blank", "<script>x()</script>")).toBe("[App] Blank");
expect(htmlVisibleText("<p>a</p><script>never closed")).toBe("a");
});
it("reads an MCP row as <server> · <tool> and the result's text", () => {
expect(mcpAppFallbackText("Excalidraw", "Create view", { content: [{ type: "text", text: "Diagram\nready" }, { type: "image", data: "x" }] }))
.toBe("Excalidraw · Create view\nDiagram ready");
expect(mcpAppFallbackText("Debug", "debug-tool", undefined)).toBe("Debug · debug-tool");
});
it("names why a frame's server calls are off", () => {
expect(appBlockedText("signed-out", "Asana")).toBe("Sign in to Asana again");
expect(appBlockedText("server-removed", "Debug")).toBe("Debug is no longer connected");
expect(appBlockedText("too-large")).toBe("This app is too large to store");
});
});
describe("utf8Bytes", () => {
it("counts like the encoder does", () => {
for (const text of ["", "abc", "é", "€", "😀", "a😀é€", "\ud800 lone"]) expect(utf8Bytes(text), text).toBe(new TextEncoder().encode(text).length);
});
});
describe("appDoneOf", () => {
const text = (value: string) => ({ type: "text", text: value });
it("takes the plain text as one line and its first https link", () => {
expect(appDoneOf("Create issue", { content: [text("Created issue #12\n https://github.com/o/r/issues/12 (label not applied)")] }, 5))
.toEqual({ tool: "Create issue", text: "Created issue #12 https://github.com/o/r/issues/12 (label not applied)", url: "https://github.com/o/r/issues/12", at: 5 });
});
it("shows no raw JSON: a JSON block gives only its html_url, else its url", () => {
expect(appDoneOf("Create issue", { content: [text('{"url":"https://api.github.com/repos/o/r/issues/12","html_url":"https://github.com/o/r/issues/12","number":12}')] }, 1))
.toEqual({ tool: "Create issue", url: "https://github.com/o/r/issues/12", at: 1 });
expect(appDoneOf("Create issue", { content: [text('{"id":"1","url":"https://github.com/o/r/issues/13"}')] }, 1))
.toEqual({ tool: "Create issue", url: "https://github.com/o/r/issues/13", at: 1 });
expect(appDoneOf("Create issue", { content: [], structuredContent: { html_url: "https://github.com/o/r/pull/7" } }, 1).url).toBe("https://github.com/o/r/pull/7");
});
it("keeps only https links and clips long text", () => {
const done = appDoneOf("Run", { content: [text(`see http://x.test/a and javascript:alert(1) ${"y".repeat(400)}`)], structuredContent: { url: "javascript:alert(1)" } }, 1);
expect(done.url).toBeUndefined();
expect(done.text!.length).toBeLessThanOrEqual(APP_LIMITS.doneText);
expect(appDoneOf("Run", { content: [] }, 1)).toEqual({ tool: "Run", at: 1 });
});
});
describe("htmlVisibleText numeric character references", () => {
it("leaves surrogate code points as written, like other unrepresentable code points", () => {
expect(htmlVisibleText("�")).toBe("�");
expect(htmlVisibleText("�")).toBe("�");
expect(htmlVisibleText("�")).toBe("�");
expect(htmlVisibleText("�")).toBe("�");
expect(htmlVisibleText("😀")).toBe("😀");
});
});
const USES_HELP = 'uses: a list of "data", "state" and connector tools by exact name: "gmail:GMAIL_FETCH_EMAILS" for a connected app, '
+ '"mcp:<server>:<tool>" for an MCP server; no wildcards. E.g. ["data", "gmail:GMAIL_FETCH_EMAILS"].';
describe("show_html uses", () => {
it("reads a list of data and state, and the shapes models send for one", () => {
expect(parseShowHtmlInput({ html: "<p>x</p>", title: "t", uses: ["data", "state", "data"] })).toMatchObject({ ok: true, value: { uses: ["data", "state"] } });
expect(parseShowHtmlInput({ html: "<p>x</p>", title: "t", uses: "data" })).toMatchObject({ ok: true, value: { uses: ["data"] } });
expect(parseShowHtmlInput({ html: "<p>x</p>", title: "t", uses: '["State"]' })).toMatchObject({ ok: true, value: { uses: ["state"] } });
expect(parseShowHtmlInput({ html: "<p>x</p>", title: "t", uses: [] }).ok && "uses" in (parseShowHtmlInput({ html: "<p>x</p>", title: "t", uses: [] }) as { value: object }).value).toBe(false);
expect(parseShowHtmlInput({ html: "<p>x</p>", title: "t", uses: ["network"] })).toEqual({ ok: false, error: USES_HELP });
expect(parseShowHtmlInput({ html: "<p>x</p>", title: "t", uses: [{ name: "data" }] }).ok).toBe(false);
});
it("keeps a replacement's uses as given, and leaves them out when not given", () => {
expect(parseUpdateHtmlInput({ app_id: "m1", html: "<p>x</p>", uses: [] })).toMatchObject({ ok: true, value: { uses: [] } });
expect(parseUpdateHtmlInput({ app_id: "m1", html: "<p>x</p>" }).ok && "uses" in (parseUpdateHtmlInput({ app_id: "m1", html: "<p>x</p>" }) as { value: object }).value).toBe(false);
});
});
describe("show_html uses: connector tools", () => {
const parse = (uses: unknown) => parseShowHtmlInput({ html: "<p>x</p>", title: "t", uses });
it.each([
["one app tool", ["gmail:GMAIL_FETCH_EMAILS"], [{ connector: "gmail", tools: ["GMAIL_FETCH_EMAILS"] }]],
["case as models vary it", ["Gmail:gmail_fetch_emails"], [{ connector: "gmail", tools: ["GMAIL_FETCH_EMAILS"] }]],
["one app, several tools, a repeat", ["gmail:GMAIL_FETCH_EMAILS", "gmail:GMAIL_SEND_EMAIL", "gmail:GMAIL_FETCH_EMAILS"], [{ connector: "gmail", tools: ["GMAIL_FETCH_EMAILS", "GMAIL_SEND_EMAIL"] }]],
["an underscored app", ["bland_ai:BLAND_AI_LIST_CALLS"], [{ connector: "bland_ai", tools: ["BLAND_AI_LIST_CALLS"] }]],
["an MCP server's tool, its case kept", ["mcp:Notes:searchNotes"], [{ server: "notes", tools: ["searchNotes"] }]],
["the object shapes", [{ connector: "gmail", tools: ["GMAIL_FETCH_EMAILS"] }, { server: "notes", tools: "search,open" }], [{ connector: "gmail", tools: ["GMAIL_FETCH_EMAILS"] }, { server: "notes", tools: ["search", "open"] }]],
["one string of several", "data, gmail:GMAIL_FETCH_EMAILS", [{ connector: "gmail", tools: ["GMAIL_FETCH_EMAILS"] }]],
["a JSON list", '["gmail:GMAIL_FETCH_EMAILS"]', [{ connector: "gmail", tools: ["GMAIL_FETCH_EMAILS"] }]],
])("reads %s", (_name, uses, connectors) => {
expect(parse(uses)).toMatchObject({ ok: true, value: { connectors } });
});
it("keeps data and state beside connector tools, and both are given together", () => {
expect(parse(["state", "gmail:GMAIL_FETCH_EMAILS"])).toMatchObject({ ok: true, value: { uses: ["state"], connectors: [{ connector: "gmail" }] } });
expect(parse(["data"])).toMatchObject({ ok: true, value: { uses: ["data"], connectors: [] } });
expect(parseUpdateHtmlInput({ app_id: "m1", html: "<p>x</p>", uses: ["data"] })).toMatchObject({ ok: true, value: { uses: ["data"], connectors: [] } });
expect(appUsesList(["data"], [{ connector: "gmail", tools: ["GMAIL_FETCH_EMAILS"] }, { server: "notes", tools: ["search"] }]))
.toEqual(["data", "gmail:GMAIL_FETCH_EMAILS", "mcp:notes:search"]);
});
it.each([
["a wildcard", ["gmail:*"], "wildcards"],
["a wildcard on a server", ["mcp:notes:*"], "wildcards"],
["a tool of another app", ["gmail:SLACK_SEND_MESSAGE"], "is not one of gmail's tools"],
["an app with no tool", ["gmail"], USES_HELP],
["Composio's own tools", ["composio:COMPOSIO_MULTI_EXECUTE_TOOL"], "is not a connected app's name"],
["a connection tool", ["gmail:GMAIL_MANAGE_CONNECTIONS"], "a page cannot call GMAIL_MANAGE_CONNECTIONS"],
["a bad server name", ["mcp:no such:search"], "does not name an MCP server"],
["an object with both", [{ connector: "gmail", server: "notes", tools: ["x"] }], USES_HELP],
["an object with no tools", [{ connector: "gmail", tools: [] }], USES_HELP],
])("refuses %s", (_name, uses, error) => {
const parsed = parse(uses);
expect(parsed.ok).toBe(false);
expect(parsed.ok ? "" : parsed.error).toContain(error);
});
it("caps the apps and the tools a page names", () => {
const apps = Array.from({ length: APP_LIMITS.pageConnectors + 1 }, (_, i) => `app${i}:APP${i}_LIST`);
expect(parse(apps).ok).toBe(false);
const tools = Array.from({ length: APP_LIMITS.pageConnectorTools + 1 }, (_, i) => `gmail:GMAIL_T${i}`);
expect(parse(tools)).toEqual({ ok: false, error: `uses: at most ${APP_LIMITS.pageConnectors} connected apps or servers and ${APP_LIMITS.pageConnectorTools} tools in all.` });
expect(parse(tools.slice(1)).ok).toBe(true);
});
});