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.

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

import AppKit
import CuaSDK
import CuaSpacesFFI
@testable import CuaSpacesMacKit
import CuaSpacesNotchUI
import Foundation
import SwiftUI
import Testing

/// A clock the tests step by hand: timers fire only when `advance` passes
/// their deadline, so hover timing is checked exactly without sleeping.
@MainActor
final class ManualScheduler: NotchScheduler {
    private(set) var now: UInt32 = 0
    private var timers: [(id: Int, at: UInt32, fire: @MainActor () -> Void)] = []
    private var next = 0

    func after(_ ms: UInt32, _ fire: @escaping @MainActor () -> Void) -> () -> Void {
        next += 1
        let id = next
        timers.append((id, now + ms, fire))
        return { [weak self] in self?.timers.removeAll { $0.id == id } }
    }

    var pending: Int { timers.count }

    func advance(_ ms: UInt32) {
        let end = now + ms
        // Bounded: each timer fires at most once.
        for _ in 0..<64 {
            guard let t = timers.filter({ $0.at <= end }).min(by: { $0.at < $1.at }) else { break }
            timers.removeAll { $0.id == t.id }
            now = t.at
            t.fire()
        }
        now = end
    }
}

@MainActor
@Suite("Notch")
struct NotchTests {
    let motion = appNotchMotion().data

    /// A click on the non-activating panel works without focusing it first,
    /// and the panel never activates the app.
    @Test func panelTakesTheFirstClick() {
        _ = NSApplication.shared
        let host = NotchHostingView(rootView: EmptyView())
        #expect(host.acceptsFirstMouse(for: nil))
        let panel = NotchPanel()
        #expect(panel.styleMask.contains(.nonactivatingPanel))
        #expect(panel.canBecomeKey)
        #expect(!panel.ignoresMouseEvents)
    }

    /// The debug start's highlight names one control.
    @Test func highlightParses() {
        #expect(NotchHighlight.parse("tile", firstTile: "local:aurora") == NotchHighlight(.tile("local:aurora")))
        #expect(NotchHighlight.parse("tile=direct:x:pressed", firstTile: nil) == NotchHighlight(.tile("direct:x"), pressed: true))
        #expect(NotchHighlight.parse("settings:pressed", firstTile: nil) == NotchHighlight(.button(.settings), pressed: true))
        #expect(NotchHighlight.parse("list", firstTile: nil) == NotchHighlight(.button(.list)))
        #expect(NotchHighlight.parse("search", firstTile: nil) == NotchHighlight(.search))
        #expect(NotchHighlight.parse("tab", firstTile: nil) == NotchHighlight(.tab))
        #expect(NotchHighlight.parse("tile", firstTile: nil) == nil)
        #expect(NotchHighlight.parse("nope", firstTile: nil) == nil)
        let h: NotchHighlight? = NotchHighlight(.tab, pressed: true)
        #expect(h.state(for: .tab) == NotchInteractionState(hovered: true, pressed: true))
        #expect(h.state(for: .search) == nil)
    }

    /// Hit targets: every control is at least 28 pt, and the press spring
    /// settles in about 120 ms (much faster than the island's open spring).
    @Test func hitTargetsAndTiming() {
        #expect(NotchPress.minHit >= 28)
        let g = NotchGeometry.fallback
        #expect(g.tab.height >= 28)
        #expect(g.tab.width >= 28)
        #expect(motion.openResponse > 0.12)
    }

    /// The closed hover area ends at the drawn tab, so the menu bar item
    /// right of it (a status item, the overflow chevron) keeps its hover
    /// and clicks; before the tab is measured it is the core's widest tab.
    @Test func closedHoverEndsAtTheDrawnTab() {
        let g = NotchGeometry.fallback
        let tabStart = g.stage.width / 2 + g.notch.width / 2
        let y = g.tab.height / 2
        let drawn: CGFloat = 44
        let hover = g.closedHover(tabWidth: drawn)
        #expect(hover.contains(CGPoint(x: tabStart + drawn - 1, y: y)))
        #expect(!hover.contains(CGPoint(x: tabStart + drawn + 1, y: y)))
        #expect(!hover.contains(CGPoint(x: tabStart + g.tab.width - 1, y: y)))
        #expect(g.closedHover(tabWidth: nil).contains(CGPoint(x: tabStart + g.tab.width - 1, y: y)))
        // The notch and its margin stay hoverable.
        #expect(hover.contains(CGPoint(x: g.stage.width / 2, y: y)))
        #expect(hover.contains(CGPoint(x: (g.stage.width - g.closedHit.width) / 2 + 1, y: y)))
    }

