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.

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

// This machine, its devices and the system for the host area's tests (the
// SwiftUI app's FixtureHost, ScriptedHost, FakeAccountTokens,
// FixtureDevices, FixturePresence and FixtureLoginItem). In memory: no
// service, launchd, network, notification, login item or keychain.
import type { DefaultSpace, DeviceEnrollment, DevicesLike, DevicesSnapshot, HostLike, HostSettingsChange, HostSetupOptions, HostStatus, PairingCode, RelayDevice, SpacesSettings } from "../src/native/generated/index";
import type { SystemNote, SystemServices } from "../src/bridge/system";
import type { HostModel } from "../src/model/host";
import { FixtureLoginItem } from "../src/model/login-item";
import { FixtureUpdater } from "../src/model/updates";
import { unconfiguredHost } from "./fixtures";

/** An SDK error as the bindings throw it (`CuaError.<tag>: message`). */
export function sdkError(tag: string, message: string): Error {
  const e = new Error(`CuaError.${tag}: ${message}`);
  return Object.assign(e, { [Symbol.for("typeName")]: "CuaError", tag });
}

/** An in-memory host: setup succeeds (or fails with the script), sharing toggles. */
export class FixtureHost {
  current: HostStatus = unconfiguredHost();
  readonly calls: string[] = [];
  /** Setup's token each time. */
  readonly tokens: (string | undefined)[] = [];
  /** The next setups fail with these first. */
  script: Error[] = [];
  /** The next `configure` fails with this (once). */
  failConfigure: string | null = null;
  /** The next resume for another account fails with this (once), after pausing the host as the real one does. */
  failResume: string | null = null;
  owner = "user-1";
  ownerEmail = "[email protected]";

  get like(): HostLike {
    return this as unknown as HostLike;
  }

  async status() {
    return this.current;
  }

  async setup(o: HostSetupOptions, token: string | undefined) {
    this.tokens.push(token);
    this.calls.push(`setup:${o.mode}:${o.direct ?? o.relayUrl ?? "https://relay.cua.ai"}`);
    const next = this.script.shift();
    if (next) throw next;
    const relay = o.mode === "relay";
    const spare = o.profile === "spare";
    // As cua-host decides when the request leaves it out: a machine set up
    // before keeps its setting; a new relay setup provides Spaces.
    const keep = this.current.configured ? this.current.provideSpaces : relay;
    this.current = {
      ...unconfiguredHost(),
      configured: true,
      mode: o.mode,
      relayUrl: relay ? (o.relayUrl ?? "https://relay.cua.ai") : undefined,
      directUrl: relay ? undefined : `http://${o.direct ?? ""}`,
      machineId: "0123abcd4567",
      name: o.name ?? "This machine",
      sharing: true,
      serviceInstalled: true,
      serviceRunning: true,
      serviceKind: "launchd",
      online: true,
      allow: o.allow,
      shareDesktop: o.shareDesktop ?? !spare,
      provideSpaces: o.provideSpaces ?? (spare || keep),
      owner: relay ? this.owner : undefined,
      ownerEmail: relay ? this.ownerEmail : undefined,
    };
    return this.current;
  }

  async configure(change: HostSettingsChange) {
    this.calls.push(`configure:${change.shareDesktop ?? "-"}:${change.provideSpaces ?? "-"}`);
    if (this.failConfigure) {
      const why = this.failConfigure;
      this.failConfigure = null;
      throw sdkError("Runtime", why);
    }
    if (change.shareDesktop !== undefined) this.current = { ...this.current, shareDesktop: change.shareDesktop };
    if (change.provideSpaces !== undefined) this.current = { ...this.current, provideSpaces: change.provideSpaces };
    // Both off: nothing to share, so sharing stops (as the host does).
    if (!this.current.shareDesktop && !this.current.provideSpaces) this.current = { ...this.current, sharing: false, clients: [] };
    return this.current;
  }

  async stopSharing() {
    this.calls.push("stop");
    this.current = { ...this.current, sharing: false, clients: [] };
    return this.current;
  }

  async startSharing() {
    this.calls.push("start");
    this.current = { ...this.current, sharing: true, pausedSignedOut: false };
    return this.current;
  }

