Files
程序员阿江(Relakkes) 8336cd8cc9 feat(computer-use): finalize native input and global app consent
Route coordinate clicks through the exact AX hit, reuse render-local
attributes and keep keyboard macros within validated native boundaries.
Preserve lossless capture evidence while returning bounded model images,
and propagate cancellation through daemon and lock acquisition.

Remove legacy per-app restrictions after feature-wide consent on both
platforms while retaining global disablement and target validation.
Include native helpers, regression tests and a stable receiver fixture.

Validation: 224 focused TypeScript tests, 516 XCTest cases and 15 Swift
Testing cases passed. Local sidecar and Electron builds, development
package creation and package smoke checks passed.
2026-09-10 20:58:39 +08:00

70 lines
2.7 KiB
Swift

import XCTest
@testable import cc_haha_computer_use
@MainActor
final class FocusedElementRoutingTests: XCTestCase {
func testBackgroundTargetNeverContactsUnrelatedSystemFocusServer() {
var globalQueries = 0
let result = FocusedElementRouting.select(
targetPID: 42, frontmostPID: 99,
systemFocusedPID: { globalQueries += 1; return 99 },
systemFocusedElement: { globalQueries += 1; return "other-app" },
targetFocusedElement: { "target" }
)
XCTAssertEqual(result, "target")
XCTAssertEqual(globalQueries, 0, "a background snapshot must not wait for an unrelated AX server")
}
func testForegroundTargetKeepsValidatedSystemFocus() {
let result = FocusedElementRouting.select(
targetPID: 42, frontmostPID: 42,
systemFocusedPID: { 42 },
systemFocusedElement: { "target-system-focus" },
targetFocusedElement: { XCTFail("valid system focus should retain precedence"); return "target-fallback" }
)
XCTAssertEqual(result, "target-system-focus")
}
func testForegroundHintDoesNotAuthorizeAnotherProcessAfterFocusChanges() {
let result = FocusedElementRouting.select(
targetPID: 42, frontmostPID: 42,
systemFocusedPID: { 99 },
systemFocusedElement: { XCTFail("must retain system AX PID validation"); return "other-app" },
targetFocusedElement: { "target" }
)
XCTAssertEqual(result, "target")
}
func testUnknownFrontmostStillUsesSystemAXWithPIDValidation() {
for focusedPID: Int32? in [42, 99, nil] {
var queries = 0
let result = FocusedElementRouting.select(
targetPID: 42, frontmostPID: nil,
systemFocusedPID: { queries += 1; return focusedPID },
systemFocusedElement: { "system" },
targetFocusedElement: { "target" }
)
XCTAssertEqual(queries, 1)
XCTAssertEqual(result, focusedPID == 42 ? "system" : "target")
}
}
func testMissingSystemElementFallsBackToTargetAndMissingTargetStaysNil() {
let result: String? = FocusedElementRouting.select(
targetPID: 42, frontmostPID: 42,
systemFocusedPID: { 42 },
systemFocusedElement: { nil },
targetFocusedElement: { "target" }
)
XCTAssertEqual(result, "target")
let absent: String? = FocusedElementRouting.select(
targetPID: 42, frontmostPID: 99,
systemFocusedPID: { XCTFail("must not contact another app"); return 99 },
systemFocusedElement: { "other-app" },
targetFocusedElement: { nil }
)
XCTAssertNil(absent)
}
}