import ParcelConfig import ParcelCore import ParcelEngine import Testing @MainActor @Test func unknownModesAreRejectedAtEveryEngineEntryPoint() async throws { var restored = engineInitialState() restored.activeMode = "missing" #expect(throws: EngineError.self) { try EngineHarness(initialState: restored) } let harness = try EngineHarness() try await harness.start() await #expect(throws: EngineError.self) { try await harness.engine.perform(.enterMode("missing")) } harness.engine.send(.commandIssued(.enterMode("missing"))) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.activeMode == nil) #expect(harness.announcer.announcements.isEmpty) #expect( harness.log.diagnostics.contains { if case .configuration(let message) = $0 { return message.contains("missing") } return false }) } @MainActor @Test func modeChangesAreAnnouncedAndConfigurationRemovalReturnsToDefault() async throws { var configuration = engineConfiguration() configuration.modes = ["resize": [:]] let harness = try EngineHarness(configuration: configuration) try await harness.start() harness.engine.send(.commandIssued(.enterMode("resize"))) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.activeMode == "resize") #expect(harness.announcer.announcements == [.modeChanged("resize")]) guard case .object(let query) = try await EngineIPCHandler(engine: harness.engine).query(.config) else { Issue.record("Configuration query must return an object") return } #expect(query["active_mode"] == .string("resize")) try harness.engine.applyConfiguration(engineConfiguration()) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.activeMode == nil) #expect(harness.announcer.announcements == [.modeChanged("resize"), .modeChanged(nil)]) } @MainActor @Test func engineRejectsMalformedModeConfigurationsAtomically() throws { var invalid = engineConfiguration() invalid.modes = ["resize": [.enterMode("missing"): try KeyChord(parsing: "r")]] #expect(throws: EngineError.self) { try EngineHarness(configuration: invalid) } let harness = try EngineHarness() let snapshot = harness.engine.snapshot let configuration = harness.engine.configuration #expect(throws: EngineError.self) { try harness.engine.applyConfiguration(invalid) } #expect(harness.engine.configuration == configuration) #expect(harness.engine.snapshot == snapshot) } @MainActor @Test func engineKeepsExistingWorkspaceLayoutAndAppliesDefaultsToNewWorkspaces() throws { var restored = engineInitialState() restored.workspaces[engineWorkspace] = Workspace(key: engineWorkspace, layout: .tree) let configured = engineConfiguration(layout: .rows) let existing = try EngineHarness(configuration: configured, initialState: restored) #expect(existing.engine.snapshot.state.workspaces[engineWorkspace]?.layout == .tree) let fresh = try EngineHarness(configuration: configured) #expect(fresh.engine.snapshot.state.workspaces[engineWorkspace]?.layout == .rows) } @MainActor @Test func modeReloadReconcilesQueuedEntriesInOrder() async throws { var configuration = engineConfiguration() configuration.modes = ["old": [:], "new": [:]] let harness = try EngineHarness(configuration: configuration) try await harness.start() harness.engine.send(.commandIssued(.enterMode("old"))) var removed = configuration removed.modes = [:] try harness.engine.applyConfiguration(removed) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.activeMode == nil) try harness.engine.applyConfiguration(configuration) harness.engine.send(.commandIssued(.enterMode("old"))) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.activeMode == "old") harness.engine.send(.commandIssued(.enterMode("new"))) removed.modes = ["new": [:]] try harness.engine.applyConfiguration(removed) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.activeMode == "new") } @MainActor @Test func restoredAndReconfiguredLayoutsKeepTheirTopology() throws { let ids = (1...4).map { WindowID(rawValue: UInt32($0)) } var restored = engineInitialState() restored.workspaces[engineWorkspace] = Workspace(key: engineWorkspace, layout: .columns, order: ids) let harness = try EngineHarness(configuration: engineConfiguration(layout: .grid), initialState: restored) let initial = try #require(harness.engine.snapshot.state.workspaces[engineWorkspace]) #expect(initial.layout == .columns) #expect(LayoutEngine.arrange(initial, in: engineScreen).frames[ids[0]]?.size.height == 800) try harness.engine.applyConfiguration(engineConfiguration(layout: .accordion)) let accordion = try #require(harness.engine.snapshot.state.workspaces[engineWorkspace]) #expect(accordion.layout == .accordion) let frames = LayoutEngine.arrange(accordion, in: engineScreen).frames for stack in try #require(accordion.tree?.stacks) { for window in stack.windows { let frame = try #require(frames[window]) #expect(frame.minY == 0) #expect(frame.size.height == 800) } } }