// The session's last answers: a screen you come back to draws at once.//// The failure this holds against: every push built a fresh model, the read's// first answer started pending again, and a skeleton stood in front of an// answer the app had held a second ago. The rule: a model that is// `AnswerSeeded` draws the cache's answer on its first frame, its own read// replaces it, and a `data` event drops the entries whose families moved.//// Every fixture is synthetic ([nothing-real-ever-goes-in-cotton-s-public-tree]).importCottonKitimport XCTest@testable importCottonUI// MARK: - Two cores the tests need/// A core that HOLDS one method until the test lets it go, and says when the/// call arrived. What lets a test assert "the first answer drew before the/// second read came back" without a sleep.private actor GatedCore: CottonCalling {privatelet inner: ScriptedCoreprivatelet gated: Stringprivatevaropen = falseprivatevar waiting: [CheckedContinuation<Void, Never>] = []privatevar arrivals: [CheckedContinuation<Void, Never>] = []privatevar arrived = falseinit(_ replies: [String: ScriptedCore.Reply], holding gated: String) { inner = ScriptedCore(replies)self.gated = gated }/// Resolves once the held method has been asked.func arrival() async {if arrived { return }await withCheckedContinuation { arrivals.append($0) } }func release() {open = truefor continuation in waiting { continuation.resume() } waiting = [] }func callRaw(_ method: String, requestJSON: String) asyncthrows -> String {if method == gated { arrived = truefor continuation in arrivals { continuation.resume() } arrivals = []if !open { await withCheckedContinuation { waiting.append($0) } } }returntryawait inner.callRaw(method, requestJSON: requestJSON) }func subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) async {await inner.subscribe(handler) }func close() async { await inner.close() }}privatestructOrdered: Encodable {let a: Intlet b: Int}privatestructReordered: Encodable {let b: Intlet a: Int}// MARK: - The cache@MainActorfinalclassAnswerCacheTests: XCTestCase {}// MARK: - The scaffolded read@MainActorfinalclassScreenReadSeedingTests: XCTestCase {privatelet old = JSONValue.object(["projects": .array([.object(["slug": .string("old")])])])privatelet fresh = JSONValue.object(["projects": .array([.object(["slug": .string("fresh")])])])privatefunc cache(holding value: JSONValue, families: Set<ScreenFamily> = [.projects, .core]) -> AnswerCache {let cache = AnswerCache() cache.store("read.projects", ReadRequest(args: .object([:])), value: value, families: families)return cache }}// MARK: - The one wirefinalclassHostedSeedingSourceTests: XCTestCase {}// MARK: - The review pass: the answer version, the request once, the byte budget@MainActorfinalclassAnswerVersionTests: XCTestCase {}/// Every screen that derives state from a read's answer keys on the answer/// VERSION with `initial: true` — never on `value`, and never in `onAppear`.finalclassAnswerVersionSweepTests: XCTestCase {func testNoScreenDerivesStateFromTheValueOrOnAppear() throws {var offenders: [String] = []for (name, text) intrySourceSweep.swiftSources() {let code = SourceSweep.withoutComments(text)for line in code.components(separatedBy: "\n") {let trimmed = line.trimmingCharacters(in: .whitespaces)if trimmed.contains(".onChange(of: read.value") || trimmed.contains(".onChange(of: document.value") { offenders.append("\(name): \(trimmed) — key on `read.answerVersion`") }if trimmed.contains(".onChange(of: read.answerVersion") && !trimmed.contains("initial: true") { offenders.append("\(name): \(trimmed) — needs `initial: true`, the seed is already there") }if trimmed.hasPrefix(".onAppear") && trimmed.contains("seed(") { offenders.append("\(name): \(trimmed) — seeding on appear misses the fresh answer") } } } XCTAssertEqual(offenders, [], offenders.joined(separator: "\n")) }}@MainActorfinalclassRequestOnceTests: XCTestCase {}@MainActorfinalclassAnswerCacheBudgetTests: XCTestCase {privatefunc answer(ofAbout bytes: Int) -> JSONValue { .object(["text": .string(String(repeating: "x", count: bytes))]) }}// MARK: - The review pass: the day, and the database replaced behind the app@MainActorfinalclassSeededDayTests: XCTestCase {}