Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

9.8 KB
Raw Blame
import { describe, expect, it } from "vitest";

import { connectorCallsIn, effectiveOutboundPolicy, effectiveToolName, isOutboundTool, isReadOnlyTool, normalizeOutboundPolicy, outboundCallsIn, trimOutboundPolicy } from "./outbound.ts";

describe("isOutboundTool", () => {
  it("flags tools that send, reply, post, publish, invite, share, or move money", () => {
    for (const name of [
      "GMAIL_SEND_EMAIL",
      "GMAIL_SEND_DRAFT",
      "GMAIL_REPLY_TO_THREAD",
      "SLACK_SEND_MESSAGE",
      "SLACK_CHAT_POST_MESSAGE",
      "TWITTER_CREATION_OF_A_POST",
      "LINKEDIN_CREATE_LINKED_IN_POST",
      "STRIPE_CREATE_REFUND",
      "GITHUB_CREATE_AN_ISSUE_COMMENT",
      "HUBSPOT_SEND_EMAIL",
      "GOOGLECALENDAR_INVITE_ATTENDEES",
      "mcp__composio__WHATSAPP_SEND_MESSAGE",
      "mcp__claude_ai_Gmail__send_message",
      "mcp__claude_ai_Gmail__reply",
      "mcp__claude_ai_Gmail__forward",
      "mcp__claude_ai_Slack__slack_send_message",
      // Only the agents server's own X reads are exempt; another server's post still posts.
      "mcp__x__post",
      "mcp__other__x_post",
      "TWITTER_X_POST",
      // Placing a phone call reaches a person; buying a number moves money.
      "mcp__vapi__create_call",
      "mcp__agentphone__make_call",
      "mcp__agentphone__make_conversation_call",
      "mcp__agentphone__buy_number",
      "TWILIO_MAKE_CALL",
      "mcp__telnyx__dial",
      "mcp__shop__purchase_item",
      // A raw API call can place a call, send a text or buy a number.
      "mcp__telnyx__invoke_api_endpoint",
      "mcp__telnyx-2__invoke_api_endpoint",
      // Publishing an app puts it on the public web.
      "mcp__agents__publish_app",
      "publish_app",
    ]) {
      expect(isOutboundTool(name), name).toBe(true);
    }
  });

  it("leaves reading, searching, drafting, and internal record-keeping alone", () => {
    for (const name of [
      "GMAIL_FETCH_EMAILS",
      "GMAIL_LIST_THREADS",
      "GMAIL_CREATE_EMAIL_DRAFT",
      "SLACK_SEARCH_MESSAGES",
      "REDDIT_GET_POST",
      "STRIPE_LIST_CHARGES",
      "GITHUB_GET_A_PULL_REQUEST",
      "GITHUB_CREATE_AN_ISSUE",
      "LINEAR_CREATE_ISSUE",
      "NOTION_CREATE_PAGE",
      "GOOGLESHEETS_BATCH_UPDATE",
      "GOOGLECALENDAR_CREATE_EVENT",
      "mcp__claude_ai_Gmail__search_threads",
      "mcp__claude_ai_Linear__save_issue",
      "mcp__computer__click",
      "mcp__agents__x_post",
      "agents__x_post",
      "agents_x_post",
      "x_post",
      "mcp__agents__x_user_posts",
      "mcp__vapi__get_call",
      "mcp__vapi__list_calls",
      "mcp__vapi__create_assistant",
      "mcp__agentphone__end_call",
      "mcp__agentphone__list_numbers",
      "mcp__telnyx__list_api_endpoints",
      "mcp__telnyx__get_api_endpoint_schema",
      "mcp__agents__check_app_hosting",
      "Bash",
      "Read",
      "Edit",
    ]) {
      expect(isOutboundTool(name), name).toBe(false);
    }
  });
});

describe("isReadOnlyTool", () => {
  it("recognizes exact MCP reads without treating other non-outbound actions as reads", () => {
    for (const name of [
      "mcp__linear__list_issues",
      "mcp__linear__list_comments",
      "mcp__linear__get_issue",
      "GMAIL_SEARCH_MESSAGES",
      "mcp__agents__check_app_hosting",
      "_1PASSWORD_GET_ITEM",
      "_1PASSWORD_LIST_ITEMS",
    ]) expect(isReadOnlyTool(name), name).toBe(true);

    for (const name of [
      "mcp__linear__save_issue",
      "mcp__linear__delete_issue",
      "mcp__linear__create_comment",
      "mcp__computer__click",
      "Bash",
      "_1PASSWORD_CREATE_ITEM",
    ]) expect(isReadOnlyTool(name), name).toBe(false);
  });
});