  async pauseSignedOut() {
    this.calls.push("pause");
    if (!this.current.configured || this.current.mode !== "relay") return this.current;
    this.current = { ...this.current, pausedSignedOut: true, sharing: false, online: false, clients: [], serviceInstalled: false, serviceRunning: false };
    return this.current;
  }

  async resumeSignedIn(account: string) {
    this.calls.push(`resume:${account}`);
    const isOwner = account === this.owner || account.toLowerCase() === this.ownerEmail.toLowerCase();
    // Another account: the host registers again for it, paused or not.
    if (!isOwner && this.failResume) {
      const why = this.failResume;
      this.failResume = null;
      this.current = { ...this.current, pausedSignedOut: true, sharing: false, online: false, serviceRunning: false };
      throw sdkError("InvalidArgument", why);
    }
    if (!isOwner) {
      this.owner = account;
      this.ownerEmail = "";
      this.current = { ...this.current, owner: account, ownerEmail: undefined };
    }
    if (!this.current.pausedSignedOut && isOwner) return this.current;
    this.current = { ...this.current, pausedSignedOut: false, sharing: true, online: true, serviceInstalled: true, serviceRunning: true };
    return this.current;
  }

  async remove() {
    this.calls.push("remove");
    this.current = unconfiguredHost();
  }
}

/** The account as host setup sees it: a token store that can be signed out, offline, or refuse to refresh, and a sign-in that can finish or not. */
export class FakeAccountTokens {
  token: string | null = null;
  /** What a forced refresh yields (null: refused). */
  refreshed: string | null = null;
  failures: Error[] = [];
  /** What a sign-in leaves signed in as (null: cancelled). */
  signInGives: string | null = null;
  readonly reads: boolean[] = [];
  signIns = 0;
  progressDuringSignIn: string | null = null;

  wire(model: HostModel): void {
    model.tokenRetryDelays = [0, 0];
    model.accountToken = async (force) => {
      this.reads.push(force);
      const fail = this.failures.shift();
      if (fail) throw fail;
      if (force) this.token = this.refreshed;
      return this.token;
    };
    model.signIn = async () => {
      this.signIns += 1;
      this.progressDuringSignIn = model.progress;
      this.token = this.signInGives;
      return this.signInGives !== null;
    };
  }
}

/** An account on a relay: this machine enrolled, a new device and an expired one asking, and two machines. */
export class FixtureDevices {
  current: DevicesSnapshot;
  readonly calls: string[] = [];
  enrollEnrolled = false;
  enrolledNow = false;
  failApprove: string | null = null;
  /** What `approve` leaves the device as (the relay may not enroll it). */
  approveState = "enrolled";
  /** `snapshot` fails with this (a device the relay has not enrolled). */
  failSnapshot: string | null = null;

  constructor(now = BigInt(Math.floor(Date.now() / 1000))) {
    const day = 86_400n;
    const device = (id: string, name: string, state: string, platform: string, until?: bigint, seen?: bigint, current = false): RelayDevice => ({
      id,
      name,
      state,
      platform,
      createdAt: now - 40n * day,
      enrolledUntil: until,
      lastSeen: seen,
      current,
    });
    this.current = {
      localDeviceId: "dev_mac",
      devices: [
        device("dev_mac", "MacBook Pro", "enrolled", "macos", now + 24n * day, now - 60n, true),
        device("dev_work", "Work laptop", "pending", "windows"),
        device("dev_old", "Old laptop", "expired", "linux", now - 2n * day, now - 33n * day),
      ],
      enforceAfter: now - day,
      audit: [],
      machineNames: new Map([["m2", "build-box"]]),
      machines: [
        { id: "m2", spaceId: "relay:m2", name: "build-box", ownerId: "me", ownerEmail: undefined, role: "owner", online: false, sharing: true, version: "0.5.0", url: "", allow: [], clients: [], confirmed: false },
      ],
    };
  }

  get like(): DevicesLike {
    return this as unknown as DevicesLike;
  }

  async snapshot() {
    if (this.failSnapshot) throw sdkError("PermissionDenied", this.failSnapshot);
    return this.current;
  }

  async enroll(): Promise<DeviceEnrollment> {
    this.calls.push("enroll");
    const me = this.current.devices.find((d) => d.current)!;
    return { device: me, enrolled: this.enrollEnrolled, code: this.enrollEnrolled ? undefined : "K7QX-M2RP" };
  }

