#if canImport(UIKit)
import CottonValues
import ImageIO
import SwiftUI
import UniformTypeIdentifiers
import XCTest
@testable import CottonUI
/// A capture utility, not a guard: with `SESSION_ARTIFACT_OUT=
` set
/// (through xcodebuild, `TEST_RUNNER_SESSION_ARTIFACT_OUT`) it draws the
/// coding session and artifact faces on the numbers of the house references
/// (session 99, 100; artifact 4/31, 5/33, 90) — each on its stock, a 360 x 450
/// card at scale 3 — into ``, to be laid beside the references. Only the
/// slots the core sends these kinds are filled, and every name is invented.
/// Without the out variable it skips.
@MainActor
final class SessionArtifactShotsTests: XCTestCase {
private func outDir() throws -> URL {
guard let dir = ProcessInfo.processInfo.environment["SESSION_ARTIFACT_OUT"], !dir.isEmpty else {
throw XCTSkip("set SESSION_ARTIFACT_OUT= to draw the session and artifact references")
}
let out = URL(fileURLWithPath: dir, isDirectory: true)
try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true)
return out
}
func testDrawTheSessionReferences() throws {
let out = try outDir()
let tasks = [CardPage(label: "Task list", text: "Read the scheme\nBuild for the simulator\nRun the walk")]
let running = card("session", [
"state": .text("Running"), "title": .text("Rigby card decks"), "date": .text("2 minutes ago"),
"subtitle": .text("Building the Rigby scheme for the simulator"),
"source": .text("mac-d \u{00B7} main \u{00B7} Opus"),
])
let waiting = card("session", [
"state": .text("Waiting"), "title": .text("Phone storage"), "date": .text("8 minutes ago"),
"subtitle": .text("Which window should the phone keep?"),
"source": .text("mac-c \u{00B7} main \u{00B7} Opus"),
])
shoot("session-99", running, SessionFace(card: running, mark: .green, pages: tasks + tasks), to: out)
shoot("session-100", waiting, SessionFace(card: waiting, mark: .red, asking: true, pages: tasks), to: out)
shoot("session-summary", SessionFaceCatalogue.runningSession,
SessionFace(card: SessionFaceCatalogue.runningSession, mark: .green, pages: GalleryFaces.runningTasks), to: out)
shoot("session-failed", SessionFaceCatalogue.failedSession, SessionFace(card: SessionFaceCatalogue.failedSession, mark: .red), to: out)
}
func testDrawTheArtifactReferences() throws {
let out = try outDir()
let planner = card("artifact", [
"title": .text("Trip budget planner"), "source": .text("made with Claude"),
"date": .text("14 Oct 2026"), "group": .text("Travel"),
])
let recipe = card("artifact", [
"title": .text("Sunday dosa batter"), "source": .text("made with Claude"), "date": .text("2 Oct 2026"),
])
let checklist = card("artifact", [
"title": .text("Tax checklist for FY 2025-26"), "source": .text("made with Claude"),
"date": .text("20 Oct 2026"), "group": .text("Finance"),
])
shoot("artifact-4", planner, ArtifactFace(card: planner, picture: GalleryFaces.picture("verandah-wide")), to: out)
shoot("artifact-5", recipe, ArtifactFace(card: recipe, picture: GalleryFaces.picture("form-page")), to: out)
shoot("artifact-90", checklist, ArtifactFace(card: checklist, picture: nil), to: out)
}
private func card(_ face: String, _ fields: [String: RecordValue]) -> DeckCard {
DeckCard(face: face, corner: nil, 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