// Test-only executable; isolated credentials arrive through stdin, never argv. import AppKit import SwiftUI import RastrilloApple import SheetsKit @main struct Review: App { private let input: ReviewInput private let client: NativeHTTP init() { input = try! JSONDecoder().decode(ReviewInput.self, from: FileHandle.standardInput.readDataToEndOfFile()) precondition(input.origin.scheme == "http" && input.origin.host == "127.0.0.1") client = NativeHTTP(origin: input.origin, credential: input.credential) } var body: some Scene { WindowGroup("Sheets native review") { SheetsWorkspace(client: client, signOut: {}) .preferredColorScheme(.light) .task { await capture() } }.defaultSize(width: 1200, height: 820) } @MainActor private func capture() async { do { guard CGPreflightScreenCaptureAccess() else { FileHandle.standardError.write(Data("macOS screen-capture access is unavailable for the review executable.\n".utf8)) exit(2) } try await Task.sleep(for: .milliseconds(500)) guard let window = NSApplication.shared.windows.first(where: { $0.title == "Sheets native review" }) else { throw NativeClientError.message("Review window did not open.") } window.makeKeyAndOrderFront(nil) NSApplication.shared.activate() for _ in 0..<40 { if let view = window.contentView, pressFixture(in: view) { break } try await Task.sleep(for: .milliseconds(100)) } try await Task.sleep(for: .seconds(2)) let screenshot = Process() screenshot.executableURL = URL(fileURLWithPath: "/usr/sbin/screencapture") screenshot.arguments = ["-x", "-o", "-l", String(window.windowNumber), input.output] try screenshot.run() while screenshot.isRunning { try await Task.sleep(for: .milliseconds(50)) } guard screenshot.terminationStatus == 0 else { throw NativeClientError.message("Review window capture failed.") } NSApplication.shared.terminate(nil) } catch { FileHandle.standardError.write(Data("Native review capture failed.\n".utf8)) exit(1) } } @MainActor private func pressFixture(in element: Any) -> Bool { guard let accessible = element as? any NSAccessibilityProtocol else { return false } if accessible.accessibilityLabel() == "Native review workbook", accessible.accessibilityPerformPress() { return true } for child in accessible.accessibilityChildren() ?? [] { if pressFixture(in: child) { return true } } return false } }