mirror of
https://github.com/stablyai/orca.git
synced 2026-10-31 17:17:38 +00:00
* test: remove redundant cases and repair behavioral assertions * docs: reconcile audit records with current main * fix(deps): apply security updates from #32101 Apply the six-file DOMPurify 3.4.17 and Next.js 16.4.1 patch from PR #32101 to resolve the dependency security gate for this test audit. Original patch by Brennan Benson. * test: satisfy changed-line quality gates Add required braces and replace filter indexing with find. Rerun both CI quality gates and the 27 affected relay tests; refresh audit hashes and record the corrections. --------- Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com> Co-authored-by: Brennan Benson <79079362+brennanb2025@users.noreply.github.com>
301 lines
11 KiB
Swift
301 lines
11 KiB
Swift
import XCTest
|
|
@testable import OrcaComputerUseMacOSCore
|
|
|
|
final class SyntheticMouseClickDeliveryTests: XCTestCase {
|
|
func testSingleClickPlanPairsDownAndUpAfterMove() {
|
|
XCTAssertEqual(
|
|
SyntheticMouseClickDelivery.steps(clickCount: 1),
|
|
[.move, .buttonDown(pressIndex: 1), .buttonUp(pressIndex: 1)]
|
|
)
|
|
}
|
|
|
|
func testMultiClickPlanNumbersEachPressForClickState() {
|
|
XCTAssertEqual(
|
|
SyntheticMouseClickDelivery.steps(clickCount: 2),
|
|
[
|
|
.move,
|
|
.buttonDown(pressIndex: 1), .buttonUp(pressIndex: 1),
|
|
.buttonDown(pressIndex: 2), .buttonUp(pressIndex: 2),
|
|
]
|
|
)
|
|
}
|
|
|
|
func testNonPositiveClickCountStillDeliversOnePress() {
|
|
for count in [0, -3] {
|
|
XCTAssertEqual(
|
|
SyntheticMouseClickDelivery.steps(clickCount: count),
|
|
[.move, .buttonDown(pressIndex: 1), .buttonUp(pressIndex: 1)]
|
|
)
|
|
}
|
|
}
|
|
|
|
func testExcessiveClickCountIsCappedAtTripleClick() {
|
|
XCTAssertEqual(
|
|
SyntheticMouseClickDelivery.steps(clickCount: Int.max),
|
|
[
|
|
.move,
|
|
.buttonDown(pressIndex: 1), .buttonUp(pressIndex: 1),
|
|
.buttonDown(pressIndex: 2), .buttonUp(pressIndex: 2),
|
|
.buttonDown(pressIndex: 3), .buttonUp(pressIndex: 3),
|
|
]
|
|
)
|
|
}
|
|
|
|
func testClickStateMatchesPressIndexAndSkipsMove() {
|
|
XCTAssertEqual(SyntheticMouseClickDelivery.clickState(for: .move), 0)
|
|
XCTAssertEqual(SyntheticMouseClickDelivery.clickState(for: .buttonDown(pressIndex: 1)), 1)
|
|
XCTAssertEqual(SyntheticMouseClickDelivery.clickState(for: .buttonUp(pressIndex: 2)), 2)
|
|
}
|
|
|
|
func testAmbiguousWindowFrameFallbackDoesNotSelectRecipient() {
|
|
let candidates = [(windowID: 101, frame: 7), (windowID: 202, frame: 7)]
|
|
|
|
XCTAssertNil(SyntheticMouseClickDelivery.uniqueWindowCandidate(
|
|
from: candidates,
|
|
matching: { $0.frame == 7 }
|
|
))
|
|
XCTAssertNil(SyntheticMouseClickDelivery.uniqueWindowCandidate(
|
|
from: candidates,
|
|
matching: { $0.frame == 8 }
|
|
))
|
|
XCTAssertEqual(
|
|
SyntheticMouseClickDelivery.uniqueWindowCandidate(
|
|
from: candidates,
|
|
matching: { $0.windowID == 101 }
|
|
)?.windowID,
|
|
101
|
|
)
|
|
}
|
|
|
|
func testRecipientChangeBeforeMouseDownPostsNoClickAndReportsBothWindows() {
|
|
let target = SyntheticMouseClickDelivery.Recipient(ownerPID: 41, windowID: 101)
|
|
let intruder = SyntheticMouseClickDelivery.Recipient(ownerPID: 52, windowID: 202)
|
|
var posted: [SyntheticMouseClickDelivery.Step] = []
|
|
|
|
XCTAssertThrowsError(
|
|
try SyntheticMouseClickDelivery.deliver(
|
|
clickCount: 1,
|
|
target: target,
|
|
currentObservation: { .focused(intruder) },
|
|
makeEvent: { $0 },
|
|
post: { posted.append($0) },
|
|
pause: { _ in }
|
|
)
|
|
) { error in
|
|
XCTAssertEqual(
|
|
error as? SyntheticMouseClickDelivery.FenceFailure,
|
|
.recipientChanged(expected: target, actual: intruder, deliveredPresses: 0)
|
|
)
|
|
}
|
|
XCTAssertEqual(posted, [.move])
|
|
}
|
|
|
|
func testFinalMouseUpMayDismissTheTargetWithoutFailingTheClick() throws {
|
|
let target = SyntheticMouseClickDelivery.Recipient(ownerPID: 41, windowID: 101)
|
|
var observations: [SyntheticMouseClickDelivery.RecipientObservation] = [.focused(target), .dismissed]
|
|
var posted: [SyntheticMouseClickDelivery.Step] = []
|
|
|
|
try SyntheticMouseClickDelivery.deliver(
|
|
clickCount: 1,
|
|
target: target,
|
|
currentObservation: { observations.removeFirst() },
|
|
makeEvent: { $0 },
|
|
post: { posted.append($0) },
|
|
pause: { _ in }
|
|
)
|
|
|
|
XCTAssertEqual(posted, [.move, .buttonDown(pressIndex: 1), .buttonUp(pressIndex: 1)])
|
|
}
|
|
|
|
func testFinalUnavailableObservationRemainsFailClosed() {
|
|
let target = SyntheticMouseClickDelivery.Recipient(ownerPID: 41, windowID: 101)
|
|
var observations: [SyntheticMouseClickDelivery.RecipientObservation] = [.focused(target), .unavailable]
|
|
var posted: [SyntheticMouseClickDelivery.Step] = []
|
|
|
|
XCTAssertThrowsError(
|
|
try SyntheticMouseClickDelivery.deliver(
|
|
clickCount: 1,
|
|
target: target,
|
|
currentObservation: { observations.removeFirst() },
|
|
makeEvent: { $0 },
|
|
post: { posted.append($0) },
|
|
pause: { _ in }
|
|
)
|
|
) { error in
|
|
XCTAssertEqual(
|
|
error as? SyntheticMouseClickDelivery.FenceFailure,
|
|
.recipientChanged(expected: target, actual: nil, deliveredPresses: 1)
|
|
)
|
|
}
|
|
XCTAssertEqual(posted, [.move, .buttonDown(pressIndex: 1), .buttonUp(pressIndex: 1)])
|
|
}
|
|
|
|
func testFinalMouseUpRejectsADifferentFocusedRecipient() {
|
|
let target = SyntheticMouseClickDelivery.Recipient(ownerPID: 41, windowID: 101)
|
|
let intruder = SyntheticMouseClickDelivery.Recipient(ownerPID: 52, windowID: 202)
|
|
var recipients: [SyntheticMouseClickDelivery.Recipient?] = [target, intruder]
|
|
var posted: [SyntheticMouseClickDelivery.Step] = []
|
|
|
|
XCTAssertThrowsError(
|
|
try SyntheticMouseClickDelivery.deliver(
|
|
clickCount: 1,
|
|
target: target,
|
|
currentObservation: {
|
|
recipients.removeFirst().map(SyntheticMouseClickDelivery.RecipientObservation.focused) ?? .dismissed
|
|
},
|
|
makeEvent: { $0 },
|
|
post: { posted.append($0) },
|
|
pause: { _ in }
|
|
)
|
|
) { error in
|
|
XCTAssertEqual(
|
|
error as? SyntheticMouseClickDelivery.FenceFailure,
|
|
.recipientChanged(expected: target, actual: intruder, deliveredPresses: 1)
|
|
)
|
|
}
|
|
XCTAssertEqual(posted, [.move, .buttonDown(pressIndex: 1), .buttonUp(pressIndex: 1)])
|
|
}
|
|
|
|
func testMouseUpPostsBeforeRecipientCheckForTheNextPress() throws {
|
|
let target = SyntheticMouseClickDelivery.Recipient(ownerPID: 41, windowID: 101)
|
|
var trace: [String] = []
|
|
|
|
try SyntheticMouseClickDelivery.deliver(
|
|
clickCount: 2,
|
|
target: target,
|
|
currentObservation: {
|
|
trace.append("recipient")
|
|
return .focused(target)
|
|
},
|
|
makeEvent: { $0 },
|
|
post: {
|
|
switch $0 {
|
|
case .move:
|
|
trace.append("move")
|
|
case .buttonDown:
|
|
trace.append("down")
|
|
case .buttonUp:
|
|
trace.append("up")
|
|
}
|
|
},
|
|
pause: { interval in
|
|
XCTAssertGreaterThan(interval, 0)
|
|
trace.append("pause")
|
|
}
|
|
)
|
|
|
|
XCTAssertEqual(trace, [
|
|
"move", "pause", "recipient", "down", "pause", "up", "recipient",
|
|
"pause", "recipient", "down", "pause", "up", "recipient", "pause"
|
|
])
|
|
}
|
|
|
|
func testRecipientChangeBeforeLaterPressReportsCompletedPresses() {
|
|
let target = SyntheticMouseClickDelivery.Recipient(ownerPID: 41, windowID: 101)
|
|
let intruder = SyntheticMouseClickDelivery.Recipient(ownerPID: 52, windowID: 202)
|
|
var recipients = [target, target, intruder]
|
|
var posted: [SyntheticMouseClickDelivery.Step] = []
|
|
|
|
XCTAssertThrowsError(
|
|
try SyntheticMouseClickDelivery.deliver(
|
|
clickCount: 2,
|
|
target: target,
|
|
currentObservation: {
|
|
.focused(recipients.removeFirst())
|
|
},
|
|
makeEvent: { $0 },
|
|
post: { posted.append($0) },
|
|
pause: { _ in }
|
|
)
|
|
) { error in
|
|
XCTAssertEqual(
|
|
error as? SyntheticMouseClickDelivery.FenceFailure,
|
|
.recipientChanged(expected: target, actual: intruder, deliveredPresses: 1)
|
|
)
|
|
}
|
|
XCTAssertEqual(posted, [
|
|
.move,
|
|
.buttonDown(pressIndex: 1),
|
|
.buttonUp(pressIndex: 1),
|
|
])
|
|
}
|
|
|
|
func testRecipientChangeAfterIntermediateMouseUpStopsBeforeNextPress() {
|
|
let target = SyntheticMouseClickDelivery.Recipient(ownerPID: 41, windowID: 101)
|
|
let intruder = SyntheticMouseClickDelivery.Recipient(ownerPID: 52, windowID: 202)
|
|
var recipients: [SyntheticMouseClickDelivery.Recipient?] = [target, intruder]
|
|
var posted: [SyntheticMouseClickDelivery.Step] = []
|
|
|
|
XCTAssertThrowsError(
|
|
try SyntheticMouseClickDelivery.deliver(
|
|
clickCount: 2,
|
|
target: target,
|
|
currentObservation: {
|
|
recipients.removeFirst().map(SyntheticMouseClickDelivery.RecipientObservation.focused) ?? .dismissed
|
|
},
|
|
makeEvent: { $0 },
|
|
post: { posted.append($0) },
|
|
pause: { _ in }
|
|
)
|
|
) { error in
|
|
XCTAssertEqual(
|
|
error as? SyntheticMouseClickDelivery.FenceFailure,
|
|
.recipientChanged(expected: target, actual: intruder, deliveredPresses: 1)
|
|
)
|
|
}
|
|
XCTAssertEqual(posted, [
|
|
.move,
|
|
.buttonDown(pressIndex: 1),
|
|
.buttonUp(pressIndex: 1),
|
|
])
|
|
}
|
|
|
|
func testIntermediateMouseUpDismissalStopsBeforeNextPress() {
|
|
let target = SyntheticMouseClickDelivery.Recipient(ownerPID: 41, windowID: 101)
|
|
var recipients: [SyntheticMouseClickDelivery.Recipient?] = [target, nil]
|
|
var posted: [SyntheticMouseClickDelivery.Step] = []
|
|
|
|
XCTAssertThrowsError(
|
|
try SyntheticMouseClickDelivery.deliver(
|
|
clickCount: 2,
|
|
target: target,
|
|
currentObservation: {
|
|
recipients.removeFirst().map(SyntheticMouseClickDelivery.RecipientObservation.focused) ?? .dismissed
|
|
},
|
|
makeEvent: { $0 },
|
|
post: { posted.append($0) },
|
|
pause: { _ in }
|
|
)
|
|
) { error in
|
|
XCTAssertEqual(
|
|
error as? SyntheticMouseClickDelivery.FenceFailure,
|
|
.recipientChanged(expected: target, actual: nil, deliveredPresses: 1)
|
|
)
|
|
}
|
|
XCTAssertEqual(posted, [
|
|
.move,
|
|
.buttonDown(pressIndex: 1),
|
|
.buttonUp(pressIndex: 1),
|
|
])
|
|
}
|
|
|
|
func testButtonPairIsPreparedBeforeMouseDownPosts() {
|
|
enum PreparationFailure: Error { case mouseUp }
|
|
let target = SyntheticMouseClickDelivery.Recipient(ownerPID: 41, windowID: 101)
|
|
var posted: [SyntheticMouseClickDelivery.Step] = []
|
|
|
|
XCTAssertThrowsError(try SyntheticMouseClickDelivery.deliver(
|
|
clickCount: 1,
|
|
target: target,
|
|
currentObservation: { .focused(target) },
|
|
makeEvent: { step in
|
|
if case .buttonUp = step { throw PreparationFailure.mouseUp }
|
|
return step
|
|
},
|
|
post: { posted.append($0) },
|
|
pause: { _ in }
|
|
))
|
|
XCTAssertEqual(posted, [.move])
|
|
}
|
|
}
|