    func spaces() async -> [AppSpace] {
        let m = ViewModelTests().makeModel(FixtureSpacesBackend())
        await m.refresh()
        return m.notch.spaces
    }

    /// The open panel's search sits inside the panel: its hover fill clears
    /// the rounded top-left corner by a margin and, on the glass panel (no
    /// notch), is as far from the top edge as from the side (#4644). Beside
    /// a notch it clears the concave ear (the top edge is the screen's).
    @Test func searchFillClearsThePanelCorner() {
        let radii = NotchModel.radii
        let margin: CGFloat = 4
        func glass(menuBar: Double) -> NotchGeometry {
            NotchGeometry(appNotchLayout(screen: AppScreenFacts(
                frame: AppLogicalRect(x: 0, y: 0, width: 1440, height: 900),
                visibleFrame: AppLogicalRect(x: 0, y: 0, width: 1440, height: 900 - menuBar),
                safeAreaTop: 0, auxLeftWidth: nil, auxRightWidth: nil), prompt: false).data)
        }
        let notched = NotchGeometry.fallback
        #expect(notched.notchStyle)
        #expect(notched.searchFill(side: notched.side(radii: radii), width: 120).minX >= CGFloat(radii.open.top) + margin)

        for g in [glass(menuBar: 24), glass(menuBar: 0), glass(menuBar: 30)] {
            #expect(!g.notchStyle)
            let fill = g.searchFill(side: g.side(radii: radii), width: 120)
            let outline = NotchSurfaceShape(notchStyle: g.notchStyle, top: radii.open.top, bottom: radii.open.bottom,
                                             corner: g.glassCorner(open: true))
                .path(in: CGRect(origin: .zero, size: g.open))
            // The fill's top-left corner grown by the margin: every point of
            // it inside the panel's outline.
            let c = NotchGeometry.searchCorner
            let center = CGPoint(x: fill.minX + c, y: fill.minY + c)
            for deg in stride(from: 180.0, through: 270.0, by: 5.0) {
                let a = deg * .pi / 180
                let p = CGPoint(x: center.x + (c + margin) * cos(a), y: center.y + (c + margin) * sin(a))
                #expect(outline.contains(p), "menu bar \(g.notch.height): \(p) outside the panel")
            }
            #expect(fill.minY == fill.minX, "as far from the top as from the side (\(fill))")
        }
    }

    // MARK: - Hover timing (the core's dwell and close delay)

    @Test func hoverOpensOnlyAfterTheDwell() async {
        let clock = ManualScheduler()
        let notch = NotchModel(scheduler: clock)
        notch.spaces = await spaces()
        notch.send(.hoverEnter)
        #expect(notch.view.phase == .closed)
        #expect(notch.view.hoverCue, "the cue shows while the dwell runs")
        clock.advance(motion.hoverDwellMs - 1)
        #expect(notch.view.phase == .closed, "not a millisecond early")
        clock.advance(1)
        #expect(notch.view.phase == .tiles)
        #expect(!notch.view.hoverCue)
    }

    @Test func leavingDuringTheDwellNeverOpens() async {
        let clock = ManualScheduler()
        let notch = NotchModel(scheduler: clock)
        notch.spaces = await spaces()
        notch.send(.hoverEnter)
        clock.advance(200)
        notch.send(.hoverExit)
        #expect(clock.pending == 0, "the dwell is cancelled")
        clock.advance(1000)
        #expect(notch.view.phase == .closed)
        #expect(!notch.view.hoverCue)
    }

    @Test func closesAfterTheDelayUnlessThePointerComesBack() async {
        let clock = ManualScheduler()
        let notch = NotchModel(scheduler: clock)
        notch.spaces = await spaces()
        notch.send(.hoverEnter)
        clock.advance(motion.hoverDwellMs)
        notch.send(.hoverExit)
        clock.advance(motion.closeDelayMs - 1)
        #expect(notch.view.phase == .tiles)
        notch.send(.hoverEnter)
        clock.advance(2000)
        #expect(notch.view.phase == .tiles, "back inside: stays open")
        notch.send(.hoverExit)
        clock.advance(motion.closeDelayMs)
        #expect(notch.view.phase == .closed)
    }

