#if DEBUG import Foundation /// A status with every card face on it, for `-CottonCannedStatus YES` in a /// Debug build: no core is opened and nothing reaches the network. /// `-CottonCannedPasskey asking|sent|retry|confirm|linking` shows WhatsApp's /// passkey step on its card instead, `-CottonCannedSigner keys|chrome` who /// signed it at asking/sent, and `-CottonCannedVault waiting|open|trouble` /// the vault key's line under the header. Every value is invented. enum CannedStatus { static func status(now: Date = Date()) -> CoreStatus { // -CottonCannedStatusFile : a status the core answered, saved as JSON (the panel's screens as // `cargo test -p cotton-mac --lib panel_snapshots -- --ignored` writes them), drawn as it is. if let path = UserDefaults.standard.string(forKey: "CottonCannedStatusFile"), let data = FileManager.default.contents(atPath: path), let saved = try? JSONDecoder().decode(CoreStatus.self, from: data) { return saved } func ago(_ seconds: Double) -> String { let formatter = ISO8601DateFormatter() formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds] return formatter.string(from: now.addingTimeInterval(-seconds)) } func row( _ source: Source, _ schedule: String, phase: String = "idle", probe: SourceStatus.Probe = .init(ready: true, need: nil, detail: nil), progress: SourceStatus.Progress? = nil, brought: Int = 0, at: Double? = nil, error: String? = nil ) -> SourceStatus { SourceStatus( source: source.rawValue, schedule: schedule, phase: phase, probe: probe, progress: progress, lastBrought: brought, lastBroughtAt: at.map(ago), lastRunAt: at.map(ago), total: brought * 40, error: error ) } let passkey = UserDefaults.standard.string(forKey: "CottonCannedPasskey") let signer: String? = switch UserDefaults.standard.string(forKey: "CottonCannedSigner") { case "keys": "Signed by Cotton Keys on this Mac." case "chrome": "Given in Chrome (vault locked: this Mac waits for approval on your phone)." default: nil } let whatsapp = passkey.map { step in row(.whatsapp, "Continuous", phase: "needs", probe: .init( ready: false, need: .init(kind: "pair", by: "code", passkey: step, confirm: step == "confirm" ? "WXYZ-2345" : nil), detail: step == "retry" ? "The passkey was not given: the WhatsApp tab was closed" : ["asking", "sent"].contains(step) ? signer : nil )) } ?? row(.whatsapp, "Continuous", phase: "running", progress: .init(sent: 287, uploaded: 9, total: 421), brought: 38, at: 120) let vault: CoreStatus.VaultLine? = switch UserDefaults.standard.string(forKey: "CottonCannedVault") { case "waiting": .init( state: "waiting", fingerprint: "1a2b 3c4d 5e6f 7a8b", detail: "This Mac is not let in to your data yet, so it sends nothing. Choose Approve on another device and type the code it shows on your phone, or type your recovery code below." ) case "open": .init(state: "open", fingerprint: "1a2b 3c4d 5e6f 7a8b", detail: "This Mac opens your vault.") case "trouble": .init(state: "trouble", fingerprint: nil, detail: "the Keychain would not keep the vault key (-25308)") default: nil } return CoreStatus( signedIn: true, signedOut: false, host: "cottondb.com", phone: "+919876543210", lastPushAt: ago(120), sources: [ whatsapp, row(.mail, "Every 15 min", brought: 12, at: 9 * 60), row(.photos, "Hourly", brought: 7, at: 41 * 60), row(.calls, "Hourly", phase: "needs", probe: .init(ready: false, need: .init(kind: "fullDiskAccess"), detail: nil)), ], vault: vault ) } } #endif