  async checkEnrolled() {
    this.calls.push("check");
    return this.enrolledNow;
  }

  async approve(code: string | undefined, deviceId: string | undefined): Promise<RelayDevice> {
    this.calls.push(`approve:${code ?? deviceId ?? ""}`);
    if (this.failApprove) throw sdkError("PermissionDenied", this.failApprove);
    const i = this.current.devices.findIndex((d) => d.id === deviceId || (code !== undefined && d.state === "pending"));
    const d = { ...this.current.devices[i]!, state: this.approveState };
    this.current = { ...this.current, devices: this.current.devices.map((x, j) => (j === i ? d : x)) };
    return d;
  }

  async rename(id: string, name: string) {
    this.calls.push(`rename:${id}:${name}`);
    this.current = { ...this.current, devices: this.current.devices.map((d) => (d.id === id ? { ...d, name } : d)) };
    return this.current.devices.find((d) => d.id === id)!;
  }

  async renameMachine(id: string, name: string) {
    this.calls.push(`rename-machine:${id}:${name}`);
    this.current = { ...this.current, machines: this.current.machines.map((m) => (m.id === id ? { ...m, name } : m)) };
    return this.current.machines.find((m) => m.id === id)!;
  }

  async revoke(id: string) {
    this.calls.push(`revoke:${id}`);
    this.current = { ...this.current, devices: this.current.devices.map((d) => (d.id === id ? { ...d, state: "revoked" } : d)) };
    return this.current.devices.find((d) => d.id === id)!;
  }

  async confirmMachine(id: string) {
    this.calls.push(`confirm-machine:${id}`);
    this.current = { ...this.current, machines: this.current.machines.map((m) => (m.id === id ? { ...m, confirmed: true } : m)) };
    return this.current.machines.find((m) => m.id === id)!;
  }

  /** The account's pairing codes as the relay keeps them (with the codes). */
  pairing: PairingCode[] = [];

  async createPairingCode(team: string | undefined): Promise<PairingCode> {
    this.calls.push(`create-pairing-code:${team ?? ""}`);
    const now = BigInt(Math.floor(Date.now() / 1000));
    const n = this.pairing.length + 1;
    const code = `K7QM-4XPB-${String(n).padStart(4, "0")}`;
    const made: PairingCode = {
      id: `pc_fixture${n}`,
      code,
      url: `https://cua.ai/add/${code}`,
      installSh: `curl -fsSL https://cua.ai/install.sh | sh -s -- --pair ${code}`,
      installPowershell: `& ([scriptblock]::Create((irm https://cua.ai/install.ps1))) -Pair ${code}`,
      state: "active",
      team,
      createdAt: now,
      expiresAt: now + 1800n,
      usedAt: undefined,
      machine: undefined,
    };
    this.pairing = [made, ...this.pairing];
    return made;
  }

  async pairingCodes(): Promise<PairingCode[]> {
    this.calls.push("pairing-codes");
    return this.pairing.map((p) => ({ ...p, code: undefined, url: undefined, installSh: undefined, installPowershell: undefined }));
  }

  async revokePairingCode(id: string): Promise<void> {
    this.calls.push(`revoke-pairing-code:${id}`);
    this.pairing = this.pairing.map((p) => (p.id === id ? { ...p, state: "revoked" } : p));
  }

  /** The account's default Space as the relay keeps it (unset unless set). */
  spaces: SpacesSettings = { defaultSpace: undefined, fallback: [], fallbackOn: true, updatedAt: 0n };

  async spacesSettings(): Promise<SpacesSettings> {
    this.calls.push("spaces-settings");
    return this.spaces;
  }

  async setSpacesSettings(settings: SpacesSettings): Promise<SpacesSettings> {
    this.calls.push(`set-spaces-settings:${settings.defaultSpace ?? ""}`);
    this.spaces = { ...settings, updatedAt: BigInt(Math.floor(Date.now() / 1000)) };
    return this.spaces;
  }

