Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213import ParcelConfigimport ParcelCoreimport Testing
@testable import ParcelEngine
@MainActorstruct EngineCallbackTests { private var allCallbacks: CallbackConfiguration { CallbackConfiguration( commands: Dictionary(uniqueKeysWithValues: CallbackEvent.allCases.map { ($0, ["/bin/true"]) })) }
private var disabledCallbacks: CallbackConfiguration { CallbackConfiguration(commands: Dictionary(uniqueKeysWithValues: CallbackEvent.allCases.map { ($0, []) })) }
@Test func unchangedStateAndDisabledCallbacksProduceNothing() { let state = engineInitialState() #expect(EngineCallbackRouter.invocations(previous: state, current: state, configuration: allCallbacks).isEmpty) let (opened, _) = reduce( state, .windowAppeared(engineWindow(1), workspace: engineWorkspace, frame: initialEngineFrame)) for configuration in [CallbackConfiguration(), disabledCallbacks] { #expect( EngineCallbackRouter.invocations(previous: state, current: opened, configuration: configuration) .isEmpty) } }
@Test func openingAndClosingCarryWindowAndWorkspaceContext() throws { let before = engineInitialState() let (opened, _) = reduce( before, .windowAppeared( engineWindow(1, title: "Document One"), workspace: engineWorkspace, frame: initialEngineFrame)) let opening = EngineCallbackRouter.invocations(previous: before, current: opened, configuration: allCallbacks) #expect(opening.map(\.event) == [.windowOpened, .focusChanged]) let invocation = try #require(opening.first) #expect(invocation.arguments == ["/bin/true"]) #expect( invocation.environment == [ "PARCEL_EVENT": CallbackEvent.windowOpened.rawValue, "PARCEL_WINDOW_ID": "1", "PARCEL_APP_PID": "100", "PARCEL_BUNDLE_ID": "test.fixture", "PARCEL_WINDOW_TITLE": "Document One", "PARCEL_DISPLAY_ID": engineDisplay.rawValue, "PARCEL_SPACE_ID": "1", "PARCEL_LAYOUT": "grid", ]) let (closed, _) = reduce(opened, .windowDisappeared(WindowID(rawValue: 1))) let closing = EngineCallbackRouter.invocations(previous: opened, current: closed, configuration: allCallbacks) #expect(closing.map(\.event) == [.windowClosed, .focusChanged]) #expect(closing[0].environment["PARCEL_WINDOW_TITLE"] == "Document One") #expect(closing[1].environment["PARCEL_WINDOW_ID"] == nil) }
@Test func layoutAndSpaceCallbacksOnlyFireForExistingChangedWorkspaces() { let before = engineInitialState() var changed = before changed.workspaces[engineWorkspace]?.changeLayout(.side) var calls = EngineCallbackRouter.invocations(previous: before, current: changed, configuration: allCallbacks) #expect(calls.map(\.event) == [.layoutChanged]) #expect(calls.first?.environment["PARCEL_LAYOUT"] == "side") let second = WorkspaceKey(display: engineDisplay, space: SpaceID(rawValue: 2)) changed.activeSpaces[engineDisplay] = second.space changed.workspaces[second] = Workspace(key: second, layout: .rows) calls = EngineCallbackRouter.invocations(previous: before, current: changed, configuration: allCallbacks) #expect(calls.map(\.event) == [.layoutChanged, .spaceChanged]) #expect(calls.last?.environment["PARCEL_SPACE_ID"] == "2") #expect(calls.last?.environment["PARCEL_LAYOUT"] == "rows") }
@Test func changedWindowFactsDoNotLookLikeANewWindow() { let before = engineInitialState() let (opened, _) = reduce( before, .windowAppeared(engineWindow(1), workspace: engineWorkspace, frame: initialEngineFrame)) var updated = opened updated.windows[WindowID(rawValue: 1)]?.title = "Renamed" #expect( EngineCallbackRouter.invocations(previous: opened, current: updated, configuration: allCallbacks).isEmpty) }
@Test func runningEngineRoutesEventsButNotStartupReloadOrNoOp() async throws { var config = engineConfiguration() config.callbacks = allCallbacks let harness = try EngineHarness(configuration: config) var calls: [EngineCallbackInvocation] = [] harness.engine.onCallback = { calls.append($0) } try await harness.start() #expect(calls.isEmpty) try harness.add([1]) #expect(calls.map(\.event) == [.windowOpened, .focusChanged]) calls.removeAll() harness.engine.send(.commandIssued(.retile)) try harness.scheduler.runReady() #expect(calls.isEmpty) config.layout.global.defaultLayout = .rows try harness.engine.applyConfiguration(config) try harness.scheduler.runReady() #expect(calls.isEmpty) harness.engine.send(.commandIssued(.setLayout(.side))) try harness.scheduler.runReady() #expect(calls.map(\.event) == [.layoutChanged]) calls.removeAll() await harness.engine.stop() #expect(calls.isEmpty) }
@Test func reloadOnlySuppressesSyntheticEventsWhenRealEventsShareTheTurn() async throws { var config = engineConfiguration() config.callbacks = allCallbacks let harness = try EngineHarness(configuration: config) var calls: [EngineCallbackInvocation] = [] harness.engine.onCallback = { calls.append($0) } try await harness.start() try harness.add([1]) calls.removeAll() harness.world.appear(engineWindow(2)) config.rules = [RuleConfiguration(bundleID: engineApp.bundleID, layout: .rows)] try harness.engine.applyConfiguration(config) harness.world.emit(.observed(.windowDisappeared(WindowID(rawValue: 1)))) harness.engine.send(.commandIssued(.setLayout(.side))) try harness.scheduler.runReady() #expect(calls.filter { $0.event == .windowOpened }.map { $0.environment["PARCEL_WINDOW_ID"] } == ["2"]) #expect(calls.filter { $0.event == .windowClosed }.map { $0.environment["PARCEL_WINDOW_ID"] } == ["1"]) #expect(calls.filter { $0.event == .layoutChanged }.map { $0.environment["PARCEL_LAYOUT"] } == ["side"]) await harness.engine.stop() }
@Test func reloadReclassifiesManagedWindowsWithoutSyntheticOpenCloseFocusOrLayoutCallbacks() async throws { var config = engineConfiguration() config.callbacks = allCallbacks let harness = try EngineHarness(configuration: config) var calls: [EngineCallbackInvocation] = [] harness.engine.onCallback = { calls.append($0) } try await harness.start() harness.world.appear(engineWindow(1, title: "Hide on reload")) harness.world.appear(engineWindow(2, title: "Keep managed")) harness.world.emit(.observed(.focusChanged(WindowID(rawValue: 1)))) try harness.scheduler.runReady() calls.removeAll()
config.rules = [RuleConfiguration(titleContains: "Hide on reload", action: .ignore)] config.layout.global.defaultLayout = .rows try harness.engine.applyConfiguration(config) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.windows[WindowID(rawValue: 1)] == nil) #expect(harness.engine.snapshot.state.windows[WindowID(rawValue: 2)] != nil) #expect(harness.engine.snapshot.focusedWindow != WindowID(rawValue: 1)) #expect(harness.engine.snapshot.state.workspaces[engineWorkspace]?.layout == .rows) #expect(calls.isEmpty)
config.rules = [] try harness.engine.applyConfiguration(config) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.windows[WindowID(rawValue: 1)] != nil) #expect(calls.isEmpty) harness.world.disappear(WindowID(rawValue: 1)) try harness.scheduler.runReady() #expect(calls.filter { $0.event == .windowClosed }.map { $0.environment["PARCEL_WINDOW_ID"] } == ["1"]) await harness.engine.stop() }
@Test(arguments: [false, true]) func enablingCallbacksAfterDisabledBatchesReportsOnlyNewChanges(explicitEmptyArrays: Bool) async throws { var config = engineConfiguration() config.callbacks = explicitEmptyArrays ? disabledCallbacks : CallbackConfiguration() let harness = try EngineHarness(configuration: config) var calls: [EngineCallbackInvocation] = [] harness.engine.onCallback = { calls.append($0) } try await harness.start() try harness.add([1]) #expect(calls.isEmpty)
config.callbacks = CallbackConfiguration(commands: [.windowOpened: ["/bin/true"], .focusChanged: []]) try harness.engine.applyConfiguration(config) try harness.scheduler.runReady() #expect(calls.isEmpty) try harness.add([2]) #expect(calls.map(\.event) == [.windowOpened]) #expect(calls.first?.environment["PARCEL_WINDOW_ID"] == "2") #expect(calls.first?.arguments == ["/bin/true"]) calls.removeAll()
config.callbacks = disabledCallbacks try harness.engine.applyConfiguration(config) harness.world.disappear(WindowID(rawValue: 2)) try harness.scheduler.runReady() #expect(calls.isEmpty) await harness.engine.stop() }
@Test func invalidReloadKeepsCurrentCallbacksAndDoesNotSuppressQueuedRealEvents() async throws { var config = engineConfiguration() config.callbacks = allCallbacks let harness = try EngineHarness(configuration: config) var calls: [EngineCallbackInvocation] = [] harness.engine.onCallback = { calls.append($0) } try await harness.start() harness.world.appear(engineWindow(1)) config.callbacks = CallbackConfiguration(commands: [.windowOpened: ["relative"]]) #expect(throws: EngineError.self) { try harness.engine.applyConfiguration(config) } try harness.scheduler.runReady() #expect(calls.map(\.event) == [.windowOpened, .focusChanged]) #expect(calls.first?.arguments == ["/bin/true"]) #expect(calls.first?.environment["PARCEL_WINDOW_ID"] == "1") await harness.engine.stop() }
@Test func invalidProgrammaticCallbackFailsEngineInitialization() { var config = engineConfiguration() config.callbacks = CallbackConfiguration(commands: [.windowOpened: ["relative"]]) #expect(throws: EngineError.self) { try EngineHarness(configuration: config) } }}