import CottonKit import CottonValues import XCTest @testable import CottonUI @MainActor final class CouldNotReadTests: XCTestCase { /// A could-not-read line of the shell's `reading` set, as the app has it. private func words(_ key: String) -> String { ShellLines.line("reading", "could_not_read.\(key)") } /// A synthetic database whose tools draw AI usage — no tool ships it /// now — made from a kit table the core answered, renamed. private var drawn: TableDefinitions { get throws { let fixture = try XCTUnwrap(try DefinitionCatalogue.fixture(in: CottonUIResources.bundle)) let note = try XCTUnwrap(fixture.tableRows["note"]) func table(_ id: String, _ plural: String, _ extra: [String: Any]) throws -> TableDefinition { var row = try XCTUnwrap(JSONSerialization.jsonObject(with: note) as? [String: Any]) row["id"] = id row["plural"] = plural row.removeValue(forKey: "role") row.merge(extra) { $1 } return try TableDefinition.decode(JSONSerialization.data(withJSONObject: row)) } return TableDefinitions([try table("ai_calls", "AI usage", [:])]) } } /// RULE: a read the core cut (`could_not_read`, details.why busy|too_long) is said in a line of the shell's `reading` set (`could_not_read.*`), filled with the database's name — on every database, whatever tools it holds: the words lived on the `database` definition until it was deleted, and every failed read then drew a blank (Documents on the owner's phone, 22 Oct 2026) — never the core's own sentence, and on Home with no Try again, since Home keeps trying by itself; every other refusal keeps its own path (architect's rulings 21:00 (4) and 22:03 IST 16 Oct 2026: the raw refusal under the caps on 289). func testACutReadIsSaidInTheDefinitionsLineNeverTheCoresSentence() async throws { let shipped = try drawn let busy = CottonCallError(code: "could_not_read", message: "the pool is busy", details: .object(["why": .string("busy")])) let slow = CottonCallError(code: "could_not_read", message: "past the deadline", details: .object(["why": .string("too_long")])) XCTAssertEqual(CouldNotRead.of(busy), .busy) XCTAssertEqual(CouldNotRead.of(slow), .tooLong) XCTAssertNil(CouldNotRead.of(CottonCallError(code: "unavailable", message: "no")), "another refusal keeps its own path") let line = CouldNotRead.tooLong.line(database: "Heron", definitions: shipped) XCTAssertEqual(line, words("too_long").replacingOccurrences(of: "{database}", with: "Heron"), "the set's words, filled") XCTAssertFalse(line.contains("{database}")) XCTAssertFalse(line.contains(slow.message), "never the core's sentence") let bare = CouldNotRead.busy.line(database: "Heron", definitions: TableDefinitions([])) XCTAssertTrue(bare.hasPrefix("Heron") && !bare.contains("could_not_read"), "a database with no tools still says the line: \(bare)") // Home says the core's line (`shell.today`'s `failed`), and offers // Try again only where the core says the read does not keep trying. let notices = SourceSweep.withoutComments( try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Surfaces/Board/BoardNotices.swift", ownedBy: "home")) let cut = try XCTUnwrap(notices.range(of: "if home.answer?.failed?.keeps_trying ?? true {")) let other = try XCTUnwrap(notices.range(of: "} else {", range: cut.upperBound..