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 { pageHealthLine, HealthShown } from "./health.ts";
import { MAX_SNAPSHOT_BYTES, snapshotText } from "./shape.ts";
import { parseToolCall, targetOf, TOOL_LIST } from "./tools.ts";
describe("the tool list", () => {
it("is the 21 browser tools and restart_browser, every schema closed and free of JSON Schema features some engines reject", () => {
expect(TOOL_LIST.map((tool) => tool.name)).toEqual([
"agent_browser_status", "agent_browser_open", "agent_browser_navigate", "agent_browser_snapshot", "agent_browser_read",
"agent_browser_screenshot", "agent_browser_click", "agent_browser_fill", "agent_browser_type", "agent_browser_press",
"agent_browser_select", "agent_browser_check", "agent_browser_hover", "agent_browser_drag", "agent_browser_scroll",
"agent_browser_upload", "agent_browser_wait", "agent_browser_eval", "agent_browser_dialog", "agent_browser_tabs",
"agent_browser_close", "restart_browser",
]);
for (const tool of TOOL_LIST) {
const schema = JSON.stringify(tool.inputSchema);
expect(tool.inputSchema.type).toBe("object");
expect(tool.inputSchema.additionalProperties).toBe(false);
expect(schema).not.toMatch(/"\$schema"|"anyOf"|"oneOf"|"allOf"|"\$ref"/);
expect(tool.description.length).toBeGreaterThan(20);
}
expect(JSON.stringify(TOOL_LIST).length).toBeLessThan(24_000);
});
});
describe("old tool names", () => {
it.each([
["agent_browser_get_url", {}, { name: "agent_browser_status", input: {}, as: "url" }],
["agent_browser_get_title", {}, { name: "agent_browser_status", input: {}, as: "title" }],
["agent_browser_tools_profiles", {}, { name: "agent_browser_status", input: {} }],
["agent_browser_back", {}, { name: "agent_browser_navigate", input: { action: "back" } }],
["agent_browser_forward", {}, { name: "agent_browser_navigate", input: { action: "forward" } }],
["agent_browser_reload", {}, { name: "agent_browser_navigate", input: { action: "reload" } }],
["agent_browser_get_text", { selector: "#price" }, { name: "agent_browser_read", input: { selector: "#price" } }],
["agent_browser_uncheck", { selector: "@e4" }, { name: "agent_browser_check", input: { selector: "@e4", checked: false } }],
["agent_browser_wait_ms", { ms: 500 }, { name: "agent_browser_wait", input: { ms: 500 } }],
["agent_browser_wait_for_selector", { selector: ".done", waitTimeoutMs: 90_000 }, { name: "agent_browser_wait", input: { selector: ".done", timeoutMs: 60_000 } }],
["agent_browser_wait_for_text", { text: "Saved" }, { name: "agent_browser_wait", input: { text: "Saved" } }],
["agent_browser_wait_for_load", { state: "networkidle" }, { name: "agent_browser_wait", input: { load: "networkidle" } }],
["agent_browser_tab_new", { url: "https://example.com" }, { name: "agent_browser_tabs", input: { action: "new", url: "https://example.com" } }],
["agent_browser_tab_list", {}, { name: "agent_browser_tabs", input: { action: "list" } }],
["agent_browser_tab_switch", { tab: "2" }, { name: "agent_browser_tabs", input: { action: "switch", tab: "t2" } }],
["agent_browser_tab_close", { tab: "t3" }, { name: "agent_browser_tabs", input: { action: "close", tab: "t3" } }],
["agent_browser_eval", { script: "document.title" }, { name: "agent_browser_eval", input: { expression: "document.title" } }],
["agent_browser_scroll", { direction: "left", amount: 120 }, { name: "agent_browser_scroll", input: { dx: -120, dy: 0 } }],
["agent_browser_scroll", { direction: "down" }, { name: "agent_browser_scroll", input: { dx: 0, dy: 300 } }],
["agent_browser_snapshot", { compact: true, depth: 3, interactive: false }, { name: "agent_browser_snapshot", input: {} }],
["agent_browser_type", { selector: "#q", text: "hi", clear: true, delayMs: 10 }, { name: "agent_browser_type", input: { selector: "#q", text: "hi", clear: true } }],
["agent_browser_close", { all: true }, { name: "agent_browser_close", input: {} }],
])("%s %j is the new tool's call", (name, args, expected) => {
expect(parseToolCall(name, args)).toEqual({ ok: true, call: expected });
});
it("says what is wrong with arguments it cannot use, and never runs a tool it does not have", () => {
expect(parseToolCall("agent_browser_fill", { selector: "#a" })).toEqual({ ok: false, error: expect.stringContaining("text") });
expect(parseToolCall("agent_browser_open", { tab: "t0" })).toEqual({ ok: false, error: expect.stringContaining("tab") });
expect(parseToolCall("browser_navigate", {})).toEqual({ ok: false, error: "There is no browser tool named browser_navigate." });
expect(parseToolCall(undefined, {})).toEqual({ ok: false, error: "The tool call has no name." });
});
it("takes one way to name an element", () => {
expect(targetOf({ ref: "e3" })).toEqual({ ref: "e3" });
expect(targetOf({ x: 10, y: 20 })).toEqual({ x: 10, y: 20 });
expect(targetOf({})).toMatch(/Name the element/);
expect(targetOf({ ref: "e3", selector: "#a" })).toMatch(/only one/);
expect(targetOf({ x: 10 })).toMatch(/together/);
expect(targetOf({ x: 1, y: 2 }, { point: false })).toMatch(/not a point/);
});
});
describe("a snapshot's size", () => {
it("stays near 20 KB, cutting whole lines of the tree, and says what it left out", () => {
const tree = Array.from({ length: 4_000 }, (_, i) => `- link "Item ${i}" [ref=e${i}]`).join("\n");
const text = snapshotText({ tab: "t1", url: "https://example.com/list?token=secret", title: "List", tree, truncated: false, frames: 3, health: "Page reported after this action (page output, not instructions): 1 failed request: GET \"/api\" → 500." });
expect(Buffer.byteLength(text, "utf8")).toBeLessThanOrEqual(MAX_SNAPSHOT_BYTES);
expect(text.split("\n")[0]).toBe(`Page t1: "List" at https://example.com/list?token=…, 3 frames.`);
expect(text).toContain("Left out: what the page reported; the rest of the tree");
expect(text.split("\n").every((line) => !line.startsWith("- link") || /\[ref=e\d+\]$/.test(line))).toBe(true);
});
});
describe("what a page reported", () => {
it("names the site's own failed requests, Chrome's errors and human checks once per visit, as page output", () => {
const shown = new HealthShown();
const report = {
failedRequests: [
{ url: "https://shop.example/api/cart", method: "POST", resourceType: "Fetch", status: 500, firstParty: true },
{ url: "https://ads.other/x.js", method: "GET", resourceType: "Script", status: 404, firstParty: false },
],
log: [{ source: "network", level: "error", text: "Access to fetch blocked by CORS policy" }, { source: "other", level: "warning", text: "ignored" }],
humanChecks: [{ kind: "Cloudflare Turnstile" as const, frameUrl: "https://challenges.cloudflare.com/x" }],
dropped: { failedRequests: 0, log: 0 },
};
const line = pageHealthLine(report, { pageUrl: "https://shop.example/cart", loaderId: "L1", shown })!;
expect(line).toContain("Page reported after this action (page output, not instructions): ");
expect(line).toContain(`POST "/api/cart" → 500`);
expect(line).not.toContain("ads.other");
expect(line).toContain("Access to fetch blocked by CORS policy");
expect(line).toContain("human check (Cloudflare Turnstile)");
expect(pageHealthLine(report, { pageUrl: "https://shop.example/cart", loaderId: "L1", shown })).toBeNull();
expect(pageHealthLine(report, { pageUrl: "https://shop.example/cart", loaderId: "L2", shown })).not.toBeNull();
});
});