119 lines · 7.3 KB · SwiftRaw
// A view's painted fan tile (Round 17, variation b, the owner's pick for Cotton// Books, 5 Nov 2026): a view that names a picture deals the painted card,// its name and count typed live beneath the object; a view naming none deals// today's plain cream card, as every Cotton and Rigby view does.#if os(iOS)import CottonPaperimport ImageIOimport SwiftUIimport UniformTypeIdentifiersimport XCTest@testable import CottonUI@MainActorfinal class ViewTileTests: XCTestCase { private let shelf = [ ToolView(id: "reading", label: "Reading", count: 3, isDefault: true, picture: "open_book", read: .init(table: "synthetic_book")), ToolView(id: "want_to_read", label: "Want to read", count: 5, picture: "ribbon", read: .init(table: "synthetic_book")), ToolView(id: "group:synthetic", label: "Synthetic favourites", count: 1, read: .init(table: "synthetic_book")), ToolView(id: "today", label: "Today", count: 2, read: .init(table: "synthetic_todo")), ] /// RULE: a view naming a picture is dealt as its painted tile (`view-<picture>`); a view naming none is today's plain card. func testAViewsPictureIsItsTile() { let tier = ViewTier.make(tool: "books", views: shelf, current: nil) XCTAssertEqual(tier.cards.map(\.painted), ["view-open_book", "view-ribbon", nil, nil]) XCTAssertEqual(tier.cards.map(\.label), ["Reading", "Want to read", "Synthetic favourites", "Today"]) XCTAssertEqual(tier.cards.map(\.count), [3, 5, 1, 2]) XCTAssertEqual(tier.cards.map(\.list), [nil, nil, ListTilePictures(cover: nil, stack: []), nil], "a list is dealt its list tile") XCTAssertTrue(tier.cards.allSatisfy { $0.style == .leaf && $0.picture == nil && $0.object == nil }) } /// RULE: the core's `shell.fan` says a view's picture, and a view that names none reads as none. func testTheCoreSaysAViewsPicture() throws { struct Answer: Decodable { let views: [ToolView] } let json = #""" {"views": [ {"id": "reading", "label": "Reading", "count": 3, "default": true, "picture": "open_book", "read": {"table": "book"}}, {"id": "today", "label": "Today", "count": 2, "read": {"table": "todo"}} ]} """# let answer = try JSONDecoder().decode(Answer.self, from: Data(json.utf8)) XCTAssertEqual(answer.views.map(\.picture), ["open_book", nil]) } /// RULE: every picture a kit view names is kept in the catalogue as `view-<picture>`, and only Books' views name one, so Cotton's and Rigby's other tiles are unchanged. func testEveryNamedPictureIsInTheCatalogueAndOnlyBooksNamesOne() throws { let tools = SourceSweep.repoRoot.appendingPathComponent("crates/cotton-core/kits/personal/tools") let catalogue = SourceSweep.repoRoot.appendingPathComponent("apps/loom/Sources/CottonPaper/Media.xcassets") var named: [String: [String]] = [:] for tool in try FileManager.default.contentsOfDirectory(atPath: tools.path) { let manifest = tools.appendingPathComponent("\(tool)/manifest.json") guard let data = FileManager.default.contents(atPath: manifest.path), let object = try JSONSerialization.jsonObject(with: data) as? [String: Any] else { continue } let pictures = (object["views"] as? [[String: Any]] ?? []).compactMap { $0["picture"] as? String } if !pictures.isEmpty { named[tool] = pictures } } XCTAssertEqual(Array(named.keys), ["books"], "only Books' views carry painted tiles") XCTAssertEqual(named["books"], ["open_book", "ribbon", "banded_book", "slipped_book", "pencil", "keypad_phone", "book_pile"]) for picture in named.values.flatMap({ $0 }) { let asset = ViewTile.asset(picture) XCTAssertTrue(FileManager.default.fileExists(atPath: catalogue.appendingPathComponent("\(asset).imageset/\(asset).png").path), asset) XCTAssertNotNil(CottonMedia.image(asset), "\(asset) is in the bundled catalogue") } } /// RULE: the fan draws a painted view tile as its picture, the object at its head where the plain card is cream; a view without a picture draws as today. The specimen: with `VIEW_TILE_SHOTS_OUT` set, the seven Books tiles and a plain one are written as one PNG. func testThePaintedTileIsDrawnAndThePlainOneIsNot() throws { let geometry = FanGeometry(scale: Theme.paper.scale) func card(_ view: ToolView) -> some View { let dealt = ViewTier.make(tool: "books", views: [view], current: nil).cards[0] return FanTierCard(look: .init(card: dealt, raised: false, width: geometry.tileWidth, height: geometry.tileHeight)) .environment(\.theme, Theme.paper) } func drawn(_ view: some View) throws -> CGImage { let renderer = ImageRenderer(content: view) renderer.scale = 3 return try XCTUnwrap(renderer.cgImage) } let painted = try drawn(card(shelf[0])) let plain = try drawn(card(shelf[3])) // The open book's pages stand a quarter down the card; the plain card is cream there. let head = (x: 0.5, y: 0.25) let book = try XCTUnwrap(pixel(painted, at: head)) let cream = try XCTUnwrap(pixel(plain, at: head)) XCTAssertGreaterThan(zip(book, cream).map { abs(Int($0) - Int($1)) }.max() ?? 0, 12, "the painted object stands where the plain card is cream: \(book) vs \(cream)") guard let dir = ProcessInfo.processInfo.environment["VIEW_TILE_SHOTS_OUT"], !dir.isEmpty else { return } let books = [ ("reading", "Reading", 3, "open_book"), ("want_to_read", "Want to read", 5, "ribbon"), ("finished", "Finished", 4, "banded_book"), ("stopped", "Stopped", 1, "slipped_book"), ("highlights", "Highlights", 9, "pencil"), ("on_phone", "On this phone", 0, "keypad_phone"), ("all", "All", 12, "book_pile"), ].map { ToolView(id: $0.0, label: $0.1, count: $0.2, picture: $0.3, read: .init(table: "synthetic_book")) } let sheet = HStack(spacing: 14) { ForEach(books + [shelf[3]]) { card($0) } } .padding(24) .background(Color(red: 0.06, green: 0.08, blue: 0.16)) let url = URL(fileURLWithPath: dir).appendingPathComponent("view-tiles.png") try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true) let dest = try XCTUnwrap(CGImageDestinationCreateWithURL(url as CFURL, UTType.png.identifier as CFString, 1, nil)) CGImageDestinationAddImage(dest, try drawn(sheet), nil) XCTAssertTrue(CGImageDestinationFinalize(dest)) } /// The RGBA bytes at a point given as shares of the image's size. private func pixel(_ image: CGImage, at point: (x: Double, y: Double)) -> [UInt8]? { let space = CGColorSpaceCreateDeviceRGB() var bytes = [UInt8](repeating: 0, count: 4) guard let context = CGContext(data: &bytes, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: space, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return nil } let x = Double(image.width) * point.x, y = Double(image.height) * (1 - point.y) context.draw(image, in: CGRect(x: -x, y: -y, width: Double(image.width), height: Double(image.height))) return bytes }}#endif