Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487import AppKitimport Foundationimport ParcelEngineimport Testing
@testable import ParcelApp
@Suite @MainActorstruct AppSetupApplicationsTests { @Test func runningBundleIDsIncludeHiddenAndAccessoryButNotTerminatedOrUnidentifiedApps() { let workspace = Workspace() let hidden = Application(pid: 12, bundleID: "test.hidden") hidden.hidden = true let accessory = Application(pid: 13, bundleID: "test.accessory", policy: .accessory) let terminated = Application(pid: 14, bundleID: "test.terminated") terminated.terminated = true workspace.running = [hidden, accessory, terminated, Application(pid: 15, bundleID: nil)] #expect(workspace.helper().runningBundleIDs == ["test.hidden", "test.accessory"]) }
@Test func runningApplicationIsUnhiddenWithoutLookupOrLaunch() async throws { let workspace = Workspace() let application = Application(pid: 12, bundleID: "test.editor") application.hidden = true workspace.running = [application] #expect(try await workspace.helper().launch(bundleID: "test.editor") == nil) #expect(application.unhides == 1) #expect(!application.hidden) #expect(workspace.lookups.isEmpty) #expect(workspace.opened.isEmpty) }
@Test func visibleRunningApplicationIsNotReopenedOrUnhidden() async throws { let workspace = Workspace() let application = Application(pid: 12, bundleID: "test.editor") workspace.running = [application] #expect(try await workspace.helper().launch(bundleID: "test.editor") == nil) #expect(application.unhides == 0) #expect(workspace.opened.isEmpty) }
@Test func everyRunningInstanceIsUnhiddenWithoutLaunchingANewInstance() async throws { let workspace = Workspace() let first = Application(pid: 12, bundleID: "test.editor") let second = Application(pid: 13, bundleID: "test.editor") first.hidden = true second.hidden = true workspace.running = [first, second] #expect(try await workspace.helper().launch(bundleID: "test.editor") == nil) #expect(first.unhides == 1 && second.unhides == 1) #expect(workspace.opened.isEmpty) }
@Test func launchDisablesFocusRecentsPromptsAndNewInstancesAndCapturesActualIdentity() async throws { let workspace = Workspace() workspace.running = [Application(pid: 41, bundleID: "test.other"), Application(pid: 18, bundleID: nil)] let launched = Application(pid: 72, bundleID: "test.editor") launched.launchDate = 1234 workspace.launched = launched let receipt = try #require(try await workspace.helper().launch(bundleID: "test.editor")) let configuration = try #require(workspace.configurations.first) #expect(!configuration.activates) #expect(!configuration.addsToRecentItems) #expect(!configuration.promptsUserIfNeeded) #expect(!configuration.createsNewApplicationInstance) #expect(receipt.requestedBundleID == "test.editor") #expect(receipt.applicationURL == workspace.url) #expect(receipt.preexistingPIDs == [18, 41]) #expect(receipt.pid == 72) #expect(receipt.bundleID == "test.editor") #expect(receipt.bundleURL == launched.bundleURL) #expect(receipt.launchDate == 1234) #expect(!receipt.isTerminated) #expect(launched.unhides == 0) }
@Test(arguments: ["", " test.editor", "test.editor\n", "test..editor", "test/editor"]) func invalidRequestedIdentityNeverLooksUpOrLaunches(bundleID: String) async { let workspace = Workspace() await #expect(throws: AppSetupApplications.Failure.self) { try await workspace.helper().launch(bundleID: bundleID) } #expect(workspace.lookups.isEmpty) #expect(workspace.opened.isEmpty) }
@Test func missingApplicationAndNativeLaunchErrorAreNotSilentlyIgnored() async { let workspace = Workspace() workspace.url = nil await #expect(throws: AppSetupApplications.Failure.self) { try await workspace.helper().launch(bundleID: "test.editor") } #expect(workspace.opened.isEmpty) workspace.url = URL(fileURLWithPath: "/Applications/Editor.app") workspace.openError = NativeFailure.denied await #expect(throws: NativeFailure.denied) { try await workspace.helper().launch(bundleID: "test.editor") } }
@Test(arguments: ["wrongBundle", "terminated", "invalidPID", "missingURL", "wrongURL", "invalidDate"]) func invalidLiveLaunchReceiptIsRejected(kind: String) async { let workspace = Workspace() let application = Application(pid: kind == "invalidPID" ? 0 : 72, bundleID: "test.editor") switch kind { case "wrongBundle": application.bundleID = "test.other" case "terminated": application.terminated = true case "missingURL": application.bundleURL = nil case "wrongURL": application.bundleURL = URL(fileURLWithPath: "/Applications/Other.app") case "invalidDate": application.launchDate = .nan default: break } workspace.launched = application await #expect(throws: AppSetupApplications.Failure.self) { try await workspace.helper().launch(bundleID: "test.editor") } }
@Test func refusedOrUnconfirmedUnhideSurfacesAnError() async { let workspace = Workspace() let application = Application(pid: 12, bundleID: "test.editor") application.hidden = true application.acceptUnhide = false application.applyUnhide = false workspace.running = [application] await #expect(throws: AppSetupApplications.Failure.self) { try await workspace.helper().launch(bundleID: "test.editor") } application.acceptUnhide = true application.applyUnhide = false await #expect(throws: AppSetupApplications.Failure.self) { try await workspace.helper().launch(bundleID: "test.editor") } #expect(workspace.opened.isEmpty) }
@Test func hideOnlySelectsVisibleRegularUserAppsOutsideTheSetup() async throws { let workspace = Workspace() let eligible = Application(pid: 10, bundleID: "test.other") let appleUserApp = Application(pid: 11, bundleID: "com.apple.userapp") let kept = Application(pid: 12, bundleID: "test.kept") let selfApp = Application(pid: 1, bundleID: "test.runner") let parcel = Application(pid: 13, bundleID: "dev.vinnymac.parcel") let parcelVariant = Application(pid: 14, bundleID: "dev.vinnymac.parcel.reality") let accessory = Application(pid: 15, bundleID: "test.accessory", policy: .accessory) let background = Application(pid: 16, bundleID: "test.background", policy: .prohibited) let finder = Application(pid: 17, bundleID: "com.apple.finder") let hidden = Application(pid: 18, bundleID: "test.hidden") hidden.hidden = true let dead = Application(pid: 19, bundleID: "test.dead") dead.terminated = true let system = Application(pid: 20, bundleID: "test.system") system.bundleURL = URL(fileURLWithPath: "/System/Library/CoreServices/System.app") let unidentified = Application(pid: 21, bundleID: nil) let uncertain = Application(pid: 22, bundleID: "test.uncertain") uncertain.bundleURL = nil let sameSelfBundle = Application(pid: 23, bundleID: "test.runner") let excluded = [ kept, selfApp, parcel, parcelVariant, accessory, background, finder, hidden, dead, system, unidentified, uncertain, sameSelfBundle, ] workspace.running = [eligible, appleUserApp] + excluded #expect( try await workspace.helper().hideOtherApps(keeping: ["test.kept"]) == ["test.other", "com.apple.userapp"]) #expect(eligible.hidden && appleUserApp.hidden) #expect(excluded.allSatisfy { $0.hides == 0 }) }
@Test func hiddenBundleIDsAreUniqueForMultipleInstances() async throws { let workspace = Workspace() workspace.running = [ Application(pid: 10, bundleID: "test.other"), Application(pid: 11, bundleID: "test.other"), ] #expect(try await workspace.helper().hideOtherApps(keeping: []) == ["test.other"]) #expect(workspace.running.allSatisfy { $0.hides == 1 }) }
@Test func hideRefusalRestoresEarlierHiddenApplications() async throws { let workspace = Workspace() let hidden = Application(pid: 10, bundleID: "test.hidden") let refused = Application(pid: 11, bundleID: "test.refused") refused.acceptHide = false refused.applyHide = false workspace.running = [hidden, refused] do { _ = try await workspace.helper().hideOtherApps(keeping: []) Issue.record("A refused hide must throw") } catch AppSetupApplications.Failure.hideRefused(let bundleID, let hiddenBundleIDs) { #expect(bundleID == "Editor.app") #expect(hiddenBundleIDs == ["test.hidden"]) } #expect(!hidden.hidden) #expect(hidden.unhides == 1) #expect(!refused.hidden) }
@Test func unconfirmedHideIsNotReportedAsSuccessful() async { let workspace = Workspace() let application = Application(pid: 10, bundleID: "test.other") application.applyHide = false workspace.running = [application] await #expect(throws: AppSetupApplications.Failure.self) { try await workspace.helper().hideOtherApps(keeping: []) } }
@Test(arguments: [false, true]) func nativeVisibilityStatusCannotOverrideConfirmedEffects(_ status: Bool) async throws { let workspace = Workspace() let application = Application(pid: 10, bundleID: "test.other") application.acceptHide = status application.acceptUnhide = status workspace.running = [application] let helper = workspace.helper() var confirmed: Set<Int32> = [] helper.onHidden = { confirmed.formUnion($0) } #expect(try await helper.hideOtherApps(keeping: []) == ["test.other"]) #expect(application.hidden && confirmed == [application.pid]) try await helper.unhideApps(["test.other"]) #expect(!application.hidden && application.unhides == 1) try await helper.unhideApps(["test.other"]) #expect(application.unhides == 1) }
@Test func falseUnhideStatusWithVerifiedEffectAllowsAnExistingSetupApp() async throws { let workspace = Workspace() let application = Application(pid: 10, bundleID: "test.editor") application.hidden = true application.acceptUnhide = false workspace.running = [application] #expect(try await workspace.helper().launch(bundleID: "test.editor") == nil) #expect(!application.hidden && application.unhides == 1) #expect(workspace.opened.isEmpty) }
@Test func falseHideStatusStillTracksTheActualEffectForFailureRollback() async throws { let workspace = Workspace() let hidden = Application(pid: 10, bundleID: "test.hidden") hidden.acceptHide = false hidden.acceptUnhide = false let refused = Application(pid: 11, bundleID: "test.refused") refused.applyHide = false workspace.running = [hidden, refused] let helper = workspace.helper() do { _ = try await helper.hideOtherApps(keeping: []) Issue.record("An unconfirmed hide must throw even when another app becomes hidden") } catch AppSetupApplications.Failure.hideRefused(_, let hiddenBundleIDs) { #expect(hiddenBundleIDs == ["test.hidden"]) } #expect(!hidden.hidden && hidden.unhides == 1) #expect(!refused.hidden) try await helper.unhideApps(["test.hidden"]) #expect(hidden.unhides == 1) }
@Test func cancellationRecoversAnAttemptedHideEvenWhenItsNativeStatusIsFalse() async { let workspace = Workspace() let first = Application(pid: 10, bundleID: "test.first") let second = Application(pid: 11, bundleID: "test.second") first.acceptHide = false first.acceptUnhide = false first.unhideIgnoresVisible = true first.afterHide = { withUnsafeCurrentTask { $0?.cancel() } } workspace.running = [first, second] let helper = workspace.helper() await Task { @MainActor in _ = await #expect(throws: CancellationError.self) { try await helper.hideOtherApps(keeping: []) } }.value #expect(!first.hidden && first.hides == 1 && first.unhides == 1) #expect(second.hides == 0 && second.unhides == 0) }
@Test func cancelledTasksDoNotLaunchUnhideOrHide() async { let workspace = Workspace() let application = Application(pid: 10, bundleID: "test.other") application.hidden = true workspace.running = [application] let helper = workspace.helper() let task = Task { @MainActor in withUnsafeCurrentTask { $0?.cancel() } await #expect(throws: CancellationError.self) { try await helper.launch(bundleID: "test.closed") } await #expect(throws: CancellationError.self) { try await helper.launch(bundleID: "test.other") } application.hidden = false await #expect(throws: CancellationError.self) { try await helper.hideOtherApps(keeping: []) } } await task.value #expect(workspace.opened.isEmpty) #expect(application.unhides == 0 && application.hides == 0) }
@Test func cancellationBetweenHidesPreventsTheNextIrreversibleAction() async { let workspace = Workspace() let first = Application(pid: 10, bundleID: "test.first") let second = Application(pid: 11, bundleID: "test.second") first.afterHide = { withUnsafeCurrentTask { $0?.cancel() } } workspace.running = [first, second] let helper = workspace.helper() await Task { @MainActor in _ = await #expect(throws: CancellationError.self) { try await helper.hideOtherApps(keeping: []) } }.value #expect(first.hides == 1) #expect(second.hides == 0) }
@Test func realityLaunchCanRequestAFreshInstanceWithoutActivatingIt() async throws { let workspace = Workspace() let receipt = try await workspace.helper(createsNewInstances: true).launch(bundleID: "test.editor") #expect(receipt?.pid == 72) let configuration = try #require(workspace.configurations.first) #expect(configuration.createsNewApplicationInstance) #expect(!configuration.activates && !configuration.promptsUserIfNeeded && !configuration.addsToRecentItems) }
@Test func cancellationDuringNativeOpenStillReturnsItsAccountingReceipt() async throws { let workspace = Workspace() workspace.afterOpen = { withUnsafeCurrentTask { $0?.cancel() } } let helper = workspace.helper() let receipt = try await Task { @MainActor in try await helper.launch(bundleID: "test.editor") }.value #expect(receipt?.pid == 72) }
@Test func hideRequestsTheWholeBatchBeforeAnyDelayedFlagIsConfirmed() async throws { let workspace = Workspace() let first = Application(pid: 10, bundleID: "test.first") let second = Application(pid: 11, bundleID: "test.second") workspace.running = [first, second] first.afterHide = { #expect(!first.hidden && second.hides == 0) } second.afterHide = { #expect(!second.hidden && first.hides == 1) } let helper = workspace.helper() var confirmed: Set<Int32> = [] helper.onHidden = { pids in #expect(first.hides == 1 && second.hides == 1) #expect(pids.allSatisfy { pid in workspace.running.contains { $0.pid == pid && $0.hidden } }) confirmed.formUnion(pids) } #expect(try await helper.hideOtherApps(keeping: []) == ["test.first", "test.second"]) #expect(confirmed == [10, 11]) }
@Test func rollbackDoesNotUnhideAnAlreadyHiddenInstanceOfTheSameBundle() async throws { let workspace = Workspace() let visible = Application(pid: 10, bundleID: "test.editor") let alreadyHidden = Application(pid: 11, bundleID: "test.editor") alreadyHidden.hidden = true workspace.running = [visible, alreadyHidden] let helper = workspace.helper() _ = try await helper.hideOtherApps(keeping: []) try await helper.unhideApps(["test.editor"]) #expect(!visible.hidden && visible.unhides == 1) #expect(alreadyHidden.hidden && alreadyHidden.unhides == 0) }
@Test func validUnderscoreIdentifierCanLaunch() async throws { let workspace = Workspace() workspace.launched.bundleID = "test.my_editor" #expect(try await workspace.helper().launch(bundleID: "test.my_editor")?.bundleID == "test.my_editor") }
@Test func hideFailureReadbackIsBoundedOnceForTheWholeBatch() async { let workspace = Workspace() workspace.running = (10...20).map { Application(pid: Int32($0), bundleID: "test.app\($0)") } for application in workspace.running { application.applyHide = false } let start = ContinuousClock.now var now = start var lastHideDeadline: ContinuousClock.Instant? let timing = AppSetupVisibilityTiming( now: { now }, pause: { deadline, cancellable in if cancellable { lastHideDeadline = deadline } now = deadline }) await #expect(throws: AppSetupApplications.Failure.self) { try await workspace.helper(timing: timing).hideOtherApps(keeping: []) } #expect(lastHideDeadline == start.advanced(by: .milliseconds(500))) #expect(workspace.running.allSatisfy { $0.hides == 1 && !$0.hidden }) }
@Test func cancellationWaitsForThePendingHideBeforeRequestingItsReversal() async throws { let workspace = Workspace() let application = Application(pid: 10, bundleID: "test.editor") application.unhideIgnoresVisible = true application.afterHide = { withUnsafeCurrentTask { $0?.cancel() } } workspace.running = [application] let helper = workspace.helper() await Task { @MainActor in _ = await #expect(throws: CancellationError.self) { try await helper.hideOtherApps(keeping: []) } }.value try await Task.sleep(for: .milliseconds(30)) #expect(!application.hidden) #expect(application.unhides == 1) }
@Test func allowlistChecksPIDAndBirthAndRejectsMissingOrMalformedIdentities() throws { let value = try AppSetupHideAllowlist("[{\"pid\":12,\"launchDate\":123.0}]") #expect(value.contains(pid: 12, launchDate: 123)) #expect(!value.contains(pid: 12, launchDate: 124)) #expect(!value.contains(pid: 13, launchDate: 123)) #expect(!value.contains(pid: 12, launchDate: nil)) #expect(throws: (any Error).self) { try AppSetupHideAllowlist(nil) } #expect(throws: (any Error).self) { try AppSetupHideAllowlist("broken") } #expect(throws: (any Error).self) { try AppSetupHideAllowlist("[{\"pid\":0,\"launchDate\":123}]") } #expect(throws: (any Error).self) { try AppSetupHideAllowlist("[{\"pid\":12,\"launchDate\":123},{\"pid\":12,\"launchDate\":123}]") } #expect(try AppSetupHideAllowlist("[]").identities.isEmpty) }
private enum NativeFailure: Error { case denied }
@MainActor private final class Workspace { var running: [Application] = [] var launched = Application(pid: 72, bundleID: "test.editor") var url: URL? = URL(fileURLWithPath: "/Applications/Editor.app") var lookups: [String] = [] var opened: [URL] = [] var configurations: [NSWorkspace.OpenConfiguration] = [] var openError: NativeFailure? var afterOpen: @MainActor () -> Void = {}
func helper(createsNewInstances: Bool = false, timing: AppSetupVisibilityTiming = .live) -> AppSetupApplications { AppSetupApplications( applications: { self.running.map { $0.snapshot() } }, applicationURL: { self.lookups.append($0); return self.url }, openApplication: { url, configuration in self.opened.append(url) self.configurations.append(configuration) if let error = self.openError { throw error } self.afterOpen() return self.launched.snapshot() }, selfPID: 1, selfBundleID: "test.runner", createsNewInstances: createsNewInstances, timing: timing) } }
@MainActor private final class Application { let pid: Int32 var bundleID: String? var bundleURL: URL? = URL(fileURLWithPath: "/Applications/Editor.app") var launchDate: TimeInterval? = 1000 let policy: NSApplication.ActivationPolicy var hidden = false var terminated = false var hides = 0 var unhides = 0 var acceptHide = true var acceptUnhide = true var applyHide = true var applyUnhide = true var unhideIgnoresVisible = false var afterHide: @MainActor () -> Void = {}
init(pid: Int32, bundleID: String?, policy: NSApplication.ActivationPolicy = .regular) { self.pid = pid self.bundleID = bundleID self.policy = policy }
func snapshot() -> AppSetupApplications.Application { AppSetupApplications.Application( pid: pid, bundleID: bundleID, bundleURL: bundleURL, launchDate: launchDate, activationPolicy: policy, isTerminated: { self.terminated }, isHidden: { self.hidden }, hide: { self.hides += 1 if self.applyHide { DispatchQueue.main.async { self.hidden = true } } self.afterHide() return self.acceptHide }, unhide: { self.unhides += 1 if self.unhideIgnoresVisible && !self.hidden { return self.acceptUnhide } if self.applyUnhide { DispatchQueue.main.async { self.hidden = false } } return self.acceptUnhide }) } }}