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.1 KB
Raw Blame
// SPDX-License-Identifier: FSL-1.1-MIT
// Copyright (c) 2026 Cua AI, Inc.

import AppKit
import CuaSpacesFFI
@testable import CuaSpacesMacKit
import Foundation
import Testing

/// The first run on the Mac is the shared web onboarding: what the bridge
/// answers it (`onboarding.get`, `onboarding.complete`), the Space it
/// creates ("<Space> is ready", `notifyWhenReady`) and the usage notice it
/// shows.
@MainActor
@Suite("First run (web onboarding)", .serialized)
struct FirstRunTests {
    init() { _ = NSApplication.shared }

    func bridge(backend: SpacesBackend = FixtureSpacesBackend(),
                loginItem: LoginItemControlling? = nil) -> (WebUIBridge, AppModel, FixtureTelemetry, URL) {
        let dir = FileManager.default.temporaryDirectory.appendingPathComponent("cua-firstrun-\(UUID().uuidString)")
        let state = dir.appendingPathComponent("onboarding.json")
        let telemetry = FixtureTelemetry()
        let model = AppModel(backend: backend, keyvault: KeyvaultModel(client: nil),
                             onboarding: OnboardingModel(statePath: state.path),
                             settingsPath: dir.appendingPathComponent("settings.json").path, telemetry: telemetry,
                             loginItem: loginItem)
        model.settings.experiments = AppExperiments(cuaVolume: false, secureSpaces: false, childSpaces: false, askFirst: false, ciRunners: false, tcpShares: false, mobile: false, warmPools: false)
        let bridge = WebUIBridge(model: model, allowedOrigins: [])
        bridge.notifySpaceReady = { _ in }
        return (bridge, model, telemetry, state)
    }

