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.

ci: deflake the SwiftUI app tests that waited a fixed time (#5836)

Francesco Bonacci62015024e44d
diff --git a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/AppServices.swift b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/AppServices.swift
index ba0989b46d..4d336a10c0 100644
--- a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/AppServices.swift
+++ b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/AppServices.swift
@@ -462,11 +462,17 @@ public final class LiveTeams: TeamsReading, @unchecked Sendable {
/// A fixture account's team (captures, tests).
public final class FixtureTeams: TeamsReading, @unchecked Sendable {
public var current: AppTeamsInput
- /// Who the fixture's owner has not been told was removed yet.
- public var removals: [AppShareRemovalInput]
+ private let lock = NSLock()
+ private var pending: [AppShareRemovalInput]
+ /// Who the fixture's owner has not been told was removed yet. Read by a
+ /// test while the acknowledgement clears it on another task.
+ public var removals: [AppShareRemovalInput] {
+ get { lock.withLock { pending } }
+ set { lock.withLock { pending = newValue } }
+ }
public init(_ teams: [AppTeamInput]? = nil, removals: [AppShareRemovalInput] = []) {
- self.removals = removals
+ pending = removals
current = AppTeamsInput(accountAppUrl: "https://app.cua.ai", state: .ready(teams: teams ?? [
AppTeamInput(
id: "o1", alias: "acme", name: "Acme Inc", role: "owner", plan: "teams", seats: 3,
diff --git a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/ProviderFailureTests.swift b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/ProviderFailureTests.swift
index aeda5ce594..3907908244 100644
--- a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/ProviderFailureTests.swift
+++ b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/ProviderFailureTests.swift
@@ -83,10 +83,17 @@ struct ProviderFailureTests {
return request.results ?? []
}
- func press(_ label: String) throws {
- let observations = try text()
- let match = try #require(observations.first { $0.topCandidates(1).first?.string == label },
- "the actual detail renders \(label)")
+ /// Presses `label` once the detail shows it. The view updates after
+ /// the model does, so look again (up to 30 s) instead of reading the
+ /// rendered text once after a fixed wait.
+ func press(_ label: String) async throws {
+ let deadline = ContinuousClock.now + .seconds(30)
+ var found = try text().first { $0.topCandidates(1).first?.string == label }
+ while found == nil, ContinuousClock.now < deadline {
+ try await Task.sleep(for: .milliseconds(100))
+ found = try text().first { $0.topCandidates(1).first?.string == label }
+ }
+ let match = try #require(found, "the actual detail renders \(label)")
let box = match.boundingBox
let local = NSPoint(x: box.midX * host.bounds.width,
y: (host.isFlipped ? 1 - box.midY : box.midY) * host.bounds.height)
@@ -161,7 +168,7 @@ struct ProviderFailureTests {
if !autoConnect {
try await f.settle()
#expect(f.backend.requestedIds.isEmpty)
- try f.press("Connect")
+ try await f.press("Connect")
}
try await f.expectProviders(1)
#expect(f.model.banner?.contains("scripted provider failure") == true)
@@ -169,7 +176,7 @@ struct ProviderFailureTests {
try await f.settle()
#expect(f.backend.requestedIds == [f.space.id], "polling does not retry a failure")
for attempt in 2...3 {
- try f.press("Try again")
+ try await f.press("Try again")
try await f.expectProviders(attempt)
}
for _ in 0..<80 where f.backend.provider.attempts < 1 {
@@ -192,7 +199,7 @@ struct ProviderFailureTests {
try await f.approve(true)
#expect(f.model.detail(f.space).access == nil)
#expect(f.backend.requestedIds == [f.space.id])
- try f.press("Try again")
+ try await f.press("Try again")
try await f.expectProviders(2)
}
}
diff --git a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/ViewModelTests.swift b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/ViewModelTests.swift
index bd8e04f887..39677bce39 100644
--- a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/ViewModelTests.swift
+++ b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/ViewModelTests.swift
@@ -557,8 +557,11 @@ struct ViewModelTests {
task.cancel()
}
+ /// Waits for `done`, up to 30 s (it ends at once when it holds; the
+ /// bound only matters on a failure, and a loaded runner is slow).
func waitUntil(_ done: @MainActor () -> Bool) async throws {
- for _ in 0..<200 where !done() { try await Task.sleep(for: .milliseconds(10)) }
+ let deadline = ContinuousClock.now + .seconds(30)
+ while !done(), ContinuousClock.now < deadline { try await Task.sleep(for: .milliseconds(10)) }
#expect(done())
}
@@ -1476,7 +1479,8 @@ struct ViewModelTests {
#expect(note.action.label == "Invite to your team")
model.dismissShareRemovalNote()
#expect(model.shareRemovalNote == nil)
- try await Task.sleep(for: .milliseconds(100))
+ // The acknowledgement runs in a task the dismissal starts: wait for it.
+ try await waitUntil { teams.removals.isEmpty }
#expect(teams.removals.isEmpty, "acknowledged, so it does not come back")
await model.reloadShareRemovals()
#expect(model.shareRemovalNote == nil)