import ParcelCore import ParcelEngine import Testing @Suite("Engine observations") @MainActor struct ParcelEngineTests { @Test func newWindowsProduceOneLayoutAndOnlyChangedWrites() async throws { let harness = try EngineHarness() try await harness.start() let passes = harness.engine.layoutPassCount try harness.add([1, 2]) let first = WindowID(rawValue: 1) let second = WindowID(rawValue: 2) #expect(harness.engine.layoutPassCount == passes + 1) #expect(harness.engine.snapshot.state.frames[first] == Rect(x: 0, y: 0, width: 600, height: 800)) #expect(harness.engine.snapshot.state.frames[second] == Rect(x: 600, y: 0, width: 600, height: 800)) #expect(harness.world.writes.count == 1) #expect(harness.world.allWrites.map(\.window) == [first, second]) #expect(harness.world.frames == harness.engine.snapshot.state.frames) #expect(harness.engine.snapshot.focusedWindow == second) #expect(harness.world.focused == [second]) #expect(harness.announcer.announcements == [.focused(second)]) #expect(harness.engine.isIdle) let writeCount = harness.world.writes.count harness.world.emit(.observed(.focusChanged(second))) try harness.scheduler.runReady() #expect(harness.world.writes.count == writeCount) #expect(harness.engine.snapshot.tiles.map(\.window) == [first, second]) #expect(harness.engine.snapshot.layouts[engineDisplay] == .columns) await harness.engine.stop() } @Test func closingFocusedWindowExpandsSurvivorAndRestoresFocus() async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1, 2]) let first = WindowID(rawValue: 1) let second = WindowID(rawValue: 2) let previousWrites = harness.world.allWrites.count harness.world.disappear(second) try harness.scheduler.runReady() try harness.finishMotion() #expect(harness.engine.snapshot.state.windows[second] == nil) #expect(harness.engine.snapshot.state.frames[second] == nil) #expect(harness.engine.snapshot.state.observedFrames[second] == nil) #expect(harness.engine.snapshot.state.workspaces[engineWorkspace]?.order == [first]) #expect(harness.engine.snapshot.focusedWindow == first) #expect(harness.world.focused.last == first) #expect(harness.world.frames[first] == engineScreen) #expect(harness.world.allWrites.dropFirst(previousWrites).allSatisfy { $0.window == first }) #expect(harness.engine.isIdle) await harness.engine.stop() } @Test(arguments: [false, true]) func tiledUserDragSnapsWithoutRearranging(resized: Bool) async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1, 2, 3]) let first = WindowID(rawValue: 1) let before = harness.engine.snapshot.state let dragged = Rect(x: 760, y: 140, width: 250, height: 350) harness.world.userFrame(first, frame: dragged, resized: resized) try harness.scheduler.runReady() try harness.finishMotion() #expect( harness.engine.snapshot.state.workspaces[engineWorkspace]?.tree == before.workspaces[engineWorkspace]?.tree) #expect( harness.engine.snapshot.state.workspaces[engineWorkspace]?.order == before.workspaces[engineWorkspace]?.order) #expect(harness.engine.snapshot.state.frames == before.frames) #expect(harness.engine.snapshot.state.observedFrames[first] == dragged) #expect(harness.world.frames[first] == before.frames[first]) #expect(harness.engine.isIdle) await harness.engine.stop() } @Test(arguments: [false, true]) func floatingUserFrameIsPreservedExactly(resized: Bool) async throws { let harness = try EngineHarness() try await harness.start() let floating = engineWindow(7, floating: true) harness.world.appear(floating) try harness.scheduler.runReady() let writes = harness.world.writes.count let exact = Rect(x: -35.25, y: 99.75, width: 1350.5, height: 901.125) harness.world.userFrame(floating.id, frame: exact, resized: resized) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.frames[floating.id] == exact) #expect(harness.engine.snapshot.state.observedFrames[floating.id] == exact) #expect(harness.world.frames[floating.id] == exact) #expect(harness.world.writes.count == writes) #expect(harness.engine.snapshot.state.workspaces[engineWorkspace]?.tree == nil) #expect(harness.engine.isIdle) await harness.engine.stop() } @Test func oneHundredEventsHaveOneLayoutPass() async throws { let harness = try EngineHarness() try await harness.start() let passes = harness.engine.layoutPassCount let revision = harness.engine.snapshot.revision let scheduled = harness.scheduler.scheduledCount for id in UInt32(1)...100 { harness.world.appear(engineWindow(id)) } #expect(harness.scheduler.pendingCount == 1) #expect(harness.scheduler.scheduledCount == scheduled + 1) try harness.scheduler.runReady() #expect(harness.engine.layoutPassCount == passes + 1) #expect(harness.engine.snapshot.revision == revision + 1) #expect(harness.engine.snapshot.state.windows.count == 100) #expect(harness.engine.snapshot.state.frames.count == 100) #expect(harness.world.focused == [WindowID(rawValue: 100)]) #expect(harness.world.writes.count == 1) #expect(harness.world.allWrites.count == 100) #expect(harness.engine.isIdle) await harness.engine.stop() } @Test func repeatedObservationKeepsExistingFocus() async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1, 2]) let first = WindowID(rawValue: 1) harness.world.emit(.observed(.focusChanged(first))) try harness.scheduler.runReady() let before = harness.engine.snapshot.state.workspaces[engineWorkspace] harness.world.appear(engineWindow(2), frame: try #require(harness.world.frames[WindowID(rawValue: 2)])) try harness.scheduler.runReady() #expect(harness.engine.snapshot.focusedWindow == first) #expect(harness.engine.snapshot.state.workspaces[engineWorkspace] == before) await harness.engine.stop() } @Test func unknownWindowAndInvalidGeometryAreSurfaced() async throws { let harness = try EngineHarness() try await harness.start() let unknown = WindowID(rawValue: 999) harness.engine.send(.windowMovedByUser(unknown, frame: initialEngineFrame)) harness.world.appear(engineWindow(1), frame: Rect(x: .nan, y: 0, width: 10, height: 10)) try harness.scheduler.runReady() #expect(harness.log.reports == [.unknownWindow(unknown), .invalidGeometry]) #expect(harness.engine.snapshot.state.windows.isEmpty) #expect(harness.world.writes.isEmpty) await harness.engine.stop() } } @MainActor final class EngineCommandBatch { private(set) var started = 0 private(set) var tasks: [Task] = [] init(_ commands: [Command], engine: ParcelEngine) { for command in commands { tasks.append( Task { @MainActor in self.started += 1 return try await engine.perform(command) }) } } func waitUntilEnqueued() async throws { for _ in 0..<100 where started < tasks.count { await Task.yield() } try #require(started == tasks.count, "Command tasks did not enter perform") } func results() async throws -> [EngineCommandResult] { var results: [EngineCommandResult] = [] for task in tasks { results.append(try await task.value) } return results } } @Suite("Engine commands") @MainActor struct ParcelEngineCommandTests { @Test func asyncCommandsCoalesceWithoutCreditingPriorLayoutChangesToRetile() async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1, 2]) let passes = harness.engine.layoutPassCount let beforeRevision = harness.engine.snapshot.revision let batch = EngineCommandBatch([.setLayout(.rows), .focus(.right), .retile], engine: harness.engine) try await batch.waitUntilEnqueued() #expect(harness.engine.layoutPassCount == passes) #expect(harness.scheduler.pendingCount == 1) try harness.scheduler.runReady() try harness.finishMotion() let results = try await batch.results() #expect(harness.engine.layoutPassCount == passes + 1) #expect(results.allSatisfy { $0.revision == beforeRevision + 1 }) #expect(results[0].isAccepted) #expect(results[0].reports.isEmpty) #expect(!results[1].isAccepted) #expect(results[1].reports == [.noNeighbor(.right)]) #expect(results[2].isAccepted) #expect(results[2].reports == [.retiled(moved: 0)]) #expect(harness.engine.snapshot.layouts[engineDisplay] == .rows) #expect( harness.engine.snapshot.state.frames[WindowID(rawValue: 1)] == Rect(x: 0, y: 0, width: 1200, height: 400)) #expect( harness.engine.snapshot.state.frames[WindowID(rawValue: 2)] == Rect(x: 0, y: 400, width: 1200, height: 400)) #expect(harness.engine.isIdle) await harness.engine.stop() } @Test func acceptedCommandReturnsBeforeDeferredWindowWrite() async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1, 2]) try harness.scheduler.advance(by: 0.01) harness.world.deferWrites = true let batch = EngineCommandBatch([.setLayout(.rows)], engine: harness.engine) try await batch.waitUntilEnqueued() try harness.scheduler.runReady() let result = try #require(try await batch.results().first) #expect(result.isAccepted) #expect(result.revision == harness.engine.snapshot.revision) #expect(harness.world.pendingWriteCount == 1) #expect(!harness.engine.isIdle) try harness.world.completeNextWrite() #expect(harness.engine.isIdle) await harness.engine.stop() } @Test func commandsWithoutWorkspaceReturnFailureReports() async throws { let harness = try EngineHarness(initialState: State()) try await harness.start() let batch = EngineCommandBatch([.retile, .toggleFloat], engine: harness.engine) try await batch.waitUntilEnqueued() try harness.scheduler.runReady() let results = try await batch.results() #expect(results.map(\.isAccepted) == [false, false]) #expect(results.map(\.reports) == [[.noActiveWorkspace], [.noFocusedWindow]]) #expect(harness.log.reports == [.noActiveWorkspace, .noFocusedWindow]) await harness.engine.stop() } }