Cotton code
90 lines · 5.0 KB · SwiftRaw
import CottonFacesimport CottonPaperimport CottonValuesimport SwiftUIimport XCTestfinal class ThreadSlipsTests: XCTestCase {    /// RULE: The deck holds the six last messages in reading order, with    /// each message's own direction, time and sender when supplied.    func testThePairReadsOldestToNewest() {        let lines = ThreadSlipsReading.lines(ThreadSlipsFaceCatalogue.pair)        XCTAssertEqual(lines.map(\.words), ["All clear. Bringing the lemon rice", "Perfect, I'll do the salad", "Call me when free", "See you at 8?"])        XCTAssertEqual(lines.map(\.mine), [false, true, false, false])        XCTAssertEqual(lines.map(\.time), ["11:06 am", "11:40 am", "12:01 pm", "12:06 pm"])    }    /// RULE: A legacy specimen with no clock times, sender names or last    /// direction gains no such fact from its date or unread flag.    func testTheLegacyCardDoesNotInventMetadata() {        var card = DeckCard(face: .threadSlips, fields: [            "subtitle": .text("Waiting on your reply"), "title": .text("Amma"), "body": .text("Did you eat?"),            "unread": .bool(true), "date": .text("Thu 22 Oct"),        ])        card.thread = ThreadHistory(count: "1,204 messages since 2019", earlier: [            .init(id: "1", words: "Reached home?", mine: false),            .init(id: "2", words: "Yes, just now.", mine: true),        ])        let lines = ThreadSlipsReading.lines(card)        XCTAssertEqual(lines.count, 3)        XCTAssertEqual(lines.map(\.words), ["Reached home?", "Yes, just now.", "Did you eat?"])        XCTAssertTrue(lines.allSatisfy { $0.time == nil && $0.sender == nil })        XCTAssertEqual(lines.last?.id, "last-preview")        XCTAssertNil(lines.last?.mine, "an unassigned preview is not a claim about who wrote it")        XCTAssertNil(card.corner, "a boolean unread flag is no unread count")    }    /// RULE: Only five messages before the last join the card; a preview    /// with no history is one slip, and no body or history draws no slip.    func testTheWindowAndSparseCards() {        var card = ThreadSlipsFaceCatalogue.one        card.thread = ThreadHistory(earlier: (0..<8).map { .init(id: String($0), words: String($0), mine: false) })        XCTAssertEqual(ThreadSlipsReading.lines(card).map(\.words), ["3", "4", "5", "6", "7", "The kettle is on."])        card.thread = nil        XCTAssertEqual(ThreadSlipsReading.lines(card).count, 1)        card.fields.removeValue(forKey: "body")        XCTAssertTrue(ThreadSlipsReading.lines(card).isEmpty)    }    /// RULE: The group holds three invented portraits and four named slips;    /// mail has a subject and two slips; the long specimen keeps fourteen lines.    func testTheGroupMailAndLongSpecimens() {        XCTAssertEqual(ThreadSlipsFaceCatalogue.group.entries("people").count, 3)        XCTAssertEqual(ThreadSlipsReading.lines(ThreadSlipsFaceCatalogue.group).map(\.sender), ["Tara", "You", "Dev", "Nila"])        XCTAssertEqual(ThreadSlipsFaceCatalogue.email.text("subject"), "Sunday's drawing class")        XCTAssertEqual(ThreadSlipsReading.lines(ThreadSlipsFaceCatalogue.email).count, 2)        XCTAssertEqual(ThreadSlipsReading.lines(ThreadSlipsFaceCatalogue.long).last?.words.split(separator: "\n").count, 14)        XCTAssertEqual(ThreadSlipsReading.lines(ThreadSlipsFaceCatalogue.six).count, 6)    }    /// RULE: The face tags its name, portrait, slips and count; groups tag    /// their people instead of one picture, and email tags its subject.    @MainActor func testTheDrawnRegionsAreTheRequestedParts() {        func parts(_ card: DeckCard) -> Set<FacePart> {            final class Tagged { var parts: Set<FacePart> = [] }            let tagged = Tagged()            let drawn = ThreadSlipsFace(card: card, portrait: Optional<Color>.none)                .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        }        XCTAssertEqual(parts(ThreadSlipsFaceCatalogue.pair), [.picture, .title, .body, .corner])        XCTAssertEqual(parts(ThreadSlipsFaceCatalogue.group), [.people, .title, .body])        XCTAssertEqual(parts(ThreadSlipsFaceCatalogue.email), [.picture, .title, .body, .subtitle])    }    /// RULE: This face stands bare on Messages' yellow pegboard and changes    /// specimens by a cut; the current thread face keeps its own stock.    func testTheFaceAndGround() {        XCTAssertEqual(FaceRegistry.stock("thread.slips"), .own)        XCTAssertEqual(FaceRegistry.ground("thread.slips"), .pegboard)        XCTAssertFalse(FaceGround.pegboard.dark)        XCTAssertEqual(FaceGround.pegboard.tile, "messages-pegboard")        XCTAssertEqual(ThreadSlipsFaceCatalogue.face.steps, .cut)        XCTAssertEqual(ThreadSlipsFaceCatalogue.face.specimens.map(\.name), ["pair", "group", "no-picture", "group-no-pictures", "email", "one-message", "six", "long"])    }}