Cotton code
201 lines · 13.3 KB · SwiftRaw
// The one-sheet in its lobby frame: the marquee says the film's title, year// and length (from its minutes), or a series' seasons and episodes as the// deck counted them, taken down before it wraps and the title first to go// to a line of its own; the strip is filled as far as the resume point is// of the length; a watched film wears the stub, stamped and dated; a series// fans its newest three seasons, the newest at the back; a film kept// without a poster types its one-sheet; the parts tagged; every word a// line or the card's; the frame hangs on the velvet and cuts.@testable import CottonFacesimport CottonPaperimport CottonSpecimensimport CottonValuesimport SwiftUIimport XCTest/// The parts a drawn face tagged.private final class PosterPartsTagged {    var parts: Set<FacePart> = []}@MainActorfinal class FilmPosterTests: XCTestCase {    private typealias Catalogue = FilmPosterFaceCatalogue    private typealias Knobs = FilmPosterFaceKnobs    private let join = ShellLines.line("cards", "join")    private func film(_ status: String, _ fields: [String: RecordValue], kind: String = "film",                      related: [(String, String, Int)] = []) -> DeckCard {        Catalogue.film(status, fields, kind: kind, related: related)    }    /// A film being watched.    private func film(_ fields: [String: RecordValue], kind: String = "film", related: [(String, String, Int)] = []) -> DeckCard {        film("watching", fields, kind: kind, related: related)    }    /// RULE: The marquee is the card's title, its year and its length said from its minutes in the film's lines, joined as the cards join words; on one line when it fits.    func testTheMarqueeSaysTheFilmFromItsCard() {        let length = ShellLines.line("cards", "film.length_hours", ["hours": "1", "minutes": "52"])        XCTAssertEqual(length, "1 h 52")        XCTAssertEqual(FilmPosterSetting.length(Catalogue.poster), length)        XCTAssertEqual(FilmPosterSetting.marquee(Catalogue.poster).lines, ["Night Ferry" + join + "2019" + join + length])        XCTAssertEqual(FilmPosterSetting.marquee(Catalogue.poster).size, Knobs.marqueeSize, "a marquee that fits is set at its own size")        // Under an hour, minutes; whole hours keep two figures of minutes.        XCTAssertEqual(FilmPosterSetting.length(film(["limit": .number(48)])),                       ShellLines.line("cards", "film.length_minutes", ["minutes": "48"]))        XCTAssertEqual(FilmPosterSetting.length(film(["limit": .number(125)])), "2 h 05")        XCTAssertNil(FilmPosterSetting.length(film([:])), "no minutes, no length")        // Nothing kept but the title: the title alone.        XCTAssertEqual(FilmPosterSetting.marquee(film(["title": .text("Fog")])).lines, ["Fog"])        XCTAssertEqual(FilmPosterSetting.marquee(film([:])).lines, [])    }    /// RULE: A marquee too long is taken down before it wraps; past the least size the title takes the first line alone (cut if it must be) and the year and length go under it, both inside the marquee.    func testTheMarqueeShrinksBeforeItWraps() {        let (lines, size) = FilmPosterSetting.marquee(Catalogue.longestTitle)        XCTAssertEqual(lines.count, 2)        XCTAssertEqual(lines.last, "2017" + join + "3 h 07")        XCTAssertGreaterThanOrEqual(size, Knobs.marqueeLeast)        XCTAssertLessThanOrEqual(CGFloat(lines.count) * size * Knobs.marqueeStep, Knobs.marquee.height + 0.5)        for line in lines {            XCTAssertLessThanOrEqual(HighlightPassageSetting.wide(line, FilmPosterSetting.caslon(size)), Knobs.marquee.width + 0.5)        }        // A title a little long is only taken down, never wrapped.        let shrunk = FilmPosterSetting.marquee(film(["title": .text("The Harbour Lamp and the Fog Bell"), "year": .text("2019"),                                                     "limit": .number(112)]))        XCTAssertEqual(shrunk.lines.count, 1)        XCTAssertLessThan(shrunk.size, Knobs.marqueeSize)    }    /// RULE: The strip is filled as far as the resume point (seconds) is of the length (minutes); a film not begun, watched or a series' wears none.    func testTheStripIsFilledByThePosition() throws {        XCTAssertEqual(try XCTUnwrap(FilmPosterSetting.share(Catalogue.halfWatched)), 0.5, accuracy: 0.0001)        XCTAssertEqual(try XCTUnwrap(FilmPosterSetting.share(film(["used": .number(672), "limit": .number(112)]))), 0.1, accuracy: 0.0001)        XCTAssertEqual(FilmPosterSetting.share(film(["used": .number(9000), "limit": .number(112)])), 1, "past the end, full")        XCTAssertNil(FilmPosterSetting.share(Catalogue.poster), "not begun")        XCTAssertNil(FilmPosterSetting.share(film(["used": .number(0), "limit": .number(112)])))        XCTAssertNil(FilmPosterSetting.share(film(["used": .number(600)])), "no length, no share")        XCTAssertNil(FilmPosterSetting.share(film("watched", ["used": .number(600), "limit": .number(112)])), "watched wears the stub")        XCTAssertNil(FilmPosterSetting.share(film(["used": .number(600), "limit": .number(112)], kind: "series")))    }    /// RULE: A film watched by its status wears the stub, stamped in the film's line and typed with the day it was watched, bare of its weekday; any other status, or a series, none.    func testTheStubIsByTheStatus() throws {        let stub = try XCTUnwrap(FilmPosterSetting.stub(Catalogue.watched))        XCTAssertEqual(stub.stamp, ShellLines.line("cards", "film.stub").uppercased())        XCTAssertEqual(stub.stamp, "WATCHED")        XCTAssertEqual(stub.day, "4 NOV")        XCTAssertNil(try XCTUnwrap(FilmPosterSetting.stub(film("watched", ["date": .text(ShellLines.line("cards", "date.add"))]))).day,                     "watched with no day kept: stamped, not dated")        for status in ["want_to_watch", "watching", "stopped"] {            XCTAssertNil(FilmPosterSetting.stub(film(status, ["date": .text("Wed 4 Nov")])), status)        }        XCTAssertNil(FilmPosterSetting.stub(Catalogue.largest), "a series wears no stub")    }    /// RULE (Cinemas, 5 Nov 2026): A film still converting on the Mac says so before any tap: its state stores `needs_mac` ahead of its status, and the stub is stamped CONVERTING from the film's line, with no day.    func testAFilmConvertingOnTheMacWearsTheConvertingStub() throws {        let stub = try XCTUnwrap(FilmPosterSetting.stub(Catalogue.converting))        XCTAssertEqual(stub.stamp, ShellLines.line("cards", "film.stub_converting").uppercased())        XCTAssertEqual(stub.stamp, "CONVERTING")        XCTAssertNil(stub.day)        XCTAssertTrue(FilmPosterSetting.converting(Catalogue.converting))        XCTAssertFalse(FilmPosterSetting.converting(Catalogue.watched))    }    /// RULE: A series fans as many season cards as it has, three at most and the newest three of more, laid back, left, right with the newest at the back, each typed with its season in the film's line; a film, or a series with no seasons counted, none.    func testTheSeasonsAreCountedAndOrdered() {        XCTAssertEqual(FilmPosterSetting.seasons(Catalogue.series), [3, 1, 2])        XCTAssertEqual(FilmPosterSetting.seasons(Catalogue.seriesOneSeason), [1])        XCTAssertEqual(FilmPosterSetting.seasons(film([:], kind: "series", related: [("film_season", "2 seasons", 2)])), [2, 1])        XCTAssertEqual(FilmPosterSetting.seasons(Catalogue.largest), [7, 5, 6])        XCTAssertEqual(FilmPosterSetting.seasons(film([:], kind: "series")), [])        XCTAssertEqual(FilmPosterSetting.seasons(film([:], kind: "series", related: [("film_season", "0 seasons", 0)])), [])        XCTAssertEqual(FilmPosterSetting.seasons(film([:], related: [("film_season", "3 seasons", 3)])), [], "a film fans none")        XCTAssertEqual(FilmPosterSetting.season(2), ShellLines.line("cards", "film.season", ["n": "2"]).uppercased())        XCTAssertEqual(FilmPosterSetting.season(2), "SEASON 2")    }    /// RULE: A series' marquee is its title and the seasons and episodes the deck counted, in the deck's own words; never a count it did not read.    func testTheSeriesMarqueeSaysItsCounts() {        XCTAssertEqual(FilmPosterSetting.marquee(Catalogue.series).lines, ["Night Ferry" + join + "3 seasons" + join + "24 episodes"])        XCTAssertEqual(FilmPosterSetting.marquee(Catalogue.largest).lines.last, "7 seasons" + join + "68 episodes")        XCTAssertEqual(FilmPosterSetting.marquee(film(["title": .text("Fog"), "year": .text("2020")], kind: "series")).lines, ["Fog"],                       "a series says no year and no count it was not given")    }    /// RULE: A film kept without a poster types its one-sheet's title on one line taken down until it fits, then on two balanced lines, never more, past the least size cut with an ellipsis, inside the sheet's room.    func testTheOneSheetShrinksBeforeItWraps() {        let (short, size) = FilmPosterSetting.oneSheet(film(["title": .text("Fog")]))        XCTAssertEqual(short, ["Fog"])        XCTAssertEqual(size, Knobs.oneSheetSize)        let (one, taken) = FilmPosterSetting.oneSheet(Catalogue.noPoster)        XCTAssertEqual(one, ["The Fog Bell"], "taken down to one line before it wraps")        XCTAssertLessThan(taken, Knobs.oneSheetSize)        let (two, _) = FilmPosterSetting.oneSheet(film(["title": .text("The Harbour Lamp at Midnight")]))        XCTAssertEqual(two.count, 2, "too long for one line at the least size: two balanced lines")        XCTAssertFalse(two.last?.hasSuffix("\u{2026}") ?? true)        let (long, least) = FilmPosterSetting.oneSheet(Catalogue.longestTitle)        XCTAssertEqual(long.count, Knobs.oneSheetLines)        XCTAssertEqual(long.last?.last, "\u{2026}", "too long even at the least size: cut")        XCTAssertGreaterThanOrEqual(least, Knobs.oneSheetLeast)        for line in long + one {            XCTAssertLessThanOrEqual(HighlightPassageSetting.wide(line, FilmPosterSetting.caslon(least)), Knobs.oneSheet.width + 0.5)        }        XCTAssertEqual(FilmPosterSetting.oneSheet(film([:])).lines, [])    }    /// RULE: The face tags the poster as its picture and the marquee as its title; a part-watched film its strip as its state, a watched one its stub as done, a series its season cards as its body.    func testTheFrameTagsItsParts() {        let poster = FixturePictures.image("poster-ferry")        XCTAssertNotNil(poster)        func parts(_ card: DeckCard, poster: Image?) -> Set<FacePart> {            let tagged = PosterPartsTagged()            let drawn = FilmPosterFace(card: card, poster: poster)                .frame(width: 340, height: 451)                .backgroundPreferenceValue(FacePartsKey.self) { regions in                    let _ = tagged.parts.formUnion(regions.map(\.part))                    Color.clear                }            XCTAssertNotNil(ImageRenderer(content: drawn).cgImage)            return tagged.parts        }        XCTAssertEqual(parts(Catalogue.poster, poster: poster), [.picture, .title])        XCTAssertEqual(parts(Catalogue.noPoster, poster: nil), [.picture, .title])        XCTAssertEqual(parts(Catalogue.halfWatched, poster: poster), [.picture, .title, .state])        XCTAssertEqual(parts(Catalogue.watched, poster: poster), [.picture, .title, .done])        XCTAssertEqual(parts(Catalogue.series, poster: poster), [.picture, .title, .body])    }    /// RULE: The frame stays the same size watched or not, its stub kept inside the card; a series' cards fan inside the card too, the frame smaller for them.    func testTheFrameKeepsItsSizeAndItsReachInsideTheCard() {        let film = FilmPosterSetting.reach(series: false)        XCTAssertTrue(film.contains(Knobs.stub))        XCTAssertTrue(film.contains(Knobs.shadow))        let series = FilmPosterSetting.reach(series: true)        XCTAssertLessThan(series.minY, Knobs.seasons[0].rect.minY + 1, "the back card's top is inside")        XCTAssertLessThan(series.minX, Knobs.seasons[1].rect.minX, "the left card, turned, reaches past its rect")        XCTAssertGreaterThan(series.height, film.height)    }    /// RULE: The frame hangs bare on the cinema's velvet under its house light, draws its own sheet and is stepped by a cut; its specimens show a film with a poster and without, half watched, watched, a series of three seasons and of one, the longest title and the fullest; every material it lays is in the catalogue.    func testTheFrameHangsOnTheVelvetAndCuts() {        XCTAssertEqual(FaceRegistry.ground("film.poster"), .velvet)        XCTAssertEqual(FaceRegistry.stock("film.poster"), .own)        XCTAssertEqual(Catalogue.face.steps, .cut)        XCTAssertEqual(Catalogue.face.specimens.map(\.name),                       ["poster", "no-poster", "half-watched", "watched", "converting", "series", "series-one-season", "longest-title", "largest"])        XCTAssertNil(Catalogue.face.specimens.first { $0.name == "no-poster" }?.picture)        XCTAssertTrue(FaceGround.velvet.dark)        XCTAssertEqual(FaceGround.velvet.tile, "velvet")        XCTAssertEqual(FaceGround.velvet.colour, CinemasMaterial.velvet.colour)        XCTAssertEqual(FaceGround.velvet.lamp, FaceGroundLamp(name: "house-light", rect: CGRect(x: -250, y: -280, width: 1024, height: 1024), strength: 0.42))        XCTAssertNil(FaceGround.velvet.overlay)        for name in ["frame", "frame-shadow", "progress-strip", "stub", "stub-shadow", "season-card", "velvet", "house-light"] {            XCTAssertNotNil(CottonMedia.image(name, scale: 3), "\(name) is not in the catalogue")        }    }}