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.

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

// Synthetic Spaces, host, devices, account and telemetry for the bridge's
// tests on the real app core (the SwiftUI app's FixtureSpacesBackend,
// FixtureHost, FixtureDevices, FixtureAccount and FixtureTelemetry). Never
// contacts anything.
import type { Native } from "../src/native/load";
import type {
  AppCreateSpaceArgs,
  AppTelemetryInput,
  AppTelemetrySignal,
  AppSpaceRow,
  DevicesLike,
  DevicesSnapshot,
  HostLike,
  HostStatus,
  SpaceAppIconRequest,
  SpaceCreateProgress,
  SpaceLike,
  SpacePresenceLike,
  TeleportLike,
} from "../src/native/generated/index";
import type {
  BulkResult,
  LocalRuntimes,
  MachineHealthReport,
  NetworkEventJson,
  ServiceJson,
  ShareLinkRow,
  ShareSettingsRow,
  SpaceNetworkJson,
  SpaceThumbnailData,
  SpacesBackend,
  TreeBudgetJson,
  TreeBudgetLimitsJson,
  TreeNodeJson,
} from "../src/model/backend";
import type { StreamWindow } from "../src/model/streams";
import type { AccountProfile, AccountRunning, LinkNoticeView, SignInAttempt, TelemetryRunning } from "../src/model/services";

export function sampleRows(n: Native): AppSpaceRow[] {
  const row = (r: Partial<AppSpaceRow> & Pick<AppSpaceRow, "id" | "name" | "provider">): AppSpaceRow => ({
    displayName: undefined,
    spacesdVersion: "0.4.0",
    features: [],
    addedAt: undefined,
    os: undefined,
    osName: undefined,
    osPrettyName: undefined,
    image: undefined,
    imageDigest: undefined,
    kind: undefined,
    arch: undefined,
    hidden: false,
    reachable: true,
    error: undefined,
    host: undefined,
    hostName: undefined,
    power: undefined,
    powerState: undefined,
    cloud: undefined,
    cloudPlace: undefined,
    cloudDelete: undefined,
    ...r,
  });
  return [
    row({
      id: "local:aurora",
      name: "0123456789ab",
      provider: "local",
      features: ["desktop_stream", "window_stream"],
      addedAt: "2026-09-25T08:00:00Z",
      os: n.AppSpaceOs.Linux,
      osName: "Ubuntu",
      osPrettyName: "Ubuntu 24.04.3 LTS",
      image: "ghcr.io/trycua/linux:24.04",
      kind: n.AppSpaceKind.Container,
      arch: "arm64",
      power: "suspend",
      powerState: "running",
    }),
    row({ id: "direct:builder", name: "builder", provider: "direct", features: ["desktop_stream"], addedAt: "2026-09-24T08:00:00Z", os: n.AppSpaceOs.Linux, osName: "Arch Linux", reachable: false, error: "timed out" }),
    row({ id: "direct:10.0.0.5:3211", name: "10.0.0.5:3211", provider: "direct", addedAt: "2026-09-23T08:00:00Z", os: n.AppSpaceOs.Windows, osName: "Windows" }),
  ];
}

export class FixtureSpacesBackend implements SpacesBackend {
  rowsNow: AppSpaceRow[];
  readonly created: AppCreateSpaceArgs[] = [];
  readonly removed: { id: string; removeOnly: boolean }[] = [];
  readonly power: { id: string; on: boolean }[] = [];
  readonly cancelled = new Set<string>();
  /** Progress a fixture create reports, in order (the SDK's words). */
  phases: Omit<SpaceCreateProgress, "space">[] = [
    { phase: "preparing", fraction: undefined, detail: "", bytesDone: undefined, bytesTotal: undefined, bytesPerSecond: undefined },
    { phase: "pulling", fraction: 0.5, detail: "ghcr.io/trycua/linux:24.04", bytesDone: 5n, bytesTotal: 10n, bytesPerSecond: 2.5 },
    { phase: "ready", fraction: undefined, detail: "", bytesDone: undefined, bytesTotal: undefined, bytesPerSecond: undefined },
  ];
  /** Holds each create until `release` (to cancel it meanwhile). */
  hold: Promise<void> | null = null;
  createError: Error | null = null;

