import XCTest@testable importCottonUI/// 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.finalclassInkCaptureTests: 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() asyncthrows {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 in0..<12 { tryawait capture.append(Data("{\"n\":\(index)}\n".utf8), to: url) }var lines = tryString(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}")tryawait capture.append(Data("{\"n\":12}\n".utf8), to: url) lines = tryString(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)")tryFileManager.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" }tryData((0..<6).map(line).joined().utf8).write(to: url) XCTAssertEqual(tryInkCapture.lines(in: url), 6)tryInkCapture.keepLast(2, of: 6, in: url) XCTAssertEqual(tryString(contentsOf: url, encoding: .utf8), line(4) + line(5)) XCTAssertEqual(tryInkCapture.lines(in: url), 2) }}