// A list under a stream of data events (a sync's ticks, the Devices poll):// the read on its way answers, and the list is read again in place once.// Every record is invented.importCottonValuesimportFoundationimport XCTest@testable importCottonUI@MainActorfinalclassRecordListTickTests: XCTestCase {privatestaticlet chore = TableDefinition( id: "chore", name: "Chore", plural: "Chores", columns: [ColumnDefinition(key: "title", type: .text, required: true)], title: "title", slip: SlipLayout(title: "title"), tile: TileLayout(face: .slipList), page: [PageSection(heading: nil, columns: ["title"])] )privatefunc store(_ count: Int) -> GatedRecords {let rows = (1...count).map { TableRecord(id: "example-chore-\($0)", values: ["title": .text("Example chore \($0)")]) }returnGatedRecords(FixtureRecords(definitions: [Self.chore], records: ["chore": rows])) }privatefunc until(_ what: String, _ condition: () async -> Bool) asyncthrows {for _ in0..<400 {ifawait condition() { return }tryawaitTask.sleep(for: .milliseconds(5)) } XCTFail("never: \(what)") }/// RULE: A first read under a stream of data events answers ONCE and is never restarted; the ticks that came while it was away are one re-read in place after it lands (architect, 12 Oct: the skeleton stayed up through a sync because every tick threw the first read away).func testAFirstReadUnderAStreamOfTicksAnswersOnceAndIsNotRestarted() asyncthrows {let records = store(3)await records.hold()let list = RecordListModel(definition: Self.chore, store: records, core: ListCoreFake(), seeds: ListSeeds())let first = Task { await list.reload(project: nil, query: "") }tryawait until("the first read is on its way") { await records.waiting > 0 }for _ in0..<20 { list.dataMoved() } XCTAssertEqual(list.phase, .loading)await records.release()await first.valuetryawait until("the ticks are read again in place") { list.settled == 1 } XCTAssertEqual(list.firstReads, 1, "the first read answered once") XCTAssertEqual(list.records.count, 3)let lists = await records.lists XCTAssertEqual(lists, 2, "one first read and ONE re-read for twenty ticks") }/// RULE: Ticks while a re-read is on its way are one more re-read after it, never a restart of it (a sync's 250 ms window under a slower read once starved a loaded list of every answer).func testTicksDuringAReReadAreOneMoreReReadAfterIt() asyncthrows {let records = store(3)let list = RecordListModel(definition: Self.chore, store: records, core: ListCoreFake(), seeds: ListSeeds())await list.reload(project: nil, query: "")await records.hold() list.dataMoved()tryawait until("the re-read is on its way") { await records.waiting > 0 }for _ in0..<10 { list.dataMoved() }await records.release()tryawait until("both re-reads land") { list.settled == 2 }tryawaitTask.sleep(for: .milliseconds(50)) XCTAssertEqual(list.settled, 2, "eleven ticks, one re-read on its way and one after it")let lists = await records.lists XCTAssertEqual(lists, 3) }}/// Fixture verbs whose reads can be held on their way (also `ListSeedsTests`).actor GatedRecords: Records {privatelet inner: FixtureRecordsprivatevar holding = falseprivatevar held: [CheckedContinuation<Void, Never>] = []private(set) var lists = 0init(_ inner: FixtureRecords) { self.inner = inner }var waiting: Int { held.count }func hold() { holding = true }func release() { holding = falseletopen = held held = []open.forEach { $0.resume() } }func tableDefinitions() asyncthrows -> [TableDefinition] { tryawait inner.tableDefinitions() }func count(table: String) asyncthrows -> Int { tryawait inner.count(table: table) }func list( table: String, project: String?, query: String?, page: Int, where filter: [String: RecordValue] ) asyncthrows -> RecordPage { lists += 1if holding { await withCheckedContinuation { held.append($0) } }returntryawait inner.list(table: table, project: project, query: query, page: page, where: filter) }func get(table: String, id: String) asyncthrows -> TableRecord? { tryawait inner.get(table: table, id: id) }func create(table: String, data: [String: RecordValue]) asyncthrows -> ChangeRecord {tryawait inner.create(table: table, data: data) }func update(table: String, id: String, patch: [String: RecordValue]) asyncthrows -> ChangeRecord {tryawait inner.update(table: table, id: id, patch: patch) }func delete(table: String, id: String) asyncthrows -> ChangeRecord { tryawait inner.delete(table: table, id: id) }func reorder(table: String, ids: [String]) asyncthrows -> ChangeRecord { tryawait inner.reorder(table: table, ids: ids) }func link(table: String, id: String, column: String, target: String) asyncthrows -> ChangeRecord {tryawait inner.link(table: table, id: id, column: column, target: target) }func undo(change: String) asyncthrows -> ChangeRecord { tryawait inner.undo(change: change) }}