Cotton code
68 lines · 3.5 KB · SwiftRaw
// The serving Mac's backup on its device page — on that Mac, and on a phone// that reads the serving Mac's page (you3 b4577db48): the line `read.device`'s// `backup` is drawn as (`DeviceRowFacts.backupLine`), Back up now offered on// that page alone, and `device.backupNow` said in the core's own receipt. On// the same row by the same gate, its sender (first push): `device.push`'s// line, and Test push said in `device.testPush`'s receipt or refusal.// Every id, name, stamp and reason here is invented.import CottonKitimport Foundationimport XCTest@testable import CottonUIfinal class DeviceBackupTests: XCTestCase {    private let mac = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"    private let phone = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"    private var utc: Calendar {        var calendar = Calendar(identifier: .gregorian)        calendar.timeZone = TimeZone(identifier: "UTC")!        calendar.locale = Locale(identifier: "en_US_POSIX")        return calendar    }    /// Wednesday 7 October 2026, 12:00 UTC.    private let now = Date(timeIntervalSince1970: 1_791_374_400)    private func json(_ text: String) throws -> JSONValue {        try JSONDecoder().decode(JSONValue.self, from: Data(text.utf8))    }    private var devices: String {        """        {"thisDevice":"\(mac)","devices":[          {"deviceId":"\(mac)","name":"Example Studio Mac","platform":"mac","approvedAt":"2026-09-01T10:00:00Z",           "servingRank":0,"sampledAt":"2026-10-07T11:59:00Z"},          {"deviceId":"\(phone)","name":"Example Phone","platform":"ios","approvedAt":"2026-09-02T10:00:00Z"}]}        """    }    private var macPage: String {        """        {"deviceId":"\(mac)","thisDevice":true,"hostAnsweredBy":"this_mac","backupAnsweredBy":"this_mac",         "host":{"startAtLogin":true,"stayAwake":true,"errorReports":true,"autoUpdates":true},         "backup":{"state":"ok","lastAt":"2026-10-07T09:40:00.000Z","lastError":null,                   "nextAt":"2026-10-08T09:00:00.000Z","keptBytes":null}}        """    }    /// RULE: A backup is read only off the serving Mac's page — said by that Mac or forwarded to a phone — and a null line or a state this build does not know is none rather than a guess.    func testTheBackupIsReadOnlyOffTheServingMacsPage() throws {        let said = try XCTUnwrap(DeviceBackup.answered(try json(macPage)))        XCTAssertEqual(said.backup, DeviceBackup(state: .ok, lastAt: "2026-10-07T09:40:00.000Z", nextAt: "2026-10-08T09:00:00.000Z"))        XCTAssertEqual(said.by, .thisMac)        XCTAssertEqual(            DeviceBackup.answered(try json(#"{"backupAnsweredBy":"serving_mac","backup":{"state":"running"}}"#))?.by, .servingMac)        XCTAssertNil(DeviceBackup.answered(try json(#"{"backupAnsweredBy":"not_serving","backup":{"state":"ok"}}"#)))        XCTAssertNil(DeviceBackup.answered(try json(#"{"backupAnsweredBy":"no_serving_mac","backup":null}"#)))        XCTAssertNil(DeviceBackup.answered(try json(#"{"backupAnsweredBy":"not_a_mac","backup":null}"#)))        XCTAssertNil(DeviceBackup.answered(try json(#"{"hostAnsweredBy":"this_mac","backup":{"state":"ok"}}"#)), "who said it is the backup's own key")        XCTAssertNil(DeviceBackup.answered(try json(#"{"backupAnsweredBy":"this_mac","backup":null}"#)))        XCTAssertNil(DeviceBackup.answered(try json(#"{"backupAnsweredBy":"this_mac","backup":{"state":"paused"}}"#)))    }    /// RULE: The serving Mac's own row carries its backup line and offers Back up now, while a phone's page and another device's page carry neither.}