    /// The app core's reset plan (`contracts/onboarding-reset.json`, the golden of `onboarding_reset::plan`).
    static var resetPlan: [String: Any] {
        let url = URL(fileURLWithPath: #filePath)
            .deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
            .appendingPathComponent("../../libs/cua/crates/cua-spaces-app-core/contracts/onboarding-reset.json").standardized
        let data = (try? Data(contentsOf: url)) ?? Data()
        return ((try? JSONSerialization.jsonObject(with: data)) as? [String: Any]) ?? [:]
    }

    func yield() async {
        await withCheckedContinuation { k in DispatchQueue.main.async { k.resume() } }
    }

    // MARK: - The first run's window

    /// A launch (or a window that opens again) while the first run is not
    /// done asks for it as due, so the page can keep a "Skip for now": the
    /// welcome came back at every launch, in Cua Spaces Local as in the
    /// released app, because the app opened `/onboarding` itself. A person
    /// asking for it (Settings, Welcome) still gets the page unconditionally.
    @Test func aWindowOpeningWhileTheFirstRunIsDueLetsThePageHonourSkipForNow() {
        #expect(MainUIRoute.firstRunDue.webPath == "/onboarding?due=1")
        #expect(MainUIRouter.launchRoute(onboarded: false) == .firstRunDue)
        #expect(MainUIRouter.launchRoute(onboarded: true) == nil, "done: the app as it was")
        #expect(MainUIRoute.onboarding.webPath == "/onboarding", "Show again has no due: it always shows")
    }

    /// The app's first launch here opens the first run whatever the page
    /// kept from an earlier install (a stale "Skip for now" hid it behind
    /// the sidebar's small "Finish setting up", CUA-3063); from the next
    /// launch on, `due` applies again.
    @Test func theFirstLaunchAlwaysShowsTheFirstRun() throws {
        #expect(MainUIRoute.firstLaunch.webPath == "/onboarding?fresh=1")
        #expect(MainUIRouter.launchRoute(onboarded: false, firstLaunch: true) == .firstLaunch)
        #expect(MainUIRouter.launchRoute(onboarded: true, firstLaunch: true) == nil)
        let dir = FileManager.default.temporaryDirectory.appendingPathComponent("cua-firstlaunch-\(UUID().uuidString)")
        let path = dir.appendingPathComponent("onboarding.json").path
        let first = OnboardingModel(statePath: path)
        #expect(first.firstLaunch && !first.completed)
        first.markSeen()
        #expect(!first.firstLaunch)
        let next = OnboardingModel(statePath: path)
        #expect(!next.firstLaunch && !next.completed, "seen, not done")
        #expect(MainUIRouter.launchRoute(onboarded: next.completed, firstLaunch: next.firstLaunch) == .firstRunDue)
        try? FileManager.default.removeItem(at: dir)
    }

    // MARK: - onboarding.get, onboarding.complete

    @Test func theFirstRunIsDueUntilItFinishesAndPersists() async throws {
        let (bridge, model, _, state) = bridge()
        let due = try #require(try await bridge.handle("onboarding.get", [:]) as? [String: Any])
        #expect(due["completed"] as? Bool == false && due["mode"] as? String == "client")
        let session = try #require(try await bridge.handle("session.get", [:]) as? [String: Any])
        #expect((session["onboarding"] as? [String: Any])?["completed"] as? Bool == false)

        let done = try #require(try await bridge.handle("onboarding.complete", ["mode": "host"]) as? [String: Any])
        #expect(done["completed"] as? Bool == true && done["mode"] as? String == "host")
        #expect(model.onboarding.completed)
        #expect(try String(contentsOf: state, encoding: .utf8) == #"{"completed":true,"mode":"host"}"#)
        let again = try #require(try await bridge.handle("onboarding.get", [:]) as? [String: Any])
        #expect(again["completed"] as? Bool == true && again["mode"] as? String == "host")
        #expect(((try await bridge.handle("session.get", [:]) as? [String: Any])?["onboarding"] as? [String: Any])?["completed"] as? Bool == true)
        // Settings, Welcome, shows it again.
        model.onboarding.restart()
        #expect((try await bridge.handle("onboarding.get", [:]) as? [String: Any])?["completed"] as? Bool == false)
    }

    /// Settings, About, "Reset & Show": the saved first run is forgotten, so it
    /// is due from its first step; only `onboarding.json` goes (the app core's
    /// reset plan lists it), and a second reset changes nothing.
    @Test func resetForgetsTheSavedFirstRunAndNothingElse() async throws {
        let (bridge, model, _, state) = bridge()
        let other = state.deletingLastPathComponent().appendingPathComponent("checklist.json")
        try FileManager.default.createDirectory(at: state.deletingLastPathComponent(), withIntermediateDirectories: true)
        try Data("{}".utf8).write(to: other)
        _ = try await bridge.handle("onboarding.complete", ["mode": "host"])
        #expect(FileManager.default.fileExists(atPath: state.path))

        let reset = try #require(try await bridge.handle("onboarding.reset", [:]) as? [String: Any])
        #expect(reset["completed"] as? Bool == false && reset["mode"] as? String == "client")
        #expect(!model.onboarding.completed)
        #expect(!FileManager.default.fileExists(atPath: state.path))
        #expect(FileManager.default.fileExists(atPath: other.path), "nothing else is touched")
        // A new launch reads it as due.
        #expect(!OnboardingModel(statePath: state.path).completed)

        _ = try await bridge.handle("onboarding.reset", [:])
        #expect(!model.onboarding.completed, "safe to run twice")
        // The file it removes is the one the app core's reset plan names.
        #expect(Self.resetPlan["hostFiles"] as? [String] == [state.lastPathComponent])
        #expect(Self.resetPlan["firstStep"] as? String == "welcome")
    }

    @Test func completingNeedsAModeAndAppliesTheLoginCheckbox() async throws {
        let item = FixtureLoginItem()
        let (bridge, model, _, _) = bridge(loginItem: item)
        for bad: [String: Any] in [[:], ["mode": "team"], ["mode": 1]] {
            do {
                _ = try await bridge.handle("onboarding.complete", bad)
                Issue.record("expected bad_args for \(bad)")
            } catch let f as WebUIBridge.Failure {
                #expect(f.code == "bad_args")
            }
        }
        #expect(!model.onboarding.completed)
        _ = try await bridge.handle("onboarding.complete", ["mode": "client", "launchAtLogin": true])
        #expect(item.calls == ["register"])
        #expect(model.settings.launchAtLogin == true)
    }

    // MARK: - The usage notice

    @Test func theUsageNoticeIsSeenWhenWelcomeIsLeftNotBefore() async throws {
        let (bridge, _, telemetry, _) = bridge()
        func track(_ page: String, _ action: String) async throws {
            _ = try await bridge.handle("telemetry.track", ["signals": [
                ["type": "onboarding-page", "page": page, "action": action, "choice": "none"]]])
        }
        try await track("welcome", "shown")
        #expect(telemetry.acknowledgements == 0, "Welcome showing is not Welcome left")
        try await track("signin", "shown")
        #expect(telemetry.acknowledgements == 1)

        // Skipping the first run also moves past it.
        let (skip, _, skipped, _) = self.bridge()
        _ = try await skip.handle("telemetry.track", ["signals": [["type": "step", "step": "onboarding_skipped", "ok": true]]])
        #expect(skipped.acknowledgements == 1)

        // The switch beside the note, and finishing, count as seen too.
        let (settings, _, viaSwitch, _) = self.bridge()
        _ = try await settings.handle("settings.choose", ["row": "telemetry", "option": "off"])
        #expect(viaSwitch.acknowledgements == 1 && viaSwitch.current.enabled == false)
        let (finish, _, finished, _) = self.bridge()
        _ = try await finish.handle("onboarding.complete", ["mode": "client"])
        #expect(finished.acknowledgements == 1)
    }

    // MARK: - "<Space> is ready"

    @Test func theNotificationSaysWhatIsReadyAndWhereAClickGoes() {
        let linux = SpaceReadyNote(spaceId: "local:aurora", name: "aurora", os: .linux)
        #expect(linux.title == "aurora is ready")
        #expect(linux.body == "A fresh Linux computer inside this Mac. Your agent can use it now.")
        #expect(SpaceReadyNote(spaceId: "x", name: "x", os: .macos).body.hasPrefix("A fresh macOS computer"))
        #expect(SpaceReadyNote(spaceId: "x", name: "x", os: .windows).body.hasPrefix("A fresh Windows computer"))
        #expect(SpaceReadyNote(spaceId: "x", name: "x", os: .unknown).body
                == "A fresh computer inside this Mac. Your agent can use it now.")
        #expect(linux.identifier == "space-ready-local:aurora")
        #expect(SpaceReadyNote.spaceId(fromIdentifier: linux.identifier) == "local:aurora")
        #expect(SpaceReadyNote.spaceId(fromIdentifier: "agent-1") == nil)
        #expect(SpaceReadyNote.spaceId(fromIdentifier: "space-ready-") == nil)
    }

