| 1 | // Test-only executable; isolated credentials arrive through stdin, never argv. |
| 2 | import AppKit |
| 3 | import SwiftUI |
| 4 | import RastrilloApple |
| 5 | import SheetsKit |
| 6 | |
| 7 | @main struct Review: App { |
| 8 | private let input: ReviewInput |
| 9 | private let client: NativeHTTP |
| 10 | |
| 11 | init() { |
| 12 | input = try! JSONDecoder().decode(ReviewInput.self, from: FileHandle.standardInput.readDataToEndOfFile()) |
| 13 | precondition(input.origin.scheme == "http" && input.origin.host == "127.0.0.1") |
| 14 | client = NativeHTTP(origin: input.origin, credential: input.credential) |
| 15 | } |
| 16 | |
| 17 | var body: some Scene { |
| 18 | WindowGroup("Sheets native review") { |
| 19 | SheetsWorkspace(client: client, signOut: {}) |
| 20 | .preferredColorScheme(.light) |
| 21 | .task { await capture() } |
| 22 | }.defaultSize(width: 1200, height: 820) |
| 23 | } |
| 24 | |
| 25 | @MainActor private func capture() async { |
| 26 | do { |
| 27 | guard CGPreflightScreenCaptureAccess() else { |
| 28 | FileHandle.standardError.write(Data("macOS screen-capture access is unavailable for the review executable.\n".utf8)) |
| 29 | exit(2) |
| 30 | } |
| 31 | try await Task.sleep(for: .milliseconds(500)) |
| 32 | guard let window = NSApplication.shared.windows.first(where: { $0.title == "Sheets native review" }) else { |
| 33 | throw NativeClientError.message("Review window did not open.") |
| 34 | } |
| 35 | window.makeKeyAndOrderFront(nil) |
| 36 | NSApplication.shared.activate() |
| 37 | for _ in 0..<40 { |
| 38 | if let view = window.contentView, pressFixture(in: view) { break } |
| 39 | try await Task.sleep(for: .milliseconds(100)) |
| 40 | } |
| 41 | try await Task.sleep(for: .seconds(2)) |
| 42 | let screenshot = Process() |
| 43 | screenshot.executableURL = URL(fileURLWithPath: "/usr/sbin/screencapture") |
| 44 | screenshot.arguments = ["-x", "-o", "-l", String(window.windowNumber), input.output] |
| 45 | try screenshot.run() |
| 46 | while screenshot.isRunning { try await Task.sleep(for: .milliseconds(50)) } |
| 47 | guard screenshot.terminationStatus == 0 else { throw NativeClientError.message("Review window capture failed.") } |
| 48 | NSApplication.shared.terminate(nil) |
| 49 | } catch { |
| 50 | FileHandle.standardError.write(Data("Native review capture failed.\n".utf8)) |
| 51 | exit(1) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | @MainActor private func pressFixture(in element: Any) -> Bool { |
| 56 | guard let accessible = element as? any NSAccessibilityProtocol else { return false } |
| 57 | if accessible.accessibilityLabel() == "Native review workbook", accessible.accessibilityPerformPress() { return true } |
| 58 | for child in accessible.accessibilityChildren() ?? [] { |
| 59 | if pressFixture(in: child) { return true } |
| 60 | } |
| 61 | return false |
| 62 | } |
| 63 | } |
| 64 | |