import XCTest @testable import EPODWindowBar // WindowOrderTests checks the stable order of the WindowStore (decision D5). // The store sorts windows by firstSeen. A known window keeps its firstSeen // value on a refresh. A new window gets the firstSeen value "now". A window // that closes and opens again gets a new firstSeen value. @MainActor final class WindowOrderTests: XCTestCase { private let pid: pid_t = 42 private func makeWindow(id: CGWindowID, title: String) -> WindowInfo { return WindowInfo( id: id, pid: pid, bundleID: "com.test.app", title: title, spaceIDs: [], isOnCurrentSpace: true, isMinimized: false, isFullScreen: false, firstSeen: Date(timeIntervalSince1970: 0)) } func testEqualFirstSeenSortsByID() { let store = WindowStore() let t0 = Date(timeIntervalSince1970: 100) store.replaceWindows([makeWindow(id: 2, title: "B"), makeWindow(id: 1, title: "A")], for: pid, now: t0) let order = store.windows(for: pid).map { $0.id } XCTAssertEqual(order, [1, 2]) } func testNewWindowKeepsStableOrder() { let store = WindowStore() let t0 = Date(timeIntervalSince1970: 100) store.replaceWindows([makeWindow(id: 1, title: "A"), makeWindow(id: 2, title: "B")], for: pid, now: t0) // A later refresh adds window 3. The order of 1 and 2 does not change. let t1 = Date(timeIntervalSince1970: 200) store.replaceWindows([makeWindow(id: 2, title: "B"), makeWindow(id: 1, title: "A"), makeWindow(id: 3, title: "C")], for: pid, now: t1) let order = store.windows(for: pid).map { $0.id } XCTAssertEqual(order, [1, 2, 3]) } func testReopenGetsNewFirstSeen() { let store = WindowStore() let t0 = Date(timeIntervalSince1970: 100) store.replaceWindows([makeWindow(id: 1, title: "A"), makeWindow(id: 2, title: "B")], for: pid, now: t0) // Window 1 closes. let t1 = Date(timeIntervalSince1970: 200) store.replaceWindows([makeWindow(id: 2, title: "B")], for: pid, now: t1) // Window 1 opens again. Window 1 gets a new firstSeen value. Window 1 // moves after window 2. let t2 = Date(timeIntervalSince1970: 300) store.replaceWindows([makeWindow(id: 2, title: "B"), makeWindow(id: 1, title: "A")], for: pid, now: t2) let order = store.windows(for: pid).map { $0.id } XCTAssertEqual(order, [2, 1]) } func testFrontmostWindows() { let store = WindowStore() store.replaceWindows([makeWindow(id: 1, title: "A")], for: pid, now: Date(timeIntervalSince1970: 100)) store.setFrontmost(pid) XCTAssertEqual(store.frontmostWindows.map { $0.id }, [1]) } }