Cotton code
83 lines · 4.1 KB · SwiftRaw
#if canImport(UIKit)import CottonValuesimport ImageIOimport SwiftUIimport UniformTypeIdentifiersimport XCTest@testable import CottonUI/// A capture utility, not a guard: with `SUBSCRIPTION_SECRET_OUT=<dir>` set/// (through xcodebuild, `TEST_RUNNER_SUBSCRIPTION_SECRET_OUT`) it draws the/// subscription and secret faces on the numbers of the house references/// (subscription 27/57, 28/58, 87; secret 19/49, 20/50, 72, 73) — each as the/// deck draws it (a face with its own sheet bare, every other on its stock),/// a 360 x 450 card at scale 3 — into `<dir>`, to be laid beside the/// references. Only the slots the core sends these kinds are filled, and/// every name, number and site is invented. Without the out variable it/// skips.@MainActorfinal class SubscriptionSecretShotsTests: XCTestCase {    func testDrawTheSubscriptionAndSecretReferences() throws {        guard let dir = ProcessInfo.processInfo.environment["SUBSCRIPTION_SECRET_OUT"], !dir.isEmpty else {            throw XCTSkip("set SUBSCRIPTION_SECRET_OUT=<dir> to draw the subscription and secret references")        }        let out = URL(fileURLWithPath: dir, isDirectory: true)        try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true)        let streaming = card("subscription", [            "title": .text("Streamflix"), "price": .text("\u{20B9}649"), "period": .text("Monthly"),            "next_charge": .text("Mon 2 Nov"), "body": .text("Example Bank credit card"),        ])        let storage = card("subscription", [            "title": .text("Example Cloud 2 TB"), "subtitle": .text("Example Cloud"), "price": .text("\u{20B9}749"),            "period": .text("Monthly"), "next_charge": .text("Wed 28 Oct"),        ])        let cancelled = card("subscription", [            "title": .text("Example Plus"), "price": .text("\u{20B9}299"), "period": .text("Monthly"),            "state": .text("Cancelled"), "body": .text("Example Bank credit card"),        ])        for (name, tag) in [("subscription-57", streaming), ("subscription-58", storage), ("subscription-87", cancelled)] {            shoot(name, tag, SubscriptionFace(card: tag), to: out)        }        let login = card("secret", [            "title": .text("Example Code"), "subtitle": .text("code.example.com"), "body": .text("priya"),        ])        let id = card("secret", [            "state": .text("ID"), "title": .text("Example ID card"), "body": .text("Priya Example"),        ])        let apiKey = card("secret", [            "state": .text("API key"), "title": .text("Example Labs"), "subtitle": .text("console.example.com"),            "body": .text("priya-dev"),        ])        let bankCard = card("secret", [            "state": .text("Card"), "title": .text("Example Bank Regal card"), "subtitle": .text("Example Bank"),            "body": .text("Priya Example"),        ])        for (name, secret) in [("secret-19", login), ("secret-20", id), ("secret-72", apiKey), ("secret-73", bankCard)] {            shoot(name, secret, SecretFace<EmptyView>(card: secret, picture: nil), to: out)        }    }    private func card(_ face: String, corner: String? = nil, _ fields: [String: RecordValue]) -> DeckCard {        DeckCard(face: face, corner: corner, pressing: false, pace: nil, plate: nil, fields: fields)    }    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