import Foundation @main struct ReleaseUpdateChecks { @MainActor static func main() async throws { func release(_ tag: String, draft: Bool = false, prerelease: Bool = false) -> PublishedRelease { PublishedRelease(tagName: tag, draft: draft, prerelease: prerelease, assets: [ .init(name: "kymo-1.3-15-arm64-adhoc.zip"), .init(name: "kymo-1.3-16-arm64-adhoc.zip.sha256"), .init(name: "kymo-1.2-99-arm64-adhoc.zip") ]) } let stable = release("v1.3") let comparisons = try [ stable.isNewer(than: "1.3.0", build: 15), stable.isNewer(than: "1.3", build: 14), stable.isNewer(than: "1.2", build: 99), stable.isNewer(than: "2.0", build: 1), release("v1.10").isNewer(than: "1.9", build: 30) ] precondition(comparisons == [false, true, true, false, true]) precondition(stable.page.absoluteString == "https://git.airnan.cn/wangairnan/Kymo/releases/tag/v1.3") for invalid in [release("v1.4", draft: true), release("v1.4", prerelease: true), release("v1.4-rc.1"), release("main")] { precondition((try? invalid.isNewer(than: "1.3", build: 15)) == nil) } let suite = "KymoReleaseUpdateChecks." + UUID().uuidString let defaults = UserDefaults(suiteName: suite)! defer { defaults.removePersistentDomain(forName: suite) } var pending: [CheckedContinuation] = [] let updates = ReleaseUpdates(defaults: defaults, version: "1.2", build: 10) { try await withCheckedThrowingContinuation { pending.append($0) } } func wait(_ condition: () -> Bool) async { let deadline = ContinuousClock.now.advanced(by: .seconds(3)) while !condition() && ContinuousClock.now < deadline { await Task.yield() } precondition(condition()) } precondition(updates.automatic) updates.start() updates.check() await wait { pending.count == 1 } updates.automatic = false precondition(updates.phase == .idle) let restarted = ReleaseUpdates(defaults: defaults) restarted.start() precondition(!restarted.automatic && restarted.phase == .idle) updates.check() await wait { pending.count == 2 } pending.removeLast().resume(returning: stable) await wait { updates.phase == .checked } pending.removeLast().resume(throwing: URLError(.cancelled)) await Task.yield() precondition(updates.available == stable && updates.phase == .checked) updates.automatic = true await wait { pending.count == 1 } pending.removeLast().resume(throwing: URLError(.notConnectedToInternet)) await wait { updates.phase == .failed } updates.check() await wait { pending.count == 1 } pending.removeLast().resume(returning: release("v1.0")) await wait { updates.phase == .checked } precondition(updates.available == nil) updates.stop() print("PASS: release versions and build assets, stable releases, automatic preference, cancellation, single request, manual retry and recovery") } }