import ParcelCore import ParcelPlatform import Testing @testable import ParcelEngine @Suite("Native visibility and event reconciliation") @MainActor struct NativeVisibilityTests { private func suspensions(_ harness: NativeHarness) -> [Set] { harness.events.compactMap { event in if case .suspendedDisplaysChanged(let displays) = event { return displays } return nil } } private func conflicts(_ harness: NativeHarness) -> [SystemWindowConflict] { harness.events.compactMap { event in if case .systemWindowConflictChanged(let conflict) = event { return conflict } return nil } } @Test(arguments: [Command.toggleFloat, .toggleFocus]) func incompleteAXListPreservesLiveWindowModes(command: Command) async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } let window = WindowID(rawValue: 1) harness.apply(.commandForWindow(window, command, workspace: engineWorkspace)) let floating = harness.state.windows[window]?.isFloating let modes = harness.state.modes(for: engineWorkspace) let returned = try #require(harness.first.snapshots.first) harness.clearEvents() harness.first.snapshots = [] harness.world.reconcile([engineApp]) try await harness.drainReconciliation() #expect(!harness.disappeared.contains(window)) #expect(harness.state.windows[window]?.isFloating == floating) #expect(harness.state.modes(for: engineWorkspace) == modes) var updated = returned.window updated.title = "Next browser page" harness.first.snapshots = [NativeWindowSnapshot(window: updated, frame: returned.frame)] harness.world.reconcile([engineApp]) try await harness.drainReconciliation() #expect(harness.state.windows[window]?.isFloating == floating) #expect(harness.state.modes(for: engineWorkspace) == modes) #expect(harness.state.windows[window]?.title == updated.title) } @Test func failedReconciliationCompletesWithoutAWindowObservation() async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } var idleNotifications = 0 var windowNotifications = 0 harness.world.onReconciliationIdle = { idleNotifications += 1 } harness.world.onWindowObservation = { _ in windowNotifications += 1 } harness.backend.visibleError = AXPlatformError.timedOut harness.world.reconcile([engineApp]) try await harness.drainReconciliation() #expect(!harness.world.hasPendingReconciliation) #expect(!harness.failures.isEmpty) #expect(idleNotifications >= 1) #expect(windowNotifications == 0) harness.backend.visibleError = nil harness.world.reconcile([engineApp]) try await harness.drainReconciliation() #expect(!harness.world.hasPendingReconciliation) #expect(idleNotifications >= 2) #expect(windowNotifications > 0) } @Test(arguments: [-1.0, 1.0]) func refreshUsesCGFramesDespiteAnAXOffset(_ offset: Double) async throws { let harness = NativeHarness() let id = WindowID(rawValue: 1) let compositor = initialEngineFrame var accessibility = compositor accessibility.size.width += offset harness.first.snapshots[0].frame = accessibility try await harness.start() defer { harness.world.stop() } try await harness.drainReconciliation() #expect(harness.state.observedFrames[id] == compositor) let writes = [EngineFrameWrite(window: id, frame: compositor, transaction: 1, isFinal: true)] let receipt: EngineWriteReceipt = await withCheckedContinuation { continuation in harness.world.write(writes, for: engineApp) { continuation.resume(returning: $0) } } #expect(receipt.frames == [WindowFrame(window: id, frame: compositor)]) harness.first.snapshots[0].frame = accessibility harness.clearEvents() for _ in 0..<3 { harness.world.reconcile([engineApp]) harness.backend.emit(.activated(NativeApplicationInfo(id: engineApp))) harness.first.emit(.focusedWindowChanged) // The stream event must be consumed before reconciliation can be awaited. try await harness.drain() try await harness.drainReconciliation() } #expect(!harness.events.contains { if case .frameChanged = $0 { true } else { false } }) #expect(harness.first.writes.count == 1) } @Test func refreshStillReportsARealOnePointCGResize() async throws { let harness = NativeHarness() let id = WindowID(rawValue: 1) harness.first.snapshots[0].frame.size.width += 1 try await harness.start() defer { harness.world.stop() } try await harness.drainReconciliation() harness.clearEvents() var resized = initialEngineFrame resized.size.width += 1 harness.backend.visible[id] = resized harness.world.reconcile([engineApp]) try await harness.drainReconciliation() let frames = harness.events.compactMap { event -> WindowFrame? in if case .frameChanged(let window, let frame, let changedSize, _) = event { #expect(changedSize) return WindowFrame(window: window, frame: frame) } return nil } #expect(frames == [WindowFrame(window: id, frame: resized)]) } @Test func conflictingPreferencesSuspendAndRestoreTheCurrentSpaceGate() async throws { let harness = NativeHarness() try await harness.start() harness.backend.suspendedDisplays = [engineDisplay] harness.backend.emit(.spaceChanged) try await harness.drain() harness.clearEvents() var snapshot = SystemWindowPreferencesSnapshot( stageManagerGloballyEnabled: .enabled, edgeDragTilingEnabled: .disabled, topDragTilingEnabled: .disabled, optionKeyTilingAcceleratorEnabled: .disabled) harness.backend.emit(.preferencesChanged(snapshot)) #expect(suspensions(harness).last == [engineDisplay]) #expect(conflicts(harness).last == .stageManager) snapshot.edgeDragTilingEnabled = .enabled harness.backend.emit(.preferencesChanged(snapshot)) #expect(conflicts(harness).last == .stageManager) harness.clearEvents() snapshot.stageManagerGloballyEnabled = .disabled harness.backend.emit(.preferencesChanged(snapshot)) #expect( harness.events.contains { if case .systemWindowConflictChanged(nil) = $0 { return true }; return false }) #expect(conflicts(harness).isEmpty) snapshot.edgeDragTilingEnabled = .unknown(.missingKey) harness.backend.emit(.preferencesChanged(snapshot)) #expect(conflicts(harness).isEmpty) snapshot.stageManagerGloballyEnabled = .unknown(.missingKey) harness.backend.emit(.preferencesChanged(snapshot)) #expect(conflicts(harness).last == .unverified) snapshot.stageManagerGloballyEnabled = .disabled snapshot.edgeDragTilingEnabled = .disabled harness.backend.emit(.preferencesChanged(snapshot)) #expect(suspensions(harness).last == [engineDisplay]) harness.backend.suspendedDisplays = [] harness.backend.emit(.spaceChanged) try await harness.drain() #expect(suspensions(harness).last == []) } @Test func fullscreenSpaceSuspendsOnlyItsDisplayAndResumesAfterRefresh() async throws { let harness = NativeHarness() harness.backend.displays.append( Display(id: secondEngineDisplay, frame: Rect(x: 1200, y: 0, width: 1200, height: 800))) harness.backend.spaces[secondEngineDisplay] = SpaceID(rawValue: 2) try await harness.start() harness.clearEvents() harness.backend.suspendedDisplays = [secondEngineDisplay] harness.backend.emit(.spaceChanged) #expect(suspensions(harness).last == [engineDisplay, secondEngineDisplay]) try await harness.drain() #expect(suspensions(harness).last == [secondEngineDisplay]) harness.clearEvents() harness.backend.suspendedDisplays = [] harness.backend.emit(.spaceChanged) try await harness.drain() #expect(suspensions(harness).last == []) } @Test func failedSpaceReadRemainsSuspendedUntilAnActivityRetrySucceeds() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.backend.spaceError = WindowServer.ReadError.currentSpaceUnavailable(displayID: engineDisplay.rawValue) harness.backend.emit(.spaceChanged) try await harness.drain() #expect(suspensions(harness).contains([engineDisplay])) #expect(suspensions(harness).last == [engineDisplay]) harness.clearEvents() harness.backend.spaceError = nil harness.backend.emit(.activated(NativeApplicationInfo(id: engineApp))) try await harness.drain() #expect(suspensions(harness).last == []) } @Test func failedAppReadKeepsSpaceTransitionSuspendedUntilRetry() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.first.error = AppThreadFailure.windowEnumeration("unavailable") harness.backend.emit(.woke) try await harness.drain() #expect(suspensions(harness).last == [engineDisplay]) #expect(!harness.failures.isEmpty) harness.backend.emit(.activated(NativeApplicationInfo(id: secondEngineApp))) try await harness.drain() #expect(suspensions(harness).last == [engineDisplay]) harness.first.error = nil harness.backend.emit(.activated(NativeApplicationInfo(id: secondEngineApp))) try await harness.drain() #expect(suspensions(harness).last == []) } @Test func exitingAnUnresponsiveAppRetriesTheSpaceTransition() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.first.error = AppThreadFailure.windowEnumeration("unavailable") harness.backend.emit(.woke) try await harness.drain() #expect(suspensions(harness).last == [engineDisplay]) harness.backend.visible[WindowID(rawValue: 1)] = nil harness.backend.emit(.terminated(engineApp)) try await harness.drain() #expect(suspensions(harness).last == []) } @Test func activationDiscoversAnotherAppsMissedWindowCreation() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.second.snapshots.append( NativeWindowSnapshot(window: engineWindow(3, app: secondEngineApp), frame: initialEngineFrame)) harness.backend.visible[WindowID(rawValue: 3)] = initialEngineFrame harness.backend.emit(.activated(NativeApplicationInfo(id: engineApp))) try await harness.drain() #expect(harness.appeared.map(\.window.id) == [WindowID(rawValue: 3)]) } @Test func hideReleasesTileEvenWhenServerStillListsWindow() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.backend.emit(.hidden(NativeApplicationInfo(id: engineApp, isHidden: true))) try await harness.drain() #expect(harness.disappeared == [WindowID(rawValue: 1)]) #expect(harness.state.windows[WindowID(rawValue: 1)] == nil) #expect(harness.state.visibleWindows == [WindowID(rawValue: 2)]) harness.backend.emit(.spaceChanged) try await harness.drain() #expect(harness.appeared.isEmpty) harness.backend.emit(.unhidden(NativeApplicationInfo(id: engineApp))) try await harness.drain() #expect(harness.appeared.map(\.window.id) == [WindowID(rawValue: 1)]) #expect(harness.state.visibleWindows.count == 2) } @Test(arguments: [ NativeTabGroup.windowIDs([WindowID(rawValue: 1), WindowID(rawValue: 3)]), .titles(["First Native Tab", "Second Native Tab"]), ]) func hiddenNativeTabMembersJoinTheirVisiblePeerAndSurviveCachedSelection(group: NativeTabGroup) async throws { let harness = nativeTabHarness(group: group) try await harness.start() #expect( Set(harness.appeared.map(\.window.id)) == [ WindowID(rawValue: 1), WindowID(rawValue: 2), WindowID(rawValue: 3), ]) let key = WorkspaceKey(display: engineDisplay, space: engineSpace) let stack = try #require( harness.state.workspaces[key]?.tree?.stacks.first { $0.windows.contains(WindowID(rawValue: 1)) }) #expect(Set(stack.windows) == [WindowID(rawValue: 1), WindowID(rawValue: 3)]) let reads = harness.first.windowReads harness.clearEvents() harness.backend.visible[WindowID(rawValue: 1)] = initialEngineFrame harness.backend.visible[WindowID(rawValue: 3)] = nil harness.backend.emit(.spaceChanged) try await harness.drain() #expect(harness.disappeared.isEmpty) #expect( harness.state.windows[WindowID(rawValue: 1)] != nil && harness.state.windows[WindowID(rawValue: 3)] != nil) #expect(harness.first.windowReads == reads) } @Test(arguments: [ NativeTabGroup.windowIDs([WindowID(rawValue: 1), WindowID(rawValue: 3)]), .titles(["First Native Tab", "Second Native Tab"]), ]) func axSelectionAheadOfCGKeepsThePreviousNativeTabMember(group: NativeTabGroup) async throws { let harness = nativeTabHarness(group: group) try await harness.start() defer { harness.world.stop() } let first = WindowID(rawValue: 1) let second = WindowID(rawValue: 3) harness.first.snapshots = [harness.first.snapshots[0]] harness.first.focused = first harness.first.deferFocusRead = true harness.first.emit(.focusedWindowChanged) try await harness.drain() try #require(harness.first.hasPendingFocus) #expect(harness.state.windows[first] != nil && harness.state.windows[second] != nil) harness.backend.visible[first] = initialEngineFrame harness.backend.visible[second] = nil try harness.first.completeFocus(first) try await harness.drain() let key = WorkspaceKey(display: engineDisplay, space: engineSpace) #expect(harness.state.focusedWindow == first) #expect(harness.state.workspaces[key]?.tree?.tileCount == 2) #expect(harness.state.workspaces[key]?.tree?.stacks.contains { Set($0.windows) == [first, second] } == true) #expect(harness.scheduler.pendingCount == 0) } @Test func aCataloguedHiddenOriginalJoinsItsFreshSelectedNativeTabWithoutLosingItsID() async throws { let harness = NativeHarness() var original = harness.first.snapshots[0].window original.facts = WindowFacts(bundleID: engineApp.bundleID, title: original.title, hasNativeTabs: false) harness.first.snapshots[0] = NativeWindowSnapshot(window: original, frame: initialEngineFrame) try await harness.start() defer { harness.world.stop() } let first = harness.first.snapshots[0].window var tab = engineWindow(3, title: "New Native Tab") tab.facts = WindowFacts( bundleID: engineApp.bundleID, title: tab.title, hasNativeTabs: true, nativeTabGroup: .titles([first.title, tab.title])) harness.first.snapshots = [NativeWindowSnapshot(window: tab, frame: initialEngineFrame)] harness.backend.visible[first.id] = nil harness.backend.visible[tab.id] = initialEngineFrame harness.first.focused = tab.id harness.first.emit(.focusedWindowChanged) try await harness.drain() let key = WorkspaceKey(display: engineDisplay, space: engineSpace) #expect(harness.state.windows[first.id] != nil) #expect(harness.state.workspaces[key]?.tree?.stacks.contains { Set($0.windows) == [first.id, tab.id] } == true) #expect(harness.state.focusedWindow == tab.id) } @Test func aNewNativeTabWhoseCGVisibilityLagsItsAXFocusIsReconciledAfterTheFocusRead() async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } let first = WindowID(rawValue: 1) var tab = engineWindow(3, title: "Selected Native Tab") tab.facts = WindowFacts( bundleID: engineApp.bundleID, title: tab.title, hasNativeTabs: true, nativeTabGroup: .titles(["Original Document", tab.title])) harness.first.snapshots = [NativeWindowSnapshot(window: tab, frame: initialEngineFrame)] harness.first.deferFocusRead = true harness.first.emit(.focusedWindowRead(tab.id.rawValue)) try await harness.drain() try #require(harness.first.hasPendingFocus) #expect(harness.state.windows[tab.id] == nil) let visibilityReads = harness.backend.visibleReads let axReads = harness.first.windowReads harness.backend.visible[first] = nil harness.backend.visible[tab.id] = initialEngineFrame try harness.first.completeFocus(tab.id) try await harness.drain() #expect(harness.state.windows[tab.id] != nil) #expect(harness.state.focusedWindow == tab.id) #expect(harness.backend.visibleReads == visibilityReads + 1) #expect(harness.first.windowReads == axReads + 1) #expect(harness.scheduler.pendingCount == 0) } @Test func nativeTabsDiscoveredBySelectionRetainTheirIDsWhenOnlySelectedMemberIsEnumerated() async throws { let harness = NativeHarness() harness.second.snapshots = [] harness.backend.visible[WindowID(rawValue: 2)] = nil var first = engineWindow(1, title: "First Native Tab") first.facts = WindowFacts(bundleID: engineApp.bundleID, title: first.title, hasNativeTabs: false) harness.first.snapshots = [NativeWindowSnapshot(window: first, frame: initialEngineFrame)] try await harness.start() #expect(Set(harness.state.windows.keys) == [first.id]) let group = NativeTabGroup.titles([first.title, "Second Native Tab"]) var second = engineWindow(3, title: "Second Native Tab") second.facts = WindowFacts( bundleID: engineApp.bundleID, title: second.title, hasNativeTabs: true, nativeTabGroup: group) harness.first.snapshots = [NativeWindowSnapshot(window: second, frame: initialEngineFrame)] harness.backend.visible[first.id] = nil harness.backend.visible[second.id] = initialEngineFrame harness.backend.emit(.activated(NativeApplicationInfo(id: engineApp))) try await harness.drain() #expect(Set(harness.state.windows.keys) == [first.id, second.id]) first.facts?.hasNativeTabs = true first.facts?.nativeTabGroup = group harness.first.snapshots = [NativeWindowSnapshot(window: first, frame: initialEngineFrame)] harness.backend.visible[first.id] = initialEngineFrame harness.backend.visible[second.id] = nil harness.backend.emit(.activated(NativeApplicationInfo(id: engineApp))) try await harness.drain() #expect(Set(harness.state.windows.keys) == [first.id, second.id]) let key = WorkspaceKey(display: engineDisplay, space: engineSpace) let stack = try #require(harness.state.workspaces[key]?.tree?.stacks.first) #expect(Set(stack.windows) == [first.id, second.id]) #expect(harness.state.workspaces[key]?.tree?.tileCount == 1) harness.clearEvents() harness.first.snapshots = [NativeWindowSnapshot(window: second, frame: initialEngineFrame)] harness.backend.visible[first.id] = nil harness.backend.visible[second.id] = initialEngineFrame harness.backend.emit(.activated(NativeApplicationInfo(id: engineApp))) try await harness.drain() #expect(harness.disappeared.isEmpty) #expect(Set(harness.state.windows.keys) == [first.id, second.id]) #expect(harness.state.workspaces[key]?.tree?.tileCount == 1) } @Test(arguments: [ "unknown", "different-member-ids", "excluded-member", "excluded-peer", "hidden-app", "ambiguous-titles", ]) func invisibleWindowsWithoutAnUnambiguousVisibleNativeTabPeerStayOut(condition: String) async throws { let group = NativeTabGroup.windowIDs([WindowID(rawValue: 1), WindowID(rawValue: 3)]) let harness = nativeTabHarness(group: group) var first = harness.first.snapshots[0].window var peer = harness.first.snapshots[1].window var excludedFirst = false var excludedPeer = false switch condition { case "unknown": first.facts?.hasNativeTabs = nil case "different-member-ids": peer.facts?.nativeTabGroup = .windowIDs([WindowID(rawValue: 3), WindowID(rawValue: 4)]) case "excluded-member": excludedFirst = true case "excluded-peer": excludedPeer = true case "hidden-app": harness.backend.infos[0].isHidden = true case "ambiguous-titles": let titles = NativeTabGroup.titles(["First Native Tab", "Second Native Tab"]) first.facts?.nativeTabGroup = titles peer.facts?.nativeTabGroup = titles var other = engineWindow(4, title: "Second Native Tab") other.facts = peer.facts harness.first.snapshots.append(NativeWindowSnapshot(window: other, frame: initialEngineFrame)) harness.backend.visible[other.id] = initialEngineFrame default: preconditionFailure("Unknown native tab rejection case") } harness.first.snapshots[0] = NativeWindowSnapshot( window: first, frame: initialEngineFrame, excluded: excludedFirst) harness.first.snapshots[1] = NativeWindowSnapshot( window: peer, frame: initialEngineFrame, excluded: excludedPeer) try await harness.start() #expect(harness.state.windows[first.id] == nil) #expect(!harness.appeared.contains { $0.window.id == first.id }) } private func nativeTabHarness(group: NativeTabGroup) -> NativeHarness { let harness = NativeHarness() let members = [(UInt32(1), "First Native Tab"), (UInt32(3), "Second Native Tab")] harness.first.snapshots = members.map { id, title in var window = engineWindow(id, title: title) window.facts = WindowFacts( bundleID: engineApp.bundleID, title: title, hasNativeTabs: true, nativeTabGroup: group) return NativeWindowSnapshot(window: window, frame: initialEngineFrame) } harness.backend.visible[WindowID(rawValue: 1)] = nil harness.backend.visible[WindowID(rawValue: 3)] = initialEngineFrame harness.first.focused = WindowID(rawValue: 3) return harness } @Test func spaceVisibilityReleasesOffscreenTilesAndRestoresCachedWindows() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.backend.visible[WindowID(rawValue: 1)] = nil harness.backend.emit(.spaceChanged) try await harness.drain() #expect(harness.disappeared == [WindowID(rawValue: 1)]) #expect(harness.state.visibleWindows == [WindowID(rawValue: 2)]) #expect(harness.first.windowReads == 2) harness.clearEvents() harness.backend.visible[WindowID(rawValue: 1)] = initialEngineFrame harness.backend.emit(.spaceChanged) try await harness.drain() #expect(harness.appeared.map(\.window.id) == [WindowID(rawValue: 1)]) #expect(harness.first.windowReads == 2) #expect(harness.state.visibleWindows.count == 2) } @Test func appRefreshReleasesWindowsMissingFromVisibleServerList() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.backend.visible[WindowID(rawValue: 1)] = nil harness.backend.emit(.activated(NativeApplicationInfo(id: engineApp))) try await harness.drain() #expect(harness.disappeared == [WindowID(rawValue: 1)]) #expect(harness.state.visibleWindows == [WindowID(rawValue: 2)]) #expect(harness.second.windowReads == 1) } @Test(arguments: [false, true]) func activationAndFocusRepairAnotherAppsPhantomTile(focus: Bool) async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.backend.visible[WindowID(rawValue: 2)] = nil if focus { harness.first.emit(.focusedWindowChanged) } else { harness.backend.emit(.activated(NativeApplicationInfo(id: engineApp))) } try await harness.drain() #expect(harness.disappeared == [WindowID(rawValue: 2)]) #expect(harness.state.visibleWindows == [WindowID(rawValue: 1)]) #expect(harness.second.windowReads == 2) #expect(harness.scheduler.pendingCount == 0) } @Test func focusingACachedUnmanagedWindowRefreshesItsFactsBeforePublishingFocus() async throws { let harness = NativeHarness() harness.first.snapshots[0].excluded = true try await harness.start() defer { harness.world.stop() } let id = WindowID(rawValue: 1) #expect(harness.state.windows[id] == nil) let reads = harness.first.windowReads harness.first.snapshots[0].excluded = false harness.first.emit(.focusedWindowRead(1)) try await harness.drain() #expect(harness.first.windowReads > reads) #expect(harness.state.windows[id] != nil) #expect(harness.state.focusedWindow == id) } @Test func newerActivationSuppressesAnOlderSuspendedFocusRead() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.first.deferFocusRead = true harness.backend.emit(.activated(NativeApplicationInfo(id: engineApp))) try await harness.drain() #expect(harness.first.hasPendingFocus) harness.backend.emit(.activated(NativeApplicationInfo(id: secondEngineApp))) try harness.first.completeFocus(WindowID(rawValue: 1)) try await harness.drain() let focused = harness.events.compactMap { event -> WindowID? in if case .observed(.focusChanged(let window)) = event { return window } return nil } #expect(focused == [WindowID(rawValue: 2)]) } @Test func axErrorBurstTriggersOneFreshReconciliation() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.backend.visible[WindowID(rawValue: 2)] = nil harness.first.emit(.failure(.windowFrame("one"))) harness.first.emit(.failure(.windowFrame("two"))) try await harness.drain() #expect(harness.disappeared.isEmpty) let reads = harness.backend.visibleReads harness.first.emit(.failure(.windowFrame("three"))) try await harness.drain() #expect(harness.disappeared == [WindowID(rawValue: 2)]) #expect(harness.backend.visibleReads == reads + 1) for _ in 0..<3 { harness.first.emit(.failure(.windowFrame("new failure burst"))) } try await harness.drain() #expect(harness.backend.visibleReads == reads + 2) try harness.scheduler.advance(by: 60) try await harness.drain() #expect(harness.backend.visibleReads == reads + 2) } @Test func failedFocusAndRaiseOperationsAlsoTriggerRecovery() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.backend.visible[WindowID(rawValue: 2)] = nil harness.first.error = AppThreadFailure.windowFrame("AX unavailable") harness.world.focus(WindowID(rawValue: 1)) { _ in } harness.world.raise(WindowID(rawValue: 1)) { _ in } harness.world.focus(WindowID(rawValue: 1)) { _ in } try await harness.drain() #expect(harness.disappeared == [WindowID(rawValue: 2)]) let reads = harness.backend.visibleReads try harness.scheduler.advance(by: 60) try await harness.drain() #expect(harness.backend.visibleReads == reads) } @Test func failedVisibilityReadDoesNotRemoveCachedWindows() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.backend.visible.removeAll() harness.backend.visibleError = AppThreadFailure.windowServer("unavailable") harness.backend.emit(.activated(NativeApplicationInfo(id: engineApp))) try await harness.drain() #expect(harness.disappeared.isEmpty) #expect(harness.state.visibleWindows.count == 2) #expect(!harness.failures.isEmpty) harness.backend.visibleError = nil harness.backend.emit(.activated(NativeApplicationInfo(id: engineApp))) try await harness.drain() #expect(Set(harness.disappeared) == [WindowID(rawValue: 1), WindowID(rawValue: 2)]) } @Test func minimizeStaysExcludedThroughSpaceChangesUntilDeminimized() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.first.emit(.windowMinimized(1)) try await harness.drain() #expect( harness.events.contains { if case .observed(.windowMinimized(WindowID(rawValue: 1))) = $0 { true } else { false } }) harness.backend.emit(.spaceChanged) try await harness.drain() #expect(harness.appeared.isEmpty) #expect(harness.state.visibleWindows == [WindowID(rawValue: 2)]) harness.first.emit(.windowDeminimized(1)) try await harness.drain() #expect(harness.appeared.map(\.window.id) == [WindowID(rawValue: 1)]) #expect(harness.state.visibleWindows.count == 2) } @Test func excludedAXSnapshotReleasesExistingTile() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.first.snapshots[0].excluded = true harness.world.reconcile([engineApp]) try await harness.drainReconciliation() #expect(harness.disappeared == [WindowID(rawValue: 1)]) #expect(harness.state.visibleWindows == [WindowID(rawValue: 2)]) } @Test func onscreenEntryOutsideDisplaysDoesNotKeepTile() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() let offscreen = Rect(x: -1000, y: -1000, width: 300, height: 200) harness.backend.visible[WindowID(rawValue: 1)] = offscreen harness.backend.emit(.spaceChanged) try await harness.drain() #expect(harness.disappeared == [WindowID(rawValue: 1)]) #expect(harness.state.visibleWindows == [WindowID(rawValue: 2)]) } @Test func missingSpaceIsReportedAndCachedWindowRetriesOnNextRefresh() async throws { let harness = NativeHarness() harness.backend.spaces.removeAll() try await harness.start() #expect(harness.appeared.isEmpty) #expect(harness.failures.contains(.operation(.missingSpace(WindowID(rawValue: 1), display: engineDisplay)))) #expect(suspensions(harness).last == [engineDisplay]) harness.clearEvents() harness.backend.spaces[engineDisplay] = engineSpace harness.backend.emit(.spaceChanged) try await harness.drain() #expect(harness.appeared.count == 2) #expect(harness.first.windowReads == 1) #expect(harness.second.windowReads == 1) #expect(harness.failures.isEmpty) } @Test func missingSpaceRetriesOnSingleAppRefreshWithoutIdlePolling() async throws { let harness = NativeHarness() harness.backend.spaces.removeAll() try await harness.start() harness.backend.spaces[engineDisplay] = engineSpace harness.clearEvents() harness.world.reconcile([engineApp]) try await harness.drain() #expect(harness.backend.spaceReads == 2) #expect(Set(harness.appeared.map(\.window.id)) == [WindowID(rawValue: 1), WindowID(rawValue: 2)]) #expect(harness.second.windowReads == 1) } @Test func wakeAndDisplayChangeReconcileMissingAXNotifications() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.first.snapshots.append(NativeWindowSnapshot(window: engineWindow(3), frame: initialEngineFrame)) harness.backend.visible[WindowID(rawValue: 3)] = initialEngineFrame let visibleReads = harness.backend.visibleReads harness.backend.emit(.woke) harness.backend.emit(.displaysChanged(harness.backend.displays)) try await harness.drain() #expect(harness.appeared.map(\.window.id) == [WindowID(rawValue: 3)]) #expect(harness.first.windowReads == 2) #expect(harness.second.windowReads == 2) #expect(harness.backend.visibleReads == visibleReads + 1) #expect(harness.scheduler.pendingCount == 0) } @Test func afterSettleRequestsCoalesceAndDoNotInstallIdleWork() async throws { let harness = NativeHarness() try await harness.start() harness.clearEvents() harness.first.snapshots.removeAll() for _ in 0..<100 { harness.world.reconcile([engineApp]) } #expect(harness.scheduler.pendingCount == 1) try await harness.drainReconciliation() #expect(harness.first.windowReads == 2) #expect(harness.second.windowReads == 1) #expect(harness.disappeared.isEmpty) #expect(harness.state.windows[WindowID(rawValue: 1)] != nil) #expect(harness.backend.visibleReads == 2) #expect(harness.scheduler.nextDeadline == nil) let reads = harness.backend.visibleReads try harness.scheduler.advance(by: 60) try await harness.drain() #expect(harness.backend.visibleReads == reads) harness.world.reconcile([]) #expect(harness.scheduler.pendingCount == 0) } }