    @Test func theCuaKeyIsAttachedAsACopy() throws {
        let key = try #require(CuaKey.url, "the key is in the module's resources")
        #expect(CuaKey.image != nil)
        let dir = FileManager.default.temporaryDirectory.appendingPathComponent("cua-key-\(UUID().uuidString)")
        try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
        defer { try? FileManager.default.removeItem(at: dir) }
        let copy = try #require(SpaceReadyNotifier.attachmentCopy(of: key, in: dir))
        #expect(copy != key && copy.pathExtension == "png")
        #expect(try Data(contentsOf: copy) == Data(contentsOf: key))
        // The system moves the attachment's file: the original stays.
        try FileManager.default.removeItem(at: copy)
        #expect(FileManager.default.fileExists(atPath: key.path))
        #expect(SpaceReadyNotifier.attachmentCopy(of: nil, in: dir) == nil)
    }

    func config(_ name: String) -> [String: Any] {
        ["image": "ghcr.io/trycua/linux:24.04", "on": "local", "kind": "container", "runtime": "auto", "name": name]
    }

    @Test func aCreateFromTheOnboardingNotifiesWhenItSucceeds() async throws {
        let (bridge, _, _, _) = bridge()
        var posted: [SpaceReadyNote] = []
        bridge.notifySpaceReady = { posted.append($0) }
        _ = try await bridge.handle("spaces.create", ["config": config("aurora"), "pendingId": "pending:a1",
                                                       "os": "linux", "notifyWhenReady": true])
        #expect(posted == [SpaceReadyNote(spaceId: "local:aurora", name: "aurora", os: .linux)])
        // Any other create, and a flag that is not true, stay quiet.
        _ = try await bridge.handle("spaces.create", ["config": config("quiet"), "pendingId": "pending:a2", "os": "linux"])
        _ = try await bridge.handle("spaces.create", ["config": config("no"), "pendingId": "pending:a3",
                                                       "os": "linux", "notifyWhenReady": false])
        _ = try await bridge.handle("spaces.create", ["config": config("odd"), "pendingId": "pending:a4",
                                                       "os": "linux", "notifyWhenReady": "yes", "something": 1])
        #expect(posted.count == 1)
    }

