// The hi-fi deck and the specimen player it is played under in Loom: it// stands playing or paused by the player's word alone; its knob turns from// its start to its end by the time played over the song's length; its// needles read the player's levels on the painted scale and rest at −40 when// paused; its figures and words are the card's and the lines'; and the// specimen player meters an invented level while it plays. With// `COTTON_FACE_SHOTS=<dir>` set, the deck in each state is written there// beside the face shots.importCottonFacesimportCottonPaperimportCottonSpecimensimportCottonValuesimportSwiftUIimport XCTest#if canImport(UIKit)import UIKit#elseimportAppKit#endif@MainActorfinalclassSongHifiTests: XCTestCase {privatelet t0 = Date(timeIntervalSinceReferenceDate: 800_000_000)privatelet playing = TrackPlay(held: true, playing: true, elapsed: 134, duration: 242, level: TrackLevel(left: 0.8, right: 0.6))/// RULE: The deck plays while the player plays or fetches the song, and stands paused while it holds it paused or holds none of it.func testTheDeckStandsByThePlayersWord() { XCTAssertEqual(HifiDeck.state(.still), .paused) XCTAssertEqual(HifiDeck.state(playing), .playing) XCTAssertEqual(HifiDeck.state(TrackPlay(held: true, loading: true)), .playing) XCTAssertEqual(HifiDeck.state(TrackPlay(held: true, playing: false, elapsed: 134, duration: 242)), .paused) }/// RULE: The knob turns from −135 degrees at nought to +135 at the song's end, by the time played over the length — the player's length where it knows one, else the card's `duration`.func testTheKnobIsTheClock() { XCTAssertEqual(HifiDeck.knob(0, of: 242), -135) XCTAssertEqual(HifiDeck.knob(242, of: 242), 135) XCTAssertEqual(HifiDeck.knob(121, of: 242), 0, accuracy: 1e-9) XCTAssertEqual(HifiDeck.knob(134, of: 242), 14.504132231404952, accuracy: 1e-9, "the layout's own reading at 2:14 of 4:02") XCTAssertEqual(HifiDeck.knob(500, of: 242), 135) XCTAssertEqual(HifiDeck.knob(10, of: 0), -135) XCTAssertEqual(HifiDeck.length(.still, SongHifiFaceCatalogue.harbour), 242) XCTAssertEqual(HifiDeck.length(TrackPlay(held: true, duration: 250), SongHifiFaceCatalogue.harbour), 250) }/// RULE: A turn of the knob seeks by its share of the sweep, the whole sweep the whole song, held to the song.func testATurnOfTheKnobSeeks() { XCTAssertEqual(HifiDeck.turned(100, by: 27, of: 270), 127, accuracy: 1e-9) XCTAssertEqual(HifiDeck.turned(100, by: -270, of: 270), 0) XCTAssertEqual(HifiDeck.turned(100, by: 270, of: 270), 270) }/// RULE: A level of nought to one reads −40 to +3 dB, and a needle stands at its reading on the painted scale: on each painted mark at its decibels, straight between marks.func testANeedleReadsTheLevelOnThePaintedScale() { XCTAssertEqual(HifiDeck.decibels(0), -40) XCTAssertEqual(HifiDeck.decibels(1), 3) XCTAssertEqual(HifiDeck.decibels(0.5), -18.5, accuracy: 1e-9) XCTAssertEqual(HifiDeck.decibels(2), 3) XCTAssertEqual(HifiDeck.needle(-40), -55.5, accuracy: 1e-9) XCTAssertEqual(HifiDeck.needle(-10), 0, accuracy: 1e-9) XCTAssertEqual(HifiDeck.needle(3), 48.6, accuracy: 1e-9) XCTAssertEqual(HifiDeck.needle(-5), 10.65, accuracy: 1e-9, "halfway from −10 to 0") XCTAssertEqual(HifiDeck.needle(-90), -55.5, accuracy: 1e-9)let needles = HifiDeck.needles(playing) XCTAssertEqual(needles[0], HifiDeck.needle(HifiDeck.decibels(0.8)), accuracy: 1e-9) XCTAssertEqual(needles[1], HifiDeck.needle(HifiDeck.decibels(0.6)), accuracy: 1e-9) XCTAssertGreaterThan(needles[0], needles[1], "the louder channel stands further right") }/// RULE: Paused, holding none of the song, or playing with no level metered, both needles rest at −40.func testPausedTheNeedlesRest() {let rest = HifiDeck.needle(-40)var paused = playing paused.playing = false XCTAssertEqual(HifiDeck.needles(paused), [rest, rest]) XCTAssertEqual(HifiDeck.needles(.still), [rest, rest])var unmetered = playing unmetered.level = nil XCTAssertEqual(HifiDeck.needles(unmetered), [rest, rest]) }/// RULE: The meters' figures are typed from the lines — "−40" … "0" … "+3" — and the line under the name is the card's artist and album joined as the cards join words.func testTheFiguresAndWordsAreTheCardsAndTheLines() { XCTAssertEqual(HifiDeck.figure(-40), ShellLines.line("faces", "hifi.below", ["n": "40"])) XCTAssertEqual(HifiDeck.figure(-40), "\u{2212}40") XCTAssertEqual(HifiDeck.figure(0), "0") XCTAssertEqual(HifiDeck.figure(3), "+3")for key in ["decibels", "left", "right", "below", "above", "position", "next", "previous"] { XCTAssertNotEqual(ShellLines.line("faces", "hifi.\(key)"), "faces.hifi.\(key)", "the kit ships faces.hifi.\(key)") } XCTAssertEqual(HifiDeck.byline(SongHifiFaceCatalogue.harbour), "The Low Tide" + ShellLines.line("cards", "join") + "Night Ferry") XCTAssertNil(HifiDeck.byline(DeckCard(face: "song.hifi", corner: nil, pressing: false, pace: nil, plate: nil, fields: [:]))) }/// RULE: The specimen player plays a card's song for its `duration`, and meters an invented level in each channel — the same at the same place, stepped twenty times a second, within the scale — while it plays, and none while paused.func testTheSpecimenPlayerMetersALevel() {let player = SpecimenPlayer(lengths: [242], standing: TrackPlay(held: true, playing: true, elapsed: 134), at: t0)let level = try? XCTUnwrap(player.play(at: t0).level) XCTAssertEqual(level, SpecimenLevels.at(134)) XCTAssertEqual(SpecimenLevels.at(10.01), SpecimenLevels.at(10.04), "one reading for a twentieth of a second") XCTAssertNotEqual(SpecimenLevels.at(10), SpecimenLevels.at(10.5))for step in0..<400 {let reading = SpecimenLevels.at(Double(step) * 0.05) XCTAssert((0...1).contains(reading.left) && (0...1).contains(reading.right)) } player.toggle(at: t0) XCTAssertNil(player.play(at: t0).level) }/// RULE: Every hi-fi specimen names the player's word it is reviewed under, and a playing one is 2:14 into 4:02 with its cover.func testTheSpecimensStandPlayingAndPaused() {let byName = Dictionary(uniqueKeysWithValues: SongHifiFaceCatalogue.face.specimens.map { ($0.name, $0) }) XCTAssertEqual(byName["playing"]?.play.map(HifiDeck.state), .playing) XCTAssertEqual(byName["playing"]?.play?.elapsed, 134) XCTAssertNotNil(byName["playing"]?.picture) XCTAssertEqual(byName["paused"]?.play.map(HifiDeck.state), .paused) XCTAssertNil(byName["no cover"]?.picture) XCTAssertEqual(FaceRegistry.ground("song.hifi"), .tolex) }/// RULE: The deck stands on the music's black tolex, dark, laid with its painted tile and the lamp's glow upper left, at its full strength; of the other grounds only the velvet, the formica, the bedside table, the leather desk and the books' felt have one.func testTheDeckStandsOnTheTolexInTheLamp() { XCTAssertTrue(FaceGround.tolex.dark) XCTAssertEqual(FaceGround.tolex.tile, "tolex") XCTAssertEqual(FaceGround.tolex.lamp?.name, "lamp-glow") XCTAssertEqual(FaceGround.tolex.lamp?.rect, CGRect(x: -250, y: -250, width: 1450, height: 1450)) XCTAssertEqual(FaceGround.tolex.lamp?.strength, 1) XCTAssertNotNil(CottonMedia.image("tolex", scale: 3)) XCTAssertNotNil(CottonMedia.image("lamp-glow", scale: 3))let lit: Set<FaceGround> = [.tolex, .velvet, .formicaCounter, .bedsideTable, .leatherDesk, .feltNavy]for ground inFaceGround.allCases where !lit.contains(ground) { XCTAssertNil(ground.lamp, "\(ground) has no lamp") } }/// RULE: The deck 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 testTheDeckDrawsInEveryState() throws {let out = ProcessInfo.processInfo.environment["COTTON_FACE_SHOTS"].flatMap { $0.isEmpty ? nil : URL(fileURLWithPath: $0, isDirectory: true) }iflet out { tryFileManager.default.createDirectory(at: out, withIntermediateDirectories: true) }let paused = TrackPlay(held: true, playing: false, elapsed: 134, duration: 242)let states: [(String, DeckCard, Bool, TrackPlay)] = [ ("playing", SongHifiFaceCatalogue.harbour, true, playing), ("paused", SongHifiFaceCatalogue.harbour, true, paused), ("no cover", SongHifiFaceCatalogue.harbour, false, playing), ("long title", SongHifiFaceCatalogue.lanterns, false,TrackPlay(held: true, playing: true, elapsed: 312, duration: 403, level: TrackLevel(left: 1, right: 0.95))), ]for size in [DynamicTypeSize.xSmall, .large, .accessibility5] {for (name, card, covered, play) in states {let cover = covered ? FacePicture(key: "poster-ferry", edge: 120) : nillet renderer = ImageRenderer(content: SongHifiFace(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 = 2let image = try XCTUnwrap(renderer.cgImage, "the deck \(name) drew nothing")iflet out { try write(image, to: out.appendingPathComponent("song.hifi-\(name)-\(size).png")) } } } }privatefunc write(_ image: CGImage, to url: URL) throws { #if canImport(UIKit)let data = UIImage(cgImage: image).pngData() #elselet data = NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:]) #endiftry XCTUnwrap(data, "\(url.lastPathComponent) was not encoded").write(to: url) }}