import XCTest#if canImport(UIKit)import UIKit#endif@testable importCottonUI/// Home's board of bottle caps (`Paper/ProjectCaps.swift`): which cap/// a project gets, how many bare caps follow, and that every cap the code/// names is a picture the bundle carries.finalclassProjectCapsTests: XCTestCase {// ── Which cap ─────────────────────────────────────────────────────────func testTheSameSlugsAreTheSameBoardWhateverOrderTheyArrive() {let a = CapColour.assign(["cotton", "quillby", "tinmen", "triva"])let b = CapColour.assign(["triva", "tinmen", "quillby", "cotton"]) XCTAssertEqual(a, b) }/// RULE: While an enamel is free no two projects share one — a shared cap reads as one project.func testTwelveProjectsTakeTwelveDifferentEnamels() {let enamels = CapColour.allCases.countlet slugs = (1...enamels).map { "project-\($0)" }let picks = Set(CapColour.assign(slugs).values) XCTAssertEqual(picks.count, enamels, "two projects share an enamel while one is still free") }/// RULE: A project past the last free enamel shares one rather than going without a cap.func testAThirteenthProjectSharesRatherThanFailing() {let enamels = CapColour.allCases.countlet slugs = (1...(enamels + 1)).map { "project-\($0)" }let assigned = CapColour.assign(slugs) XCTAssertEqual(assigned.count, slugs.count) XCTAssertEqual(Set(assigned.values).count, enamels) }func testAProjectAloneTakesTheEnamelItsSlugNames() {// Alone, nothing is taken, so the hash's own pick stands.let slug = "cotton"let expected = CapColour.allCases[Int(CapColour.fnv1a(slug) % 12)] XCTAssertEqual(CapColour.assign([slug])[slug], expected) }func testTheHashIsFNV1aNotTheSeededHasher() {// The published test vectors, so a launch-seeded `Hasher` sneaking in// here fails loudly rather than reshuffling the board every launch. XCTAssertEqual(CapColour.fnv1a(""), 0xcbf2_9ce4_8422_2325) XCTAssertEqual(CapColour.fnv1a("a"), 0xaf63_dc4c_8601_ec8c) }// ── How many bare caps ────────────────────────────────────────────────// ── The pictures ──────────────────────────────────────────────────────// ── The faces ─────────────────────────────────────────────────────────/// Every family ships under the SIL Open Font License, and the licence/// sits beside the files — the terms of the OFL, not a nicety.func testEveryFontFamilyCarriesItsLicenceBesideTheFiles() {let fonts = SourceSweep.repoRoot.appendingPathComponent("apps/loom/Sources/CottonPaper/Resources/Fonts")for family in ["InstrumentSerif", "Fraunces", "CourierPrime", "Caveat", "Montserrat", "BarlowCondensed"] {let file = fonts.appendingPathComponent("OFL-\(family).txt") XCTAssertTrue(FileManager.default.fileExists(atPath: file.path), "OFL-\(family).txt is not beside the faces") } }/// EVERY imageset declares a rendition the Mac can compile. A 3x-only one/// does not survive actool on macOS: it is dropped from `Assets.car`/// entirely, so the picture is not missing at RUNTIME, it is missing from/// the build — which is how the grain went out in a shipped app.func testEveryImagesetCarriesARenditionMacOSCanTake() throws {let catalogue = SourceSweep.repoRoot .appendingPathComponent("apps/loom/Sources/CottonPaper/Media.xcassets")let sets = tryFileManager.default.contentsOfDirectory( at: catalogue, includingPropertiesForKeys: nil ).filter { $0.pathExtension == "imageset" } XCTAssertFalse(sets.isEmpty, "the sweep read no imagesets — it proves nothing")var dropped: [String] = []for set in sets {let data = tryData(contentsOf: set.appendingPathComponent("Contents.json"))let json = try XCTUnwrap(JSONSerialization.jsonObject(with: data) as? [String: Any])let images = try XCTUnwrap(json["images"] as? [[String: Any]])let reachable = images.contains { image inlet scale = image["scale"] as? Stringlet idiom = image["idiom"] as? String ?? "universal"return (scale == "1x" || scale == "2x") && (idiom == "universal" || idiom == "mac") }if !reachable { dropped.append(set.deletingPathExtension().lastPathComponent) } } XCTAssertEqual( dropped.sorted(), [],""" these imagesets declare no 1x or 2x rendition, so actool drops them when it \ compiles for macOS and the Mac draws nothing where they are named — add a \ `mac` twin of the same file at 2x, as `paper-grain` does """ ) }/// A PNG's pixel size from its IHDR chunk — the first chunk, at a fixed/// offset — so the check runs the same on the Mac lane, without UIKit.privatestaticfunc pngSize(_ url: URL) throws -> (Int, Int) {let data = tryData(contentsOf: url) XCTAssertGreaterThanOrEqual(data.count, 24, "\(url.lastPathComponent) is not a PNG")let be = { (at: Int) -> Intin data[at..<at + 4].reduce(0) { ($0 << 8) | Int($1) } }return (be(16), be(20)) }}