Skip to content

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

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

import { OutboundRequestService } from "./outbound-requests.ts";

const open = (service: OutboundRequestService, timeoutMs = 60_000) =>
  service.open({ botId: "b1", threadId: "t1", tool: "GMAIL_SEND_EMAIL", timeoutMs });

describe("OutboundRequestService", () => {
  it("settles the waiter with the person's answer, and claims the request exactly once", async () => {
    const service = new OutboundRequestService();
    const held = open(service);
    expect(service.pending("t1")).toEqual([held.requestId]);

    expect(service.resolve({ threadId: "t1", requestId: held.requestId, behavior: "allow" })).toEqual({
      claimed: true,
      state: "allowed",
    });
    await expect(held.answer).resolves.toBe("allow");
    expect(service.pending("t1")).toEqual([]);

    expect(service.resolve({ threadId: "t1", requestId: held.requestId, behavior: "deny" })).toEqual({
      claimed: true,
      state: "already_settled",
      behavior: "allow",
    });
  });

  it("does not claim a request it never opened, or one in another thread", () => {
    const service = new OutboundRequestService();
    const held = open(service);
    expect(service.resolve({ threadId: "t1", requestId: "nope", behavior: "allow" })).toEqual({ claimed: false });
    expect(service.resolve({ threadId: "t2", requestId: held.requestId, behavior: "allow" })).toEqual({ claimed: false });
  });

  it("denies when the person says no", async () => {
    const service = new OutboundRequestService();
    const held = open(service);
    expect(service.resolve({ threadId: "t1", requestId: held.requestId, behavior: "deny" })).toEqual({
      claimed: true,
      state: "denied",
    });
    await expect(held.answer).resolves.toBe("deny");
  });

  it("times out as a denial rather than waiting forever", async () => {
    const service = new OutboundRequestService();
    const held = open(service, 20);
    await expect(held.answer).resolves.toBe("timeout");
    expect(service.pending("t1")).toEqual([]);
    expect(service.resolve({ threadId: "t1", requestId: held.requestId, behavior: "allow" })).toEqual({
      claimed: true,
      state: "already_settled",
      behavior: "timeout",
    });
  });
});

describe("outboundCard", () => {
  it("lists every file a send uploads before its arguments are cut", async () => {
    const { outboundCard } = await import("./outbound-requests.ts");
    const card = outboundCard([{
      slug: "GMAIL_SEND_EMAIL",
      arguments: { to: "[email protected]", body: "x".repeat(800), attachment: "/Users/sam/.private/plans.pdf" },
      files: [{ name: "plans.pdf", path: "/Users/sam/.private/plans.pdf", bytes: 2_500_000 }],
    }]);
    expect(card.summary).toContain("Uploads plans.pdf (/Users/sam/.private/plans.pdf, 2.4 MB)");
    expect(card.summary.indexOf("plans.pdf")).toBeLessThan(card.summary.indexOf("[arguments truncated]"));
    expect(outboundCard([{ slug: "GMAIL_SEND_EMAIL", arguments: { to: "[email protected]" } }]).summary).not.toContain("Uploads");
  });
});