// 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. import CottonValues import Foundation import XCTest @testable import CottonUI @MainActor final class RecordListTickTests: XCTestCase { private static let 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"])] ) private func store(_ count: Int) -> GatedRecords { let rows = (1...count).map { TableRecord(id: "example-chore-\($0)", values: ["title": .text("Example chore \($0)")]) } return GatedRecords(FixtureRecords(definitions: [Self.chore], records: ["chore": rows])) } private func until(_ what: String, _ condition: () async -> Bool) async throws { for _ in 0..<400 { if await condition() { return } try await Task.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() async throws { 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: "") } try await until("the first read is on its way") { await records.waiting > 0 } for _ in 0..<20 { list.dataMoved() } XCTAssertEqual(list.phase, .loading) await records.release() await first.value try await 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() async throws { 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() try await until("the re-read is on its way") { await records.waiting > 0 } for _ in 0..<10 { list.dataMoved() } await records.release() try await until("both re-reads land") { list.settled == 2 } try await Task.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 { private let inner: FixtureRecords private var holding = false private var held: [CheckedContinuation] = [] private(set) var lists = 0 init(_ inner: FixtureRecords) { self.inner = inner } var waiting: Int { held.count } func hold() { holding = true } func release() { holding = false let open = held held = [] open.forEach { $0.resume() } } func tableDefinitions() async throws -> [TableDefinition] { try await inner.tableDefinitions() } func count(table: String) async throws -> Int { try await inner.count(table: table) } func list( table: String, project: String?, query: String?, page: Int, where filter: [String: RecordValue] ) async throws -> RecordPage { lists += 1 if holding { await withCheckedContinuation { held.append($0) } } return try await inner.list(table: table, project: project, query: query, page: page, where: filter) } func get(table: String, id: String) async throws -> TableRecord? { try await inner.get(table: table, id: id) } func create(table: String, data: [String: RecordValue]) async throws -> ChangeRecord { try await inner.create(table: table, data: data) } func update(table: String, id: String, patch: [String: RecordValue]) async throws -> ChangeRecord { try await inner.update(table: table, id: id, patch: patch) } func delete(table: String, id: String) async throws -> ChangeRecord { try await inner.delete(table: table, id: id) } func reorder(table: String, ids: [String]) async throws -> ChangeRecord { try await inner.reorder(table: table, ids: ids) } func link(table: String, id: String, column: String, target: String) async throws -> ChangeRecord { try await inner.link(table: table, id: id, column: column, target: target) } func undo(change: String) async throws -> ChangeRecord { try await inner.undo(change: change) } }