Cotton code
140 lines · 7.8 KB · SwiftRaw
// The money's objects: a spend as a till receipt, money in as a cheque with// the bank's CREDITED stamp, an account as its card with the balance first;// each reads the card the money tool's views already resolve, tags the parts// the shell edits, lies on the money's navy felt and is stepped by a cut.@testable import CottonFacesimport CottonPaperimport CottonValuesimport SwiftUIimport XCTest@MainActorfinal class MoneyFacesTests: XCTestCase {    /// The parts `view` tags when drawn at a phone's card.    private final class Tagged { var parts: Set<FacePart> = [] }    private func parts<Drawn: View>(_ view: Drawn) -> Set<FacePart> {        let tagged = Tagged()        let drawn = view            .frame(width: 340, height: 451)            .backgroundPreferenceValue(FacePartsKey.self) { regions in                let _ = tagged.parts.formUnion(regions.map(\.part))                Color.clear            }        XCTAssertNotNil(ImageRenderer(content: drawn).cgImage)        return tagged.parts    }    /// RULE: A transaction's balance before and after are read off its facts: by their columns' labels where the deck read them, else the view's two facts in order (before, then after); one fact unlabelled says neither.    func testTheBalancesAreReadOffTheFacts() throws {        let grocer = TransactionReceiptFaceCatalogue.grocer        let both = MoneyReading.balances(grocer)        XCTAssertEqual(both.before?.typed, "1,26,840.00")        XCTAssertEqual(both.after?.typed, "1,24,500.00")        let afterOnly = grocer.reading {            $0.fields["facts"] = .list([.object(["label": .text("Balance after"), "said": .text("\u{20B9}1,24,500")])])            $0.labels = CardLabels(byColumn: ["balance_before_minor": "Balance before", "balance_after_minor": "Balance after"])        }        let labelled = MoneyReading.balances(afterOnly)        XCTAssertNil(labelled.before)        XCTAssertEqual(labelled.after?.typed, "1,24,500.00")        let unlabelled = grocer.reading {            $0.fields["facts"] = .list([.object(["label": .text("Balance after"), "said": .text("\u{20B9}1,24,500")])])        }        XCTAssertNil(MoneyReading.balances(unlabelled).before)        XCTAssertNil(MoneyReading.balances(unlabelled).after)    }    /// RULE: A record's serial is its short id, its last six letters and figures in capitals; what a transaction is for is its description, else its category.    func testTheSerialAndThePurpose() throws {        XCTAssertEqual(MoneyReading.serial(TransactionReceiptFaceCatalogue.grocer), "A04217")        XCTAssertNil(MoneyReading.serial(AccountStatementFaceCatalogue.savings))        XCTAssertEqual(MoneyReading.purpose(TransactionReceiptFaceCatalogue.grocer)?.part, .subtitle)        let category = TransactionReceiptFaceCatalogue.grocer.reading {            $0.fields["subtitle"] = nil            $0.fields["body"] = .text("Food")        }        XCTAssertEqual(MoneyReading.purpose(category)?.words, "Food")        XCTAssertEqual(MoneyReading.purpose(category)?.part, .body)    }    /// RULE: The cheque writes the amount in words over its two rules, each line within the rule, nothing cut inside a word.    func testTheChequeWritesTheAmountInWords() throws {        let words = try XCTUnwrap(MoneyReading.words(try XCTUnwrap(SaidAmount("\u{20B9}340.50"))))        let width: CGFloat = 330 * (ChequeFaceKnobs.wordRules.to - ChequeFaceKnobs.wordRules.from - ChequeFaceKnobs.wordsLead - 0.01)        let lines = ChequeWords.lines(words, font: ChequeWords.font(ChequeFaceKnobs.wordsSize * 330), width: width)        XCTAssertEqual(lines.count, 2, "\(lines)")        XCTAssertEqual(lines.joined(separator: " "), words, "the words are the writer's, whole and in order")    }    /// RULE: The receipt tags the payee, the day, what it was for, the amount and the corner; the cheque the drawer, the day, what it was for and the amount; the account card its name, its bank and its balance.    func testTheFacesTagTheirParts() {        let rent = TransactionReceiptFaceCatalogue.grocer.reading {            $0.corner = "in 9 days"            $0.fields["state"] = .text("Expected")        }        XCTAssertEqual(parts(TransactionReceiptFace(card: rent)), [.title, .when, .subtitle, .amount, .corner, .state])        XCTAssertEqual(parts(TransactionChequeFace(card: TransactionChequeFaceCatalogue.salary)), [.title, .when, .subtitle, .amount])        XCTAssertEqual(parts(AccountStatementFace(card: AccountStatementFaceCatalogue.savings)), [.title, .subtitle, .amount])    }    private final class Regions { var frames: [FacePart: CGRect] = [:] }    /// Where `view` tags each part, drawn at a phone's card.    private func regions<Drawn: View>(_ view: Drawn) -> [FacePart: CGRect] {        let found = Regions()        let drawn = view            .frame(width: 340, height: 451)            .backgroundPreferenceValue(FacePartsKey.self) { regions in                GeometryReader { proxy in                    let _ = regions.forEach { found.frames[$0.part] = proxy[$0.bounds] }                    Color.clear                }            }        XCTAssertNotNil(ImageRenderer(content: drawn).cgImage)        return found.frames    }    /// RULE: A part is tagged where it is drawn — the cheque's date and amount at its right, under the drawer's name; the account card's bank over its name over its balance — so a tap on the phone lands on the part it touches.    func testThePartsAreTaggedWhereTheyAreDrawn() throws {        let cheque = regions(TransactionChequeFace(card: TransactionChequeFaceCatalogue.salary))        let amount = try XCTUnwrap(cheque[.amount])        let when = try XCTUnwrap(cheque[.when])        let title = try XCTUnwrap(cheque[.title])        XCTAssertGreaterThan(amount.minX, 0.6 * 340, "the amount is in its box at the leaf's right")        XCTAssertGreaterThan(when.minX, 0.6 * 340, "the date is on its rule at the leaf's right")        XCTAssertLessThan(title.maxY, amount.minY, "the drawer's name stands over the amount")        let card = regions(AccountStatementFace(card: AccountStatementFaceCatalogue.savings))        let bank = try XCTUnwrap(card[.subtitle])        let name = try XCTUnwrap(card[.title])        let balance = try XCTUnwrap(card[.amount])        XCTAssertLessThan(bank.maxY, name.minY)        XCTAssertLessThan(name.maxY, balance.minY)    }    /// RULE: The money's objects lie bare on its pink terrazzo, laid with its painted tile, print their own sheets, and are stepped by a cut; every painted layer is in the catalogue.    func testTheyLieOnTheTerrazzoAndCut() {        for face in [TransactionReceiptFaceCatalogue.face, TransactionChequeFaceCatalogue.face, AccountStatementFaceCatalogue.face] {            XCTAssertEqual(FaceRegistry.ground(face.id), .terrazzo, face.id)            XCTAssertEqual(FaceRegistry.stock(face.id), .own, face.id)            XCTAssertEqual(face.steps, .cut, face.id)            XCTAssertFalse(face.tagsNoPart, face.id)        }        XCTAssertFalse(FaceGround.terrazzo.dark)        XCTAssertEqual(FaceGround.terrazzo.tile, "money-terrazzo")        for name in ["receipt-paper", "receipt-paper-shadow", "cheque-leaf", "cheque-leaf-shadow", "statement-card",                     "statement-card-shadow", "statement-tab", "statement-tab-shadow", "money-terrazzo"] {            XCTAssertNotNil(CottonMedia.image(name, scale: 3), "\(name) is not in the catalogue")        }    }    /// RULE: The money faces are the money's only faces: the receipt, the cheque and the statement (the plain transaction and account faces left, 3 Nov 2026).    func testTheMoneyFacesAreTheReceiptTheChequeAndTheStatement() {        let ids = Set(FaceRegistry.all.map(\.id))        XCTAssertTrue(ids.isSuperset(of: ["transaction.receipt", "transaction.cheque", "account.statement"]))        XCTAssertTrue(ids.isDisjoint(with: ["transaction", "account"]))    }}