Cotton code
92 lines · 4.6 KB · SwiftRaw
// Every registered face drawn in every specimen it is reviewed in, on a Mac,// with no simulator and no core, the way the Loom app draws it// (`SpecimenShot`): each specimen's card, its fixture picture drawn from the// bundled fixtures (`FixturePictures`), its long words cut by the plain// cutter (`PlainPageCutter`), no taps, at the deck's card size. A specimen// that draws nothing fails. With `COTTON_FACE_SHOTS=<dir>` set,// each is also written there as `<face>-<specimen>.png` at the phone's scale,// so a face is looked at before it ships.import CottonFacesimport CottonPaperimport CottonSpecimensimport SwiftUIimport XCTest#if canImport(UIKit)    import UIKit#else    import AppKit#endif@MainActorfinal class FaceShotsTests: XCTestCase {    /// The deck's card, as the person face's reference shows it.    private var size: CGSize {        guard let raw = ProcessInfo.processInfo.environment["COTTON_FACE_SHOT_HEIGHT"], let height = Double(raw) else {            return CGSize(width: 340, height: 451)        }        return CGSize(width: height * 0.8, height: height)    }    /// RULE: Every face Loom holds is reviewed in at least one specimen.    func testEveryFaceHasASpecimen() {        XCTAssertFalse(FaceRegistry.all.isEmpty, "the registry holds no face, so this proved nothing")        for face in FaceRegistry.all {            XCTAssertFalse(face.specimens.isEmpty, "\(face.id) has no specimen")        }    }    /// RULE: Every specimen of every registered face draws, from its card and its fixture picture alone, with no core.    func testEverySpecimenDraws() throws {        let out = ProcessInfo.processInfo.environment["COTTON_FACE_SHOTS"].flatMap { $0.isEmpty ? nil : URL(fileURLWithPath: $0, isDirectory: true) }        if let out { try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true) }        // Drawing the projected Canvas tread changes SwiftUI's subsequent        // image-resampling rounding by one RGB level on the Mac. Keep the        // ordinary faces before that renderer, preserving their baseline bytes,        // and People's rolodex, the first that turned, before the rolodexes        // that wear its file after it, for the same reason. The party, the        // turntable drawn over its drifting lights, does the same to the        // turntable's covered label drawn after it: it comes after the rest.        // The cinema's one-sheet, its season cards and stub turned on the        // velvet, moves the video's sleeve, the lab print and the        // membership card drawn after it by one level too: it comes last of        // the still faces. The episode's lobby card, its stub turned on the        // same velvet, comes after it, so neither moves the other, and the        // season's card, its stub turned there too, after both. The        // music's tray, its whole drawing tilted in perspective, moves the        // hardback's paper and the turntable's paused label drawn after it:        // it comes last of the still faces, the album's before the song's.        let turning = FaceRegistry.all.filter { $0.steps == .turns }        let last = [            SongPartyFaceCatalogue.face.id, FilmPosterFaceCatalogue.face.id, EpisodeLobbyFaceCatalogue.face.id,            SeasonCardFaceCatalogue.face.id,            AlbumTrayFaceCatalogue.face.id, SongTrayFaceCatalogue.face.id,        ]        let faces = FaceRegistry.all.filter { $0.steps != .turns && !last.contains($0.id) }            + last.compactMap { id in FaceRegistry.all.first { $0.id == id } }            + turning.filter { $0.id == RolodexFaceCatalogue.face.id }            + turning.filter { $0.id != RolodexFaceCatalogue.face.id }        for face in faces {            for specimen in face.specimens {                let name = "\(face.id)-\(specimen.name)"                if let picture = specimen.picture {                    XCTAssertNotNil(FixturePictures.image(picture), "\(name) shows \(picture), which the fixtures do not hold")                }                guard let image = SpecimenShot.image(face, specimen, size: size) else {                    XCTFail("\(name) drew nothing")                    continue                }                if let out { try write(image, to: out.appendingPathComponent("\(name).png")) }            }        }    }    private func write(_ image: CGImage, to url: URL) throws {        #if canImport(UIKit)            let data = UIImage(cgImage: image).pngData()        #else            let data = NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:])        #endif        try XCTUnwrap(data, "\(url.lastPathComponent) was not encoded").write(to: url)    }}