    /// Hovering the notch lists the Spaces, never a machine that only
    /// provides them (CUA-1619): This machine and your other Mac on the
    /// relay stay off the tiles, the Space that Mac provides is listed, and
    /// the tab counts what the tiles list.
    @Test func hoverListsOnlySpacesNeverTheMachinesThatProvideThem() async {
        var machine = FixtureSpacesBackend.sample[0]
        machine.id = "relay:studio"
        machine.name = "Studio"
        machine.provider = "relay"
        // It only provides Spaces: no desktop to list.
        machine.features = ["host_spaces"]
        var provided = FixtureSpacesBackend.sample[0]
        provided.id = "relay:studio/dev"
        provided.name = "Dev"
        provided.provider = "relay"
        provided.host = "studio"
        let m = ViewModelTests().makeModel(FixtureSpacesBackend(rows: [machine, provided]))
        await m.refresh()
        #expect(m.notch.spaces.contains { $0.id == "relay:studio" }, "the roster still has the machine")

        let clock = ManualScheduler()
        let notch = NotchModel(scheduler: clock)
        notch.spaces = m.notch.spaces
        notch.send(.hoverEnter)
        clock.advance(motion.hoverDwellMs)
        #expect(notch.view.phase == .tiles)
        #expect(notch.view.tiles.map(\.id) == ["relay:studio/dev"])
        #expect(notch.view.tab.count == "1")
        #expect(notch.view.tiles.count == Int(appOpenableCount(spaces: notch.spaces)))
    }

    /// A machine's shared desktop is a Space and shows by default; a
    /// pin puts it first and keeps it, dimmed with its note, when the
    /// machine goes offline. The tab counts it only while it runs.
    @Test func aSharedDesktopShowsCountsWhileItRunsAndAPinKeepsItWhenOffline() async {
        var wsl = FixtureSpacesBackend.sample[0]
        wsl.id = "relay:wsl"
        wsl.name = "WSL"
        wsl.provider = "relay"
        wsl.features = ["desktop_stream", "window_stream", "host_spaces"]
        wsl.power = nil
        wsl.powerState = nil
        var studio = wsl
        studio.id = "relay:studio"
        studio.name = "Studio"
        studio.features = ["host_spaces"]
        let backend = FixtureSpacesBackend(rows: [FixtureSpacesBackend.sample[0], wsl, studio])
        let m = ViewModelTests().makeModel(backend)
        await m.refresh()

        let clock = ManualScheduler()
        let notch = NotchModel(scheduler: clock)
        notch.spaces = m.notch.spaces
        notch.send(.hoverEnter)
        clock.advance(motion.hoverDwellMs)
        let ids = notch.view.tiles.map(\.id)
        #expect(ids.contains("relay:wsl"), "the shared desktop is a tile")
        #expect(!ids.contains("relay:studio"), "a machine without a desktop is not")
        let desktop = notch.view.tiles.first { $0.id == "relay:wsl" }
        #expect(desktop?.desktop == true && desktop?.location == "Desktop" && desktop?.pinned == false)
        #expect(notch.view.tab.count == "2", "the running Space and the running desktop")

        // Pinned: first, with the pin (from the settings, as at launch).
        m.togglePin("relay:wsl")
        #expect(m.settings.notchPinned == ["relay:wsl"])
        #expect(appSettingsLoad(path: m.settingsPath).notchPinned == ["relay:wsl"], "kept across a restart")
        #expect(m.notch.state.pinned == ["relay:wsl"])
        notch.setPinned(m.settings.notchPinned)
        #expect(notch.view.tiles.first?.id == "relay:wsl" && notch.view.tiles.first?.pinned == true)

        // The machine goes offline: the pin stays, dimmed, saying so; it counts nothing.
        wsl.reachable = false
        backend.fixtureRows = [FixtureSpacesBackend.sample[0], wsl, studio]
        await m.refresh()
        notch.spaces = m.notch.spaces
        let offline = notch.view.tiles.first { $0.id == "relay:wsl" }
        #expect(offline?.note == "Offline" && offline?.dim == true && offline?.pinned == true)
        #expect(notch.view.tab.count == "1")

        // Unpinned: gone again.
        m.togglePin("relay:wsl")
        #expect(m.settings.notchPinned.isEmpty)
        notch.setPinned([])
        #expect(!notch.view.tiles.map(\.id).contains("relay:wsl"))
    }

