Cotton code
80 lines · 4.3 KB · SwiftRaw
#if canImport(UIKit)import CottonValuesimport ImageIOimport SwiftUIimport UniformTypeIdentifiersimport XCTest@testable import CottonUI/// A capture utility, not a guard: with `MEETING_MEMORY_MOOD_OUT=<dir>` set/// (through xcodebuild, `TEST_RUNNER_MEETING_MEMORY_MOOD_OUT`) it draws the/// meeting, memory and mood faces on the numbers of the house references/// (meeting 95, 96; memory 91, 93; mood 97, 98) — each on its stock, a/// 360 x 450 card at scale 3 — into `<dir>`, so they are laid beside the/// references. Each card carries only what the core's card sends for its/// kind (a meeting: title, speakers as a list, date, length, state, and the/// pages it turns to; a memory: project, sentence, date, standing; a mood:/// day, line, emotions as a list); every word is invented. Without the out/// variable it skips.@MainActorfinal class MeetingMemoryMoodShotsTests: XCTestCase {    func testDrawTheMeetingMemoryAndMoodReferences() throws {        guard let dir = ProcessInfo.processInfo.environment["MEETING_MEMORY_MOOD_OUT"], !dir.isEmpty else {            throw XCTSkip("set MEETING_MEMORY_MOOD_OUT=<dir> to draw the meeting, memory and mood references")        }        let out = URL(fileURLWithPath: dir, isDirectory: true)        try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true)        let sync = DeckCard(face: .audio, fields: [            "title": .text("Weekly sync with Ravi"), "date": .text("Tue 21 Oct, 4:00 pm"), "length": .text("42 min"),            "subtitle": .list([.text("Asha"), .text("Ravi")]),        ])        let pages = [            CardPage(label: "What was said", text: "Reviewed this week\u{2019}s progress.\nAgreed on the next priorities.\nRavi will share the updated plan."),            CardPage(label: "Decisions", text: "Ship the export door on Friday.\nRavi owns the updated plan."),            CardPage(label: "Transcript", text: "Ravi 12:04\nLet\u{2019}s review the launch plan.\n\nAsha 12:18\nThe new screens are ready.\n\nRavi 12:32\nCan we test them on Friday?\n\nAsha 12:45\nYes, I\u{2019}ll send the invite."),        ]        shoot("meeting-95", sync, AudioFace(card: sync, peaks: [], preview: pages.first?.text, pages: pages), to: out)        shoot("meeting-96", sync, AudioFace(card: sync, peaks: [], preview: pages.first?.text, pages: pages, opened: 3), to: out)        let rule = DeckCard(face: .memory, fields: [            "subtitle": .text("Cotton"), "date": .text("22 Oct"),            "body": .text("Push straight to main, small and often; fix forward, never revert."),        ])        let kept = DeckCard(face: .memory, fields: [            "subtitle": .text("Personal"), "date": .text("20 Oct"),            "body": .text("Call me Asha; I work from my phone in IST."),        ])        shoot("memory-91", rule, MemoryFace(card: rule), to: out)        shoot("memory-93", kept, MemoryFace(card: kept), to: out)        let evening = DeckCard(face: .mood, fields: [            "date": .text("Wed 22 Oct"), "title": .text("Tired but happy, shipped the export door"),            "subtitle": .list([.text("tired"), .text("proud"), .text("calm")]),        ])        let heavy = DeckCard(face: .mood, fields: [            "date": .text("Mon 20 Oct"), "title": .text("Anxious about the release"),            "subtitle": .list([.text("anxious"), .text("restless")]),        ])        shoot("mood-97", evening, MoodFace(card: evening), to: out)        shoot("mood-98", heavy, MoodFace(card: heavy), to: out)    }    private func shoot(_ name: String, _ card: DeckCard, _ face: some View, to out: URL) {        let theme = Theme.paper        let renderer = ImageRenderer(content: face            .environment(\.theme, theme)            .environment(\.colorScheme, .light)            .environment(\.facePictures) { FixturePictures.image($0) }            .frame(width: 360, height: 450)            .background(theme.colors.paper.color))        renderer.scale = 3        guard let image = renderer.cgImage,              let dest = CGImageDestinationCreateWithURL(out.appendingPathComponent("\(name).png") as CFURL, UTType.png.identifier as CFString, 1, nil)        else { return XCTFail("\(name) drew nothing") }        CGImageDestinationAddImage(dest, image, nil)        XCTAssertTrue(CGImageDestinationFinalize(dest), "\(name) was not written")    }}#endif