// 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]). import CottonKit import XCTest @testable import CottonUI // 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 { private let inner: ScriptedCore private let gated: String private var open = false private var waiting: [CheckedContinuation] = [] private var arrivals: [CheckedContinuation] = [] private var arrived = false init(_ 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 = true for continuation in waiting { continuation.resume() } waiting = [] } func callRaw(_ method: String, requestJSON: String) async throws -> String { if method == gated { arrived = true for continuation in arrivals { continuation.resume() } arrivals = [] if !open { await withCheckedContinuation { waiting.append($0) } } } return try await inner.callRaw(method, requestJSON: requestJSON) } func subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) async { await inner.subscribe(handler) } func close() async { await inner.close() } } private struct Ordered: Encodable { let a: Int let b: Int } private struct Reordered: Encodable { let b: Int let a: Int } // MARK: - The cache @MainActor final class AnswerCacheTests: XCTestCase { } // MARK: - The scaffolded read @MainActor final class ScreenReadSeedingTests: XCTestCase { private let old = JSONValue.object(["projects": .array([.object(["slug": .string("old")])])]) private let fresh = JSONValue.object(["projects": .array([.object(["slug": .string("fresh")])])]) private func cache(holding value: JSONValue, families: Set = [.projects, .core]) -> AnswerCache { let cache = AnswerCache() cache.store("read.projects", ReadRequest(args: .object([:])), value: value, families: families) return cache } } // MARK: - The one wire final class HostedSeedingSourceTests: XCTestCase { } // MARK: - The review pass: the answer version, the request once, the byte budget @MainActor final class AnswerVersionTests: 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`. final class AnswerVersionSweepTests: XCTestCase { func testNoScreenDerivesStateFromTheValueOrOnAppear() throws { var offenders: [String] = [] for (name, text) in try SourceSweep.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")) } } @MainActor final class RequestOnceTests: XCTestCase { } @MainActor final class AnswerCacheBudgetTests: XCTestCase { private func 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 @MainActor final class SeededDayTests: XCTestCase { }