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