#if canImport(UIKit)
import CottonValues
import ImageIO
import SwiftUI
import UniformTypeIdentifiers
import XCTest
@testable import CottonUI
/// A capture utility, not a guard: with `FILM_FACES_OUT=
` set (through
/// xcodebuild, `TEST_RUNNER_FILM_FACES_OUT`) it draws the episode face on
/// the numbers of the Codex round 2 design (series-1) — on its stock, a 360 x 450 card at scale 3 — into ``
/// so they are laid beside the designs. Every title and name is invented.
/// With `FACES_ART` (passed as `TEST_RUNNER_FACES_ART`) each keeps its
/// design's still, else none. Without the out variable it skips.
@MainActor
final class FilmDesignShotsTests: XCTestCase {
func testDrawTheFilmDesigns() throws {
guard let dir = ProcessInfo.processInfo.environment["FILM_FACES_OUT"], !dir.isEmpty else {
throw XCTSkip("set FILM_FACES_OUT= to draw the film designs")
}
let out = URL(fileURLWithPath: dir, isDirectory: true)
try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true)
// No still: the episode draws its television, never a real actor's likeness.
let episode = DeckCard(face: .episode, fields: [
"title": .text("Cicadas"), "source": .text("Invented Precinct"), "subtitle": .text("S2 \u{00B7} E3"),
"length": .text("48 min"), "place": .text("aired 2022"), "date": .text("Mon 12 Oct"), "count": .number(4),
])
shoot("series-1", episode, EpisodeFace(card: episode, still: picture(episode)), to: out)
}
/// The record's own picture, as the deck hands it to a face.
private func picture(_ card: DeckCard) -> PagePicture? {
card.text("photo").map { PagePicture(key: $0, edge: DeckFaceKnobs.photoEdge) }
}
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, FacesArt.picture)
.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