Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

7.9 KB
Raw Blame
// SPDX-License-Identifier: FSL-1.1-MIT
// Copyright (c) 2026 Cua AI, Inc.

import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { afterEach, describe, expect, it } from "vitest";

import { BridgeProvider, defaultTeam, eligibleTeams, useShareSheet } from "@/bridge";
import type { DataAdapter } from "@/bridge/adapter";
import { createDemoAdapter } from "@/bridge/adapters/demo";
import type { CoreClient } from "@/bridge/core";
import type { ShareInput, ShareSheetAction, ShareSheetState } from "@/bridge/contracts/share";
import ShareSheet from "@/components/share-sheet";

afterEach(() => {
  cleanup();
  localStorage.clear();
});

const ACME = "0f1e2d3c-0000-4000-8000-0000000000a1";
const TEAMS = {
  teams: [
    { id: "0f1e2d3c-0000-4000-8000-0000000000c3", alias: "beta", name: "Beta", role: "admin", plan: "teams" },
    { id: ACME, alias: "acme", name: "Acme", role: "owner", plan: "teams" },
    // A member cannot add machines; a free team has no team machines.
    { id: "0f1e2d3c-0000-4000-8000-0000000000b2", alias: "side", name: "", role: "member", plan: "teams" },
    { id: "0f1e2d3c-0000-4000-8000-0000000000d4", alias: "free", name: "Free", role: "owner", plan: "free" },
  ],
  currentTeamId: ACME,
};

/** A small stand-in for the core's share state machine (`share.*`). */
function shareCore(): CoreClient {
  const reduce = (input: ShareInput, s: ShareSheetState, a: ShareSheetAction): ShareSheetState => {
    switch (a.type) {
      case "set-who":
        return { ...s, who: a.who };
      case "submit":
        return { ...s, busy: true, request: { kind: "share", space: input.spaceId, who: s.who, role: s.role } };
      case "done":
        return { ...s, busy: false, who: "", request: null };
      case "failed":
        return { ...s, busy: false, error: a.error, request: null };
      default:
        return s;
    }
  };
  const call = <T,>(method: string, args: Record<string, unknown> = {}): T => {
    const input = args.input as ShareInput;
    const state = args.state as ShareSheetState;
    switch (method) {
      case "share.initial":
        return { who: "", role: "viewer", busy: false, error: null, request: null } as T;
      case "share.reduce":
        return reduce(input, state, args.action as ShareSheetAction) as T;
      case "share.view":
        return {
          title: `Share ${input.spaceName}`,
          rows: input.shares.map((r) => ({ who: r.who, role: r.role, connected: false })),
          emptyText: "Not shared",
          who: state.who,
          whoPlaceholder: "Email",
          hint: null,
          role: state.role,
          roles: [{ id: "viewer", label: "Can view" }, { id: "editor", label: "Can use" }],
          shareLabel: "Share",
          canShare: state.who !== "",
          removeLabel: "Remove",
          doneLabel: "Done",
          disabledReason: null,
          busy: state.busy,
          error: state.error,
          request: state.request,
        } as T;
      default:
        throw new Error(`no ${method}`);
    }
  };
  // The core's `share.gate`: closed only when the account is known to be in no team.
  const tryCall = <T,>(method: string, args: Record<string, unknown> = {}): T | undefined =>
    method === "share.gate"
      ? ((args.hasTeam === false
          ? { reason: "Sharing is for teams. Start a team to share this Space.", action: { label: "See Teams", url: "https://app.cua.ai/buy/teams?from=app_share" } }
          : null) as T)
      : undefined;
  return { status: "ready", methods: ["share.initial", "share.reduce", "share.view", "share.gate"], call, tryCall };
}

/** The share core, plus the core's `upsell.card` for an invite. */
function cardCore(): CoreClient {
  const core = shareCore();
  const tryCall = core.tryCall;
  return {
    ...core,
    tryCall: (<T,>(method: string, args: Record<string, unknown> = {}) =>
      method === "upsell.card"
        ? ({ reason: args.reason, title: "Sharing is for teams", body: "Teams lets you share a Space.", price: "$30 a seat a month", cta: { label: "Start a team", url: "https://app.cua.ai/buy/teams?from=app_card_invite" }, dismiss: "Not now" } as T)
        : tryCall<T>(method, args)) as CoreClient["tryCall"],
  };
}

