fix(computer-use): prevent repeated screen recording prompts (#13427)
This commit is contained in:
+31
@@ -0,0 +1,31 @@
|
||||
import XCTest
|
||||
|
||||
final class ScreenCapturePermissionPreflightSafetyTests: XCTestCase {
|
||||
func testPermissionPreflightCannotRequestOrCaptureTheScreen() throws {
|
||||
let source = try computerUseSource()
|
||||
let start = try XCTUnwrap(source.range(of: "private func screenCaptureTrusted()"))
|
||||
let end = try XCTUnwrap(
|
||||
source.range(of: "private func requestScreenCaptureAccess()", range: start.upperBound..<source.endIndex)
|
||||
)
|
||||
let preflightSource = source[start.lowerBound..<end.lowerBound]
|
||||
|
||||
XCTAssertTrue(preflightSource.contains("CGPreflightScreenCaptureAccess()"))
|
||||
XCTAssertTrue(preflightSource.contains("timeoutMs: 2_000"))
|
||||
XCTAssertFalse(preflightSource.contains("CGRequestScreenCaptureAccess()"))
|
||||
XCTAssertFalse(preflightSource.contains("CGWindowListCreateImage"))
|
||||
XCTAssertFalse(preflightSource.contains("SCScreenshotManager"))
|
||||
}
|
||||
|
||||
private func computerUseSource() throws -> String {
|
||||
let testFile = URL(fileURLWithPath: #filePath)
|
||||
let packageRoot = testFile
|
||||
.deletingLastPathComponent()
|
||||
.deletingLastPathComponent()
|
||||
.deletingLastPathComponent()
|
||||
let mainPath = packageRoot
|
||||
.appendingPathComponent("Sources")
|
||||
.appendingPathComponent("OrcaComputerUseMacOS")
|
||||
.appendingPathComponent("main.swift")
|
||||
return try String(contentsOf: mainPath, encoding: .utf8)
|
||||
}
|
||||
}
|
||||
-79
@@ -1,79 +0,0 @@
|
||||
import Testing
|
||||
@testable import OrcaComputerUseMacOSCore
|
||||
|
||||
@Suite("Screen Capture permission settling")
|
||||
struct ScreenCapturePermissionSettlingTests {
|
||||
@Test("initial preflight success skips fallback")
|
||||
func initialSuccess() {
|
||||
var fallbackCalls = 0
|
||||
let trusted = PermissionTrustSettling.settleWithFallback(
|
||||
finalTimeoutMs: 500,
|
||||
sleepMs: { _ in },
|
||||
probe: { true },
|
||||
fallbackProbe: {
|
||||
fallbackCalls += 1
|
||||
return true
|
||||
}
|
||||
)
|
||||
|
||||
#expect(trusted)
|
||||
#expect(fallbackCalls == 0)
|
||||
}
|
||||
|
||||
@Test("capture fallback can establish trust")
|
||||
func fallbackSuccess() {
|
||||
var fallbackCalls = 0
|
||||
let trusted = PermissionTrustSettling.settleWithFallback(
|
||||
initialTimeoutMs: 200,
|
||||
finalTimeoutMs: 200,
|
||||
intervalMs: 100,
|
||||
sleepMs: { _ in },
|
||||
probe: { false },
|
||||
fallbackProbe: {
|
||||
fallbackCalls += 1
|
||||
return true
|
||||
}
|
||||
)
|
||||
|
||||
#expect(trusted)
|
||||
#expect(fallbackCalls == 1)
|
||||
}
|
||||
|
||||
@Test("preflight is retried after fallback failure")
|
||||
func finalPreflightSuccess() {
|
||||
var probeCalls = 0
|
||||
let trusted = PermissionTrustSettling.settleWithFallback(
|
||||
initialTimeoutMs: 100,
|
||||
finalTimeoutMs: 200,
|
||||
intervalMs: 100,
|
||||
sleepMs: { _ in },
|
||||
probe: {
|
||||
probeCalls += 1
|
||||
return probeCalls == 4
|
||||
},
|
||||
fallbackProbe: { false }
|
||||
)
|
||||
|
||||
#expect(trusted)
|
||||
#expect(probeCalls == 4)
|
||||
}
|
||||
|
||||
@Test("persistent denial remains denied")
|
||||
func persistentDenial() {
|
||||
var fallbackCalls = 0
|
||||
let trusted = PermissionTrustSettling.settleWithFallback(
|
||||
initialTimeoutMs: 100,
|
||||
finalTimeoutMs: 100,
|
||||
intervalMs: 100,
|
||||
sleepMs: { _ in },
|
||||
probe: { false },
|
||||
fallbackProbe: {
|
||||
fallbackCalls += 1
|
||||
return false
|
||||
}
|
||||
)
|
||||
|
||||
#expect(!trusted)
|
||||
#expect(fallbackCalls == 1)
|
||||
}
|
||||
}
|
||||
-44
@@ -1,44 +0,0 @@
|
||||
import Testing
|
||||
@testable import OrcaComputerUseMacOSCore
|
||||
|
||||
@Suite("Screen capture probe window selection")
|
||||
struct ScreenCaptureProbeWindowSelectionTests {
|
||||
@Test("own process windows cannot prove cross-app capture permission")
|
||||
func excludesOwnWindows() {
|
||||
let selected = ScreenCaptureProbeWindowSelection.firstCrossProcessNormalWindow(
|
||||
ownPid: 42,
|
||||
windows: [
|
||||
.init(layer: 0, ownerPid: 42, windowId: 1),
|
||||
.init(layer: 0, ownerPid: 84, windowId: 2)
|
||||
]
|
||||
)
|
||||
|
||||
#expect(selected == 2)
|
||||
}
|
||||
|
||||
@Test("non-normal windows are skipped")
|
||||
func excludesNonNormalWindows() {
|
||||
let selected = ScreenCaptureProbeWindowSelection.firstCrossProcessNormalWindow(
|
||||
ownPid: 42,
|
||||
windows: [
|
||||
.init(layer: 1, ownerPid: 84, windowId: 1),
|
||||
.init(layer: 0, ownerPid: 84, windowId: 2)
|
||||
]
|
||||
)
|
||||
|
||||
#expect(selected == 2)
|
||||
}
|
||||
|
||||
@Test("no cross-process normal window returns nil")
|
||||
func noCandidate() {
|
||||
let selected = ScreenCaptureProbeWindowSelection.firstCrossProcessNormalWindow(
|
||||
ownPid: 42,
|
||||
windows: [
|
||||
.init(layer: 0, ownerPid: 42, windowId: 1),
|
||||
.init(layer: 1, ownerPid: 84, windowId: 2)
|
||||
]
|
||||
)
|
||||
|
||||
#expect(selected == nil)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user