describe("normalizeOutboundPolicy", () => {
  it("accepts ask and allow, with no limit of any kind", () => {
    expect(normalizeOutboundPolicy({ policy: "ask" })).toEqual({ policy: "ask" });
    expect(normalizeOutboundPolicy({ policy: "allow" })).toEqual({ policy: "allow" });
  });

  it("accepts a rule an older app saved or sends with a daily cap, whatever the cap, and drops it", () => {
    for (const dailyCap of [10, 0, 2.5, 100_000, "25", null]) {
      expect(normalizeOutboundPolicy({ policy: "allow", dailyCap })).toEqual({ policy: "allow" });
    }
    expect(normalizeOutboundPolicy({ policy: "ask", dailyCap: 25 })).toEqual({ policy: "ask" });
  });

  it("refuses anything else", () => {
    expect(normalizeOutboundPolicy({ policy: "yes" })).toBeNull();
    expect(normalizeOutboundPolicy({ dailyCap: 10 })).toBeNull();
    expect(normalizeOutboundPolicy("allow")).toBeNull();
    expect(normalizeOutboundPolicy(null)).toBeNull();
  });
});

describe("effectiveOutboundPolicy", () => {
  it("sends without asking at Full access when the bot has no rule of its own", () => {
    expect(effectiveOutboundPolicy(undefined, true)).toEqual({ policy: "allow" });
    expect(effectiveOutboundPolicy(undefined, false)).toEqual({ policy: "ask" });
  });

  it("keeps a stored rule at every level", () => {
    expect(effectiveOutboundPolicy({ policy: "ask" }, true)).toEqual({ policy: "ask" });
    expect(effectiveOutboundPolicy({ policy: "allow" }, false)).toEqual({ policy: "allow" });
  });
});

describe("trimOutboundPolicy", () => {
  it("drops a stored daily cap and says so, once", () => {
    const bot = { outbound: { policy: "allow", dailyCap: 25 } as never };
    expect(trimOutboundPolicy(bot)).toBe(true);
    expect(bot.outbound).toEqual({ policy: "allow" });
    expect(trimOutboundPolicy(bot)).toBe(false);
  });

  it("leaves no rule, a current rule and a malformed rule as they are", () => {
    for (const outbound of [undefined, { policy: "ask" }, { policy: "yes", dailyCap: 25 }] as never[]) {
      const bot = { outbound };
      expect(trimOutboundPolicy(bot)).toBe(false);
      expect(bot.outbound).toBe(outbound);
    }
  });
});

