401 lines
16 KiB
Swift
401 lines
16 KiB
Swift
import AppKit
|
|
import Darwin
|
|
import Foundation
|
|
import ShotdeckCore
|
|
|
|
/// In-process self-test for the region picker, driven by `SHOTDECK_PICKER_SELFTEST`.
|
|
/// Posts synthetic mouse events through `NSApp.postEvent` only — never CGEventPost/taps.
|
|
@MainActor
|
|
enum PickerSelfTest {
|
|
private static let pointA = NSPoint(x: 200, y: 300)
|
|
private static let pointB = NSPoint(x: 600, y: 600)
|
|
private static let dragSteps = 6
|
|
|
|
/// Called from `main.swift` before `ShotdeckApp.main()`. Returns immediately when the
|
|
/// env var is unset; otherwise waits for launch, drives the production picker, and `exit`s.
|
|
static func runIfRequested() {
|
|
guard let raw = ProcessInfo.processInfo.environment["SHOTDECK_PICKER_SELFTEST"],
|
|
!raw.isEmpty
|
|
else { return }
|
|
|
|
let output = URL(fileURLWithPath: raw, isDirectory: true)
|
|
// Hop onto a plain main-queue turn after NSApp starts. Nested run loops
|
|
// from a Swift Task do not drain NSApp's event queue.
|
|
DispatchQueue.main.async {
|
|
MainActor.assumeIsolated {
|
|
execute(outputDirectory: output)
|
|
}
|
|
}
|
|
}
|
|
|
|
private static func execute(outputDirectory: URL) {
|
|
do {
|
|
try FileManager.default.createDirectory(
|
|
at: outputDirectory,
|
|
withIntermediateDirectories: true
|
|
)
|
|
} catch {
|
|
fail(expected: expectedLabel(), got: "could not create output dir: \(error)")
|
|
}
|
|
|
|
guard let screen = NSScreen.screens.first(where: { $0.frame.contains(pointA) })
|
|
?? NSScreen.screens.first
|
|
else {
|
|
fail(expected: expectedLabel(), got: "no NSScreen")
|
|
}
|
|
|
|
let appKitRect = CGRect(
|
|
x: min(pointA.x, pointB.x),
|
|
y: min(pointA.y, pointB.y),
|
|
width: abs(pointB.x - pointA.x),
|
|
height: abs(pointB.y - pointA.y)
|
|
).intersection(screen.frame)
|
|
let expected = CaptureRegion.fromAppKit(rect: appKitRect, on: screen)
|
|
|
|
let picker = RegionPickerController()
|
|
var result: CaptureRegion??
|
|
picker.pick { region in
|
|
result = .some(region)
|
|
}
|
|
|
|
guard let overlay = overlayWindow(containing: pointA) else {
|
|
fail(expected: format(expected.rect), got: "no overlay window after pick()")
|
|
}
|
|
overlay.makeKey()
|
|
|
|
var eventNumber = 1
|
|
func post(_ type: NSEvent.EventType, at screenPoint: NSPoint, clickCount: Int) {
|
|
let locationInWindow = overlay.convertPoint(fromScreen: screenPoint)
|
|
guard let event = NSEvent.mouseEvent(
|
|
with: type,
|
|
location: locationInWindow,
|
|
modifierFlags: [],
|
|
timestamp: ProcessInfo.processInfo.systemUptime,
|
|
windowNumber: overlay.windowNumber,
|
|
context: nil,
|
|
eventNumber: eventNumber,
|
|
clickCount: clickCount,
|
|
pressure: 1
|
|
) else {
|
|
fail(expected: format(expected.rect), got: "NSEvent.mouseEvent(\(type.rawValue)) returned nil")
|
|
}
|
|
eventNumber += 1
|
|
NSApp.postEvent(event, atStart: false)
|
|
// Local monitors run during NSApp.sendEvent (production dispatch),
|
|
// not during nextEvent dequeue. Drive that same path here.
|
|
NSApp.sendEvent(event)
|
|
}
|
|
|
|
post(.leftMouseDown, at: pointA, clickCount: 1)
|
|
for step in 1...(dragSteps / 2) {
|
|
post(.leftMouseDragged, at: interpolate(step), clickCount: 0)
|
|
}
|
|
|
|
writeOverlayBitmap(overlay, to: outputDirectory.appendingPathComponent("overlay-middrag.png"))
|
|
|
|
for step in ((dragSteps / 2) + 1)...dragSteps {
|
|
post(.leftMouseDragged, at: interpolate(step), clickCount: 0)
|
|
}
|
|
post(.leftMouseUp, at: pointB, clickCount: 1)
|
|
|
|
guard let wrapped = result else {
|
|
fail(expected: format(expected.rect), got: "completion never fired")
|
|
}
|
|
guard let got = wrapped else {
|
|
fail(expected: format(expected.rect), got: "nil")
|
|
}
|
|
|
|
if got.rect != expected.rect {
|
|
fail(expected: format(expected.rect), got: format(got.rect))
|
|
}
|
|
|
|
print("PICKER-SELFTEST PASS rect=\(format(got.rect))")
|
|
fflush(stdout)
|
|
|
|
runRegionPersistPhase()
|
|
if !startUpdateSelfTestIfRequested() {
|
|
exit(0)
|
|
}
|
|
// UPDATE-SELFTEST hops to a later main-actor turn and exits itself.
|
|
}
|
|
|
|
/// Phase 2: writes a known region under `CaptureRegion.defaultsKey`, reloads it through
|
|
/// `AppModel.loadPersistedRegion()` (the same path init uses), then restores whatever
|
|
/// value was stored before so a real picked region is untouched.
|
|
private static func runRegionPersistPhase() {
|
|
let defaults = UserDefaults.standard
|
|
let previous = defaults.data(forKey: CaptureRegion.defaultsKey)
|
|
|
|
let known = CaptureRegion(
|
|
displayID: CGMainDisplayID(),
|
|
rect: CGRect(x: 10, y: 10, width: 100, height: 100),
|
|
capturedScale: 2.0
|
|
)
|
|
var failure: String?
|
|
if let encoded = try? JSONEncoder().encode(known) {
|
|
defaults.set(encoded, forKey: CaptureRegion.defaultsKey)
|
|
if let loaded = AppModel.loadPersistedRegion() {
|
|
if loaded.rect != known.rect {
|
|
failure = "expected=\(format(known.rect)) got=\(format(loaded.rect))"
|
|
}
|
|
} else {
|
|
failure = "loadPersistedRegion returned nil"
|
|
}
|
|
} else {
|
|
failure = "could not encode CaptureRegion"
|
|
}
|
|
|
|
if let previous {
|
|
defaults.set(previous, forKey: CaptureRegion.defaultsKey)
|
|
} else {
|
|
defaults.removeObject(forKey: CaptureRegion.defaultsKey)
|
|
}
|
|
|
|
if let failure {
|
|
print("REGION-PERSIST FAIL \(failure)")
|
|
fflush(stdout)
|
|
exit(1)
|
|
}
|
|
print("REGION-PERSIST PASS")
|
|
fflush(stdout)
|
|
}
|
|
|
|
/// Phase 3: builds a fake 99.0.0 bundle, serves a local appcast, stages via
|
|
/// `checkNow`, then `installStaged` into the env dir — never `/Applications`.
|
|
/// Returns true when the async phase was scheduled (it calls `exit` itself).
|
|
@discardableResult
|
|
private static func startUpdateSelfTestIfRequested() -> Bool {
|
|
guard let raw = ProcessInfo.processInfo.environment["SHOTDECK_UPDATE_SELFTEST"],
|
|
!raw.isEmpty
|
|
else { return false }
|
|
|
|
let output = URL(fileURLWithPath: raw, isDirectory: true)
|
|
Task { @MainActor in
|
|
do {
|
|
try await runUpdateSelfTest(outputDirectory: output)
|
|
print("UPDATE-SELFTEST PASS version=99.0.0")
|
|
fflush(stdout)
|
|
exit(0)
|
|
} catch let error as UpdateSelfTestError {
|
|
updateFail(error.description)
|
|
} catch {
|
|
updateFail(String(describing: error))
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
private static func runUpdateSelfTest(outputDirectory: URL) async throws {
|
|
let fm = FileManager.default
|
|
try fm.createDirectory(at: outputDirectory, withIntermediateDirectories: true)
|
|
|
|
guard let sourceApp = ownAppBundleURL() else {
|
|
throw UpdateSelfTestError.detail("own bundle is not a .app (\(Bundle.main.bundleURL.path))")
|
|
}
|
|
|
|
let payload = outputDirectory.appendingPathComponent("payload", isDirectory: true)
|
|
if fm.fileExists(atPath: payload.path) {
|
|
try fm.removeItem(at: payload)
|
|
}
|
|
try fm.createDirectory(at: payload, withIntermediateDirectories: true)
|
|
let fakeApp = payload.appendingPathComponent("Redline.app")
|
|
try fm.copyItem(at: sourceApp, to: fakeApp)
|
|
|
|
let plistURL = fakeApp.appendingPathComponent("Contents/Info.plist")
|
|
let plistData = try Data(contentsOf: plistURL)
|
|
guard var plist = try PropertyListSerialization.propertyList(from: plistData, format: nil) as? [String: Any] else {
|
|
throw UpdateSelfTestError.detail("could not parse copied Info.plist")
|
|
}
|
|
plist["CFBundleShortVersionString"] = "99.0.0"
|
|
let rewritten = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0)
|
|
try rewritten.write(to: plistURL)
|
|
|
|
let zipURL = outputDirectory.appendingPathComponent("Redline-99.0.0.zip")
|
|
if fm.fileExists(atPath: zipURL.path) {
|
|
try fm.removeItem(at: zipURL)
|
|
}
|
|
try runDitto(arguments: ["-c", "-k", payload.path, zipURL.path])
|
|
|
|
let zipData = try Data(contentsOf: zipURL)
|
|
let hex = UpdateChecker.sha256Hex(zipData)
|
|
|
|
let appcastURL = outputDirectory.appendingPathComponent("appcast.json")
|
|
let appcast: [String: String] = [
|
|
"version": "99.0.0",
|
|
"zipURL": zipURL.absoluteString,
|
|
"sha256": hex,
|
|
"notes": "UPDATE-SELFTEST",
|
|
]
|
|
let appcastData = try JSONSerialization.data(withJSONObject: appcast, options: [.sortedKeys])
|
|
try appcastData.write(to: appcastURL)
|
|
|
|
let defaults = UserDefaults.standard
|
|
let previous = defaults.string(forKey: UpdateChecker.appcastURLDefaultsKey)
|
|
defaults.set(appcastURL.absoluteString, forKey: UpdateChecker.appcastURLDefaultsKey)
|
|
defer {
|
|
if let previous {
|
|
defaults.set(previous, forKey: UpdateChecker.appcastURLDefaultsKey)
|
|
} else {
|
|
defaults.removeObject(forKey: UpdateChecker.appcastURLDefaultsKey)
|
|
}
|
|
}
|
|
|
|
let (model, isolatedRoot) = try makeIsolatedUpdateModel()
|
|
defer { try? fm.removeItem(at: isolatedRoot) }
|
|
|
|
await model.updateChecker.checkNow()
|
|
|
|
guard model.updateAvailable?.version == "99.0.0" else {
|
|
throw UpdateSelfTestError.detail(
|
|
"updateAvailable=\(model.updateAvailable?.version ?? "nil")"
|
|
)
|
|
}
|
|
guard let staged = model.updateChecker.stagedAppURL else {
|
|
throw UpdateSelfTestError.detail("staged payload missing")
|
|
}
|
|
guard staged.lastPathComponent == "Redline.app" else {
|
|
throw UpdateSelfTestError.detail("staged name \(staged.lastPathComponent)")
|
|
}
|
|
let stagedExe = staged.appendingPathComponent("Contents/MacOS/Shotdeck")
|
|
guard fm.fileExists(atPath: stagedExe.path) else {
|
|
throw UpdateSelfTestError.detail("staged Contents/MacOS/Shotdeck missing")
|
|
}
|
|
|
|
let targetRoot = outputDirectory.appendingPathComponent("target", isDirectory: true)
|
|
if fm.fileExists(atPath: targetRoot.path) {
|
|
try fm.removeItem(at: targetRoot)
|
|
}
|
|
let target = targetRoot.appendingPathComponent("Redline.app")
|
|
model.updateChecker.installStaged(to: target)
|
|
|
|
let installedPlist = target.appendingPathComponent("Contents/Info.plist")
|
|
guard let installed = NSDictionary(contentsOf: installedPlist) as? [String: Any],
|
|
let installedVersion = installed["CFBundleShortVersionString"] as? String
|
|
else {
|
|
throw UpdateSelfTestError.detail("installed Info.plist unreadable")
|
|
}
|
|
guard installedVersion == "99.0.0" else {
|
|
throw UpdateSelfTestError.detail("installed version \(installedVersion)")
|
|
}
|
|
}
|
|
|
|
private static func ownAppBundleURL() -> URL? {
|
|
let bundle = Bundle.main.bundleURL
|
|
if bundle.pathExtension == "app" { return bundle }
|
|
let up3 = bundle
|
|
.deletingLastPathComponent()
|
|
.deletingLastPathComponent()
|
|
.deletingLastPathComponent()
|
|
if up3.pathExtension == "app" { return up3 }
|
|
return nil
|
|
}
|
|
|
|
private static func makeIsolatedUpdateModel() throws -> (AppModel, URL) {
|
|
let root = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent("shotdeck-update-selftest-\(UUID().uuidString)", isDirectory: true)
|
|
let paths = try AppSupportPaths(
|
|
root: root,
|
|
outbox: root.appendingPathComponent("outbox", isDirectory: true),
|
|
watchFolder: root.appendingPathComponent("watch", isDirectory: true)
|
|
)
|
|
let ledger = try ReturnLedger(paths: paths)
|
|
let model = AppModel(
|
|
paths: paths,
|
|
spool: try SpoolStore(paths: paths),
|
|
composer: PDFComposer(),
|
|
capturer: ScreenCapturer(),
|
|
hotkeys: HotkeyCenter(),
|
|
picker: RegionPickerController(),
|
|
ledger: ledger,
|
|
watcher: ReturnWatcher(paths: paths, ledger: ledger)
|
|
)
|
|
return (model, root)
|
|
}
|
|
|
|
private static func runDitto(arguments: [String]) throws {
|
|
let process = Process()
|
|
process.executableURL = URL(fileURLWithPath: "/usr/bin/ditto")
|
|
process.arguments = arguments
|
|
let err = Pipe()
|
|
process.standardError = err
|
|
process.standardOutput = Pipe()
|
|
try process.run()
|
|
process.waitUntilExit()
|
|
guard process.terminationStatus == 0 else {
|
|
let message = String(data: err.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
|
|
throw UpdateSelfTestError.detail("ditto failed: \(message)")
|
|
}
|
|
}
|
|
|
|
private static func updateFail(_ detail: String) -> Never {
|
|
print("UPDATE-SELFTEST FAIL \(detail)")
|
|
fflush(stdout)
|
|
exit(1)
|
|
}
|
|
|
|
private static func interpolate(_ step: Int) -> NSPoint {
|
|
let t = CGFloat(step) / CGFloat(dragSteps)
|
|
return NSPoint(
|
|
x: pointA.x + (pointB.x - pointA.x) * t,
|
|
y: pointA.y + (pointB.y - pointA.y) * t
|
|
)
|
|
}
|
|
|
|
private static func overlayWindow(containing point: NSPoint) -> RegionPickerWindow? {
|
|
let overlays = NSApp.windows.compactMap { $0 as? RegionPickerWindow }
|
|
return overlays.first(where: { $0.coveringScreen.frame.contains(point) }) ?? overlays.first
|
|
}
|
|
|
|
private static func writeOverlayBitmap(_ overlay: RegionPickerWindow, to url: URL) {
|
|
guard let view = overlay.contentView else {
|
|
fail(expected: expectedLabel(), got: "overlay has no contentView")
|
|
}
|
|
overlay.layoutIfNeeded()
|
|
view.layoutSubtreeIfNeeded()
|
|
view.display()
|
|
let bounds = view.bounds
|
|
guard let rep = view.bitmapImageRepForCachingDisplay(in: bounds) else {
|
|
fail(expected: expectedLabel(), got: "bitmapImageRepForCachingDisplay failed")
|
|
}
|
|
view.cacheDisplay(in: bounds, to: rep)
|
|
guard let png = rep.representation(using: .png, properties: [:]) else {
|
|
fail(expected: expectedLabel(), got: "PNG encode failed")
|
|
}
|
|
do {
|
|
try png.write(to: url)
|
|
} catch {
|
|
fail(expected: expectedLabel(), got: "could not write \(url.path): \(error)")
|
|
}
|
|
print(url.path)
|
|
fflush(stdout)
|
|
}
|
|
|
|
private static func expectedLabel() -> String {
|
|
format(CGRect(
|
|
x: min(pointA.x, pointB.x),
|
|
y: min(pointA.y, pointB.y),
|
|
width: abs(pointB.x - pointA.x),
|
|
height: abs(pointB.y - pointA.y)
|
|
))
|
|
}
|
|
|
|
private static func format(_ rect: CGRect) -> String {
|
|
"(\(rect.origin.x), \(rect.origin.y), \(rect.width), \(rect.height))"
|
|
}
|
|
|
|
private static func fail(expected: String, got: String) -> Never {
|
|
print("PICKER-SELFTEST FAIL expected=\(expected) got=\(got)")
|
|
fflush(stdout)
|
|
exit(1)
|
|
}
|
|
}
|
|
|
|
private enum UpdateSelfTestError: Error, CustomStringConvertible {
|
|
case detail(String)
|
|
var description: String {
|
|
switch self {
|
|
case .detail(let s): return s
|
|
}
|
|
}
|
|
}
|