import XCTest import ApplicationServices import CoreGraphics @testable import EPODWindowBar // WindowFilterTests checks the window filter of WindowEnumerator.buildWindows // (spec section 7.3). The test uses the Teams case from the dump: a real window // shows, and a ghost placeholder with the application name as the title hides. // A window on another Space with a distinct title also shows. final class WindowFilterTests: XCTestCase { private let enumerator = WindowEnumerator() private let pid: pid_t = 1234 private let appName = "Microsoft Teams" private func axWindow(title: String) -> WindowEnumerator.AXRawWindow { // The element is a placeholder. buildWindows does not read the element. let element = AXUIElementCreateApplication(ProcessInfo.processInfo.processIdentifier) return WindowEnumerator.AXRawWindow(element: element, title: title, isMinimized: false, subrole: kAXStandardWindowSubrole as String, isFullScreen: false) } private func cgWindow(id: CGWindowID, title: String?, bounds: CGRect) -> WindowEnumerator.CGRawWindow { return WindowEnumerator.CGRawWindow(windowID: id, title: title, layer: 0, bounds: bounds) } private func shownIDs(ax: [WindowEnumerator.AXRawWindow], axIDs: [CGWindowID?], cg: [WindowEnumerator.CGRawWindow], screenBoundsCG: CGRect = CGRect(x: 0, y: 0, width: 2560, height: 1440)) -> Set { let result = enumerator.buildWindows(pid: pid, bundleID: "com.microsoft.teams2", appName: appName, ax: ax, axWindowIDs: axIDs, cg: cg, spaceIDsByWindow: [:], currentSpaceIDs: [], ownPID: 9999, screenBoundsCG: screenBoundsCG, now: Date()) return Set(result.windows.map { $0.id }) } func testShowsRealAXWindow() { let real = axWindow(title: "Chat | Team") let cg = [cgWindow(id: 7476, title: "Chat | Team", bounds: CGRect(x: 215, y: 249, width: 1307, height: 865))] let shown = shownIDs(ax: [real], axIDs: [7476], cg: cg) XCTAssertTrue(shown.contains(7476)) } func testHidesGhostWithAppNameTitle() { // Ghost: no AX element, title equals the application name. let cg = [cgWindow(id: 8274, title: "Microsoft Teams", bounds: CGRect(x: 0, y: 0, width: 800, height: 600))] let shown = shownIDs(ax: [], axIDs: [], cg: cg) XCTAssertFalse(shown.contains(8274)) } func testHidesEmptyTitleGhost() { // No AX element, empty CG title. The title falls back to the app name. let cg = [cgWindow(id: 7475, title: nil, bounds: CGRect(x: 0, y: 0, width: 2560, height: 300))] let shown = shownIDs(ax: [], axIDs: [], cg: cg) XCTAssertFalse(shown.contains(7475)) } func testShowsOtherSpaceWindowWithDistinctTitle() { // No AX element, distinct title: a real window on another Space. let cg = [cgWindow(id: 999, title: "Terminal — ~/project", bounds: CGRect(x: 0, y: 0, width: 1268, height: 1319))] let shown = shownIDs(ax: [], axIDs: [], cg: cg) XCTAssertTrue(shown.contains(999)) } func testHidesOffScreenLimboWindow() { // The Eclipse limbo shell sits at y=10000, off every screen. let cg = [cgWindow(id: 9231, title: "PartRenderingEngine's limbo", bounds: CGRect(x: 0, y: 10000, width: 1600, height: 900))] let shown = shownIDs(ax: [], axIDs: [], cg: cg) XCTAssertFalse(shown.contains(9231)) } func testKeepsMinimizedOffScreenWindow() { // A minimized window keeps its tab, even with off-screen bounds. let element = AXUIElementCreateApplication(ProcessInfo.processInfo.processIdentifier) let ax = [WindowEnumerator.AXRawWindow(element: element, title: "Minimized Chat", isMinimized: true, subrole: kAXStandardWindowSubrole as String, isFullScreen: false)] let cg = [cgWindow(id: 555, title: "Minimized Chat", bounds: CGRect(x: 0, y: 10000, width: 1200, height: 800))] let shown = shownIDs(ax: ax, axIDs: [555], cg: cg) XCTAssertTrue(shown.contains(555)) } }