  /** The relay's answer over the fixture account's machines: the default, every other machine, then this one. */
  async resolveDefaultSpace(): Promise<DefaultSpace> {
    this.calls.push("resolve-default-space");
    const machines = this.current.machines.map((m) => ({ space: `relay:${m.id}`, name: m.name, online: m.online }));
    const def = machines.find((m) => m.space === this.spaces.defaultSpace);
    const order = [
      ...(def ? [{ ...def, role: "default" }] : []),
      ...(this.spaces.fallbackOn ? machines.filter((m) => m !== def).map((m) => ({ ...m, role: "fallback" })) : []),
      { space: "relay:self-id", name: "This Mac", online: true, role: "here" },
    ];
    const first = order.find((o) => o.online && o.role !== "here");
    return {
      space: first?.space,
      reason: first ? (first.role === "default" ? "default" : this.spaces.defaultSpace ? "fallback" : "unset") : "here",
      defaultSpace: this.spaces.defaultSpace,
      defaultOnline: def ? def.online : undefined,
      order,
    };
  }
}

/** The system in memory: what was opened, shown, posted and asked. */
export class FixtureSystem implements SystemServices {
  readonly opened: string[] = [];
  readonly revealed: string[] = [];
  readonly notes: SystemNote[] = [];
  readonly asked: string[] = [];
  allow = true;
  loginItem = new FixtureLoginItem();
  updater = new FixtureUpdater(Date.UTC(2026, 9, 7, 12, 0));
  shortcut: { label: string; registered: boolean } | null = { label: "\u2318\u21e7Space", registered: false };
  constructor(readonly home: string) {}
  globalShortcut() {
    return this.shortcut;
  }
  /** Settings, General, Change: the keys asked for; "Command+K" is held by another app. */
  readonly shortcutChanges: string[] = [];
  setGlobalShortcut(accelerator: string) {
    if (accelerator === "Command+K") throw new Error("Another app is using \u2318K. Pick other keys.");
    this.shortcutChanges.push(accelerator);
    this.shortcut = { label: accelerator, registered: true };
    return this.shortcut;
  }
  async openExternal(url: string) {
    this.opened.push(url);
  }
  reveal(p: string) {
    this.revealed.push(p);
  }
  notify(note: SystemNote) {
    this.notes.push(note);
  }
  async confirmPresence(reason: string) {
    this.asked.push(reason);
    if (!this.allow) throw new Error("Approval was cancelled");
  }
}

/** The bridge methods `host-bridge.test.ts` checks (the shared shape test leaves them to it). */
export const HOST_METHODS = [
  "volume.overview", "volume.storage", "volume.storageSet", "volume.mount", "volume.unmount",
  "volume.approve", "volume.deny", "volume.revoke", "volume.resolve", "volume.reveal",
  "about.get", "about.set", "about.checkNow", "update.get", "update.install", "loginItem.get", "loginItem.set", "loginItem.openSettings",
  "devices.get", "devices.enroll", "devices.checkEnrolled", "devices.approve", "devices.rename", "devices.revoke", "devices.confirmMachine",
  "devices.createPairingCode", "devices.pairingCodes", "devices.revokePairingCode", "teams.list",
  "storage.get", "storage.run", "notifications.list", "notifications.markAllRead", "notifications.markRead",
  "host.setUp", "host.joinWithCode", "host.action", "host.limits", "host.openSettings", "settings.get", "settings.choose", "settings.update",
  "onboarding.get", "onboarding.complete", "onboarding.reset",
];

/** The page's operations over them (the shared adapter test leaves them to it too). */
export const HOST_OPS = [
  "volume.overview", "volume.storage", "volume.storageSet", "volume.mount", "volume.unmount",
  "volume.approve", "volume.deny", "volume.revoke", "volume.resolve", "volume.reveal",
  "about.get", "about.set", "about.checkNow", "update.get", "update.install", "loginItem.get", "loginItem.set", "loginItem.openSettings",
  "devices.get", "devices.enroll", "devices.checkEnrolled", "devices.approve", "devices.rename", "devices.revoke", "devices.confirmMachine",
  "devices.createPairingCode", "devices.pairingCodes", "devices.revokePairingCode", "teams.list",
  "storage.get", "storage.run", "notifications.list", "notifications.markAllRead", "notifications.markRead",
  "host.setUp", "host.joinWithCode", "host.action", "host.limits", "host.openSettings", "settings.get", "settings.set", "settings.choose", "experiments.set", "session.completeOnboarding", "session.resetOnboarding",
];