    /// Pin to notch is the core's menu item: it flips to Unpin from notch,
    /// the pins keep their order, and This Mac is never pinned.
    @Test func pinToNotchIsTheCoresMenuItem() async throws {
        let m = ViewModelTests().makeModel()
        await m.refresh()
        let aurora = try #require(m.spaces.first { $0.id == "local:aurora" })
        var input = m.menuInput(aurora)
        input.canPin = true
        #expect(appSpaceMenu(input: input).map(\.label).contains("Pin to notch"))
        m.runMenuItem(.pin, on: aurora)
        #expect(m.settings.notchPinned == ["local:aurora"])
        input.pinned = m.settings.notchPinned.contains(aurora.id)
        let labels = appSpaceMenu(input: input).map(\.label)
        #expect(labels.contains("Unpin from notch") && !labels.contains("Pin to notch"))
        m.togglePin("direct:builder")
        #expect(m.settings.notchPinned == ["local:aurora", "direct:builder"], "in the order pinned")
        m.runMenuItem(.pin, on: aurora)
        #expect(m.settings.notchPinned == ["direct:builder"])
        m.togglePin("this-mac")
        #expect(m.settings.notchPinned == ["direct:builder"])
        #expect(appSettingsLoad(path: m.settingsPath).notchPinned == ["direct:builder"])
    }

    // MARK: - The presentation state machine

