Cotton code
153 lines · 9.0 KB · SwiftRaw
// The amp head: its big knob turns across layout.json's sweep by the time// played over the song's length, and steps between the painted ticks under// Reduce Motion; its jewel is lit by the player's word alone; its record// turns at 33⅓ while it plays and stands under Reduce Motion; its small// knobs read the card's volume, else seven o'clock; its words are the card's// and the lines'. With `COTTON_FACE_SHOTS=<dir>` set, the amp in each state// is written there beside the face shots.import CottonFacesimport CottonPaperimport CottonSpecimensimport CottonValuesimport SwiftUIimport XCTest#if canImport(UIKit)    import UIKit#else    import AppKit#endif@MainActorfinal class SongAmpTests: XCTestCase {    private let playing = TrackPlay(held: true, playing: true, elapsed: 134, duration: 242)    private let paused = TrackPlay(held: true, playing: false, elapsed: 134, duration: 242)    /// RULE: The big knob turns from −90 degrees at nought to +90 at the song's end, by the time played over the length, held to the sweep.    func testTheBigKnobIsTheClock() {        XCTAssertEqual(AmpHead.knob(0, of: 242), -90)        XCTAssertEqual(AmpHead.knob(242, of: 242), 90)        XCTAssertEqual(AmpHead.knob(121, of: 242), 0, accuracy: 1e-9)        XCTAssertEqual(AmpHead.knob(134, of: 242), 9.669421487603302, accuracy: 1e-9, "layout.json's own reading at 2:14 of 4:02")        XCTAssertEqual(AmpHead.knob(500, of: 242), 90)        XCTAssertEqual(AmpHead.knob(10, of: 0), -90)    }    /// RULE: Under Reduce Motion the big knob steps: it stands on the painted tick at or before its reading, never between ticks.    func testUnderReduceMotionTheKnobSteps() {        XCTAssertEqual(AmpHead.knob(134, of: 242, stepped: true), 7.1, accuracy: 1e-9, "9.67° stands on the tick at 7.1°")        XCTAssertEqual(AmpHead.knob(0, of: 242, stepped: true), -90)        XCTAssertEqual(AmpHead.knob(242, of: 242, stepped: true), 90)        XCTAssertEqual(AmpHead.knob(121, of: 242, stepped: true), 0, accuracy: 1e-9)        XCTAssertEqual(AmpHead.knob(133, of: 242, stepped: true), AmpHead.knob(134, of: 242, stepped: true), "a second on, the same tick")    }    /// RULE: A turn of the big knob seeks by its share of the sweep, the whole sweep the whole song, held to the song.    func testATurnOfTheKnobSeeks() {        XCTAssertEqual(AmpHead.turned(100, by: 18, of: 180), 118, accuracy: 1e-9)        XCTAssertEqual(AmpHead.turned(100, by: -180, of: 180), 0)        XCTAssertEqual(AmpHead.turned(100, by: 180, of: 180), 180)    }    /// RULE: The jewel is lit while the player plays or fetches the song, and dark while it holds it paused or holds none of it.    func testTheJewelIsLitByThePlayersWord() {        XCTAssertEqual(AmpHead.state(.still), .paused)        XCTAssertEqual(AmpHead.state(playing), .playing)        XCTAssertEqual(AmpHead.state(TrackPlay(held: true, loading: true)), .playing)        XCTAssertEqual(AmpHead.state(paused), .paused)    }    /// RULE: The record turns at 33⅓ turns a minute of the time played while it plays, stands where it stopped when paused, and never turns under Reduce Motion.    func testTheRecordTurnsWhilePlaying() {        XCTAssertTrue(AmpHead.turning(playing, reduceMotion: false))        XCTAssertFalse(AmpHead.turning(paused, reduceMotion: false))        XCTAssertFalse(AmpHead.turning(.still, reduceMotion: false))        XCTAssertFalse(AmpHead.turning(playing, reduceMotion: true))        XCTAssertEqual(AmpHead.record(0, reduceMotion: false), 0)        XCTAssertEqual(AmpHead.record(1.8, reduceMotion: false), 0, accuracy: 1e-9, "one whole turn in 1.8 seconds")        XCTAssertEqual(AmpHead.record(0.45, reduceMotion: false), 90, accuracy: 1e-9)        XCTAssertEqual(AmpHead.record(134, reduceMotion: false), (134 * 200).truncatingRemainder(dividingBy: 360), accuracy: 1e-9)        XCTAssertEqual(AmpHead.record(134, reduceMotion: true), 0)    }    /// RULE: The volume knob reads the card's `volume` across seven o'clock to five, and stands at seven o'clock where the card carries none; tone stands where it is painted.    func testTheSmallKnobsReadTheCard() {        XCTAssertEqual(AmpHead.volume(SongAmpFaceCatalogue.lanterns), -150)        XCTAssertEqual(AmpHead.volume(SongAmpFaceCatalogue.harbour), -150 + 300 * 0.7, accuracy: 1e-9)        let loud = DeckCard(face: .songAmp, fields: ["volume": .number(3)])        XCTAssertEqual(AmpHead.volume(loud), 150)    }    /// RULE: Every word on the amp is the card's or a line's: "ARTIST · ALBUM" in capitals on the strip, the blank label's artist then album, and VOLUME and TONE from the kit's lines.    func testTheWordsAreTheCardsAndTheLines() {        XCTAssertEqual(AmpHead.byline(SongAmpFaceCatalogue.harbour), ("The Low Tide" + ShellLines.line("cards", "join") + "Night Ferry").uppercased())        XCTAssertNil(AmpHead.byline(DeckCard(face: .songAmp, fields: [:])))        XCTAssertEqual(AmpHead.labelLines(SongAmpFaceCatalogue.harbour), ["The Low Tide", "Night Ferry"])        XCTAssertEqual(AmpHead.labelLines(DeckCard(face: .songAmp, fields: ["album": .text("Paper Moons")])), ["Paper Moons"])        for key in ["volume", "tone"] {            XCTAssertNotEqual(ShellLines.line("faces", "amp.\(key)"), "faces.amp.\(key)", "the kit ships faces.amp.\(key)")        }        for key in ["position", "next", "previous"] {            XCTAssertNotEqual(ShellLines.line("faces", "hifi.\(key)"), "faces.hifi.\(key)", "the kit ships faces.hifi.\(key)")        }    }    /// RULE: Every amp specimen names the player's word it is reviewed under, a playing one 2:14 into 4:02 with its cover, and the amp stands on the music's black tolex with every layer it lays in the catalogue.    func testTheSpecimensAndTheirMaterials() {        let byName = Dictionary(uniqueKeysWithValues: SongAmpFaceCatalogue.face.specimens.map { ($0.name, $0) })        XCTAssertEqual(byName["playing"]?.play.map(AmpHead.state), .playing)        XCTAssertEqual(byName["playing"]?.play?.elapsed, 134)        XCTAssertEqual(byName["playing"]?.play?.duration, 242)        XCTAssertNotNil(byName["playing"]?.picture)        XCTAssertEqual(byName["paused"]?.play.map(AmpHead.state), .paused)        XCTAssertNil(byName["no cover"]?.picture)        XCTAssertNotNil(byName["long title"])        XCTAssertEqual(FaceRegistry.ground("song.amp"), .tolex)        XCTAssertEqual(FaceRegistry.stock("song.amp"), .own)        for name in [            "amp-frame", "amp-panel", "amp-ticks", "amp-knob-big", "amp-knob-small", "amp-grille", "amp-jewel-on", "amp-jewel-off",            "amp-shadow", "record", "label-blank", "sleeve-front", "sleeve-blank", "sleeve-shadow",        ] {            XCTAssertNotNil(CottonMedia.image(name, scale: 3), "\(name) is in the catalogue")        }    }    /// RULE: The amp draws playing, paused, with no cover and with a long name, from its card and the player's word alone, at every text size.    func testTheAmpDrawsInEveryState() 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) }        let states: [(String, DeckCard, Bool, TrackPlay)] = [            ("playing", SongAmpFaceCatalogue.harbour, true, playing),            ("paused", SongAmpFaceCatalogue.harbour, true, paused),            ("no cover", SongAmpFaceCatalogue.harbour, false, TrackPlay(held: true, playing: true, elapsed: 61, duration: 242)),            ("long title", SongAmpFaceCatalogue.lanterns, false, TrackPlay(held: true, playing: true, elapsed: 312, duration: 403)),        ]        for size in [DynamicTypeSize.xSmall, .large, .accessibility5] {            for (name, card, covered, play) in states {                try shoot(card, covered: covered, play: play, size: size, name: "\(name)-\(size)", out: out)            }        }    }    private func shoot(_ card: DeckCard, covered: Bool, play: TrackPlay, size: DynamicTypeSize, name: String, out: URL?) throws {        let cover = covered ? FacePicture(key: "poster-ferry", edge: 120) : nil        let renderer = ImageRenderer(content: SongAmpFace(card: card, cover: cover, play: play, toggle: {})            .frame(width: 340, height: 451)            .background(FaceGroundSurface(.tolex))            .environment(\.facePictureDrawer, FixturePictures.drawer)            .dynamicTypeSize(size)            .themed(.paper))        renderer.scale = 2        let image = try XCTUnwrap(renderer.cgImage, "the amp \(name) drew nothing")        if let out { try write(image, to: out.appendingPathComponent("song.amp-\(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)    }}