Files
orca/native/computer-use-macos/Tests/OrcaComputerUseMacOSTests/ScreenshotEncodingTests.swift
T
Kelvin Amoaba 8e693efda4 feat(computer): add flags for smaller screenshots on macOS, Windows and Linux (#42801)
* feat(computer): add opt-in flags for smaller computer-use screenshots

Every observe and action screenshot came back as a full-resolution PNG, so a Retina window cost about four times the pixels it needed. --screenshot-scale caps pixels per window point, --screenshot-max-edge caps the longer side, and --screenshot-format jpeg re-encodes it, on macOS, Windows and Linux. Defaults are unchanged.

* fix(computer): treat requested screenshot sizes as ceilings and skip repeat encodes
2026-11-15 22:06:12 +00:00

42 lines
1.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import CoreGraphics
import XCTest
@testable import OrcaComputerUseMacOSCore
final class ScreenshotEncodingTests: XCTestCase {
// A 200×100 pixel capture of a 100-point-wide window, as on a Retina display.
private let retinaCapture: CGImage = {
let context = CGContext(
data: nil,
width: 200,
height: 100,
bitsPerComponent: 8,
bytesPerRow: 0,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
)!
context.setFillColor(CGColor(red: 0.2, green: 0.4, blue: 0.8, alpha: 1))
context.fill(CGRect(x: 0, y: 0, width: 200, height: 100))
return context.makeImage()!
}()
func testDefaultsKeepTheFullCaptureAsPNG() throws {
let encoded = try XCTUnwrap(ScreenshotEncoding.encode(retinaCapture, windowWidth: 100, options: ScreenshotOptions()))
XCTAssertEqual([encoded.width, encoded.height], [200, 100])
XCTAssertEqual(Array(encoded.data.prefix(4)), [0x89, 0x50, 0x4E, 0x47])
}
func testScaleOneGivesOnePixelPerWindowPointAsJPEG() throws {
let options = ScreenshotOptions(format: .jpeg, maxScale: 1)
let encoded = try XCTUnwrap(ScreenshotEncoding.encode(retinaCapture, windowWidth: 100, options: options))
XCTAssertEqual([encoded.width, encoded.height], [100, 50])
XCTAssertEqual(Array(encoded.data.prefix(3)), [0xFF, 0xD8, 0xFF])
}
func testMaxEdgeCapsTheLongerSideWithoutUpscaling() throws {
let capped = try XCTUnwrap(ScreenshotEncoding.encode(retinaCapture, windowWidth: 100, options: ScreenshotOptions(maxEdge: 80)))
XCTAssertEqual([capped.width, capped.height], [80, 40])
let loose = try XCTUnwrap(ScreenshotEncoding.encode(retinaCapture, windowWidth: 100, options: ScreenshotOptions(maxScale: 4, maxEdge: 4000)))
XCTAssertEqual([loose.width, loose.height], [200, 100])
}
}