Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266import ParcelCoreimport ParcelPlatformimport Testing
@testable import ParcelEngine
@MainActorstruct NativeCloseTests { @Test(arguments: [false, true, nil] as [Bool?], [false, true]) func closingUsesTheOwnerAndConfirmsEditedOrUnknownDocuments(edited: Bool?, approve: Bool) async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } harness.first.edited = edited harness.first.focused = nil var confirmations: [Bool?] = [] try await harness.world.close(WindowID(rawValue: 1)) { value in confirmations.append(value) return edited == false || approve } #expect(confirmations == [edited]) let closes = edited == false || approve #expect(harness.first.closed == (closes ? [WindowID(rawValue: 1)] : [])) #expect(harness.second.closed.isEmpty) #expect(harness.first.writes.isEmpty) #expect(harness.first.focused == (closes && edited != false ? WindowID(rawValue: 1) : nil)) try await harness.drain() #expect(harness.disappeared.contains(WindowID(rawValue: 1)) == closes) }
@Test func failedEditedReadDoesNotCloseOrPretendTheDocumentIsClean() async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } harness.first.error = AXPlatformError.timedOut var confirmed = false await #expect(throws: EnginePlatformFailure.self) { try await harness.world.close(WindowID(rawValue: 1)) { _ in confirmed = true return true } } #expect(!confirmed) #expect(harness.first.closed.isEmpty) }
@Test func stoppedWorldCannotCloseAfterConfirmation() async throws { let harness = NativeHarness() try await harness.start() harness.first.edited = true await #expect(throws: EnginePlatformFailure.vanished(WindowID(rawValue: 1))) { try await harness.world.close(WindowID(rawValue: 1)) { _ in harness.world.stop() return true } } #expect(harness.first.closed.isEmpty) }
@Test(arguments: [true, nil] as [Bool?]) func commandCloseUsesTheExistingEditedDocumentPath(edited: Bool?) async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } harness.first.edited = edited harness.first.focused = nil try await harness.world.close(WindowID(rawValue: 1)) #expect(harness.first.focused == WindowID(rawValue: 1)) #expect(harness.first.closed == [WindowID(rawValue: 1)]) #expect(harness.second.closed.isEmpty) try await harness.drain() #expect(harness.disappeared.contains(WindowID(rawValue: 1))) }
@Test func fullscreenToggleUsesTheOwnerAndReconcilesObservedClassificationBothWays() async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } let window = WindowID(rawValue: 1) harness.clearEvents() try await harness.world.toggleFullscreen(window) #expect(harness.first.fullscreenToggles == [window]) #expect(harness.second.fullscreenToggles.isEmpty) try await harness.drain() #expect(harness.disappeared == [window]) #expect(harness.state.windows[window] == nil) #expect(harness.world.inspectionWindow(window)?.facts?.isFullScreen == true) #expect(try await harness.world.focusedCommandWindow()?.id == window) harness.clearEvents() try await harness.world.toggleFullscreen(window) try await harness.drain() #expect(harness.appeared.map(\.window.id) == [window]) #expect(harness.world.inspectionWindow(window)?.facts?.isFullScreen == false) #expect(harness.first.fullscreenToggles == [window, window]) }
@Test func fullscreenExitReconcilesDelayedInventoryAndVisibilityWithoutAnotherNotification() async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } let window = WindowID(rawValue: 1) try await harness.world.toggleFullscreen(window) try await harness.drain() try await harness.world.toggleFullscreen(window) let returned = harness.first.snapshots let visible = harness.backend.visible[window] harness.first.snapshots = [] harness.backend.visible[window] = nil try await harness.drain() #expect(harness.state.windows[window] == nil) try harness.scheduler.advance(by: 0.4) try await harness.drain() harness.first.snapshots = returned try harness.scheduler.advance(by: 0.4) try await harness.drain() #expect(harness.state.windows[window] == nil) harness.backend.visible[window] = visible try harness.scheduler.advance(by: 0.4) try await harness.drain() #expect(harness.state.windows[window] != nil) try harness.scheduler.advance(by: 0.4) try await harness.drain() let reads = harness.first.windowReads try harness.scheduler.advance(by: 60) try await harness.drain() #expect(harness.first.windowReads == reads) }
@Test(arguments: ["budget", "destroy", "stop"]) func fullscreenReconciliationIsBoundedAndCancelledWhenItsTargetEnds(ending: String) async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } let window = WindowID(rawValue: 1) try await harness.world.toggleFullscreen(window) try await harness.drain() try await harness.world.toggleFullscreen(window) harness.first.snapshots = [] harness.backend.visible[window] = nil try await harness.drain() if ending == "destroy" { harness.first.emit(.windowDestroyed(window.rawValue)) try await harness.drain() } else if ending == "stop" { harness.world.stop() } else { for _ in 0..<8 { try harness.scheduler.advance(by: 0.4) try await harness.drain() } } let reads = harness.first.windowReads try harness.scheduler.advance(by: 60) try await harness.drain() #expect(harness.first.windowReads == reads) #expect(harness.state.windows[window] == nil) }
@Test(arguments: ["confirmed", "wrong-owner", "missing-catalog", "ordinary"]) func omittedFullscreenInspectionRequiresFreshCatalogOwnership(proof: String) async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } let window = WindowID(rawValue: 1) if proof != "ordinary" { try await harness.world.toggleFullscreen(window) try await harness.drain() #expect(harness.world.inspectionWindow(window)?.facts?.isFullScreen == true) } harness.first.snapshots = [] if proof == "wrong-owner" { harness.backend.catalogOwners[window] = secondEngineApp.pid } else if proof == "missing-catalog" { harness.backend.catalogOwners[window] = nil harness.backend.catalogFrames[window] = nil harness.backend.visible[window] = nil } harness.world.reconcile([engineApp]) try await harness.drain() if proof == "ordinary" { #expect(harness.state.windows[window] != nil) #expect(harness.world.inspectionWindow(window)?.id == window) #expect(try await harness.world.focusedCommandWindow()?.id == window) return } #expect(harness.state.windows[window] == nil) if proof == "confirmed" { #expect(harness.world.inspectionWindow(window)?.facts?.isFullScreen == true) #expect(try await harness.world.focusedCommandWindow()?.id == window) harness.first.emit(.windowDestroyed(1)) try await harness.drain() #expect(harness.world.inspectionWindow(window) == nil) } else { #expect(harness.world.inspectionWindow(window) == nil) await #expect(throws: EngineWindowCommandError.noFocusedWindow) { try await harness.world.focusedCommandWindow() } } }
@Test func unverifiedFullscreenChangeFailsWithoutChangingTheObservedState() async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } let window = WindowID(rawValue: 1) harness.first.fullscreenError = EngineWindowCommandError.fullscreenUnchanged(window) harness.clearEvents() await #expect(throws: EngineWindowCommandError.fullscreenUnchanged(window)) { try await harness.world.toggleFullscreen(window) } #expect(harness.disappeared.isEmpty) #expect(harness.first.fullscreenToggles.isEmpty) #expect(harness.state.windows[window] != nil) }
@Test func nativeFocusedInspectionUsesTheFrontmostOwnerAndSurfacesReadFailures() async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } harness.backend.frontmost = secondEngineApp #expect(try await harness.world.focusedCommandWindow()?.id == WindowID(rawValue: 2)) harness.second.error = AXPlatformError.timedOut await #expect(throws: EnginePlatformFailure.accessibility(secondEngineApp, window: nil, cause: .timedOut)) { try await harness.world.focusedCommandWindow() } }
@Test(arguments: ["no-app", "no-window", "missing-snapshot"]) func missingNativeFocusNeverClosesTheStaleCoreTarget(cause: String) async throws { let harness = NativeHarness() try await harness.start() let links = FakeEngineLinks() let state = reduce(harness.state, .focusChanged(WindowID(rawValue: 1))).0 let engine = try ParcelEngine( world: harness.world, clock: harness.clock, scheduler: harness.scheduler, displayLink: { try links.make($0) }, announcer: FakeEngineAnnouncer(), log: FakeEngineLog(), configuration: engineConfiguration(), initialState: state, trace: nil, reduceMotionSource: nil) try await engine.start() try await harness.drain() #expect(engine.snapshot.focusedWindow == WindowID(rawValue: 1)) switch cause { case "no-app": harness.backend.frontmost = nil case "no-window": harness.first.focused = nil default: harness.first.focused = WindowID(rawValue: 99) } let before = engine.snapshot await #expect(throws: EngineWindowCommandError.noFocusedWindow) { try await engine.perform(.close) } #expect(harness.first.closed.isEmpty) #expect(harness.second.closed.isEmpty) #expect(engine.snapshot == before) await engine.stop() }
@Test func missingWindowFailsWithoutPrompting() async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } await #expect(throws: EnginePlatformFailure.vanished(WindowID(rawValue: 999))) { try await harness.world.close(WindowID(rawValue: 999)) { _ in Issue.record("A missing window must not prompt") return true } } }}