import CottonKit import CottonValues import Foundation import XCTest @testable import CottonUI /// S7: what is archived can be found again. The list of the archived reads /// the archived and brings one back; search marks an archived hit. The /// "Archived · n ›" link is the core's (`shell_list_tests.rs`). @MainActor final class ArchivedFoundAgainTests: XCTestCase { private static let places = TableDefinition( id: "location", name: "Place", plural: "Places", columns: [ColumnDefinition(key: "name", type: .text), ColumnDefinition(key: "archived_at", type: .date)], title: "name", slip: SlipLayout(title: "name"), tile: TileLayout(face: .slipList), page: [PageSection(heading: nil, columns: ["name"])], archived: "archived_at") private static func place(_ id: String, archived: Bool = false) -> TableRecord { var values: [String: RecordValue] = ["name": .text(id)] if archived { values["archived_at"] = .text("2026-10-05T09:00:00.000Z") } return TableRecord(id: id, values: values) } /// RULE (shell-list-reader, 7 Nov 2026): The list of the archived asks the store's archived read, and bringing one back writes the cleared column and takes it off that list at once. func testTheArchivedListReadsTheArchivedAndBringsThemBack() async throws { let store = ArchivingRecords(live: [Self.place("Boathouse")], archived: [Self.place("Allotment", archived: true)]) let model = RecordListModel(definition: Self.places, store: store, core: ListCoreFake(titleColumn: "name"), archived: true, seeds: ListSeeds()) await model.reload(project: nil, query: "") let asked = await store.archivedReads XCTAssertEqual(asked, 1) XCTAssertEqual(model.records.map(\.id), ["Allotment"]) let record = try XCTUnwrap(model.records.first) // Bring back's write is the core's (`press[].write`): the column cleared. try await model.write(record, ["archived_at": .null]) XCTAssertTrue(model.records.isEmpty, "brought back, it leaves the list of the archived") let patch = await store.patches.last XCTAssertEqual(patch?["archived_at"], .null) } /// RULE: A list of the archived is a route of its own (?archived=1 on /lists/), so Back from a record opened in it comes back to it. func testTheArchivedListIsARoute() throws { let opening = FilteredListOpening(table: "location", filter: [:], title: "Archived · Places", archived: true) let route = opening.route XCTAssertTrue(route.path.contains("archived=1"), route.path) guard case .tableList(let table, _, let title, _, let archived, _, _)? = Route.parse(route.path) else { return XCTFail("not a list route: \(route.path)") } XCTAssertEqual(table, "location") XCTAssertEqual(title, "Archived · Places") XCTAssertTrue(archived) XCTAssertFalse(FilteredListOpening(table: "location", filter: [:], title: "").route.path.contains("archived")) } /// RULE: Search finds what is archived and says so: the core's caption ("Archived · Junk") is the line a hit's card reads, drawn as answered. func testASearchHitReadsTheCaptionTheCoreWorded() throws { let hit = FindHit(JSONValue.object([ "kind": .string("email"), "id": .string("m1"), "title": .string("Synthetic"), "caption": .string("Archived \u{00B7} Junk"), ])) XCTAssertEqual(hit.caption, "Archived \u{00B7} Junk") XCTAssertNil(FindHit(JSONValue.object(["kind": .string("location"), "id": .string("p1")])).caption) } } /// A store holding some live and some archived places: its ordinary read /// answers the live, its count read how many are archived, its archived /// read the archived. private actor ArchivingRecords: Records { var live: [TableRecord] var archived: [TableRecord] private(set) var archivedReads = 0 private(set) var patches: [[String: RecordValue]] = [] init(live: [TableRecord], archived: [TableRecord]) { self.live = live self.archived = archived } /// A record brought back somewhere else: off the archived, onto the live. func bringBack(_ id: String) { guard let index = archived.firstIndex(where: { $0.id == id }) else { return } var record = archived.remove(at: index) record.values["archived_at"] = nil live.append(record) } func tableDefinitions() async throws -> [TableDefinition] { [] } func count(table: String) async throws -> Int { live.count } func lastChanged(table: String) async throws -> String? { nil } func list(table: String, project: String?, query: String?, page: Int, where filter: [String: RecordValue]) async throws -> RecordPage { RecordPage(records: live, next: nil, total: live.count) } func archivedCount( table: String, project: String?, query: String?, where filter: [String: RecordValue] ) async throws -> Int? { archived.count } func listArchived( table: String, project: String?, query: String?, page: Int, where filter: [String: RecordValue] ) async throws -> RecordPage { archivedReads += 1 return RecordPage(records: archived, next: nil, total: archived.count) } func get(table: String, id: String) async throws -> TableRecord? { (live + archived).first { $0.id == id } } func create(table: String, data: [String: RecordValue]) async throws -> ChangeRecord { throw RecordsError.unknownTable(table) } func update(table: String, id: String, patch: [String: RecordValue]) async throws -> ChangeRecord { patches.append(patch) guard let before = (live + archived).first(where: { $0.id == id }) else { throw RecordsError.unknownTable(table) } var after = before for (key, value) in patch { after.values[key] = value } // The row moves between the live and the archived, as in a database. live.removeAll { $0.id == id } archived.removeAll { $0.id == id } if case .null? = patch["archived_at"] { live.append(after) } else if patch["archived_at"] != nil { archived.append(after) } else { live.append(after) } return ChangeRecord(id: "change-\(patches.count)", byline: "You", before: before, after: after) } func delete(table: String, id: String) async throws -> ChangeRecord { throw RecordsError.unknownTable(table) } func reorder(table: String, ids: [String]) async throws -> ChangeRecord { throw RecordsError.unknownTable(table) } func link(table: String, id: String, column: String, target: String) async throws -> ChangeRecord { throw RecordsError.unknownTable(table) } func undo(change: String) async throws -> ChangeRecord { throw RecordsError.unknownChange(change) } }