Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

5.3 KB
Raw Blame
// server/tests/shared-wiring.integration.test.ts
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { randomUUID } from "node:crypto";
import { eq } from "drizzle-orm";
import { createApp } from "../src/app";
import { createAuditStore } from "../src/audit";
import { loadConfig } from "../src/config";
import { createDatabase } from "../src/db/client";
import { mcpServers } from "../src/db/schema";
import { createAccountModeSwitch } from "../src/plugins/account-mode";
import type { ConnectedAppBroker } from "../src/plugins/broker";
import { createSharedUseStore } from "../src/plugins/shared-use-store";
import { createPluginStore } from "../src/plugins/store";
import { TEST_POOL, testDatabaseUrl } from "./support/database";
import { testEnvironment } from "./support/environment";

/*
 * Every Shared-app surface was once built, tested through its own factory, and never mounted: the
 * route tests passed while production answered 404. These go through `createApp` itself, so a
 * surface that is not wired in fails here rather than in front of an administrator.
 */

const database = createDatabase(testDatabaseUrl(), TEST_POOL);
const suite = randomUUID().slice(0, 8);
const app = `wired-${suite}`;

const broker: ConnectedAppBroker = {
  // This stub stands in for Composio, whose grant is held locally; see `ConnectedAppBroker.grantIsLocal`.
  grantIsLocal: true,
  // Nothing here asks what auth provider a connection is held under.
  authProviderOf: async () => null,
  // Nothing here reads either member; both are declared because the seam requires a broker to
  // SAY what it runs — see `ConnectedAppBroker.authorizations`.
  authorizations: null,
  grantsFor: async () => [],
  listApps: async () => [],
  ensureAuthConfig: async () => "standing",
  deleteAuthConfig: async () => {},
  authorize: async () => ({
    redirectUrl: "https://vendor.example/consent",
    authorizationId: null,
  }),
  isConnected: async () => true,
  revoke: async () => true,
  connectionFields: async () => [],
  connectWithFields: async () => ({ accountId: "ca_1" }),
  revokeAccount: async () => {},
  accountName: async () => null,
};

function build(options: { shared: boolean }) {
  const auditStore = createAuditStore(database);
  const pluginStore = createPluginStore({
    brokerProvider: "composio",
    database,
    auditStore,
    credentials: {} as never,
    encryptionKey: "x".repeat(44),
    policy: () => ({ mode: "enforce", deny: [], allow: ["true"] }),
    broker,
    deploymentId: `acme-${suite}`,
  });
  const use = createSharedUseStore(database, "composio");
  const modes = createAccountModeSwitch({
    store: pluginStore,
    sharedUse: use,
    teamRules: undefined,
    deploymentId: `acme-${suite}`,
    audit: auditStore,
  });
  /*
   * Positional, 36 long. Placed by index rather than by a run of `undefined`s, so a parameter added
   * in the middle moves a type error onto the wrong index here rather than shifting an argument
   * silently. `shared` itself is a field on the `coworker` bag, which is why it needs no index.
   */
  const args = new Array(createApp.length) as unknown as Parameters<
    typeof createApp
  >;
  args[0] = loadConfig(testEnvironment());
  args[1] = {
    handler: () => new Response(null, { status: 204 }),
    api: {
      getSession: async () => ({
        user: {
          id: `admin-${suite}`,
          email: `admin-${suite}@example.test`,
          name: "Admin",
        },
      }),
    },
  } as never;
  args[2] = { rolesForUser: async () => ["admin"] } as never;
  args[12] = auditStore;
  args[14] = pluginStore;
  args[34] = options.shared ? { shared: { modes, use } } : {};
  return createApp(...args);
}

beforeEach(async () => {
  await database.insert(mcpServers).values({
    id: app,
    title: "Team GitHub",
    vendor: "Composio",
    url: `composio://${app}`,
    provenance: "composio",
    authScheme: "OAUTH2",
    accountMode: "personal",
  });
});
afterEach(async () => {
  await database.delete(mcpServers).where(eq(mcpServers.id, app));
});

describe("a deployment built with shared apps", () => {
  test("serves the administrators' request inbox", async () => {
    const response = await build({ shared: true }).request(
      "http://openbot.test/api/approvals/shared-use",
    );
    expect(response.status).toBe(200);
    expect(await response.json()).toMatchObject({ requests: [] });
  });

  test("serves the Personal or Shared switch", async () => {
    const response = await build({ shared: true }).request(
      `http://openbot.test/api/plugins/servers/${app}/account-mode`,
      {
        method: "PUT",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ mode: "shared" }),
      },
    );
    expect(response.status).toBe(200);
    expect(await response.json()).toMatchObject({
      changed: false,
      preview: { mode: "shared" },
    });
  });
});

describe("a deployment built without them", () => {
  test("says the switch is unavailable rather than pretending to switch", async () => {
    const response = await build({ shared: false }).request(
      `http://openbot.test/api/plugins/servers/${app}/account-mode`,
      {
        method: "PUT",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ mode: "shared", confirm: true }),
      },
    );
    expect(response.status).toBe(503);
  });
});