    @Test func openingSpringsTheShapeThenFadesTheContentIn() {
        var s = NotchStage(settledOn: .closed)
        #expect(s.shape == .closed && s.content == nil)
        let steps = s.update(phase: .tiles, cue: false, motion: motion, reduceMotion: false)
        #expect(steps == [
            .shape(.tiles, .open),
            .content(.tiles, .contentIn(delay: Double(motion.contentDelayMs) / 1000)),
        ])
        #expect(s.shape == .tiles && s.content == .tiles)
        // No change, no steps.
        #expect(s.update(phase: .tiles, cue: false, motion: motion, reduceMotion: false).isEmpty)
    }

    @Test func closingFadesTheContentFirstThenClosesCriticallyDamped() {
        var s = NotchStage(settledOn: .tiles)
        let steps = s.update(phase: .closed, cue: false, motion: motion, reduceMotion: false)
        #expect(steps == [.content(nil, .contentOut), .shape(.closed, .close(delay: motion.contentOut))])
        #expect(motion.closeDamping == 1, "no overshoot above the notch")
        #expect(s.shape == .closed && s.content == nil)
    }

    @Test func theHoverCueGrowsAndSettlesWithoutContent() {
        var s = NotchStage(settledOn: .closed)
        #expect(s.update(phase: .closed, cue: true, motion: motion, reduceMotion: false) == [.shape(.cue, .cue)])
        #expect(s.update(phase: .closed, cue: false, motion: motion, reduceMotion: false) == [.shape(.closed, .cue)])
        #expect(s.content == nil)
    }

    /// Entering the dwell zone answers at once: the cue starts on the
    /// first hover event (not when the dwell ends), with a springy curve a
    /// few points wider and taller; leaving early springs it back. Reduce
    /// Motion swaps the spring for a fade (the view draws a faint rim).
    @Test func theCueRespondsAtOnceWithASpringAndFadesUnderReduceMotion() {
        let clock = ManualScheduler()
        let notch = NotchModel(scheduler: clock)
        var s = NotchStage(settledOn: notch.view.phase.data, cue: notch.view.hoverCue)
        notch.send(.hoverEnter)
        #expect(notch.view.hoverCue, "the cue shows before any time passes")
        #expect(s.update(phase: notch.view.phase.data, cue: notch.view.hoverCue, motion: motion,
                         reduceMotion: false) == [.shape(.cue, .cue)])
        clock.advance(motion.hoverDwellMs / 2)
        notch.send(.hoverExit)
        #expect(!notch.view.hoverCue && notch.view.phase == .closed, "left early: back, never opened")
        #expect(s.update(phase: notch.view.phase.data, cue: notch.view.hoverCue, motion: motion,
                         reduceMotion: false) == [.shape(.closed, .cue)])
        // The spring: quick, a little bounce, wider and taller.
        #expect(motion.hoverResponse <= 0.3)
        #expect(motion.hoverDamping >= 0.6 && motion.hoverDamping <= 0.7)
        #expect(motion.hoverScale > 1 && motion.hoverScaleY > 1)
        #expect(NotchStage.animation(.cue, motion)
                == .spring(response: motion.hoverResponse, dampingFraction: motion.hoverDamping))
        var r = NotchStage(settledOn: .closed)
        #expect(r.update(phase: .closed, cue: true, motion: motion, reduceMotion: true) == [.shape(.cue, .fade)])
        #expect(r.update(phase: .closed, cue: false, motion: motion, reduceMotion: true) == [.shape(.closed, .fade)])
    }

    @Test func aDragMorphsPromptToTilesAndBack() {
        var s = NotchStage(settledOn: .closed)
        let open = s.update(phase: .prompt, cue: false, motion: motion, reduceMotion: false)
        #expect(open.first == .shape(.prompt, .open))
        let morph = s.update(phase: .tiles, cue: false, motion: motion, reduceMotion: false)
        #expect(morph == [.shape(.tiles, .open),
                          .content(.tiles, .contentIn(delay: Double(motion.contentDelayMs) / 2000))])
        let back = s.update(phase: .prompt, cue: false, motion: motion, reduceMotion: false)
        #expect(back.first == .shape(.prompt, .open))
        #expect(s.content == .prompt)
    }

    @Test func reduceMotionIsOpacityOnly() {
        var s = NotchStage(settledOn: .closed)
        let steps = s.update(phase: .tiles, cue: false, motion: motion, reduceMotion: true)
        #expect(steps == [.shape(.tiles, .fade), .content(.tiles, .fade)])
    }

    @Test func springsComeFromTheCore() {
        // The animations are the core's numbers (a smoke check that the
        // mapping does not drop the delay).
        #expect(NotchStage.animation(.contentIn(delay: 0.09), motion) != NotchStage.animation(.contentIn(delay: 0), motion))
        #expect(NotchStage.animation(.open, motion) != NotchStage.animation(.close(delay: 0), motion))
    }

    // MARK: - Geometry

    @Test func theClosedShapeCoversTheHardwareNotchWithItsEarsOutside() {
        let g = NotchGeometry.fallback
        let r = appNotchRadii()
        let closed = g.size(.closed, radii: NotchModel.radii)
        #expect(closed.height == g.notch.height)
        #expect(abs(Double(closed.width) - (Double(g.notch.width) + 2 * r[0].top)) < 0.001)
        #expect(g.size(.tiles, radii: NotchModel.radii) == g.open)
        #expect(g.stage.width >= g.open.width && g.stage.height >= g.open.height)
    }

    // MARK: - Window drags and the permission

    @Test func aMissingPermissionShowsTheLineAndItsButtonOpensAccessibility() async {
        let notch = NotchModel(scheduler: ManualScheduler())
        notch.spaces = await spaces()
        let c = NotchController(model: notch)
        var granted = false
        var started = 0
        var opened: [URL] = []
        c.permitted = { granted }
        c.startDrags = { started += 1 }
        c.openURL = { opened.append($0) }
        c.recheckPermission()
        #expect(started == 0)
        notch.send(.click)
        let p = try? #require(notch.view.permission)
        #expect(p?.action == "Open Settings")
        c.openPermissionSettings(pane: p?.pane ?? "")
        #expect(opened.map(\.absoluteString) == ["x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"])
        // Granted later: the next check starts the monitor and the line goes.
        granted = true
        c.recheckPermission()
        #expect(started == 1)
        #expect(notch.view.permission == nil)
        c.recheckPermission()
        #expect(started == 1, "started once")
        c.hide()
    }

    /// A controller on the fallback 14-inch screen (the primary too), on a
    /// manual clock. Its trigger displays: the notch at (660, 0, 192, 32)
    /// in global top-left points, so the line is at y = 27; the "Teleport
    /// to Cua" box at (641, 0, 230, 60); the open panel at (436, 0, 640, 200).
    func dragController(_ notch: NotchModel) -> (NotchController, ManualScheduler) {
        let c = NotchController(model: notch)
        let clock = ManualScheduler()
        c.scheduler = clock
        c.clock = { UInt64(clock.now) }
        c.place(screen: NotchGeometry.fallbackScreen.ffi, primary: NotchGeometry.fallbackScreen.ffi)
        return (c, clock)
    }

    static let moved = (start: AppLogicalRect(x: 400, y: 300, width: 800, height: 600),
                        now: AppLogicalRect(x: 440, y: 260, width: 800, height: 600))

    /// Global top-left points, as the SDK reports them.
    func drag(_ phase: String, _ x: Double, _ y: Double,
              frames: (start: AppLogicalRect, now: AppLogicalRect)? = moved) -> TeleportWindowDragEvent {
        TeleportWindowDragEvent(phase: phase, x: x, y: y, window: nil, app: nil,
                                startFrame: phase == "start" ? frames?.start : nil,
                                frame: phase == "start" ? frames?.now : nil)
    }

    @Test func theDropPanelOpensAboveTheLineFiveDotsAboveTheNotchBottom() async {
        let notch = NotchModel(scheduler: ManualScheduler())
        notch.spaces = await spaces()
        let (c, _) = dragController(notch)
        #expect(c.dragDisplays.count == 1)
        #expect(c.dragDisplays.first?.notch == AppLogicalRect(x: 660, y: 0, width: 192, height: 32))
        c.handle(drag("start", 700, 500))
        #expect(notch.view.phase == .prompt)
        #expect(notch.view.prompt == "Teleport to Cua")
        // Into the box, below the line: still the box (a normal move to the
        // top of the screen does not open the tiles).
        c.handle(drag("move", 756, 40))
        c.handle(drag("move", 700, 27))
        #expect(notch.view.phase == .prompt)
        // Pushed into the notch, above the line: the drop panel.
        c.handle(drag("move", 700, 26))
        #expect(notch.view.phase == .tiles)
        #expect(notch.view.dropMode)
        // The panel now shows its line above the tiles.
        c.place(screen: NotchGeometry.fallbackScreen.ffi, primary: NotchGeometry.fallbackScreen.ffi)
        let l = c.layout!
        // Over the first tile (left edge of the content, tile row), far
        // below the line: still open, the tile targeted.
        let first = notch.view.tiles.first!
        let x = l.openFrame.x + appNotchRadii()[1].top + 15 + 40
        let y = 982 - (l.openFrame.y + l.openFrame.height - l.notch.height - 15 - 36 - 40)
        c.handle(drag("move", x, y))
        #expect(notch.view.phase == .tiles)
        #expect(notch.view.tiles.first(where: \.targeted)?.id == (first.dropTarget ? first.id : nil))
        // Out of the panel: back to the box; release: nothing commits.
        var committed: String?
        c.onTeleport = { id, _ in committed = id }
        c.handle(drag("move", 10, 300))
        #expect(notch.view.phase == .prompt)
        c.handle(drag("end", 10, 300))
        #expect(committed == nil)
        #expect(notch.view.phase == .closed)
        // Again, released over the tile: it commits there.
        c.handle(drag("start", 700, 500))
        c.handle(drag("move", 756, 10))
        c.handle(drag("move", x, y))
        c.handle(drag("end", x, y))
        #expect(committed == (first.dropTarget ? first.id : nil))
        #expect(notch.view.phase == .closed)
    }

    @Test func restingInTheBoxForTheDwellOpensTheDropPanel() async {
        let notch = NotchModel(scheduler: ManualScheduler())
        notch.spaces = await spaces()
        let (c, clock) = dragController(notch)
        c.handle(drag("start", 700, 500))
        c.handle(drag("move", 756, 50))
        clock.advance(150)
        // Jitter under the tolerance: still resting.
        c.handle(drag("move", 757.5, 51))
        clock.advance(149)
        #expect(notch.view.phase == .prompt)
        clock.advance(1)
        #expect(notch.view.phase == .tiles)
        c.handle(drag("end", 756, 50))
        // Moving more than the tolerance starts the dwell over.
        c.handle(drag("start", 700, 500))
        c.handle(drag("move", 756, 50))
        clock.advance(200)
        c.handle(drag("move", 760, 50))
        clock.advance(299)
        #expect(notch.view.phase == .prompt)
        clock.advance(1)
        #expect(notch.view.phase == .tiles)
        // Leaving the box before the dwell: it never opens.
        c.handle(drag("end", 756, 50))
        c.handle(drag("start", 700, 500))
        c.handle(drag("move", 756, 50))
        clock.advance(100)
        c.handle(drag("move", 756, 400))
        clock.advance(1000)
        #expect(notch.view.phase == .prompt)
        c.handle(drag("end", 756, 400))
        #expect(notch.view.phase == .closed)
    }

    @Test func resizingAWindowNeverShowsTheNotch() async {
        let notch = NotchModel(scheduler: ManualScheduler())
        notch.spaces = await spaces()
        let (c, clock) = dragController(notch)
        var committed: String?
        c.onTeleport = { id, _ in committed = id }
        // Chrome's left edge dragged 20 pt: the origin shifts (so the SDK
        // starts) but so does the width.
        let leftEdge = (start: AppLogicalRect(x: 400, y: 300, width: 800, height: 600),
                        now: AppLogicalRect(x: 380, y: 300, width: 820, height: 600))
        c.handle(drag("start", 380, 500, frames: leftEdge))
        #expect(c.trigger.kind == .resize)
        #expect(notch.view.phase == .closed)
        c.handle(drag("move", 756, 5))
        clock.advance(1000)
        #expect(notch.view.phase == .closed)
        c.handle(drag("end", 756, 5))
        #expect(committed == nil)
        #expect(notch.view.phase == .closed)
    }

    // MARK: - The count

    /// The tab counts only the Spaces on or starting: with every Space
    /// stopped there is no tab at all (no "0"), the stopped Spaces are
    /// still tiles that take drops, and one starting brings the tab back.
    @Test func theTabCountsOnlySpacesOnAndGoesAtZero() async throws {
        let notch = NotchModel(scheduler: ManualScheduler())
        let roster = await spaces()
        let stopped = roster.map { s -> AppSpace in
            var s = s
            if s.id != "this-mac" { s.status = .suspended }
            return s
        }
        notch.spaces = stopped
        #expect(appOnCount(spaces: stopped) == 0)
        #expect(notch.view.phase == .closed && !notch.view.showTab)
        #expect(appOnStatusLine(count: 0) == "No Spaces running")
        notch.send(.click)
        let tiles = notch.view.tiles
        #expect(!tiles.isEmpty && tiles.allSatisfy { $0.dim })
        #expect(tiles.contains { $0.dropTarget }, "a reachable stopped Space still takes drops")
        notch.send(.click)

        var starting = stopped
        let i = try #require(starting.firstIndex { $0.id != "this-mac" })
        starting[i].status = .provisioning
        notch.spaces = starting
        #expect(notch.view.showTab)
        #expect(notch.view.tab.count == "1" && notch.view.tab.word == "Space")
        #expect(notch.view.countLabel == "1 Space")
    }

    // MARK: - The notch setting

    @Test func hidingTheNotchStopsHoverAndDragsAndShowingRestoresIt() async {
        let notch = NotchModel(scheduler: ManualScheduler())
        notch.spaces = await spaces()
        let c = NotchController(model: notch)
        c.makesPanel = false
        c.apply(appNotchLayout(screen: NotchGeometry.fallbackScreen.ffi, prompt: false))
        var started = 0
        c.permitted = { true }
        c.startDrags = { started += 1 }
        c.setShown(false)
        #expect(!c.isShown)
        #expect(notch.view.hidden && !notch.view.showTab)
        // Hover, clicks and window drags do nothing while hidden.
        notch.send(.hoverEnter)
        notch.send(.click)
        #expect(!notch.state.open && !notch.view.hoverCue)
        let top = NSScreen.screens.first?.frame.maxY ?? 0
        c.handle(TeleportWindowDragEvent(phase: "start", x: 100, y: top - 100, window: nil, app: nil))
        #expect(notch.view.phase == .closed)
        c.recheckPermission()
        #expect(started == 0, "no drag monitor while hidden")
        // Shown again: the tab is back, hover and drags work.
        c.setShown(true)
        #expect(c.isShown && notch.view.showTab && !notch.view.hidden)
        #expect(started == 1, "the drag monitor restarts")
        notch.send(.click)
        #expect(notch.state.open)
        c.setShown(false)
    }

    @Test func theNotchFollowsTheSettingLive() async {
        let m = ViewModelTests().makeModel(FixtureSpacesBackend())
        let c = NotchController(model: m.notch)
        c.makesPanel = false
        m.settings.menuBar = false
        AppDelegate.followNotchSetting(m, c)
        #expect(c.isShown)
        await m.choose(row: "notch", option: "hide")
        for _ in 0..<20 where c.isShown { try? await Task.sleep(for: .milliseconds(20)) }
        #expect(!c.isShown && m.notch.view.hidden)
        await m.choose(row: "notch", option: "show")
        for _ in 0..<20 where !c.isShown { try? await Task.sleep(for: .milliseconds(20)) }
        #expect(c.isShown && !m.notch.view.hidden)
        c.setShown(false)
    }

    /// CUA-1618: the setting off at launch, the SDK in, then the setting
    /// turned on while the app runs: window drags are followed (onto the
    /// notch and the New UI's tiles) without a relaunch, stop when it turns
    /// off and start again when it turns back on.
    @Test func turningTheNotchOnLiveStartsWindowDragsAndOffStopsThem() async {
        let m = ViewModelTests().makeModel(FixtureSpacesBackend())
        let c = NotchController(model: m.notch)
        c.makesPanel = false
        m.settings.menuBar = true
        AppDelegate.followNotchSetting(m, c)
        #expect(!c.isShown)
        // The SDK comes in after the launch (`followWindowDrags`).
        var started = 0
        c.permitted = { true }
        c.startDrags = { started += 1 }
        c.recheckPermission()
        #expect(started == 0 && !c.dragsRunning, "no drag monitor while hidden")
        await m.choose(row: "notch", option: "show")
        for _ in 0..<20 where !c.isShown { try? await Task.sleep(for: .milliseconds(20)) }
        #expect(c.isShown)
        #expect(started == 1 && c.dragsRunning, "turned on live: window drags are followed")
        await m.choose(row: "notch", option: "hide")
        for _ in 0..<20 where c.isShown { try? await Task.sleep(for: .milliseconds(20)) }
        #expect(!c.isShown && !c.dragsRunning, "turned off: the monitor stops")
        await m.choose(row: "notch", option: "show")
        for _ in 0..<20 where !c.isShown { try? await Task.sleep(for: .milliseconds(20)) }
        #expect(started == 2 && c.dragsRunning, "on again: it starts again")
        // Showing an already shown notch keeps the one monitor.
        c.show()
        #expect(started == 2)
        c.setShown(false)
    }
}

