Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { partialSdkFixture } from "./test-support/partialSdkFixture.ts";
import type { ToolsInput } from "@mastra/core/agent";
import type { BrowserRpcParams } from "./browser/BotBrowserTypes.ts";
import { probeTool } from "./test-support/toolProbe.ts";
import {
LocalFilesystem,
LocalSandbox,
Workspace,
type WorkspaceSandbox,
} from "@mastra/core/workspace";
import { describe, expect, it, vi } from "vite-plus/test";
import {
browserRpcErrorMessage,
browserMonitorRetryDelayMs,
createBotBrowser,
createBotBrowserTools,
lightpandaMcpCommand,
type BotBrowserRpc,
} from "./botBrowser.ts";
function rpc() {
const call = vi.fn(async (name: string) =>
name === "tree" ? "semantic tree" : `${name} complete`,
);
const attachment = vi.fn(async () => ({
browserUrl: "http://127.0.0.1:9222",
mcpSessionId: "browser-session-1",
requestHeaders: {},
localRequestHeaders: {},
availableToHostedPlugins: false,
}));
const close = vi.fn(async () => undefined);
const reconnect = vi.fn(async () => undefined);
return { call, attachment, reconnect, close } satisfies BotBrowserRpc;
}
async function executeTool(tool: ToolsInput[string] | undefined, input: BrowserRpcParams) {
return probeTool(tool).execute(input);
}
describe("sandbox bot browser", () => {
it("backs off repeated remote monitor failures to at most one request per minute", () => {
expect([1, 2, 3, 4, 5, 6, 7, 8].map(browserMonitorRetryDelayMs)).toEqual([
1_000, 2_000, 4_000, 8_000, 16_000, 32_000, 60_000, 60_000,
]);
});
it("redacts sensitive browser RPC errors", () => {
expect(
browserRpcErrorMessage({
code: -1,
message: "Browser failed for [email protected] token=secret-value-1234",
}),
).toBe("Browser failed for [REDACTED] [REDACTED]");
});
it("exposes only the Akeru navigate, snapshot, click, and type tools", () => {
expect(Object.keys(createBotBrowserTools(rpc()))).toEqual([
"browser_navigate",
"browser_snapshot",
"browser_click",
"browser_type",
]);
});
it("binds the HTTP MCP browser to loopback without stdin or CDP", () => {
expect(lightpandaMcpCommand("/tmp/lightpanda", 9223, "127.0.0.1")).toBe(
"'/tmp/lightpanda' mcp --host '127.0.0.1' --port 9223",
);
});
it("routes all four tools through one browser RPC session", async () => {
const browserRpc = rpc();
const tools = createBotBrowserTools(browserRpc);
await executeTool(tools.browser_navigate, { url: "https://example.com" });
await executeTool(tools.browser_snapshot, {});
await executeTool(tools.browser_click, { backendNodeId: 42 });
await executeTool(tools.browser_type, { selector: "#email", text: "[email protected]" });
expect(browserRpc.call.mock.calls).toEqual([
["goto", { url: "https://example.com" }],
["tree", {}],
["click", { backendNodeId: 42 }],
["fill", { selector: "#email", value: "[email protected]" }],
]);
});
it("redacts sensitive text from semantic browser snapshots", async () => {
const browserRpc = rpc();
browserRpc.call.mockResolvedValueOnce(
"Email [email protected] token=secret-value-1234 path=/root/.ssh/id_rsa",
);
const result = await executeTool(createBotBrowserTools(browserRpc).browser_snapshot, {});
expect(result).toEqual({
snapshot: "Email [REDACTED] [REDACTED]",
truncated: false,
});
});
it("redacts sensitive text from browser action results", async () => {
const browserRpc = rpc();
browserRpc.call.mockResolvedValue(
"Email [email protected] token=secret-value-1234 path=/root/.ssh/id_rsa",
);
const tools = createBotBrowserTools(browserRpc);
for (const [tool, input] of [
[tools.browser_navigate, { url: "https://example.com" }],
[tools.browser_click, { selector: "button" }],
[tools.browser_type, { selector: "#email", text: "hello" }],
] as const) {
await expect(executeTool(tool, input)).resolves.toEqual({
result: "Email [REDACTED] [REDACTED]",
});
}
});
it("uses the same browser session for tools, MCP attachment, and cleanup", async () => {
const browserRpc = rpc();
const workspace = new Workspace({
filesystem: new LocalFilesystem({ basePath: process.cwd() }),
sandbox: new LocalSandbox({ workingDirectory: process.cwd() }),
});
const makeRpc = vi.fn(() => browserRpc);
const browser = createBotBrowser({
threadId: "thread-1",
workspace,
cacheDir: "/tmp/akeru-browser-test",
makeRpc,
});
await executeTool(browser.tools.browser_click, { selector: "button" });
await expect(browser.attachment()).resolves.toEqual({
browserUrl: "http://127.0.0.1:9222",
mcpSessionId: "browser-session-1",
requestHeaders: {},
localRequestHeaders: {},
availableToHostedPlugins: false,
});
await browser.close();
expect(makeRpc).toHaveBeenCalledOnce();
expect(browserRpc.call).toHaveBeenCalledWith("click", { selector: "button" });
expect(browserRpc.attachment).toHaveBeenCalledOnce();
await browser.reconnect().catch(() => undefined);
expect(browserRpc.reconnect).toHaveBeenCalledOnce();
expect(browserRpc.close).toHaveBeenCalledOnce();
await workspace.destroy();
});
it("creates and reconnects one remote browser while preserving its current URL", async () => {
const outputForCommand = (command: string) => (command === "sh" ? "4242\n" : "");
const executeCommand = vi.fn(async (command: string, args: string[] = []) => {
if (command === "sh" && args[1]?.includes("while kill"))
return await new Promise<never>(() => {});
return {
exitCode: 0,
stdout:
command === "uname"
? args[0] === "-s"
? "Linux\n"
: "x86_64\n"
: outputForCommand(command),
stderr: "",
success: true,
executionTimeMs: 1,
};
});
const workspace = new Workspace({
filesystem: new LocalFilesystem({ basePath: process.cwd() }),
sandbox: partialSdkFixture<WorkspaceSandbox>({
id: "remote-workspace",
provider: "e2b",
executeCommand,
}),
});
const browserEndpoint = vi.fn(async () => ({
url: "https://9223-e2b.example",
requestHeaders: { "e2b-traffic-access-token": "traffic-token" },
}));
const messages: Array<{ method?: string; params?: { name?: string } }> = [];
let session = 0;
const fetchMock = vi.fn(async (_url: string | URL | Request, init?: RequestInit) => {
if (init?.method === "DELETE") return new Response("", { status: 204 });
const message = JSON.parse(String(init?.body)) as {
method?: string;
params?: { name?: string };
};
messages.push(message);
const sessionId =
message.method === "initialize" ? `browser-session-${++session}` : undefined;
const text =
message.params?.name === "session_list"
? JSON.stringify([{ url: "https://example.com/bottom-edge" }])
: "ok";
return new Response(JSON.stringify({ result: { content: [{ text }] } }), {
status: 200,
headers: sessionId ? { "mcp-session-id": sessionId } : {},
});
});
vi.stubGlobal("fetch", fetchMock);
const browser = createBotBrowser({
threadId: "remote-browser",
workspace,
cacheDir: "/tmp/unused-remote-browser-cache",
browserEndpoint,
});
try {
expect(executeCommand).not.toHaveBeenCalled();
expect(browserEndpoint).not.toHaveBeenCalled();
expect(fetchMock).not.toHaveBeenCalled();
await executeTool(browser.tools.browser_snapshot, {});
expect(executeCommand.mock.calls.filter(([command]) => command === "sh")).toHaveLength(2);
await expect(browser.attachment()).resolves.toMatchObject({
browserUrl: "https://9223-e2b.example",
requestHeaders: { "e2b-traffic-access-token": "traffic-token" },
localRequestHeaders: { "e2b-traffic-access-token": "traffic-token" },
availableToHostedPlugins: true,
});
await executeTool(browser.tools.browser_click, { selector: "#bottom-target" });
await browser.reconnect();
await expect(browser.attachment()).resolves.toMatchObject({
mcpSessionId: "browser-session-2",
});
expect(browserEndpoint).toHaveBeenNthCalledWith(1, 9223);
expect(browserEndpoint).toHaveBeenNthCalledWith(2, 9223);
expect(executeCommand.mock.calls.filter(([command]) => command === "sh")).toHaveLength(4);
expect(messages).toContainEqual(
expect.objectContaining({
method: "tools/call",
params: expect.objectContaining({ name: "goto" }),
}),
);
for (const [, init] of fetchMock.mock.calls) {
expect(init?.headers).toMatchObject({
"e2b-traffic-access-token": "traffic-token",
});
}
} finally {
await browser.close();
vi.unstubAllGlobals();
}
});
it("fails closed when a remote workspace has no browser endpoint", async () => {
const browser = createBotBrowser({
threadId: "unsupported-remote-browser",
workspace: new Workspace({
filesystem: new LocalFilesystem({ basePath: process.cwd() }),
sandbox: partialSdkFixture<WorkspaceSandbox>({
id: "unsupported-remote-workspace",
provider: "remote",
executeCommand: vi.fn(),
}),
}),
cacheDir: "/tmp/unused-remote-browser-cache",
});
await expect(browser.attachment()).rejects.toThrow("has no Akeru browser adapter");
await browser.close();
});
it("reports a browser startup failure exactly once", async () => {
const onFailure = vi.fn();
const browser = createBotBrowser({
threadId: "startup-failure",
workspace: new Workspace({
filesystem: new LocalFilesystem({ basePath: process.cwd() }),
sandbox: partialSdkFixture<WorkspaceSandbox>(
{
id: "local",
provider: "local",
executeCommand: vi.fn(),
},
["processes"],
),
}),
cacheDir: "/tmp/unused-remote-browser-cache",
onFailure,
});
await expect(browser.attachment()).rejects.toThrow("cannot host a sandbox browser");
expect(onFailure).toHaveBeenCalledOnce();
await browser.close();
});
it("reports a remote browser process exit through its liveness waiter", async () => {
let finishMonitor!: () => void;
const monitor = new Promise<void>((resolve) => (finishMonitor = resolve));
let failureReceipt!: () => void;
const failureObserved = new Promise<void>((resolve) => (failureReceipt = resolve));
const onFailure = vi.fn(() => failureReceipt());
const executeCommand = vi.fn(async (command: string, args: string[] = []) => {
if (command === "uname") {
return {
exitCode: 0,
stdout: `${args[0] === "-s" ? "Linux" : "x86_64"}\n`,
stderr: "",
success: true,
executionTimeMs: 1,
};
}
if (command === "sh" && args[1]?.includes("while kill")) {
await monitor;
return { exitCode: 0, stdout: "dead", stderr: "", success: true, executionTimeMs: 1 };
}
return { exitCode: 0, stdout: "4242\n", stderr: "", success: true, executionTimeMs: 1 };
});
const workspace = new Workspace({
filesystem: new LocalFilesystem({ basePath: process.cwd() }),
sandbox: partialSdkFixture<WorkspaceSandbox>({
id: "remote",
provider: "e2b",
executeCommand,
}),
});
vi.stubGlobal(
"fetch",
async (_url: string | URL, init?: RequestInit) =>
new Response(JSON.stringify({ result: { content: [{ text: "ok" }] } }), {
status: 200,
headers: init?.method === "POST" ? { "mcp-session-id": "session" } : {},
}),
);
const browser = createBotBrowser({
threadId: "remote-exit",
workspace,
cacheDir: "/tmp/unused-remote-browser-cache",
browserEndpoint: async () => ({ url: "https://remote.example", requestHeaders: {} }),
onFailure,
});
try {
await executeTool(browser.tools.browser_snapshot, {});
finishMonitor();
await failureObserved;
expect(onFailure).toHaveBeenCalledOnce();
} finally {
await browser.close();
vi.unstubAllGlobals();
}
});
it.each(["timed out", "rejected"])(
"continues monitoring a live remote browser after a command %s",
async (failure) => {
let finishMonitor!: () => void;
const monitor = new Promise<void>((resolve) => (finishMonitor = resolve));
let finishSecondMonitor!: () => void;
const secondMonitor = new Promise<void>((resolve) => (finishSecondMonitor = resolve));
let secondMonitorReceipt!: () => void;
const secondMonitorStarted = new Promise<void>((resolve) => (secondMonitorReceipt = resolve));
let failureReceipt!: () => void;
const failureObserved = new Promise<void>((resolve) => (failureReceipt = resolve));
const onFailure = vi.fn((_error) => failureReceipt());
const onReady = vi.fn();
let monitorCalls = 0;
const executeCommand = vi.fn(async (command: string, args: string[] = []) => {
if (command === "uname") {
return {
exitCode: 0,
stdout: `${args[0] === "-s" ? "Linux" : "x86_64"}\n`,
stderr: "",
success: true,
executionTimeMs: 1,
};
}
if (command === "sh" && args[1]?.includes("while kill")) {
monitorCalls += 1;
if (monitorCalls === 1) {
await monitor;
if (failure === "rejected") throw new Error("monitor connection lost");
return {
exitCode: 124,
stdout: "",
stderr: "monitor timed out",
success: false,
executionTimeMs: 30_000,
};
}
secondMonitorReceipt();
await secondMonitor;
return { exitCode: 0, stdout: "dead", stderr: "", success: true, executionTimeMs: 1 };
}
return { exitCode: 0, stdout: "4242\n", stderr: "", success: true, executionTimeMs: 1 };
});
const workspace = new Workspace({
filesystem: new LocalFilesystem({ basePath: process.cwd() }),
sandbox: partialSdkFixture<WorkspaceSandbox>({
id: "remote",
provider: "e2b",
executeCommand,
}),
});
vi.stubGlobal(
"fetch",
async (_url: string | URL, init?: RequestInit) =>
new Response(JSON.stringify({ result: { content: [{ text: "ok" }] } }), {
status: 200,
headers: init?.method === "POST" ? { "mcp-session-id": "session" } : {},
}),
);
const browser = createBotBrowser({
threadId: "remote-monitor-failure",
workspace,
cacheDir: "/tmp/unused-remote-browser-cache",
browserEndpoint: async () => ({ url: "https://remote.example", requestHeaders: {} }),
onFailure,
onReady,
});
try {
const attachment = await browser.attachment();
finishMonitor();
await secondMonitorStarted;
expect(onFailure).not.toHaveBeenCalled();
await expect(browser.attachment()).resolves.toEqual(attachment);
expect(onReady).toHaveBeenCalledOnce();
const readyBeforeSuccessfulTool = onReady.mock.calls.length;
await expect(executeTool(browser.tools.browser_snapshot, {})).resolves.toEqual({
snapshot: "ok",
truncated: false,
});
expect(onReady).toHaveBeenCalledTimes(readyBeforeSuccessfulTool + 1);
finishSecondMonitor();
await failureObserved;
expect(onFailure).toHaveBeenCalledOnce();
expect(executeCommand.mock.calls.filter(([command]) => command === "sh")).toHaveLength(3);
} finally {
await browser.close();
vi.unstubAllGlobals();
}
},
);
});