Cotton code
92 lines · 4.4 KB · SwiftRaw
// The passkey card: the site's name heads it with its address under it (the// address heads it when the site gave no name), the account, the note, and// the key's facts struck as stamps; its tap map is the title and the note;// its print does not move with the text size; plain framed paper on the page// ground, Keys' first face.@testable import CottonFacesimport CottonPaperimport CottonSpecimensimport CottonValuesimport SwiftUIimport XCTestprivate final class PasskeyPartsTagged {    var parts: Set<FacePart> = []}@MainActorfinal class PasskeyCardTests: XCTestCase {    private typealias Catalogue = PasskeyCardFaceCatalogue    /// The deck's card on a phone.    private let card = CGSize(width: 340, height: 451)    /// RULE: The site's own name heads the card with its address under it; a site with no name of its own is headed by its address alone; the account, the note and each fact (its label and words, in the core's order) are read as the core said them.    func testTheCardReadsTheSiteTheAccountAndTheStamps() {        let used = PasskeyReading(Catalogue.used)        XCTAssertEqual(used.site, "Example")        XCTAssertEqual(used.address, "example.com")        XCTAssertEqual(used.account, "ada@example.com")        XCTAssertEqual(used.note, "The work account.")        XCTAssertEqual(used.stamps.map(\.label), ["Made", "Last used", "Uses"])        XCTAssertEqual(used.stamps.map(\.words), ["2 Oct 2026", "5 Nov 2026", "12 uses"])        let unnamed = PasskeyReading(Catalogue.unnamed)        XCTAssertEqual(unnamed.site, "login.example.org")        XCTAssertNil(unnamed.address)        XCTAssertNil(unnamed.note)        XCTAssertEqual(PasskeyReading(Catalogue.neverUsed).stamps.map(\.label), ["Made", "Uses"])    }    /// RULE: The card reads only its own places: a credential id, a public key or a sealed private key handed to it under any name is never drawn.    func testNothingOfTheKeyIsDrawn() {        var leaky = Catalogue.used        for slot in ["credential_id", "public_key", "private_key_enc", "mask", "facts_hidden"] {            leaky.fields[slot] = .text("SYNTHETIC-NOT-A-KEY")        }        XCTAssertEqual(bytes(shot(leaky)), bytes(shot(Catalogue.used)))    }    /// RULE: The card's tap map is the title (the site's name) and the body (the note); a card with no note tags no body.    func testTheCardTagsTheTitleAndTheNote() {        func parts(_ drawn: DeckCard) -> Set<FacePart> {            let tagged = PasskeyPartsTagged()            let face = PasskeyCardFace(card: drawn)                .frame(width: card.width, height: card.height)                .backgroundPreferenceValue(FacePartsKey.self) { regions in                    let _ = tagged.parts.formUnion(regions.map(\.part))                    Color.clear                }            XCTAssertNotNil(ImageRenderer(content: face).cgImage)            return tagged.parts        }        XCTAssertEqual(parts(Catalogue.used), [.title, .body])        XCTAssertEqual(parts(Catalogue.unnamed), [.title])    }    /// RULE: Every specimen draws, and print on the card is the card's, never Dynamic Type's: each specimen draws the same at the smallest and the largest text size.    func testEverySpecimenDrawsTheSameAtEveryTextSize() {        for specimen in Catalogue.face.specimens {            let small = SpecimenShot.image(Catalogue.face, specimen, size: card, textSize: .xSmall)            let large = SpecimenShot.image(Catalogue.face, specimen, size: card, textSize: .accessibility5)            XCTAssertNotNil(small, specimen.name)            XCTAssertEqual(bytes(small), bytes(large), specimen.name)        }    }    /// RULE: The passkey card is plain framed paper on the page ground (no painted layer until its design round), registered under passkey.card with its four specimens.    func testThePlainCardStandsOnThePageGround() {        XCTAssertEqual(FaceRegistry.stock("passkey.card"), .framed)        XCTAssertEqual(FaceRegistry.ground("passkey.card"), .paper)        XCTAssertTrue(FaceRegistry.all.contains { $0.id == "passkey.card" })        XCTAssertEqual(Catalogue.face.specimens.map(\.name), ["used", "unnamed", "never-used", "largest"])    }    private func shot(_ drawn: DeckCard) -> CGImage? {        SpecimenShot.image(Catalogue.face, Specimen("shot", drawn), size: card)    }    private func bytes(_ image: CGImage?) -> Data? {        image?.dataProvider?.data as Data?    }}