    @Test func aFailedOrCancelledCreateNeverNotifies() async throws {
        let backend = FixtureSpacesBackend()
        backend.cancelledCreates = ["pending:b1"]
        let (bridge, _, _, _) = bridge(backend: backend)
        var posted: [SpaceReadyNote] = []
        bridge.notifySpaceReady = { posted.append($0) }
        for id in ["pending:b1"] {
            do {
                _ = try await bridge.handle("spaces.create", ["config": config("gone"), "pendingId": id,
                                                               "os": "linux", "notifyWhenReady": true])
                Issue.record("expected cancelled")
            } catch let f as WebUIBridge.Failure {
                #expect(f.code == "cancelled")
            }
        }
        backend.createError = "no room"
        do {
            _ = try await bridge.handle("spaces.create", ["config": config("full"), "pendingId": "pending:b2",
                                                           "os": "linux", "notifyWhenReady": true])
            Issue.record("expected a failure")
        } catch {}
        #expect(posted.isEmpty)
    }

    /// The create belongs to the app: closing the onboarding window mid-create
    /// neither cancels it nor loses the notification.
    @Test func closingTheWindowMidCreateDoesNotCancelIt() async throws {
        let backend = FixtureSpacesBackend()
        backend.holdCreates = true
        let (_, model, _, _) = bridge(backend: backend)
        let controller = WebUIWindowController(model: model, route: "/onboarding")
        var posted: [SpaceReadyNote] = []
        controller.bridge.notifySpaceReady = { posted.append($0) }
        let create = Task { @MainActor in
            try await controller.bridge.handle("spaces.create", ["config": config("aurora"), "pendingId": "pending:c1",
                                                                  "os": "linux", "notifyWhenReady": true])
        }
        for _ in 0..<200 where model.creates.pending.isEmpty { await yield() }
        #expect(model.creates.pending.map(\.id) == ["pending:c1"])
        // The window goes (as the red button does); its page and bridge with it.
        controller.window?.close()
        await yield()
        #expect(model.creates.pending.map(\.id) == ["pending:c1"], "still creating")
        backend.releaseCreate()
        let space = try #require(try await create.value as? [String: Any])
        #expect(space["id"] as? String == "local:aurora")
        #expect(model.creates.pending.isEmpty)
        #expect(backend.created.count == 1)
        #expect(posted == [SpaceReadyNote(spaceId: "local:aurora", name: "aurora", os: .linux)])
    }
}

extension FirstRunTests {
    /// `session.openExternal` is `window.open` on this host: web pages, mail
    /// and System Settings' panes (a permission row's link) reach the system;
    /// nothing else does.
    @Test func theWindowHandsOnlyPagesMailAndSystemSettingsToTheSystem() {
        for ok in ["https://cua.ai/docs", "http://localhost:3000", "mailto:[email protected]",
                   "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"] {
            #expect(WebUIWindowController.opensOutside(URL(string: ok)!), "\(ok)")
        }
        for no in ["file:///etc/hosts", "cua-spaces://app/settings", "javascript:alert(1)", "tel:123", "x-apple.systempreferences"] {
            #expect(!WebUIWindowController.opensOutside(URL(string: no)!), "\(no)")
        }
    }
}