Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift
7.8 KB
Raw Blame
import Testing
@testable import VPhoneCoreKit

struct FirmwarePickerTests {
    /// A scripted stdin: pops one line per `read()` call.
    private func reader(_ lines: [String?]) -> () -> String? {
        var i = 0
        return { defer { i += 1 }; return i < lines.count ? lines[i] : nil }
    }

    // MARK: - Catalog integrity

    @Test func `catalog has twenty seven pairings`() {
        #expect(VPhoneFirmwareCatalog.pairings.count == 27)
    }

    @Test func `every pairing is populated`() {
        for p in VPhoneFirmwareCatalog.pairings {
            #expect(!p.iosName.isEmpty)
            #expect(p.iosURL.hasPrefix("https://"))
            #expect(p.iosURL.hasSuffix(".ipsw"))
            #expect(p.cloudosName.hasPrefix("cloudOS "))
            #expect(p.cloudosURL.contains("/private-cloud-compute/"))
        }
    }

    @Test func `cloud OS options are distinct in first seen order, the research board ones last`() {
        let opts = VPhoneFirmwareCatalog.cloudOSOptions
        #expect(opts.map(\.name) == [
            "cloudOS 26.1", "cloudOS 26.2", "cloudOS 26.3", "cloudOS 26.4",
            "cloudOS 26.4 (23E244)", "cloudOS 26.5 (23F75)", "cloudOS 26.6 (23G71)", "cloudOS 26.7 (23H20)",
        ])
        #expect(Set(opts.map(\.url)).count == opts.count) // no dup URLs
    }

    // MARK: - Passthrough

    @Test func `both provided passes through`() throws {
        let out = try VPhoneFirmwarePicker.resolve(
            iphone: "a.ipsw",
            cloudos: "b.dmg",
            isInteractive: true,
            read: { nil },
            write: { _ in },
        )
        #expect(out == VPhoneFirmwareSources(iphoneSource: "a.ipsw", cloudosSource: "b.dmg"))
    }

    @Test func `non interactive passes through even when incomplete`() throws {
        let out = try VPhoneFirmwarePicker.resolve(
            iphone: nil,
            cloudos: nil,
            isInteractive: false,
            read: { nil },
            write: { _ in },
        )
        #expect(out == VPhoneFirmwareSources(iphoneSource: nil, cloudosSource: nil))
    }

    @Test func `empty strings treated as unset`() throws {
        // Non-interactive + empty → passthrough as nil (no prompt), not "".
        let out = try VPhoneFirmwarePicker.resolve(
            iphone: "",
            cloudos: "",
            isInteractive: false,
            read: { nil },
            write: { _ in },
        )
        #expect(out == VPhoneFirmwareSources(iphoneSource: nil, cloudosSource: nil))
    }

    // MARK: - Prompt: neither provided

    @Test func `neither provided picks full pairing`() throws {
        // "1" → first pairing (iOS 18.6.2 → cloudOS 26.1).
        let first = VPhoneFirmwareCatalog.pairings[0]
        let out = try VPhoneFirmwarePicker.resolve(
            iphone: nil,
            cloudos: nil,
            isInteractive: true,
            read: reader(["1"]),
            write: { _ in },
        )
        #expect(out == VPhoneFirmwareSources(iphoneSource: first.iosURL, cloudosSource: first.cloudosURL))
    }

    @Test func `neither provided resolves both from chosen pairing`() throws {
        let idx = 8 // iOS 26.4 (menu number 9), paired with cloudOS 26.4
        let p = VPhoneFirmwareCatalog.pairings[idx]
        #expect(p.iosName == "iOS 26.4")
        let out = try VPhoneFirmwarePicker.resolve(
            iphone: nil,
            cloudos: nil,
            isInteractive: true,
            read: reader([String(idx + 1)]),
            write: { _ in },
        )
        #expect(out.iphoneSource == p.iosURL)
        #expect(out.cloudosSource == p.cloudosURL)
    }

    // MARK: - Prompt: only one side missing

    @Test func `i phone provided prompts for cloud OS only`() throws {
        // cloudOS menu: [1]26.1 [2]26.2 [3]26.3 [4]26.4 → pick 4.
        let opts = VPhoneFirmwareCatalog.cloudOSOptions
        let out = try VPhoneFirmwarePicker.resolve(
            iphone: "custom.ipsw",
            cloudos: nil,
            isInteractive: true,
            read: reader(["4"]),
            write: { _ in },
        )
        #expect(out.iphoneSource == "custom.ipsw") // untouched
        #expect(out.cloudosSource == opts[3].url) // chosen cloudOS 26.4
    }

    @Test func `cloud OS provided prompts for I phone only`() throws {
        let p = VPhoneFirmwareCatalog.pairings[3] // iOS 26.1 (menu 4)
        #expect(p.iosName == "iOS 26.1")
        let out = try VPhoneFirmwarePicker.resolve(
            iphone: nil,
            cloudos: "custom.dmg",
            isInteractive: true,
            read: reader(["4"]),
            write: { _ in },
        )
        #expect(out.iphoneSource == p.iosURL) // chosen iPhone build
        #expect(out.cloudosSource == "custom.dmg") // untouched
    }

    // MARK: - Menu formatting

    @Test func `pairing menu is column aligned`() throws {
        var lines: [String] = []
        _ = try VPhoneFirmwarePicker.resolve(
            iphone: nil,
            cloudos: nil,
            isInteractive: true,
            read: reader(["1"]),
            write: { lines.append($0) },
        )
        let menu = lines.filter { $0.hasPrefix("  [") }
        #expect(menu.count == 27)
        // Label text starts in one column regardless of 1- vs 2-digit index.
        let labelStarts = Set(menu.map { $0.range(of: "] ")!.upperBound.utf16Offset(in: $0) })
        #expect(labelStarts.count == 1)
        // The "(→ cloudOS …)" arrow lands in one column across every row.
        let arrowCols = Set(menu.map { $0.range(of: "(→")!.lowerBound.utf16Offset(in: $0) })
        #expect(arrowCols.count == 1)
    }

    // MARK: - iPad

    @Test func `i pad device offers its i pad OS builds`() throws {
        var lines: [String] = []
        let pairings = VPhoneFirmwareCatalog.pairings(for: "iPad16,1")
        let out = try VPhoneFirmwarePicker.resolve(
            iphone: nil,
            cloudos: "custom.dmg",
            device: "iPad16,1",
            isInteractive: true,
            read: reader([String(pairings.count)]),
            write: { lines.append($0) },
        )
        #expect(lines.first == "Select an iPadOS firmware to download:")
        #expect(lines.filter { $0.hasPrefix("  [") }.count == pairings.count)
        #expect(out.iphoneSource == pairings.last?.iosURL)
        #expect(out.cloudosSource == "custom.dmg")
    }

    @Test func `unknown device passes through without prompting`() throws {
        let out = try VPhoneFirmwarePicker.resolve(
            iphone: nil,
            cloudos: nil,
            device: "iPad15,8",
            isInteractive: true,
            read: { Issue.record("prompted"); return nil },
            write: { _ in },
        )
        #expect(out == VPhoneFirmwareSources(iphoneSource: nil, cloudosSource: nil))
    }

    // MARK: - Retry / abort semantics

    @Test func `retries then succeeds`() throws {
        let first = VPhoneFirmwareCatalog.pairings[0]
        let out = try VPhoneFirmwarePicker.resolve(
            iphone: nil,
            cloudos: nil,
            isInteractive: true,
            read: reader(["", "999", "garbage", "1"]),
            write: { _ in },
        )
        #expect(out.iphoneSource == first.iosURL)
    }

    @Test func `eof aborts`() {
        #expect(throws: VPhoneFirmwarePickerError.aborted) {
            _ = try VPhoneFirmwarePicker.resolve(
                iphone: nil,
                cloudos: nil,
                isInteractive: true,
                read: { nil },
                write: { _ in },
            )
        }
    }

    @Test func `too many invalid throws`() {
        #expect(throws: VPhoneFirmwarePickerError.invalidSelection) {
            _ = try VPhoneFirmwarePicker.resolve(
                iphone: nil,
                cloudos: nil,
                isInteractive: true,
                maxRetries: 2,
                read: reader(["x", "y", "z"]),
                write: { _ in },
            )
        }
    }
}