// A card's Remove on the deck (`DeckActs.binned`): the receipt says the // core's words and carries an Undo only where the core says the change can // be taken back; a refused Undo is said in the core's line, never swallowed // (Books 1.0.0 (3) and (4) walks, 7 Nov 2026: Undo after a book's Remove on a // signed-in phone was refused and nothing said why). #if os(iOS) import CottonKit import XCTest @testable import CottonUI @MainActor final class DeckBinTests: XCTestCase { private func change(undoable: Bool = true, reason: String? = nil) -> ChangeRecord { ChangeRecord(id: "change-1", byline: "You", before: nil, after: nil, undoable: undoable, reason: reason, said: ChangeSaid(kind: "deleted", said: "Deleted An Invented Book")) } /// Waits for the tasks the receipt's Undo started. private func settle(until done: () -> Bool) async { for _ in 0..<300 where !done() { try? await Task.sleep(nanoseconds: 10_000_000) } } /// RULE: a Remove's receipt says the core's words (never the byline "You") and its Undo restores through the change it names. func testARemovesReceiptSaysTheCoresWordsAndItsUndoRestores() async { let store = BinRecords(change: change(), undo: .success) let toasts = ToastCenter(scheduler: ManualToastScheduler()) await DeckActs.binned(table: "book", id: "b-1", store: store, toasts: toasts) XCTAssertEqual(store.deleted, ["book/b-1"]) XCTAssertEqual(toasts.current?.message, "Deleted An Invented Book") XCTAssertNotNil(toasts.current?.undo, "the core says it can be taken back") toasts.undo() await settle { !store.undone.isEmpty } XCTAssertEqual(store.undone, ["change-1"]) XCTAssertNil(toasts.current, "an Undo that worked says nothing more") } /// RULE: an Undo the core refuses is said in the core's line, as an error; a change the core says cannot be taken back is offered no Undo; a refused Remove is said. func testARefusedUndoOrRemoveIsSaidInTheCoresLine() async { let refusal = "This change has changed since; open the bin to put it back" let store = BinRecords(change: change(), undo: .refused(refusal)) let toasts = ToastCenter(scheduler: ManualToastScheduler()) await DeckActs.binned(table: "book", id: "b-1", store: store, toasts: toasts) toasts.undo() await settle { toasts.current != nil } XCTAssertEqual(toasts.current?.message, refusal) XCTAssertEqual(toasts.current?.tone, .error) let final = BinRecords(change: change(undoable: false, reason: "cannot"), undo: .success) await DeckActs.binned(table: "book", id: "b-2", store: final, toasts: toasts) XCTAssertEqual(toasts.current?.message, "Deleted An Invented Book") XCTAssertNil(toasts.current?.undo, "no Undo the core would refuse") let refused = BinRecords(change: nil, undo: .success) await DeckActs.binned(table: "book", id: "b-3", store: refused, toasts: toasts) XCTAssertEqual(toasts.current?.message, "That book is no longer here") XCTAssertEqual(toasts.current?.tone, .error) } } /// A store whose delete answers `change` (nil: refused) and whose undo answers as told. private final class BinRecords: Records, @unchecked Sendable { enum Undo { case success, refused(String) } private let change: ChangeRecord? private let undoing: Undo private(set) var deleted: [String] = [] private(set) var undone: [String] = [] init(change: ChangeRecord?, undo: Undo) { self.change = change self.undoing = undo } func tableDefinitions() async throws -> [TableDefinition] { [] } func count(table: String) async throws -> Int { 0 } func list(table: String, project: String?, query: String?, page: Int, where filter: [String: RecordValue]) async throws -> RecordPage { RecordPage(records: [], next: nil, total: 0) } func get(table: String, id: String) async throws -> TableRecord? { nil } func create(table: String, data: [String: RecordValue]) async throws -> ChangeRecord { throw RecordsError.unknownRecord(table) } func update(table: String, id: String, patch: [String: RecordValue]) async throws -> ChangeRecord { throw RecordsError.unknownRecord(id) } func delete(table: String, id: String) async throws -> ChangeRecord { deleted.append("\(table)/\(id)") guard let change else { throw CottonCallError(code: "no_longer_here", message: "That book is no longer here") } return change } func reorder(table: String, ids: [String]) async throws -> ChangeRecord { throw RecordsError.noOrderColumn(table) } func link(table: String, id: String, column: String, target: String) async throws -> ChangeRecord { throw RecordsError.unknownRecord(id) } func undo(change: String) async throws -> ChangeRecord { undone.append(change) switch undoing { case .success: return ChangeRecord(id: "change-2", byline: "You", before: nil, after: nil) case .refused(let words): throw CottonCallError(code: "refused", message: words) } } } #endif