  constructor(private readonly native: Native) {
    this.rowsNow = sampleRows(native);
  }

  async rows() {
    return this.rowsNow;
  }

  async create(args: AppCreateSpaceArgs, createId: string, progress: (p: SpaceCreateProgress) => void) {
    this.created.push(args);
    for (const p of this.phases.slice(0, -1)) progress({ ...p, space: createId });
    if (this.hold) await this.hold;
    if (this.cancelled.has(createId)) {
      const e = new Error(`CuaError.Cancelled: Cancelled ${createId}; removed what it made.`);
      throw Object.assign(e, { [Symbol.for("typeName")]: "CuaError", tag: "Cancelled" });
    }
    if (this.createError) throw this.createError;
    progress({ ...this.phases.at(-1)!, space: createId });
    const id = `local:${args.name ?? `space-${this.created.length}`}`;
    this.rowsNow = [
      ...this.rowsNow,
      { ...this.rowsNow[0]!, id, name: args.name ?? "", image: args.image, provider: "local", powerState: undefined, power: undefined },
    ];
    return id;
  }

  async cancelCreate(createId: string) {
    this.cancelled.add(createId);
  }

  async gpuChoices() {
    return [];
  }
  async hosts() {
    return [];
  }
  reportedHostname() {
    return null;
  }
  readonly added: { url: string; token: string | null; name: string | null }[] = [];
  /** A Space at an address: listed once added. */
  async add(url: string, token: string | null, name: string | null) {
    this.added.push({ url, token, name });
    const id = `direct:${url.replace(/^\w+:\/\//, "")}`;
    this.rowsNow = [...this.rowsNow, { ...this.rowsNow[2]!, id, name: name ?? url.replace(/^\w+:\/\//, "") }];
  }
  async remove(id: string, removeOnly: boolean) {
    this.removed.push({ id, removeOnly });
    this.rowsNow = this.rowsNow.filter((r) => r.id !== id);
  }
  /** Ids whose bulk delete fails, and why. */
  readonly bulkErrors = new Map<string, string>();
  readonly left: string[] = [];
  async deleteMany(ids: string[]): Promise<BulkResult[]> {
    return ids.map((id) => {
      const error = this.bulkErrors.get(id);
      if (error) return { id, ok: false, error };
      this.removed.push({ id, removeOnly: false });
      this.rowsNow = this.rowsNow.filter((r) => r.id !== id);
      return { id, ok: true };
    });
  }
  async leave(ids: string[]): Promise<BulkResult[]> {
    return ids.map((id) => {
      const row = this.rowsNow.find((r) => r.id === id);
      if (!row?.sharing || row.sharing.role === "owner") return { id, ok: false, error: `Couldn't leave ${row?.name ?? id}: it's yours; delete it instead.` };
      this.left.push(id);
      this.rowsNow = this.rowsNow.filter((r) => r.id !== id);
      return { id, ok: true };
    });
  }
  readonly renamed: { id: string; name: string }[] = [];
  /** What the next rename throws (the SDK's error), once. */
  renameError: Error | null = null;
  async rename(id: string, name: string) {
    this.renamed.push({ id, name });
    const failure = this.renameError;
    this.renameError = null;
    if (failure) throw failure;
    this.rowsNow = this.rowsNow.map((r) => (r.id === id ? { ...r, displayName: name } : r));
  }
  /** What `machineHealth` answers, by machine (`this` for this one); a string is the error it throws. */
  health = new Map<string, MachineHealthReport | string>();
  readonly healthAsked: (string | null)[] = [];
  async machineHealth(machine: string | null): Promise<MachineHealthReport> {
    this.healthAsked.push(machine);
    const h = this.health.get(machine ?? "this");
    if (typeof h === "string") throw new Error(h);
    return h ?? { cuaVersion: "0.4.1", checks: [] };
  }
  readonly unregistered: string[] = [];
  /** The machines taken off the relay with the Spaces made on them deleted too. */
  readonly unregisteredWithSpaces: string[] = [];
  async relayUnregister(machineId: string, deleteSpaces = false) {
    this.unregistered.push(machineId);
    if (deleteSpaces) this.unregisteredWithSpaces.push(machineId);
    this.rowsNow = this.rowsNow.filter((r) => r.id !== `relay:${machineId}`);
  }
  async setPower(id: string, on: boolean) {
    this.power.push({ id, on });
    this.rowsNow = this.rowsNow.map((r) => (r.id === id ? { ...r, powerState: on ? "running" : r.power === "stop" ? "stopped" : "suspended", reachable: on } : r));
  }
  async localRuntimes(): Promise<LocalRuntimes> {
    return { ready: ["docker"], details: new Map([["lume", "not installed"]]) };
  }
  async lumeSource() {
    return "auto";
  }
  async setLumeSource() {}
  async linuxSource() {
    return "auto";
  }
  async setLinuxSource() {}
  runtimeSettings = new Map<string, string>();
  async runtimeKinds() {
    return ["container", "vm", "microvm"].map((kind) => ({
      kind,
      source: this.runtimeSettings.get(`runtime.${kind}`) ?? "auto",
      path: this.runtimeSettings.get(`runtime.${kind}_path`),
      detected: kind === "container" ? [{ tool: "docker", name: "Docker", path: "/usr/local/bin/docker", version: "27.3.1", guests: undefined }] : [],
      check: undefined,
      expected: kind === "container" ? ["Docker", "Podman"] : [],
    }));
  }
  async runtimeCheckPath(kind: string, path: string) {
    const docker = kind === "container" && path === "/usr/local/bin/docker";
    return { path, exists: docker, executable: docker, tool: docker ? "docker" : undefined, name: docker ? "Docker" : undefined, version: docker ? "27.3.1" : undefined, minVersion: undefined, expected: ["Docker", "Podman"], guests: undefined };
  }
  async setRuntimeSetting(key: string, value: string | null) {
    if (value === null) this.runtimeSettings.delete(key);
    else this.runtimeSettings.set(key, value);
  }
  repoCache = { enabled: true, sizeSetting: "auto", repos: 3, bytes: 1_288_490_189n };
  private repoCacheOf() {
    const cap = this.repoCache.sizeSetting === "auto" ? 5 : Number(this.repoCache.sizeSetting);
    return {
      enabled: this.repoCache.enabled,
      repos: this.repoCache.repos,
      bytes: this.repoCache.bytes,
      capBytes: BigInt(cap) * 1024n ** 3n,
      sizeSetting: this.repoCache.sizeSetting,
      summary: `Repos: ${this.repoCache.repos}, 1.2 GB of ${cap} GB`,
      week: "Last week: 80% of clones came from the cache, about 2 minutes saved",
      hits: 8,
      misses: 2,
      secondsSaved: 125n,
    };
  }
  async repoCacheStatus() {
    return this.repoCacheOf();
  }
  async setRepoCache(change: { enabled?: boolean; sizeGb?: number | null }) {
    if (change.enabled !== undefined) this.repoCache.enabled = change.enabled;
    if (change.sizeGb !== undefined) this.repoCache.sizeSetting = change.sizeGb === null ? "auto" : String(change.sizeGb);
    return this.repoCacheOf();
  }
  async clearRepoCache() {
    this.repoCache.repos = 0;
    this.repoCache.bytes = 0n;
    return this.repoCacheOf();
  }
  async localStorage() {
    return null;
  }
  async loggedIn() {
    return false;
  }
  async runningMacosVms() {
    return 1;
  }
  async usage() {
    return null;
  }
  /** Run records (`agent_list` JSON), per Space id. */
  fixtureRuns: Record<string, string[]> = {};
  agentRunsError: Error | null = null;
  async agentRuns(id: string) {
    if (this.agentRunsError) throw this.agentRunsError;
    return this.native.appAgentRows(this.fixtureRuns[id] ?? []);
  }
  /** The fixture Space's windows (an empty list for any other). */
  windowsNow: StreamWindow[] = [
    { id: "w-1", app: "Terminal", title: "cua@space: ~", epoch: 3, width: 1280, height: 800, appId: "org.gnome.Terminal", pid: 412 },
    { id: "w-2", app: "Thunar", title: "", epoch: 1, width: 0, height: 0, appId: "", pid: 0 },
  ];
  windowsError: Error | null = null;
  async windows(id: string): Promise<StreamWindow[]> {
    if (this.windowsError) throw this.windowsError;
    return id === "local:aurora" ? this.windowsNow : [];
  }
  async primaryDisplay() {
    return { widthPx: 1920, heightPx: 1080 };
  }
  /** Thumbnail asks, in order (`[id, maxAgeMs]`). */
  readonly thumbnailCalls: [string, number | null][] = [];
  thumbnails = new Map<string, SpaceThumbnailData>();
  async thumbnail(id: string, maxAgeMs: number | null) {
    this.thumbnailCalls.push([id, maxAgeMs]);
    return this.thumbnails.get(id) ?? null;
  }
  readonly sent: { id: string; paths: string[] }[] = [];
  async sendFiles(id: string, paths: string[]) {
    this.sent.push({ id, paths });
    return paths.map((p) => ({ name: p.split("/").pop() ?? p, dest: `/home/cua/Desktop/${p.split("/").pop()}`, bytes: 1024n }));
  }
  /** Who each Space is shared with. */
  sharesNow = new Map<string, { who: string; role: string; connected: boolean }[]>();
  async shares(id: string) {
    return this.sharesNow.get(id) ?? [];
  }
  async share(id: string, who: string, role: string) {
    const rows = (this.sharesNow.get(id) ?? []).filter((r) => r.who !== who);
    this.sharesNow.set(id, [...rows, { who, role, connected: false }]);
    return this.shares(id);
  }
  linksNow: ShareLinkRow[] = [];
  async links() {
    return [...this.linksNow];
  }
  async createLink(target: string, access: string | null, ttlSeconds: number | null, name: string | null) {
    const id = `share-${String(this.linksNow.length + 1).padStart(12, "0")}`;
    const row: ShareLinkRow = {
      id,
      url: `https://s${id}.m.relay.example/`,
      name,
      access: access === "team" || access === "public" ? access : "private",
      kind: target.startsWith("space:") ? "space_port" : /^\d+$/.test(target) ? "port" : "file",
      target,
      owner: "[email protected]",
      createdAtUnix: 1_000,
      expiresAtUnix: ttlSeconds ? 1_000 + ttlSeconds : null,
      tcpConnectWith: null,
      tcpAddress: null,
      route: null,
      routeLine: null,
      allow: [],
      waiting: null,
      waitingText: null,
    };
    this.linksNow = [row, ...this.linksNow];
    return row;
  }
  async approveLink(id: string, _always: boolean) {
    const row: ShareLinkRow = this.linksNow.find((l) => l.id === id) ?? {
      id,
      url: "tcp://tcp.m.relay.example:26001",
      name: null,
      access: "public",
      kind: "space_port",
      target: "space:dev:25565",
      owner: "[email protected]",
      createdAtUnix: 1_000,
      expiresAtUnix: 1_000 + 86_400,
      tcpConnectWith: "tcp.m.relay.example:26001",
      tcpAddress: "tcp.m.relay.example:26001",
      route: "relay",
      routeLine: "Through Cua",
      allow: [],
      waiting: null,
      waitingText: null,
    };
    return { ...row, waiting: null, waitingText: null };
  }
  async denyLink(_id: string) {}
  shareSettingsNow: ShareSettingsRow = {
    landing: "auto",
    relayFallback: true,
    dailyCapMib: 2048,
    defaultAllow: [],
    approveAgentShares: true,
    alwaysAllowSpaces: [],
    edgeConsent: [],
    usedToday: 0,
    edgeCloud: null,
    edgeHost: null,
    edgeMonthlyUsd: null,
  };
  async shareSettings() {
    return { ...this.shareSettingsNow };
  }
  async setShareSettings(s: ShareSettingsRow) {
    const cur = this.shareSettingsNow;
    this.shareSettingsNow = { ...s, usedToday: cur.usedToday, edgeCloud: cur.edgeCloud, edgeHost: cur.edgeHost, edgeMonthlyUsd: cur.edgeMonthlyUsd };
    return { ...this.shareSettingsNow };
  }
  async removeShareEdge() {
    return [] as string[];
  }
  async network(_id: string): Promise<SpaceNetworkJson> {
    return {
      preset: "internet",
      localhostPorts: [],
      allLocalhostPorts: false,
      allow: [],
      deny: [],
      customBase: "",
      widenedBy: null,
      summary: "Internet only",
      widenings: [],
      enforcement: "enforced",
      point: "the host firewall",
      note: null,
      parent: null,
      root: null,
      alwaysBlocked: [],
      noticePending: false,
      via: null,
      topologyJson: "",
      clipboard: "viewing",
      volume: "own",
    };
  }
  async setNetwork(_id: string, _word: string, _allow: string[], _deny: string[], _widen: boolean, _tree: boolean) {
    return "now" as const;
  }
  async networkLog(_id: string): Promise<NetworkEventJson[]> {
    return [];
  }
  async ackNetworkNotice(_id: string) {}
  /** The Spaces whose exit is this computer, as `exitsThroughThisMachine` reports them. */
  exitsNow: string[] = [];
  async exitsThroughThisMachine() {
    return [...this.exitsNow];
  }
  async setAccess(_id: string, _clipboard: string | null, _volume: string | null) {}
  async spaceTree(): Promise<TreeNodeJson[]> {
    return [{ id: "local:aurora", parent: null, depth: 0, children: false, home: null }];
  }
  async setChildren(_id: string, _enabled: boolean) {}
  async treeBudget(id: string): Promise<TreeBudgetJson> {
    return {
      root: id,
      set: {},
      maxSpaces: 8,
      maxVcpus: 16,
      maxMemoryMb: 32768,
      usdPerHour: 5,
      usdTotal: null,
      spaces: 1,
      vcpus: 2,
      memoryMb: 4096,
      usdPerHourUsed: 0,
      usdSpent: 0,
    };
  }
  async setTreeBudget(id: string, limits: TreeBudgetLimitsJson): Promise<TreeBudgetJson> {
    return { ...(await this.treeBudget(id)), set: limits };
  }
  async services(_id: string): Promise<ServiceJson[]> {
    return [];
  }
  async controlService(_id: string, _name: string, _action: string) {}
  async stopLink(id: string) {
    const gone = this.linksNow.filter((l) => id === "all" || l.id === id).map((l) => l.id);
    this.linksNow = this.linksNow.filter((l) => !gone.includes(l.id));
    return gone;
  }
  async unshare(id: string, who: string) {
    this.sharesNow.set(id, (this.sharesNow.get(id) ?? []).filter((r) => r.who !== who));
    return this.shares(id);
  }
  /** The daemon's tool calls, in order. */
  readonly toolCalls: [string, Record<string, unknown>][] = [];
  cloudConnected = false;
  /** What `cloud_approve` was asked, in order. */
  readonly cloudApprovals: { provider: string; deny: boolean }[] = [];
  async tool(tool: string, args: Record<string, unknown> = {}): Promise<unknown> {
    this.toolCalls.push([tool, args]);
    const aws = (connected: boolean) => ({
      name: "aws",
      title: "AWS",
      tier: "vm",
      connected,
      credentials: { found: true, source: "~/.aws profile default" },
      region: "us-west-2",
      label: "AWS \u00B7 us-west-2",
      ttl_hours: 8,
      kinds: [
        { image: "linux", kind: "container", supported: true, machine_type: "t4g.medium", usd_per_hour: 0.0368 },
        { image: "windows", kind: "vm", supported: false, reason: "Windows on AWS is not offered yet." },
      ],
    });
    if (tool === "cloud_status") return { default_on: this.cloudConnected ? "aws" : "local", providers: [aws(this.cloudConnected)] };
    if (tool === "cloud_test") return { provider: "aws", ok: true, account: "1", checks: [{ name: "credentials", ok: true, detail: "account 1" }] };
    if (tool === "cloud_connect") {
      this.cloudConnected = true;
      return { ...aws(true), checks: [{ name: "credentials", ok: true, detail: "account 1" }] };
    }
    if (tool === "cloud_disconnect") {
      this.cloudConnected = false;
      return { provider: "aws", disconnected: true };
    }
    if (tool === "cloud_approve") {
      this.cloudApprovals.push(args as { provider: string; deny: boolean });
      return aws(this.cloudConnected);
    }
    throw new Error(`unknown tool ${tool}`);
  }

  /** The SDK's teleport handle and Space (a test sets them; none: Teleport needs a reachable Space). */
  teleport: TeleportLike | null = null;
  space: SpaceLike | null = null;
  /** The icons the Space answers, by app name. */
  guestIcons = new Map<string, Uint8Array>();
  readonly iconRequests: SpaceAppIconRequest[][] = [];
  async teleportContext() {
    return this.teleport && this.space ? { teleport: this.teleport, space: this.space } : null;
  }
  teleportHandle() {
    return this.teleport;
  }
  /** Presence: no Space here has it. */
  async joinPresence(id: string): Promise<SpacePresenceLike> {
    throw new Error(`${id} has no presence`);
  }
  async appIcons(_id: string, requests: SpaceAppIconRequest[]) {
    this.iconRequests.push(requests);
    return requests.map((r) => this.guestIcons.get(r.appName) ?? null);
  }
}

export function unconfiguredHost(): HostStatus {
  return {
    configured: false,
    mode: undefined,
    relayUrl: undefined,
    directUrl: undefined,
    envTokenPath: undefined,
    machineId: undefined,
    name: undefined,
    sharing: false,
    serviceInstalled: false,
    serviceRunning: false,
    serviceKind: "process",
    online: undefined,
    clients: [],
    allow: [],
    permissions: [],
    error: undefined,
    recentAccess: [],
    accessLogError: undefined,
    shareDesktop: true,
    provideSpaces: false,
    maxSpaces: 4,
    maxMacosVms: 2,
    maxMemoryGb: 0,
    maxDiskGb: 0,
    maxRunMinutes: 0,
    usedMemoryGb: 0,
    usedDiskGb: 0,
    totalMemoryGb: 0,
    totalDiskGb: 0,
    providedSpaces: [],
    spacesAudit: [],
    spacesAuditError: undefined,
    pausedSignedOut: false,
    owner: undefined,
    ownerEmail: undefined,
    helperAvailable: false,
    legacyService: false,
  };
}

/** An in-memory host: it never installs a service or touches the network. */
export function fixtureHost(status: HostStatus = unconfiguredHost()): HostLike {
  const current = { ...status };
  return {
    status: async () => current,
    pauseSignedOut: async () => current,
    resumeSignedIn: async () => current,
    startSharing: async () => current,
    stopSharing: async () => current,
    configure: async () => current,
    remove: async () => {},
    setup: async () => current,
  } as unknown as HostLike;
}

/** An account on a relay: this machine enrolled, a device asking, and a machine online. */
export function fixtureDevices(now = BigInt(Math.floor(Date.now() / 1000))): DevicesLike {
  const day = 86_400n;
  const device = (id: string, name: string, state: string, platform: string, until?: bigint, seen?: bigint, current = false) => ({
    id,
    name,
    state,
    platform,
    createdAt: now - 40n * day,
    enrolledUntil: until,
    lastSeen: seen,
    current,
  });
  const snapshot: DevicesSnapshot = {
    localDeviceId: "dev_mac",
    devices: [
      device("dev_mac", "MacBook Pro", "enrolled", "macos", now + 24n * day, now - 60n, true),
      device("dev_work", "Work laptop", "pending", "windows"),
    ],
    enforceAfter: now - day,
    audit: [{ ts: now - 600n, kind: "machine_access", device: "dev_mac", machine: "m1", subject: undefined, detail: "owner" }],
    machineNames: new Map([["m1", "studio-mac"]]),
    machines: [
      { id: "m1", spaceId: "relay:m1", name: "studio-mac", ownerId: "me", ownerEmail: undefined, role: "owner", online: true, sharing: true, version: "0.5.0", url: "", allow: [], clients: [], confirmed: true },
    ],
  };
  return { snapshot: async () => snapshot } as unknown as DevicesLike;
}

/** Signing in answers with a device code, then the identity once `finish` runs. */
export class FixtureAccount implements AccountRunning {
  current: string | null;
  private finish: ((who: string) => void) | null = null;
  private fail: ((e: Error) => void) | null = null;
  constructor(identity: string | null = null) {
    this.current = identity;
  }
  identity() {
    return this.current;
  }
  profile(): AccountProfile | null {
    return this.current ? { email: this.current } : null;
  }
  async beginSignIn(): Promise<SignInAttempt> {
    const done = new Promise<string>((resolve, reject) => ((this.finish = resolve), (this.fail = reject)));
    return {
      userCode: "ABCD-1234",
      url: "https://cua.ai/device",
      wait: async () => {
        this.current = await done;
        return this.current;
      },
    };
  }
  completeSignIn(who: string) {
    this.finish?.(who);
  }
  /** The sign-in fails (the network dropped, the code expired). */
  failSignIn(error: Error) {
    this.fail?.(error);
  }
  async signOut() {
    this.current = null;
  }
  async accessToken() {
    return this.current ? "token" : null;
  }
}

export class FixtureTelemetry implements TelemetryRunning {
  current: AppTelemetryInput = { enabled: true, lockedBy: undefined, noticeShown: false };
  readonly recorded: AppTelemetrySignal[] = [];
  status() {
    return this.current;
  }
  setEnabled(on: boolean) {
    this.current = { ...this.current, enabled: on };
    return this.current;
  }
  record(signals: AppTelemetrySignal[]) {
    if (this.current.enabled) this.recorded.push(...signals);
  }
  acknowledged = 0;
  acknowledgeNotice() {
    this.acknowledged += 1;
    this.current = { ...this.current, noticeShown: true };
  }
  /** Whether the one-time linking notice is due (a machine that saw the older wording). */
  linkNoticeDue = false;
  linkNoticeAcknowledged = 0;
  linkNotice(): LinkNoticeView {
    return {
      due: this.linkNoticeDue,
      title: "We've updated how usage data works",
      body: "Cua collects usage data, like which features you use and when something goes wrong.",
      okLabel: "Got it",
      turnOffLabel: "Turn off usage data",
      detailsLabel: "What is collected",
      docsUrl: "https://cua.ai/docs/cua-sdk/concepts/telemetry",
    };
  }
  acknowledgeLinkNotice() {
    this.linkNoticeAcknowledged += 1;
    this.linkNoticeDue = false;
  }
}