describe("connectorCallsIn", () => {
  it("keeps the account a multi-execute item names", () => {
    const calls = connectorCallsIn("COMPOSIO_MULTI_EXECUTE_TOOL", {
      tools: [{ tool_slug: "GMAIL_SEND_EMAIL", arguments: {}, account: " work " }, { tool_slug: "GMAIL_FETCH_EMAILS", arguments: {} }],
    });
    expect(calls).toEqual([{ slug: "GMAIL_SEND_EMAIL", arguments: {}, account: "work" }, { slug: "GMAIL_FETCH_EMAILS", arguments: {} }]);
  });

  // Composio's session exposes a handful of meta tools; the app tools run
  // inside them. The gate has to see through the wrapper or it sees nothing.
  it("returns a direct app tool as itself", () => {
    expect(connectorCallsIn("GMAIL_SEND_EMAIL", { to: "[email protected]" })).toEqual([{ slug: "GMAIL_SEND_EMAIL", arguments: { to: "[email protected]" } }]);
  });

  it("unwraps every tool inside a multi-execute call", () => {
    const calls = connectorCallsIn("COMPOSIO_MULTI_EXECUTE_TOOL", {
      tools: [
        { tool_slug: "GMAIL_FETCH_EMAILS", arguments: { query: "is:unread" } },
        { tool_slug: "SLACK_SEND_MESSAGE", arguments: { channel: "#ops", text: "hi" } },
      ],
      sync_response_to_workbench: false,
    });
    expect(calls.map((call) => call.slug)).toEqual(["GMAIL_FETCH_EMAILS", "SLACK_SEND_MESSAGE"]);
    expect(calls[1].arguments).toEqual({ channel: "#ops", text: "hi" });
  });

  it("treats all arbitrary code as opaque, not as an enumerable send allowance", () => {
    const code = 'for id in ids:\n    run_tool("GMAIL_SEND_EMAIL", {"thread_id": id})\nrun_tool("GMAIL_FETCH_EMAILS", {})';
    expect(connectorCallsIn("COMPOSIO_REMOTE_WORKBENCH", { code_to_execute: code }).map((call) => call.slug)).toEqual([
      "COMPOSIO_PROXY_EXECUTE",
    ]);
    expect(connectorCallsIn("COMPOSIO_REMOTE_WORKBENCH", { code_to_execute: "proxy_execute('POST', 'https://api.x.com')" })
      .map((call) => call.slug)).toEqual(["COMPOSIO_PROXY_EXECUTE"]);
  });

  it("ignores discovery but fails closed for malformed execution arguments", () => {
    expect(connectorCallsIn("COMPOSIO_SEARCH_TOOLS", { queries: [{ use_case: "send an email" }] })).toEqual([]);
    expect(connectorCallsIn("COMPOSIO_MULTI_EXECUTE_TOOL", { tools: "nope" })[0].slug).toBe("COMPOSIO_PROXY_EXECUTE");
    expect(connectorCallsIn("COMPOSIO_MULTI_EXECUTE_TOOL", undefined)[0].slug).toBe("COMPOSIO_PROXY_EXECUTE");
    expect(connectorCallsIn("COMPOSIO_REMOTE_WORKBENCH", { code: 'run_tool("GMAIL_" + "SEND_EMAIL", {})' })[0].slug).toBe("COMPOSIO_PROXY_EXECUTE");
    expect(connectorCallsIn("COMPOSIO_REMOTE_BASH_TOOL", { command: "curl -X POST https://example.test/send" })[0].slug).toBe("COMPOSIO_PROXY_EXECUTE");
    expect(connectorCallsIn("COMPOSIO_PROXY_EXECUTE", {})[0].slug).toBe("COMPOSIO_PROXY_EXECUTE");
  });
});

describe("outboundCallsIn", () => {
  it("keeps only the calls that send", () => {
    const sends = outboundCallsIn("COMPOSIO_MULTI_EXECUTE_TOOL", {
      tools: [{ tool_slug: "GMAIL_FETCH_EMAILS" }, { tool_slug: "GMAIL_SEND_EMAIL", arguments: { to: "x" } }],
    });
    expect(sends.map((call) => call.slug)).toEqual(["GMAIL_SEND_EMAIL"]);
    expect(outboundCallsIn("GMAIL_FETCH_EMAILS", {})).toEqual([]);
    expect(isOutboundTool("COMPOSIO_PROXY_EXECUTE")).toBe(true);
  });
  it("resolves generic ACP tool placeholders from Run mcp_* summaries", () => {
    expect(effectiveToolName("other", "Run mcp_composio_GMAIL_SEND_EMAIL")).toBe("mcp_composio_GMAIL_SEND_EMAIL");
    expect(effectiveToolName("bash", "Run mcp_composio_GMAIL_SEND_EMAIL")).toBe("bash");
    expect(isOutboundTool("other", "Run mcp_composio_GMAIL_SEND_EMAIL")).toBe(true);
    expect(isOutboundTool("other", "Run mcp_composio_COMPOSIO_PROXY_EXECUTE")).toBe(true);
    expect(isOutboundTool("other", "Run mcp_telnyx_invoke_api_endpoint")).toBe(true);
    expect(isOutboundTool("other", "Run mcp_telnyx_list_numbers")).toBe(false);
    expect(isReadOnlyTool("mcp_composio_REDDIT_GET_POST")).toBe(true);
    expect(isReadOnlyTool("mcp_composio_GOOGLECALENDAR_EVENTS_LIST")).toBe(true);
    expect(isOutboundTool("other", "Run mcp_composio_REDDIT_GET_POST")).toBe(false);
    expect(isOutboundTool("other", "Run mcp_computer_click")).toBe(false);
  });
});