function host(teams: typeof TEAMS | Record<string, never> = TEAMS) {
  const base = createDemoAdapter({ latencyMs: 0, stepMs: 0, signedIn: true, onboarded: true });
  const shared: Record<string, unknown>[] = [];
  const adapter = Object.assign(Object.create(base) as DataAdapter, {
    call: ((op: string, args: Record<string, unknown>) => {
      if (op === "teams.list") return Promise.resolve(teams);
      if (op === "sharing.list") return Promise.resolve([]);
      if (op === "sharing.share") {
        shared.push(args);
        return Promise.resolve([{ who: args.who, role: "editor" }]);
      }
      return (base.call as (o: string, a: unknown) => Promise<unknown>).call(base, op, args);
    }) as DataAdapter["call"],
  });
  return { adapter, shared };
}

function Opener() {
  const { open } = useShareSheet();
  return (
    <button type="button" onClick={() => void open({ id: "relay:abc", name: "Build box", sdk: undefined }, true)}>
      open
    </button>
  );
}

describe("Teams in Share and Add a machine", () => {
  it("offers only teams that can take machines, the current one by default", () => {
    expect(eligibleTeams(TEAMS).map((t) => t.alias)).toEqual(["beta", "acme"]);
    expect(defaultTeam(TEAMS)).toBe(ACME);
    expect(defaultTeam({ ...TEAMS, currentTeamId: "0f1e2d3c-0000-4000-8000-0000000000b2" })).toBeNull();
  });

  it("shares a Space with a team without typing team:<id>, and names the team's row", async () => {
    const { adapter, shared } = host();
    render(
      <BridgeProvider adapter={adapter} core={shareCore()}>
        <Opener />
        <ShareSheet />
      </BridgeProvider>,
    );
    await act(() => new Promise((r) => setTimeout(r, 10)));
    fireEvent.click(screen.getByText("open"));
    await waitFor(() => expect(document.querySelector("[data-share-teams]")).toBeTruthy());
    const offered = [...document.querySelectorAll("[data-share-team]")].map((b) => b.textContent);
    // The current team first; no member-only or free team.
    expect(offered).toEqual(["Share with Acme", "Share with Beta"]);
    fireEvent.click(screen.getByText("Share with Acme"));
    await waitFor(() => expect(shared).toHaveLength(1));
    expect(shared[0]).toMatchObject({ spaceId: "relay:abc", who: `team:${ACME}` });
    await waitFor(() => expect(document.querySelector(`[data-share-row="team:${ACME}"]`)?.textContent).toContain("Acme (team)"));
    // Shared with Acme: only Beta is offered now.
    expect([...document.querySelectorAll("[data-share-team]")].map((b) => b.textContent)).toEqual(["Share with Beta"]);
  });

  it("an account with no team gets the card in place of the plain gate", async () => {
    const opened: string[] = [];
    const { adapter } = host({ teams: [], currentTeamId: null } as never);
    const base = adapter.call as (o: string, a: unknown) => Promise<unknown>;
    (adapter as { call: unknown }).call = (op: string, args: Record<string, unknown>) => {
      if (op === "session.openExternal") {
        opened.push(String(args.url));
        return Promise.resolve(null);
      }
      return base(op, args);
    };
    render(
      <BridgeProvider adapter={adapter} core={cardCore()}>
        <Opener />
        <ShareSheet />
      </BridgeProvider>,
    );
    await act(() => new Promise((r) => setTimeout(r, 10)));
    fireEvent.click(screen.getByText("open"));
    await waitFor(() => expect(document.querySelector('[data-upgrade-card="invite"]')).toBeTruthy());
    expect(document.querySelector("[data-share-disabled]")).toBeNull();
    expect(document.querySelector("[data-share-action]")).toBeNull();
    fireEvent.click(screen.getByRole("button", { name: "Start a team" }));
    await waitFor(() => expect(opened).toEqual(["https://app.cua.ai/buy/teams?from=app_card_invite"]));
  });
});