SPB Git

Repository Public License

Verifier.swift 3.8 KB 70 lines swift

1import AppKit2import Foundation3import PrismCore45/// Launches a published satellite through LaunchServices, as the Finder would, and checks what actually happened.6public struct LaunchReport: Sendable, Equatable {7    public var launched = false8    public var bundleIDMatches = false9    public var separateProcess = false10    public var hasDockIdentity = false11    public var windowVisible = false12    public var loaded = false13    public var exitedCleanly = false14    public var tableCounts: [String: Int] = [:]15    public var failure: String?1617    public var passed: Bool { launched && bundleIDMatches && separateProcess && hasDockIdentity && windowVisible && loaded && exitedCleanly }1819    public var lines: [(String, Bool)] {20        [("Launched by LaunchServices", launched), ("Runs under its own bundle identifier", bundleIDMatches), ("Separate process from PRISM", separateProcess),21         ("Has its own Dock entry", hasDockIdentity), ("Window on screen", windowVisible), ("App loaded its definition and data", loaded), ("Quit cleanly", exitedCleanly)]22    }23}2425@MainActor26public enum SatelliteVerifier {27    /// `dataBase` redirects the app's data folder (tests use a temporary one). The app quits itself after reporting.28    public static func verify(app: URL, bundleID: String, dataBase: URL? = nil, timeout: Double = 30) async -> LaunchReport {29        var report = LaunchReport()30        let file = FileManager.default.temporaryDirectory.appendingPathComponent("prism-launch-\(UUID().uuidString).json")31        defer { try? FileManager.default.removeItem(at: file) }32        let configuration = NSWorkspace.OpenConfiguration()33        configuration.activates = false34        configuration.createsNewApplicationInstance = true35        var environment = ["PRISM_SATELLITE_REPORT": file.path, "PRISM_SATELLITE_QUIT": "1"]36        if let dataBase { environment["PRISM_SATELLITE_DATA_DIR"] = dataBase.path }37        configuration.environment = environment38        let running: NSRunningApplication39        do { running = try await NSWorkspace.shared.openApplication(at: app, configuration: configuration) } catch {40            report.failure = "macOS could not open the app: \(error.localizedDescription)"41            return report42        }43        report.launched = true44        report.bundleIDMatches = running.bundleIdentifier == bundleID45        report.separateProcess = running.processIdentifier != ProcessInfo.processInfo.processIdentifier46        let deadline = Date().addingTimeInterval(timeout)47        var json: JSONValue?48        while Date() < deadline {49            if let data = FileManager.default.contents(atPath: file.path), let parsed = try? JSONValue.parse(data) { json = parsed; break }50            if running.isTerminated { break }51            try? await Task.sleep(for: .milliseconds(200))52        }53        guard let json else {54            report.failure = running.isTerminated ? "The app quit before it finished opening." : "The app did not report within \(Int(timeout)) seconds."55            if !running.isTerminated { running.forceTerminate() }56            return report57        }58        report.bundleIDMatches = report.bundleIDMatches && json["bundleId"]?.stringValue == bundleID59        report.hasDockIdentity = json["activationPolicy"] == "regular" && json["hasDockIcon"] == true60        report.windowVisible = (json["visibleWindows"]?.intValue ?? 0) > 061        report.loaded = json["loaded"] == true62        report.failure = json["failure"]?.stringValue63        for (k, v) in json["tables"]?.objectValue ?? [:] { report.tableCounts[k] = v.intValue ?? -1 }64        while Date() < deadline, !running.isTerminated { try? await Task.sleep(for: .milliseconds(100)) }65        report.exitedCleanly = running.isTerminated66        if !running.isTerminated { running.forceTerminate() }67        return report68    }69}70
Files main
Menu

Theme