import Foundation import ParcelConfig import ParcelCore import ParcelIPC import Testing @testable import ParcelEngine @MainActor struct EngineCommandReviewRegressionTests { @Test(arguments: [false, true]) func overflowRefreshAndRulesReloadPreserveFrameAndReason(roomAvailable: Bool) async throws { var configuration = engineConfiguration() configuration.layout.global.whenFull = .float let harness = try EngineHarness(configuration: configuration) try await harness.start() try harness.add([1, 2, 3, 4]) let id = WindowID(rawValue: 4) #expect(harness.engine.snapshot.state.windows[id]?.managementReason == .whenFull) #expect(harness.engine.classifications[id]?.reason == .whenFull) let moved = Rect(x: 80, y: 120, width: 300, height: 200) harness.world.userFrame(id, frame: moved) try harness.scheduler.runReady() if roomAvailable { harness.world.disappear(WindowID(rawValue: 1)) try harness.scheduler.runReady() } let notices = harness.announcer.announcements.count let fullReports = harness.log.reports.filter { $0 == .spaceFull(id) }.count let writes = harness.world.writes(for: id).count harness.world.appear(engineWindow(4, title: "New title"), frame: moved) try harness.scheduler.runReady() configuration.rules = [RuleConfiguration(titleContains: "other", action: .float)] try harness.engine.applyConfiguration(configuration) try harness.scheduler.runReady() let window = try #require(harness.engine.snapshot.state.windows[id]) #expect(window.isFloating) #expect(window.managementReason == .whenFull) #expect(harness.engine.snapshot.state.frames[id] == moved) #expect(harness.engine.classifications[id]?.reason == .whenFull) #expect(harness.engine.manualFloating[id] == nil) #expect(harness.world.writes(for: id).count == writes) #expect(harness.announcer.announcements.count == notices) #expect(harness.log.reports.filter { $0 == .spaceFull(id) }.count == fullReports) harness.engine.classifications[id] = nil let handler = EngineIPCHandler(engine: harness.engine) guard case .object(let why) = try await handler.why(window: id.rawValue) else { throw HarnessError.missingAction } #expect(why["reason"] == .string("Floating because the workspace is full.")) configuration.rules = [RuleConfiguration(titleContains: "New title", action: .tile)] if roomAvailable { try harness.engine.applyConfiguration(configuration) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.windows[id]?.isFloating == false) #expect(harness.engine.snapshot.state.windows[id]?.managementReason == .rule) } await harness.engine.stop() } @Test func unrelatedLayoutEditsPreserveRuntimeCapRatioAndDividersOnEverySpace() async throws { let harness = try EngineHarness(configuration: engineConfiguration(layout: .side)) try await harness.start() try harness.add([1, 2, 3]) harness.engine.send(.commandIssued(.setWindowCap(2))) harness.engine.send(.commandIssued(.setRatio(.twoThirds))) try harness.scheduler.runReady() let edge = try #require(harness.engine.snapshot.state.tileEdges.first) harness.engine.send(.tileEdgeResized(edge, delta: 20)) try harness.scheduler.runReady() let secondSpace = SpaceID(rawValue: 2) harness.engine.send(.activeSpaceChanged(display: engineDisplay, space: secondSpace)) try harness.scheduler.runReady() harness.engine.send(.commandIssued(.setWindowCap(5))) harness.engine.send(.commandIssued(.setRatio(.threeFifths))) try harness.scheduler.runReady() let previous = harness.engine.snapshot.state.workspaces var config = harness.engine.configuration config.layout.global.gaps.inner = 10 config.layout.global.reserved.top = 20 config.layout.global.whenFull = .float try harness.engine.applyConfiguration(config) try harness.scheduler.runReady() for (key, workspace) in previous { let updated = try #require(harness.engine.snapshot.state.workspaces[key]) #expect(updated.windowCap == workspace.windowCap) #expect(updated.ratio == workspace.ratio) #expect(updated.tree == workspace.tree) #expect(updated.configuredRatio == .half) #expect(updated.gaps.inner == 10) #expect(updated.reservedEdges.top == 20) #expect(updated.whenFull == .float) } await harness.engine.stop() } @Test(arguments: [false, true]) func configAppliesOnlyTheChangedCapOrRatio(changeCap: Bool) async throws { let harness = try EngineHarness(configuration: engineConfiguration(layout: .side)) try await harness.start() try harness.add([1, 2]) harness.engine.send(.commandIssued(.setWindowCap(5))) harness.engine.send(.commandIssued(.setRatio(.twoThirds))) try harness.scheduler.runReady() let before = try #require(harness.engine.snapshot.state.workspaces[engineWorkspace]) var config = harness.engine.configuration if changeCap { config.layout.global.windowCap = 4 } else { config.layout.global.ratio = 0.6 } try harness.engine.applyConfiguration(config) try harness.scheduler.runReady() let after = try #require(harness.engine.snapshot.state.workspaces[engineWorkspace]) #expect(after.windowCap == (changeCap ? 4 : 5)) #expect(after.ratio == (changeCap ? before.ratio : .threeFifths)) #expect(after.configuredRatio == (changeCap ? .half : .threeFifths)) harness.engine.send(.commandIssued(.resetSizes)) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.workspaces[engineWorkspace]?.ratio == after.configuredRatio) await harness.engine.stop() } @Test func displaySpecificChangesLeaveOtherRuntimeOverridesAloneAndNewSpacesUseConfig() async throws { let harness = try EngineHarness(initialState: engineInitialState(twoDisplays: true)) try await harness.start() harness.engine.send(.commandIssued(.setWindowCap(5))) harness.engine.send(.commandIssued(.setRatio(.twoThirds))) try harness.scheduler.runReady() let before = harness.engine.snapshot.state.workspaces[engineWorkspace] var config = harness.engine.configuration config.layout.displays[try #require(UUID(uuidString: secondEngineDisplay.rawValue))] = LayoutValues( defaultLayout: .side, ratio: 0.6, windowCap: 4) try harness.engine.applyConfiguration(config) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.workspaces[engineWorkspace] == before) let key = WorkspaceKey(display: secondEngineDisplay, space: SpaceID(rawValue: 2)) #expect(harness.engine.snapshot.state.workspaces[key]?.windowCap == 4) #expect(harness.engine.snapshot.state.workspaces[key]?.ratio == .threeFifths) harness.engine.send(.activeSpaceChanged(display: engineDisplay, space: SpaceID(rawValue: 9))) try harness.scheduler.runReady() let newKey = WorkspaceKey(display: engineDisplay, space: SpaceID(rawValue: 9)) #expect(harness.engine.snapshot.state.workspaces[newKey]?.windowCap == 3) #expect(harness.engine.snapshot.state.workspaces[newKey]?.ratio == .half) await harness.engine.stop() } @Test(arguments: [0, 1, 100]) func ipcWindowCapAcceptsConfigBounds(cap: Int) async throws { let harness = try EngineHarness() try await harness.start() let handler = EngineIPCHandler(engine: harness.engine) let action = Task { @MainActor in try await handler.run(command: "set-window-cap", args: [String(cap)]) } for _ in 0..<100 where harness.scheduler.readyCount == 0 { await Task.yield() } try #require(harness.scheduler.readyCount > 0, "IPC action did not enter the engine queue") try harness.scheduler.runReady() guard case .object(let result) = try await action.value else { throw HarnessError.missingAction } #expect(result["accepted"] == .bool(true)) #expect(harness.engine.snapshot.state.workspaces[engineWorkspace]?.windowCap == (cap == 0 ? Int.max : cap)) #expect(ConfigValidator.validate(try TOMLDocument(parsing: "[layout]\nwindow-cap=\(cap)")).configuration != nil) await harness.engine.stop() } }