import AppKit import ParcelCore import ParcelIPC import ParcelUI import Testing @testable import ParcelApp @testable import ParcelTestSupport extension RealAppEndToEndTests { @Test func nineWindowStackKeepsItsRestingGripWithoutACountNotice() async throws { var configuration = E2EConfiguration() configuration.windowCap = 2 try await withSession(configuration: configuration) { session in let (fixture, _) = try await session.addFixture(arguments: [ "--windows", "10", "--title", "Resting Stack Fixture", ]) try await session.startApp() let windows = try await session.settledWindows(for: fixture, count: 10) let workspace = try await session.workspace(for: windows) let stack = try #require(workspace.tiles?.stacks.first { $0.windows.count == 9 }) let single = try #require(workspace.tiles?.stacks.first { $0.windows.count == 1 }) let before = try session.frames(for: fixture) let snapshot = try await fixture.snapshot() let native = try #require(snapshot.windows.first { $0.number == single.visible.rawValue }) _ = try await fixture.control(FixtureControl(action: "focus", window: native.index)) try await expectStackFixtureFocus(single.visible, fixture: fixture, session: session) let selectedIndex = try #require(stack.windows.firstIndex(of: stack.visible)) let alternate = stack.windows[selectedIndex == 0 ? 1 : selectedIndex - 1] try await session.command( "e2e-hover-click", String(stack.visible.rawValue), "grip-tab", String(alternate.rawValue)) try await expectStackFixtureFocus(alternate, fixture: fixture, session: session) let feedback = try JSONDecoder().decode( StackFeedbackSnapshot.self, from: await session.cli(["run", "--json", "feedback", "list"])) #expect(feedback.recent.allSatisfy { $0.kind != .tilesMerged }) #expect(!feedback.key) #expect(try session.frames(for: fixture) == before) print( "Resting-stack frame proof members=\(stack.windows.map(\.rawValue)) visible=\(stack.visible.rawValue) CG=\(try #require(before[stack.visible.rawValue]))" ) #expect(NSWorkspace.shared.frontmostApplication?.processIdentifier == fixture.process.processIdentifier) } } @Test(arguments: [true, false]) func stackArrowsAndGripScrollCycleFixtureFocusWithoutMovingTiles(automaticTiling: Bool) async throws { var configuration = E2EConfiguration() configuration.windowCap = 1 try await withSession(configuration: configuration) { session in let (fixture, _) = try await session.addFixture(arguments: [ "--windows", "3", "--title", "Stack Scroll Fixture", ]) try await session.startApp() let windows = try await session.settledWindows(for: fixture, count: 3) let workspace = try await session.workspace(for: windows) let stack = try #require(workspace.tiles?.stacks.first) #expect(workspace.tiles?.tileCount == 1) #expect(stack.windows.count == 3) let before = try session.frames(for: fixture) let original = stack.visible let index = try #require(stack.windows.firstIndex(of: original)) let next = stack.windows[(index + 1) % 3] try await session.command("e2e-stack-input", "select", String(original.rawValue)) try await expectStackFixtureFocus(original, fixture: fixture, session: session) print("Stack cycle starts at \(original.rawValue); next is \(next.rawValue)") if !automaticTiling { try await session.command("toggle-automatic-tiling") } try await session.command("e2e-stack-input", "scroll-next", String(original.rawValue)) print("Stack scroll dispatched") try await expectStackFixtureFocus(next, fixture: fixture, session: session) #expect(try session.frames(for: fixture) == before) try await session.command("e2e-stack-input", "previous", String(original.rawValue)) print("Stack previous dispatched") try await expectStackFixtureFocus(original, fixture: fixture, session: session) try await session.command("e2e-stack-input", "next", String(original.rawValue)) print("Stack next dispatched") try await expectStackFixtureFocus(next, fixture: fixture, session: session) let after = try await session.workspace(for: session.windows(for: fixture)) #expect(after.tiles?.stacks.map(\.windows) == [stack.windows]) #expect(try session.frames(for: fixture) == before) } } @Test func chipDragMovesAHiddenFixtureMemberAndEscapePreservesBothTiles() async throws { var configuration = E2EConfiguration() configuration.windowCap = 2 try await withSession(configuration: configuration) { session in let (fixture, _) = try await session.addFixture(arguments: [ "--windows", "3", "--title", "Stack Drag Fixture", ]) try await session.startApp() let windows = try await session.settledWindows(for: fixture, count: 3) let workspace = try await session.workspace(for: windows) let tree = try #require(workspace.tiles) let sourceStack = try #require(tree.stacks.first { $0.windows.count == 2 }) let targetStack = try #require(tree.stacks.first { $0.windows.count == 1 }) let source = try #require(sourceStack.windows.first { $0 != sourceStack.visible }) let target = targetStack.visible let before = try session.frames(for: fixture) let destinationFrame = try #require(before[target.rawValue]) let sourceFrame = try #require(before[source.rawValue]) #expect(!framesMatch(sourceFrame, destinationFrame)) let snapshot = try await fixture.snapshot() let active = try #require(snapshot.windows.first { $0.number == sourceStack.visible.rawValue }) _ = try await fixture.control(FixtureControl(action: "focus", window: active.index)) try await expectStackFixtureFocus(sourceStack.visible, fixture: fixture, session: session) let appPID = try #require(session.app).processIdentifier #expect(NSWorkspace.shared.frontmostApplication?.processIdentifier == fixture.process.processIdentifier) try await session.command( "e2e-stack-input", "native-cancel-drag", String(source.rawValue), String(target.rawValue)) try await remainTrue( "Escape keeps chip membership and real fixture frames unchanged", duration: .milliseconds(400) ) { let actual = try await session.workspace(for: session.windows(for: fixture)) let actualFrames = try session.frames(for: fixture) let focus = try await fixture.snapshot() return actual.tiles == tree && stackFramesMatch(actualFrames, before) && focus.focusedWindow == active.index && NSWorkspace.shared.frontmostApplication?.processIdentifier == fixture.process.processIdentifier } #expect(NSWorkspace.shared.frontmostApplication?.processIdentifier != appPID) try await session.command( "e2e-stack-input", "native-drag", String(source.rawValue), String(target.rawValue)) let remaining = sourceStack.windows.filter { $0 != source } let destination = Set(targetStack.windows + [source]) try await waitUntil("dragged chip joins its chosen tile and receives that real frame") { let actualWindows = try await session.windows(for: fixture) let actual = try await session.workspace(for: actualWindows) let frames = try session.frames(for: fixture) guard let stacks = actual.tiles?.stacks, let sourceTile = stacks.first(where: { $0.windows == remaining }), let destinationTile = stacks.first(where: { Set($0.windows) == destination }), let moved = frames[source.rawValue] else { return false } return stacks.count == 2 && destinationTile.visible == source && sourceTile.windows == remaining && framesMatch(moved, destinationFrame) && remaining.allSatisfy { id in frames[id.rawValue].map { framesMatch($0, sourceFrame) } == true } && targetStack.windows.allSatisfy { id in frames[id.rawValue].map { framesMatch($0, destinationFrame) } == true } } try await expectStackFixtureFocus(source, fixture: fixture, session: session) #expect((try await fixture.snapshot()).windows.count == 3) #expect(NSWorkspace.shared.frontmostApplication?.processIdentifier == fixture.process.processIdentifier) #expect(NSWorkspace.shared.frontmostApplication?.processIdentifier != appPID) } } } private struct StackFeedbackSnapshot: Decodable { let recent: [FeedbackNotice] let key: Bool } @MainActor private func expectStackFixtureFocus(_ id: WindowID, fixture: FixtureProcess, session: E2ESession) async throws { try await waitUntil("stack selection \(id.rawValue) reaches the fixture's native focus") { let managed = try await session.windows(for: fixture) let actual = try await fixture.snapshot() let selected = actual.windows.first { $0.number == id.rawValue } return managed.contains { $0.id == id.rawValue && $0.focused } && selected?.index == actual.focusedWindow } } private func stackFramesMatch(_ actual: [UInt32: Rect], _ expected: [UInt32: Rect]) -> Bool { actual.keys == expected.keys && expected.allSatisfy { id, frame in actual[id].map { framesMatch($0, frame) } == true } } @Suite(.serialized) @MainActor struct FixtureStackInputTests { @Test(arguments: [ [], ["unknown"], ["select"], ["select", "0"], ["select", "1"], ["next"], ["next", "0"], ["next", "-1"], ["next", "4294967296"], ["next", "1", "extra"], ["drag", "1"], ["drag", "1", "bad"], ["cancel-drag", "1", "1"], ["scroll-next", "1"], ["native-drag", "1"], ["native-drag", "1", "2"], ["native-cancel-drag", "0", "2"], ["native-cancel-drag", "1", "2"], ["inspect-rest", "1"], ["inspect-rest", "0"], ["inspect-rest", "1", "extra"], ]) func invalidOrUnmanagedInputCannotLaunchOrControlWindows(_ arguments: [String]) async { let controller = StackChipController( focus: { _ in Issue.record("Unexpected focus") }, close: { _ in Issue.record("Unexpected close") }, move: { _, _ in Issue.record("Unexpected move") }) defer { controller.stop() } await #expect(throws: IPCError.self) { try await AppFixtureStackInput.post( arguments: arguments, state: State(), controller: controller, environment: ["PARCEL_E2E": "1"]) } #expect(controller.groups.isEmpty) #expect(controller.dropTargets.isEmpty) } @Test(arguments: [[:], ["PARCEL_E2E": "0"], ["PARCEL_E2E": "true"], ["PARCEL_E2E": "1 "]]) func stackInputRequiresExplicitHarnessMode(_ environment: [String: String]) async { let controller = StackChipController( focus: { _ in Issue.record("Unexpected focus") }, close: { _ in Issue.record("Unexpected close") }, move: { _, _ in Issue.record("Unexpected move") }) defer { controller.stop() } await #expect(throws: IPCError.self) { try await AppFixtureStackInput.post( arguments: ["next", "1"], state: State(), controller: controller, environment: environment) } } }