import XCTest @testable import CottonUI /// What the phone keeps of the ink (owner, 3 Nov 2026): every group read is /// one JSON line in Application Support — the line is the core's /// (`cotton_core::ink::captured`, held by its tests) — capped at the core's /// `keep_groups`. Synthetic lines only. final class InkCaptureTests: XCTestCase { /// RULE: the file keeps the last `keep_groups` lines, the oldest dropped; it is cut back only past a tenth more, so a write is an append. func testTheFileKeepsTheLastGroups() async throws { let folder = FileManager.default.temporaryDirectory.appendingPathComponent("ink-capture-\(UUID().uuidString)") defer { try? FileManager.default.removeItem(at: folder) } let url = folder.appendingPathComponent(InkCapture.fileName) let capture = InkCapture(keep: 10) for index in 0..<12 { try await capture.append(Data("{\"n\":\(index)}\n".utf8), to: url) } var lines = try String(contentsOf: url, encoding: .utf8).split(separator: "\n").map(String.init) XCTAssertEqual(lines.count, 10, "past 10 and a tenth: cut back to the last 10") XCTAssertEqual(lines.first, "{\"n\":2}") try await capture.append(Data("{\"n\":12}\n".utf8), to: url) lines = try String(contentsOf: url, encoding: .utf8).split(separator: "\n").map(String.init) XCTAssertEqual(lines.count, 11, "within a tenth more, appended") XCTAssertEqual(lines.last, "{\"n\":12}") } /// RULE: the file is counted and cut a piece at a time, never read whole: lines longer than a piece, and a cut that falls past the first piece, keep exactly the last lines. func testTheFileIsCutAPieceAtATime() throws { let folder = FileManager.default.temporaryDirectory.appendingPathComponent("ink-capture-\(UUID().uuidString)") try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: folder) } let url = folder.appendingPathComponent(InkCapture.fileName) let line = { (n: Int) in "{\"n\":\(n),\"pad\":\"\(String(repeating: "x", count: InkCapture.piece / 2 + 7))\"}\n" } try Data((0..<6).map(line).joined().utf8).write(to: url) XCTAssertEqual(try InkCapture.lines(in: url), 6) try InkCapture.keepLast(2, of: 6, in: url) XCTAssertEqual(try String(contentsOf: url, encoding: .utf8), line(4) + line(5)) XCTAssertEqual(try InkCapture.lines(in: url), 2) } }