Cotton code
57 lines · 3.5 KB · SwiftRaw
import CottonKitimport XCTest@testable import CottonUI/// SYNC SEEN on iPhone (owner, 25 Nov 2026): the one quiet line each home draws reads as `sync.status` says it/// (`shell_account::seen_of`), never flickers for a pass that ends inside a second, and a tap stands Settings on Sync.#if os(iOS)@MainActorfinal class SyncSeenTests: XCTestCase {    private func status(_ json: String) throws -> SyncStatusRead {        try JSONDecoder().decode(SyncStatusRead.self, from: Data(json.utf8))    }    /// RULE: The status read carries the seen line: its state, its words, what VoiceOver says and the wait before catching up shows.    func testTheStatusReadCarriesTheSeenLine() throws {        let read = try status(#"""        {"connected":true,"pending":0,"canReachServer":true,"syncs":{},         "seen":{"state":"catching_up","percent":42,"since":1795000000000,"line":"Catching up · 42%","spoken":"Catching up","showAfterMs":1000}}        """#)        XCTAssertEqual(read.seen?.state, "catching_up")        XCTAssertEqual(read.seen?.line, "Catching up · 42%")        XCTAssertEqual(read.seen?.spoken, "Catching up")        XCTAssertEqual(read.seen?.percent, 42)        XCTAssertEqual(read.seen?.catchingUp, true)        let out = try status(#"{"connected":false,"reason":"No database here.","seen":{"state":"signed_out","line":"Signed out","spoken":"Signed out","showAfterMs":1000}}"#)        XCTAssertEqual(out.seen?.state, "signed_out")        // An older core says no seen line: the read still decodes, and the home draws none.        XCTAssertNil(try status(#"{"connected":true,"pending":0,"canReachServer":true}"#).seen)    }    /// RULE: A catching-up line waits until its pass has run a second; a pass that ends sooner never shows, and a line already catching up takes each new number at once.    func testCatchingUpWaitsForItsPassToRunASecond() {        let now = Date(timeIntervalSince1970: 1_795_000_000)        let since = now.timeIntervalSince1970 * 1000        let fresh = SyncSeenRead(state: "catching_up", line: "Catching up", spoken: "Catching up", since: since - 200)        let upToDate = SyncSeenRead(state: "up_to_date", line: "Up to date · just now", spoken: "Up to date")        XCTAssertEqual(SyncSeenLine.wait(before: fresh, shown: upToDate, now: now), 800)        let long = SyncSeenRead(state: "catching_up", line: "Catching up", spoken: "Catching up", since: since - 3_000)        XCTAssertNil(SyncSeenLine.wait(before: long, shown: upToDate, now: now))        let more = SyncSeenRead(state: "catching_up", line: "Catching up · 40%", spoken: "Catching up", since: since - 100, percent: 40)        XCTAssertNil(SyncSeenLine.wait(before: more, shown: fresh, now: now), "already catching up: the new number at once")        XCTAssertNil(SyncSeenLine.wait(before: upToDate, shown: fresh, now: now), "anything else at once")        let counted = SyncSeenRead(state: "catching_up", line: "Catching up · 1,200 changes to go", spoken: "Catching up")        XCTAssertNil(SyncSeenLine.wait(before: counted, shown: upToDate, now: now), "a catch-up the engine counts, with no pass running, at once")    }    /// RULE: A tap on the line stands the Settings opened in the next moment on Sync, however many times it makes its place.    func testATapStandsSettingsOnSync() {        XCTAssertFalse(SyncSeenOpens.take())        SyncSeenOpens.ask()        XCTAssertTrue(SyncSeenOpens.take())        XCTAssertTrue(SyncSeenOpens.take(), "Settings may make its place twice as it opens")        XCTAssertEqual(SettingsHome().place, .sync)    }}#endif