@Suite("Launch")
struct LaunchTests {
    /// A normal launch opens the main window: saved window state (a quit
    /// from the menu bar with the window closed) is ignored.
    @Test func savedWindowStateIsIgnoredSoTheMainWindowOpens() throws {
        let name = "cua-spaces-launch-\(UUID().uuidString)"
        let defaults = try #require(UserDefaults(suiteName: name))
        defer { defaults.removePersistentDomain(forName: name) }
        #expect(!defaults.bool(forKey: "ApplePersistenceIgnoreState"))
        CuaSpacesMacApp.ignoreSavedWindowState(defaults)
        #expect(defaults.bool(forKey: "ApplePersistenceIgnoreState"))
    }
}


/// The hotspot indicator on the notch is on for a hotspot started by hand
/// and for as long as any Space exits through this machine; neither turns
/// the other off (CUA-2616: the hotspot keeps every feature).
@MainActor
@Suite("Notch exits")
struct NotchExitsTests {
    @Test func exitsAloneLightTheHotspotIndicator() {
        let notch = NotchModel(scheduler: ManualScheduler())
        #expect(notch.state.hotspot == false)
        notch.setExits(["local:b", "local:a", "local:a"])
        #expect(notch.state.hotspot)
        #expect(notch.exitSpaces == ["local:a", "local:b"])
        notch.setExits([])
        #expect(notch.state.hotspot == false)
    }

    @Test func aHotspotStartedByHandAndAnExitDoNotTurnEachOtherOff() {
        let notch = NotchModel(scheduler: ManualScheduler())
        notch.setActivity(hotspot: true, transfer: nil)
        notch.setExits(["local:a"])
        notch.setActivity(hotspot: false, transfer: nil)
        #expect(notch.state.hotspot, "the exit keeps it on after the hotspot stops")
        notch.setActivity(hotspot: true, transfer: nil)
        notch.setExits([])
        #expect(notch.state.hotspot, "the hotspot keeps it on after the exit ends")
        notch.setActivity(hotspot: false, transfer: nil)
        #expect(notch.state.hotspot == false)
    }

    @Test func aTransferKeepsTheExitIndicator() {
        let notch = NotchModel(scheduler: ManualScheduler())
        notch.setExits(["local:a"])
        notch.setActivity(hotspot: notch.manualHotspot, transfer: AppNotchTransfer(sent: nil, total: nil))
        #expect(notch.state.hotspot)
        #expect(notch.state.transfer != nil)
        notch.setActivity(hotspot: notch.manualHotspot, transfer: nil)
        #expect(notch.state.hotspot)
        #expect(notch.manualHotspot == false, "a transfer never turns the manual hotspot on")
    }
}