Cotton code
66 lines · 3.8 KB · SwiftRaw
// The fan's tile counts are the core's (`shell.fan`), read again whenever the// copy changes: the fan's read is keyed on `recordsRevision`, the counter the// core's `data` events move (`SyncModel`), as the deck's own count is. A// Goodreads import's books landing in the tail of a burst moved no family and// the window's trailing edge dropped them, so the counter stood still and the// Reading tile kept its count from before the import while the deck showed// the books (Books 1.0.0 (3) and (4) walks, 7 Nov 2026).import CottonKitimport XCTest@testable import CottonUI@MainActorfinal class FanCountsAfterImportTests: XCTestCase {    /// RULE: rows that move inside the coalescing window are told on its trailing edge whatever families their tables reach — the revision the fan's counts are read again on moves for an import's books as it does for the write that opened the window.    func testRowsLandingInTheWindowsTailMoveTheRevision() async {        let gate = WindowGate()        let sync = SyncModel(core: ScriptedCore([:]), clock: CoalesceClock { _ in await gate.wait() })        let before = sync.revision        // The write that opens the window: told at once.        sync.receive(.data(tables: ["cotton_book"], origin: SyncModel.writeOrigin, database: "w1"))        XCTAssertEqual(sync.revision, before + 1, "the leading edge tells the screens at once")        // The import's books, 40 ms later, inside the window.        sync.receive(.data(tables: ["cotton_book", "cotton_group_member"], origin: SyncModel.writeOrigin, database: "w1"))        XCTAssertEqual(sync.revision, before + 1, "pooled until the window closes")        XCTAssertTrue(InvalidationMap.families(for: ["cotton_book", "cotton_group_member"]).isEmpty,                      "the books' tables reach no family: the case that was dropped")        await gate.open()        for _ in 0..<300 where sync.revision == before + 1 { try? await Task.sleep(nanoseconds: 10_000_000) }        XCTAssertEqual(sync.revision, before + 2, "the trailing edge tells the screens the books landed")        // A window with nothing in its tail tells nothing more.        let quiet = SyncModel(core: ScriptedCore([:]), clock: .instant)        quiet.receive(.data(tables: ["cotton_book"], origin: SyncModel.writeOrigin, database: "w1"))        try? await Task.sleep(nanoseconds: 200_000_000)        XCTAssertEqual(quiet.revision, 1, "one event, one announcement")    }    /// RULE: the fan reads its answer again on that revision — the read's key holds it beside the database, the focus, the views chosen and the definitions — so a moved revision asks `shell.fan` for fresh counts.    func testTheFansReadIsKeyedOnTheRevision() throws {        let fan = SourceSweep.withoutComments(            try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Surfaces/Board/FanSurface.swift", ownedBy: "fan"))        let key = try XCTUnwrap(fan.range(of: ".task(id: ToolViews.Reading("))        let read = fan[key.upperBound...].prefix(400)        XCTAssertTrue(read.contains("revision: revision"), "the fan's read is asked again when the copy changes")        XCTAssertTrue(read.contains("fan.reload(core: core, database: scene?.shownId"), "and it asks the core's shell.fan of the database shown")        XCTAssertTrue(read.contains("scene == nil || scene?.shownId != nil"), "never before the window knows the database it shows")    }}/// The coalescing window, held shut until the test opens it.private actor WindowGate {    private var waiting: [CheckedContinuation<Void, Never>] = []    private var opened = false    func wait() async {        guard !opened else { return }        await withCheckedContinuation { waiting.append($0) }    }    func open() {        opened = true        waiting.forEach { $0.resume() }        waiting = []    }}