importCottonKitimportCoreGraphicsimport XCTest@testable importCottonUI/// Strokes as the simulator's touch pipeline delivered them to the deck and/// the people tier (`crates/cotton-core/tests/fixtures/ink/ink-traces.json`,/// recorded on the 2 Nov 2026 walk from synthetic touch paths driven through/// the simulator — not a person's finger), each with what was written: a/// letter, a scratch-out ("clear"), a zigzag that turns back only twice/// ("none"), or a level page swipe ("swipe"). They are read end to end as/// the phone reads them: a real core (`ink.*`, `cotton_core::ink`) asking/// the real letter model (`InkModel`, Core ML) through `InkReader`.finalclassInkTraceTests: XCTestCase {privatestructTrace: Decodable {let wrote: Stringlet strokes: [[[Double]]] }privatevar container: URL?privatevar core: (any CottonCalling)?overridefunc setUp() asyncthrows {let container = FileManager.default.temporaryDirectory.appendingPathComponent("cotton-ink-\(UUID().uuidString)")self.container = container core = tryCottonRuntime.open(CottonConfig(containerPath: container.path, timeZone: "UTC"))InkModel.shipped.load() }overridefunc tearDown() asyncthrows {await core?.close()iflet container { try? FileManager.default.removeItem(at: container) } }privatefunc traces() throws -> [Trace] {let url = SourceSweep.repoRoot.appendingPathComponent("crates/cotton-core/tests/fixtures/ink/ink-traces.json")returntry JSONDecoder().decode([Trace].self, from: Data(contentsOf: url)) }/// One trace written on `surface` among the letters, its strokes 0.25 s/// each and 0.1 s apart, then the wait run out: every run read, and how/// long each call to the core took with the model's answers (ms).privatefunc read(_ trace: Trace, on surface: String) asyncthrows -> (runs: [InkReply.Run], took: [Double]) {let core = try XCTUnwrap(core)var runs: [InkReply.Run] = [], took: [Double] = []var t = 1_000.0let clock = ContinuousClock()for stroke in trace.strokes {let (down, up) = (t, t + 0.25) t = up + 0.1let points = stroke.enumerated().map { [$0.element[0], $0.element[1], down + (up - down) * Double($0.offset) / Double(max(1, stroke.count - 1))] }let json = tryInkReader.body(["surface": surface, "admits": ["letter"], "height": 600,"stroke": ["points": points, "down": down, "up": up], "context": ["screen": [402, 874], "tier": "deck"]])var reply: InkReply?let duration = tryawait clock.measure { reply = tryawaitInkReader.read(core, "ink.stroke", surface: surface, json: json) } took.append(Double(duration.components.attoseconds) / 1e15 + Double(duration.components.seconds) * 1000) runs += reply?.groups.flatMap(\.runs) ?? [] }let due = tryInkReader.body(["surface": surface, "now": t + 2]) runs += tryawaitInkReader.read(core, "ink.due", surface: surface, json: due).groups.flatMap(\.runs)return (runs, took) }/// RULE: every recorded trace reads as what was written, through the core and the model as the phone reads it. The one exception is the one-stroke T, the templates' Graffiti ⌐ (across, then down from the right end), which no hand in the corpus writes: a doubtful letter at most, shown lighter, never a sure wrong one. The time each lift took to read (the core's calls and the model's answers) is printed, p50 and p95.func testRecordedTracesReadAsWritten() asyncthrows {var misread: [String] = [], took: [Double] = [], doubtful: [String] = []for (index, trace) intry traces().enumerated() {let read = tryawait read(trace, on: "trace:\(index)") took += read.tookif read.runs.contains(where: { $0.read == "doubtful" }) { doubtful.append(trace.wrote) }if trace.wrote == "t", trace.strokes.count == 1 { XCTAssertTrue(read.runs.count == 1 && read.runs[0].read == "doubtful", "the ⌐ T is a doubtful letter at most: \(read.runs)") } elseif !Self.reads(read.runs, as: trace.wrote) { misread.append("\(trace.wrote) → \(read.runs.map { $0.letter ?? $0.read })") } } took.sort() print(String(format: "ink-measure lift through the core, %d lifts: p50 %.2f ms, p95 %.2f ms, max %.2f ms", took.count, took[took.count / 2], took[took.count * 95 / 100], took[took.count - 1])) XCTAssertEqual(misread, [], "\(misread.count) misread") XCTAssertEqual(doubtful, ["t"], "the Graffiti ⌐ T alone is doubtful") }privatestaticfunc reads(_ runs: [InkReply.Run], as wrote: String) -> Bool {switch wrote {case"clear": runs.map(\.read) == ["clear"]case"none": runs.allSatisfy { $0.read == "nothing" }case"swipe":if runs.count == 1, runs[0].read == "line", let from = runs[0].from, let to = runs[0].to { abs(to[0] - from[0]) > DeckKnobs.swipe } else { false }default: runs.compactMap(\.letter) == [wrote] && !runs.contains { $0.read == "clear" || $0.read == "mark" } } }/// RULE: the core tells the shell its knobs once, and Cotton and each of its own apps are the flavours that read ink (owner, 11 Nov 2026: ink searches every Cotton app); Rigby, Cotton Project and the business apps never do. @MainActorfunc testTheKnobsComeFromTheCore() asyncthrows {let knobs: InkKnobs.Values = tryawait XCTUnwrap(core).call("ink.knobs", as: InkKnobs.Values.self)// Which apps read ink is the alphabet's (crates/cotton-core/src/ink/alphabet.json), held against the registry in Rust; never listed here. XCTAssertTrue(knobs.flavours.contains("cotton"), "\(knobs.flavours)")for app in knobs.flavours { XCTAssertTrue(InkGate.opens(flavour: app, flavours: knobs.flavours)) }for other in ["rigby", "development"] { XCTAssertFalse(InkGate.opens(flavour: other, flavours: knobs.flavours), other) } XCTAssertFalse(InkGate.opens(flavour: nil, flavours: knobs.flavours), "a bundle with no manifest (the Mac app, a test host)") XCTAssertEqual(knobs.inkWidth, 3) XCTAssertLessThan(knobs.provisionalInk, knobs.doubtfulInk) }}