import ImageIO
import SwiftUI
import UniformTypeIdentifiers
import XCTest
@testable import CottonUI
/// A capture utility, not a guard: with `COTTON_FACE_SHOTS=
` set (through
/// xcodebuild, `TEST_RUNNER_COTTON_FACE_SHOTS`) it draws the gallery's deck
/// specimens — each face on its stock at the deck's card size, as the deck
/// lays the front card — into `` as PNGs at the phone's scale, so a face
/// is looked at before it ships. Without the variable it skips.
@MainActor
final class FaceShotsTests: XCTestCase {
func testDrawTheDeckSpecimens() throws {
guard let dir = ProcessInfo.processInfo.environment["COTTON_FACE_SHOTS"], !dir.isEmpty else {
throw XCTSkip("set COTTON_FACE_SHOTS= to draw the specimens")
}
let out = URL(fileURLWithPath: dir, isDirectory: true)
try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true)
let faces = GalleryFaces.self
shoot("session", faces.sessionCards, to: out)
shoot("session-tasks", faces.stage(faces.card, SessionFaceCatalogue.runningSession) {
SessionFace(card: SessionFaceCatalogue.runningSession, pages: faces.runningTasks, opened: 1)
}, to: out)
shoot("memory", faces.memoryCards, to: out)
shoot("mood", faces.moodCards, to: out)
shoot("meeting", faces.meetingCards, to: out)
}
private func shoot(_ name: String, _ view: some View, to out: URL) {
let renderer = ImageRenderer(content: view
.environment(\.colorScheme, .light)
.frame(width: 393)
.background(Color(white: 0.93)))
renderer.scale = 2
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")
}
}