Cotton code
57 lines · 2.8 KB · SwiftRaw
// R4 "receipts say what happened" and R2 "the bar's words name only what it// can do there", the Swift leftovers: a Settings switch names its record, a// command's receipt is brought into sight, a new password is answered with a// receipt, the bar on a page says what typing does on that record, and a// confirmed delete closes its page.//// Every record and name here is invented.import CottonValuesimport Foundationimport XCTestimport CottonKit@testable import CottonUI@MainActorfinal class ReceiptsSayWhatHappenedTests: XCTestCase {    /// Read from the source tree, as the other definition tests read them:    /// the package's own resource bundle is not there under `swift test` on a Mac.    private static var resources: Bundle? {        Bundle(url: SourceSweep.iosSources.appendingPathComponent("CottonUI/Resources"))    }    private func bundled(_ id: String) throws -> TableDefinition {        let loaded = FixtureDefinitions.load(from: try XCTUnwrap(Self.resources))        return try XCTUnwrap(loaded.definitions.first { $0.id == id }, "no bundled \(id)")    }    // ── C: a command's receipt is in sight ──    // ── D and E: the bar's words on a page ──    // ── F: Passwords' Keep it ──    /// RULE: Keeping a new password answers a change — the list's receipt says it was added, by name, with an Undo that bins it — never a page opened with no word of what was kept.    func testKeepingANewPasswordWearsAReceipt() async throws {        let (records, core) = try PasswordsRecordsTests.records()        let acts = await records.listActions(table: "secret")        let login = try XCTUnwrap(acts.first { $0.id == "new-login" })        let answers = try XCTUnwrap(login.answers(["name": "Harbour Mutual", "password": "an-invented-password"]))        let outcome = try await records.perform(login, values: answers, table: "secret", record: nil)        guard case .change(let kept) = outcome else { return XCTFail("Keep it answered \(outcome), not a change") }        let definition = try XCTUnwrap(PasswordsRecordsTests.definitions().first { $0.id == "secret" })        let lines = ChangeLines.lines(kept, table: definition).map(\.kind)        XCTAssertEqual(lines.count, 1)        guard case .added(let name)? = lines.first else { return XCTFail("Keep it is not an added line: \(lines)") }        XCTAssertFalse(name.isEmpty, "the receipt names what was kept")        XCTAssertTrue(kept.undoable)        _ = try await records.undo(change: kept.id)        // The bin is the core's (4b2351020): Undo of a Keep it is the core's delete, which bins it.        let binned = await PasswordsRecordsTests.body(core, of: "records.delete")        XCTAssertEqual(binned?["id"] as? String, "s-new", "Undo bins the password just kept")    }    // ── G: a confirmed delete closes its page ──}