Cotton code
82 lines · 3.1 KB · SwiftRaw
import CottonKitimport XCTest@testable import CottonUI/// `/rejected` — the view model's decisions, over a scripted core.////// Nothing here draws: what is held is the rows → display mapping, what the/// retry asks for, and what the screen is told when only half the list can/// move.@MainActorfinal class RejectedModelTests: XCTestCase {    /// A fixed instant, so `stuckSince` is an assertion rather than a race.    private let now = Date(timeIntervalSince1970: 1_800_000_000)    private func subject(        rejected: String? = nil,        retry: ScriptedCore.Reply = .ok(#"{"folded":[],"stillRejected":[]}"#)    ) -> (RejectedModel, ScriptedCore, ToastCenter) {        var replies: [String: ScriptedCore.Reply] = [CottonMethod.syncRetryRejected: retry]        if let rejected { replies[CottonMethod.syncRejected] = .ok(rejected) }        let core = ScriptedCore(replies)        let toasts = ToastCenter(scheduler: ManualToastScheduler())        return (RejectedModel(core: core, toasts: toasts, now: now), core, toasts)    }    /// The shape `sync.rejected` answers, as the Rust handler projects it.    private let twoRows = """        {"rejected":[          {"opId":"op-1","direction":"pull","table":"cotton_money_transactions",           "rowId":"txn-1","kind":"upsert","reason":"the parent statement has not arrived",           "at":"2027-01-01T00:00:00.000Z","retriedAt":null,"hlc":"1","author":"device-a",           "retryable":true},          {"opId":"op-2","direction":"push","table":"cotton_notes",           "rowId":"note-1","kind":"upsert","reason":"your server would not take it",           "at":"2027-01-01T00:00:00.000Z","retriedAt":null,"hlc":"2","author":"device-b",           "retryable":false}        ],"count":2}        """    // MARK: - Rows → display    // MARK: - Empty and refused    func testAListThatCouldNotBeReadSaysSo() async {        // No reply scripted for `sync.rejected` — the core answers        // `unknown_method`, which is what a build with a stale core does.        let (model, _, _) = subject()        await model.load()        XCTAssertNotNil(model.refusal, "a sentence, never a spinner that never ends")        XCTAssertTrue(model.rows.isEmpty)    }    // MARK: - Retry    func testRetryAllAsksForAllOfThemAndNamesNoIds() async throws {        let (model, core, _) = subject(            rejected: twoRows,            retry: .ok(#"{"folded":["op-1"],"stillRejected":[]}"#)        )        await model.load()        await model.retryAll()        let called = await core.methodsCalled()        XCTAssertEqual(            called,            [CottonMethod.syncRejected, CottonMethod.syncRetryRejected, CottonMethod.syncRejected],            "the retry re-reads, so the list cannot show what has already gone"        )        let calls = await core.calls        let asked = try XCTUnwrap(calls.first { $0.method == CottonMethod.syncRetryRejected })        XCTAssertFalse(            asked.requestJSON.contains("ids"),            "an absent list is 'all of them'; sending the drawn ids would skip whatever arrived since"        )    }    // MARK: - The route}