import Foundation import RealityDriverSupport import Testing struct DriverWindowSelectionTests { @Test(arguments: [0, 1, 2]) func auxiliaryWindowsDoNotCreateAStandardWindowAmbiguity(index: Int) throws { var candidates = [ DriverWindowRole(windowID: 2, role: "AXHelpTag", subrole: "AXUnknown"), DriverWindowRole(windowID: 3, role: "AXWindow", subrole: "AXFloatingWindow"), ] candidates.insert(DriverWindowRole(windowID: 1, role: "AXWindow", subrole: "AXStandardWindow"), at: index) #expect(try DriverWindowSelection.standardWindowIDs(candidates) == [1]) } @Test func twoStandardWindowsRemainAmbiguous() throws { let candidates = [ DriverWindowRole(windowID: 2, role: "AXWindow", subrole: "AXStandardWindow"), DriverWindowRole(windowID: 1, role: "AXWindow", subrole: "AXStandardWindow"), ] #expect(try DriverWindowSelection.standardWindowIDs(candidates) == [1, 2]) } @Test(arguments: [UInt32(0), 1]) func invalidOrDuplicateStandardIdentitiesFail(windowID: UInt32) { let candidate = DriverWindowRole(windowID: windowID, role: "AXWindow", subrole: "AXStandardWindow") #expect(throws: DriverError.self) { try DriverWindowSelection.standardWindowIDs([candidate, candidate]) } } @Test func noStandardWindowIsNotReplacedWithAnAuxiliaryWindow() throws { #expect(try DriverWindowSelection.standardWindowIDs([]).isEmpty) #expect( try DriverWindowSelection.standardWindowIDs([ DriverWindowRole(windowID: 1, role: "AXWindow", subrole: "AXDialog") ]).isEmpty) } @Test(arguments: [ DriverCommand.standardWindows(pid: 0, candidates: [1]), .standardWindows(pid: 1, candidates: []), .standardWindows(pid: 1, candidates: [0]), .standardWindows(pid: 1, candidates: [1, 1]), .standardWindows(pid: 1, candidates: Array(1...65)), ]) func roleProbeRequiresABoundedUniqueIdentitySet(command: DriverCommand) { #expect(throws: DriverError.self) { try command.validate() } } @Test func unrelatedUnsupportedElementsDoNotHideTheOwnedWindow() throws { let selection = try DriverWindowSelection.select( windowID: 42, candidates: [.unsupported("window identification unsupported"), .identified(12), .identified(42)]) #expect(selection.index == 2) #expect(selection.warnings == ["AX candidate 0: window identification unsupported"]) let response = DriverResponse(id: 1, identificationWarnings: selection.warnings) #expect(try JSONDecoder().decode(DriverResponse.self, from: JSONEncoder().encode(response)) == response) } @Test func duplicateIdentityIsNeverResolvedByOrder() { #expect(throws: DriverError.self) { try DriverWindowSelection.select(windowID: 42, candidates: [.identified(42), .identified(42)]) } } @Test(arguments: [[DriverWindowIdentity](), [.identified(12)], [.unsupported("unsupported")]]) func absentTargetRemainsAnExplicitFailure(_ candidates: [DriverWindowIdentity]) { #expect(throws: DriverError.self) { try DriverWindowSelection.select(windowID: 42, candidates: candidates) } } @Test func zeroTargetIsRejected() { #expect(throws: DriverError.self) { try DriverWindowSelection.select(windowID: 0, candidates: [.identified(0)]) } } }