// What letters written on the ground narrow (owner, 2 Nov 2026): a filter // by leading letters over the showing deck's titles and a record pick's // names, case and accents aside, the core's (`ink_match`). The letters // themselves — always extended, edited by a short level stroke or a // scratch-out, what a surface admits — are the core's too (`cotton_core::ink`, // its tests); here the readout that draws them, the pick they narrow, and the // marks that act. #if os(iOS) import CottonKit import CottonValues import XCTest @testable import CottonUI @MainActor final class InkNarrowingTests: XCTestCase { // MARK: The readout private let knobs = InkKnobs.Values(flavours: ["cotton", "books"], tapTravel: 10, holdSeconds: 0.3, inkWidth: 3, inkFadeSeconds: 0.6, doubtfulInk: 0.5, provisionalInk: 0.35, keepGroups: 5000) /// `letters` as the core hands them back, the doubted ones by place. private func written(_ letters: String, doubted: Set = []) -> InkLetters { InkLetters(letters: letters, doubted: letters.indices.enumerated().map { doubted.contains($0.offset) }) } /// RULE (design, 3 Nov 2026): the readout sets the letters lower case as read, and says them to VoiceOver from the shipped lines. func testTheReadoutFollowsTheLetters() { let an = written("an") XCTAssertEqual(InkReadout.shown(an, knobs: knobs).map(\.letter), ["a", "n"]) XCTAssertEqual(InkReadout.shown(written("a"), knobs: knobs).map(\.letter), ["a"]) XCTAssertEqual(InkReadout.label(an), "Narrowed to an") XCTAssertEqual(InkReadout.label(InkLetters()), ShellLines.line("cards", "deck.search")) // A letter written in place of one taken back: a new identity, so it fades in. let ar = written("ar") XCTAssertEqual(InkReadout.shown(ar, knobs: knobs).map(\.letter), ["a", "r"]) XCTAssertNotEqual(InkReadout.shown(ar, knobs: knobs).last?.id, InkReadout.shown(an, knobs: knobs).last?.id) XCTAssertEqual(InkReadout.shown(an, knobs: nil), [], "no knobs read, no ink, nothing to set") } /// RULE (owner, 3 Nov 2026: "a bit of a lag"): the letter in progress stands after the letters at once, in the lighter `provisional_ink`, with the identity it will have once read, so it turns final in place. func testTheLetterInProgressShowsProvisionally() { let c = written("c") let shown = InkReadout.shown(c, provisional: ["a"], knobs: knobs) XCTAssertEqual(shown.map(\.letter), ["c", "a"]) XCTAssertEqual(shown.map(\.ink), [1, knobs.provisionalInk]) XCTAssertLessThan(knobs.provisionalInk, knobs.doubtfulInk) let ca = written("ca") XCTAssertEqual(InkReadout.shown(ca, knobs: knobs).last?.id, shown.last?.id, "the provisional a becomes the a in place") XCTAssertEqual(InkReadout.label(c), "Narrowed to c", "the letter in progress narrows nothing") } /// RULE (owner, 3 Nov 2026: "the 3rd letter was not recognized"): a letter the model doubts stands in the pill in the lighter `doubtful_ink` and narrows as any other; VoiceOver says which is doubtful; a tap takes it back as any other. func testADoubtfulLetterIsShownLighterAndNarrows() { let cat = written("cat", doubted: [2]) XCTAssertEqual(InkReadout.shown(cat, knobs: knobs).map(\.ink), [1, 1, knobs.doubtfulInk]) XCTAssertLessThan(knobs.doubtfulInk, 1) XCTAssertEqual(InkReadout.label(cat), "Narrowed to cat, t doubtful") let ca = written("ca") XCTAssertEqual(InkReadout.shown(ca, knobs: knobs).map(\.ink), [1, 1]) XCTAssertEqual(InkReadout.label(ca), "Narrowed to ca") } /// RULE (owner, 3 Nov 2026): the pill holding letters is their control — a tap takes the last back, a hold clears them all, and VoiceOver offers both in the shipped lines; the plain disc's tap is the typed search. func testThePillTakesBackOrClears() { XCTAssertEqual(InkReadout.edit("an", held: false), .backspace) XCTAssertEqual(InkReadout.edit("an", held: true), .clear) XCTAssertNil(InkReadout.edit("", held: false), "the plain disc opens the search") XCTAssertNil(InkReadout.edit("", held: true)) XCTAssertEqual(ShellLines.line("cards", "deck.take_back"), "Take back the last letter") XCTAssertEqual(ShellLines.line("cards", "deck.clear_letters"), "Clear the letters") XCTAssertEqual(DeckEditableKnobs.hold, 0.45) } /// RULE: what is showing changing empties the letters (the deck's reading changes to fresh letters), and fresh letters are the plain disc. func testFreshLettersAreThePlainDisc() { XCTAssertTrue(InkReadout.shown(InkLetters(), knobs: knobs).isEmpty) } // MARK: A record pick on the fan private var todo: TableDefinition { var definition = TableDefinition( id: "todo", name: "Todo", plural: "Todos", columns: [ColumnDefinition(key: "title", type: .text)], title: "title", slip: SlipLayout(title: "title"), tile: TileLayout(face: .slipList), page: []) definition.commands = [invite] return definition } private let invite: RecordCommand = { var command = RecordCommand(id: "invite", label: "People", verb: "create_link", kind: .create) command.pick = RecordCommandPick(argument: "toId", kind: .record, table: "person") return command }() private var person: TableDefinition { var definition = TableDefinition( id: "person", name: "Person", plural: "People", columns: [ColumnDefinition(key: "name", type: .text)], title: "name", slip: SlipLayout(title: "name"), tile: TileLayout(face: .slipList), page: []) definition.role = .person return definition } private let target = DeckEditTarget(table: "todo", record: "t1", date: nil) /// The core's person pick, narrowed as `ink_match` narrows it: the people /// a word of whose name starts with the letters. private func picking() async throws -> (DeckEditor, InputCore) { let core = InputCore { method, request in guard method == "shell.opened", request["step"]?.string == "pick" else { return .failure(CottonCallError(code: "missing", message: "Not here")) } let ink = request["ink"]?.string ?? "" let people = [("p-asha", "Asha Kumar"), ("p-ravi", "Ravi Sharma"), ("p-sita", "Sita Rao")].filter { _, name in ink.isEmpty || name.lowercased().split(separator: " ").contains { $0.hasPrefix(ink) } } let rows = people.map { id, name in #"{"value":"\#(id)","words":"\#(name.split(separator: " ")[0])","does":"answer"}"# } return .success(InputCore.json(#"{"step":{"label":"People","index":0,"table":"person","writes":{"kind":"pick","table":"todo","act":"invite","answers":[],"id":"t1"},"rows":[\#(rows.joined(separator: ","))]}}"#)) } let place = DeckPickPlace(store: nil, core: core, definitions: TableDefinitions([todo, person])) let editor = DeckEditor.standing() editor.openActs(target, record: TableRecord(id: "t1", values: [:])) let asked = await editor.act(invite, on: target, definition: todo, place: place) XCTAssertEqual(asked, .asking) return (editor, core) } private func people(_ editor: DeckEditor) -> [String] { (editor.picking?.options ?? []).compactMap { if case .answer = $0.does { $0.word } else { nil } } } /// RULE: letters written over a person pick are asked of the core, which narrows its people by the start of a word of their names (`ink_match`); a backspace or a clear asks it again with the letters left. func testLettersNarrowAPersonPick() async throws { let (editor, core) = try await picking() XCTAssertTrue(editor.picksRecords) XCTAssertEqual(people(editor), ["Asha", "Ravi", "Sita"]) await editor.narrowPick(to: written("s")) XCTAssertEqual(people(editor), ["Ravi", "Sita"], "S keeps Ravi Sharma and Sita Rao") XCTAssertEqual(core.asked("shell.opened").last?["ink"], "s") await editor.narrowPick(to: written("sh")) XCTAssertEqual(people(editor), ["Ravi"], "SH keeps Sharma alone") XCTAssertEqual(core.asked("shell.opened").last?["ink"], "sh") await editor.narrowPick(to: written("s")) XCTAssertEqual(people(editor), ["Ravi", "Sita"], "a backspace widens them to S again") XCTAssertEqual(editor.picking?.ink.letters, "s") await editor.narrowPick(to: InkLetters()) XCTAssertEqual(people(editor), ["Asha", "Ravi", "Sita"], "a clear widens them all again") XCTAssertNil(core.asked("shell.opened").last?["ink"]) } /// RULE (owner, 4 Nov 2026: "if there's zero results, we should show zero results"): a letter that leaves no one is taken all the same — the pick offers no one and keeps every letter written; a backspace brings the people back. func testALetterLeavingNoOneIsKept() async throws { let (editor, _) = try await picking() await editor.narrowPick(to: written("r")) XCTAssertEqual(people(editor), ["Ravi", "Sita"]) await editor.narrowPick(to: written("rz")) XCTAssertEqual(people(editor), [], "no one starts with rz") XCTAssertEqual(editor.picking?.ink.letters, "rz", "the letter is kept") await editor.narrowPick(to: written("r")) XCTAssertEqual(people(editor), ["Ravi", "Sita"]) XCTAssertEqual(editor.picking?.ink.letters, "r") } // MARK: Marks act (owner, 3 Nov 2026: "marking a todo as done is just having a check mark") /// The kit's own todo and person tables, as the build ships them. private func kit(_ id: String) throws -> TableDefinition { let json = try SourceSweep.mustRead("crates/cotton-core/kits/personal/tables/" + id + ".json", ownedBy: "ink") return try JSONDecoder().decode(TableDefinition.self, from: Data(json.utf8)) } private let doneAnswer = #"{"command":"done","kind":"update","receipt":"Done","undo":"reopen","answer":{"change":{"id":"c-done","table":"todo","record_id":"t1","verb":"update","byline":"user","before":{"status":"open"},"after":{"status":"done"},"undoable":true},"record":{"id":"t1","table":"todo","data":{"title":"Water the synthetic fern","status":"done"}}}}"# /// RULE: a check drawn on an open todo's card runs the command the todo binds to it (`marks.check`: `done`) on that record, through the card's acts' own path — the core's command, the change landed on the deck, the save's ink mark — and the record reads done; a second check on it, done now, runs nothing (`done`'s `when`). func testACheckOnATodoCardRunsDone() async throws { let todo = try kit("todo") XCTAssertEqual(todo.marks, ["check": "done"]) let core = ScriptedCore(["records.command": .ok(doneAnswer)]) let place = DeckPickPlace(store: CoreRecords(core: core, bundle: try TestResources.bundle()), core: core, definitions: TableDefinitions([todo])) let editor = DeckEditor.standing() let open = TableRecord(id: "t1", values: ["title": .text("Water the synthetic fern"), "status": .text("open")]) let outcome = await editor.act(mark: "check", on: open, definition: todo, place: place) guard case .ran(let change) = outcome else { return XCTFail("the check ran nothing: \(outcome)") } XCTAssertEqual(change.after?["status"], .text("done"), "the record reads done") XCTAssertEqual(editor.landed?.table, "todo") XCTAssertEqual(editor.landed?.change?.after?["status"], .text("done")) XCTAssertEqual(editor.marked?.record, "t1", "the save leaves its ink mark, as the acts do") let asked = await core.calls let calls = asked.filter { $0.method == "records.command" } XCTAssertEqual(calls.count, 1) XCTAssertTrue(calls.first?.requestJSON.contains(#""done""#) == true, calls.first?.requestJSON ?? "") XCTAssertTrue(calls.first?.requestJSON.contains(#""t1""#) == true, calls.first?.requestJSON ?? "") let done = try XCTUnwrap(change.after) let second = await editor.act(mark: "check", on: done, definition: todo, place: place) XCTAssertEqual(second, .none) let askedAgain = await core.calls let again = askedAgain.filter { $0.method == "records.command" } XCTAssertEqual(again.count, 1, "a check on a todo already done runs nothing") } /// RULE: a check on a card whose table binds no mark (a person) runs nothing, and a mark no table binds (a name the ink may read one day) runs nothing on a todo either. func testACheckOnAPersonCardDoesNothing() async throws { let person = try kit("person") XCTAssertNil(person.marks?["check"]) let core = ScriptedCore(["records.command": .ok(doneAnswer)]) let place = DeckPickPlace(store: CoreRecords(core: core, bundle: try TestResources.bundle()), core: core, definitions: TableDefinitions([person])) let editor = DeckEditor.standing() let someone = TableRecord(id: "p1", values: ["name": .text("Olwen Madeup")]) let onPerson = await editor.act(mark: "check", on: someone, definition: person, place: place) XCTAssertEqual(onPerson, .none) let open = TableRecord(id: "t1", values: ["status": .text("open")]) let unbound = await editor.act(mark: "circle", on: open, definition: try kit("todo"), place: place) XCTAssertEqual(unbound, .none) let calls = await core.calls XCTAssertEqual(calls.filter { $0.method == "records.command" }.count, 0) XCTAssertNil(editor.marked) } /// RULE: a mark acts on the card only where it lands on it — the middle of its strokes within the card's frame or on a sheet of its paper; drawn above the arc away from the card it is nothing. func testAMarkLandsOnTheCardByItsMiddle() { let paper = [CGRect(x: 20, y: 100, width: 350, height: 420)] let onCard = [CGPoint(x: 150, y: 300), CGPoint(x: 170, y: 320), CGPoint(x: 220, y: 250)] let offCard = onCard.map { CGPoint(x: $0.x, y: $0.y - 260) } XCTAssertTrue(DeckInk.lands(onCard, on: paper)) XCTAssertFalse(DeckInk.lands(offCard, on: paper)) XCTAssertFalse(DeckInk.lands(onCard, on: []), "no card, no landing") } /// RULE (owner, 4 Nov 2026): every letter is taken, and the core narrows the deck by them (`shell.deck {ink}`), counting and saying what they left — "Nothing starts with …" where they leave no card. func testTheDeckTakesEveryLetterAndTheCoreNarrows() throws { let surface = try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Deck/DeckSurface.swift", ownedBy: "ink") XCTAssertTrue(surface.contains("ink = answer.letters"), "the deck takes every letter the core holds") // The letters' change narrows the deck; the two lines between keep the card in front before the first letter (8a90ae6a02). let narrows = try XCTUnwrap(surface.range(of: ".onChange(of: ink.letters) { before, letters in")) XCTAssertTrue(surface[narrows.upperBound...].split(separator: "\n", maxSplits: 4).prefix(4).contains { $0.trimmingCharacters(in: .whitespaces) == "Task { await model?.narrow(letters) }" }) XCTAssertTrue(surface.contains("accessibilityIdentifier(\"deck-narrowed-nothing\")")) } } #endif