import CottonKit import CoreGraphics import XCTest @testable import CottonUI /// 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`. final class InkTraceTests: XCTestCase { private struct Trace: Decodable { let wrote: String let strokes: [[[Double]]] } private var container: URL? private var core: (any CottonCalling)? override func setUp() async throws { let container = FileManager.default.temporaryDirectory.appendingPathComponent("cotton-ink-\(UUID().uuidString)") self.container = container core = try CottonRuntime.open(CottonConfig(containerPath: container.path, timeZone: "UTC")) InkModel.shipped.load() } override func tearDown() async throws { await core?.close() if let container { try? FileManager.default.removeItem(at: container) } } private func traces() throws -> [Trace] { let url = SourceSweep.repoRoot.appendingPathComponent("crates/cotton-core/tests/fixtures/ink/ink-traces.json") return try 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). private func read(_ trace: Trace, on surface: String) async throws -> (runs: [InkReply.Run], took: [Double]) { let core = try XCTUnwrap(core) var runs: [InkReply.Run] = [], took: [Double] = [] var t = 1_000.0 let clock = ContinuousClock() for stroke in trace.strokes { let (down, up) = (t, t + 0.25) t = up + 0.1 let points = stroke.enumerated().map { [$0.element[0], $0.element[1], down + (up - down) * Double($0.offset) / Double(max(1, stroke.count - 1))] } let json = try InkReader.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 = try await clock.measure { reply = try await InkReader.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 = try InkReader.body(["surface": surface, "now": t + 2]) runs += try await InkReader.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() async throws { var misread: [String] = [], took: [Double] = [], doubtful: [String] = [] for (index, trace) in try traces().enumerated() { let read = try await read(trace, on: "trace:\(index)") took += read.took if 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)") } else if !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") } private static func 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. @MainActor func testTheKnobsComeFromTheCore() async throws { let knobs: InkKnobs.Values = try await 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) } }