151 lines · 9.0 KB · SwiftRaw
// A business look's fan (Codex round 2, "Coral Book / teal selected fan// card: keep"): the tile the page stands on is filled with the look's// current-tile role, and the primary act on the long-press fan is printed on// its primary role, both named in the look's `cards` and read from its// palette. Cotton's and Rigby's themes name neither and draw as before.#if os(iOS)import CottonKitimport SwiftUIimport XCTest@testable import CottonUI@MainActorfinal class FanBrandTests: XCTestCase { /// The business look these tests wear: the kitchen fixture (no app ships a look). private func look(_ name: String) throws -> Theme { GalleryFaces.kitchen.theme } private let width: CGFloat = 90 private func tile(current: Bool) -> some View { ToolCard(key: "schedule", name: "Schedule", picture: nil, width: width, current: current) } /// The pixel at (`x`, `y`) — fractions of the drawing — as 0…255 RGBA. private func pixel(_ view: some View, theme: Theme, size: CGSize, x: CGFloat, y: CGFloat) throws -> [Int] { let renderer = ImageRenderer(content: view.environment(\.theme, theme).environment(\.colorScheme, .light) .frame(width: size.width, height: size.height)) renderer.scale = 1 let image = try XCTUnwrap(renderer.cgImage) var bytes = [UInt8](repeating: 0, count: 4) let context = try XCTUnwrap(CGContext( data: &bytes, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: CGColorSpace(name: CGColorSpace.sRGB)!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)) let px = Int(CGFloat(image.width) * x), py = Int(CGFloat(image.height) * y) context.draw(image, in: CGRect(x: -px, y: -(image.height - 1 - py), width: image.width, height: image.height)) return bytes.map(Int.init) } private func rgba(_ hex: String) throws -> [Int] { let colour = try XCTUnwrap(ThemeColor(hex: hex)) return [colour.red, colour.green, colour.blue, colour.alpha].map { Int(($0 * 255).rounded()) } } private func assertNear(_ got: [Int], _ want: [Int], _ message: String, file: StaticString = #filePath, line: UInt = #line) { XCTAssertTrue(zip(got, want).allSatisfy { abs($0 - $1) <= 3 }, "\(message): \(got) is not \(want)", file: file, line: line) } private var tileSize: CGSize { CGSize(width: width, height: width / 0.75) } /// RULE: the kitchen look (Nancy's, as she shipped it) fills her current tile and prints her primary act on her terracotta (her `accent`), as her Codex boards draw the selected Menu card and Delivered. func testNancysCurrentTileAndPrimaryActAreTerracotta() throws { let theme = try look("kitchen") XCTAssertEqual(theme.cards.currentTileFill, "accent") XCTAssertEqual(theme.cards.primaryActFill, "accent") assertNear(try pixel(tile(current: true), theme: theme, size: tileSize, x: 0.5, y: 0.12), try rgba("#B4532A"), "the current tile") assertNear(try pixel(tile(current: false), theme: theme, size: tileSize, x: 0.5, y: 0.12), try rgba("#FFFBF3"), "another tile") } /// RULE: under Cotton's themes (paper, plain — and so Rigby's, which wears Cotton's) nothing changes: no fill is named, no current print is drawn, and the current tile draws exactly as any other. func testCottonsFanIsUnchanged() throws { for theme in [Theme.paper, Theme.plain] { XCTAssertNil(theme.currentTileFill, theme.id) XCTAssertNil(theme.primaryActFill, theme.id) XCTAssertEqual(FanCurrentPrint.kinds(theme), [false], "\(theme.id): one print a tile, as before") } let plain = Theme.plain for y in [0.12, 0.5, 0.84] { XCTAssertEqual(try pixel(tile(current: true), theme: plain, size: tileSize, x: 0.5, y: y), try pixel(tile(current: false), theme: plain, size: tileSize, x: 0.5, y: y), "at \(y)") } } /// RULE: a fill names one of the palette's roles; a colour or an unknown word in its place is no fill. func testAFillIsAPaletteRoleOrNothing() { let row = ##"{"id":"x","family":"plain","cards":{"current_tile_fill":"#2E6E6A","primary_act_fill":"lilac"}}"## let theme = Theme.decode(Data(row.utf8)) XCTAssertNil(theme.cards.currentTileFill) XCTAssertNil(theme.cards.primaryActFill) let named = Theme.decode(Data(#"{"id":"x","family":"plain","cards":{"current_tile_fill":"accent"}}"#.utf8)) XCTAssertEqual(named.cards.currentTileFill, "accent") XCTAssertEqual(named.colors.role(named: "accent"), named.colors.accent) } /// RULE: the primary act is the one the core marks (`shell.opened`'s `primary`: the first that reserves, else the kit's main act), or the one a caller marks (the customer's Book) — the fan reads it and decides none. func testThePrimaryActIsTheOneTheCoreOrTheCallerMarks() { var definition = TableDefinition( id: "yoga_session", name: "Class", plural: "Classes", columns: [ColumnDefinition(key: "title", type: .text)], title: "title", slip: SlipLayout(title: "title"), tile: TileLayout(face: .slipList), page: []) definition.commands = [ RecordCommand(id: "here", label: "Here", verb: "set", kind: .update), RecordCommand(id: "book", label: "Book", verb: "reserve_record", kind: .create), RecordCommand(id: "again", label: "Book again", verb: "reserve_record", kind: .create), ] let actions = [CardAction(id: "book", label: "Book", kind: "command", rank: "main", primary: true, red: false)] + ["here", "again"].map { CardAction(id: $0, label: $0, kind: "command", rank: "more", primary: false, red: false) } + [CardAction(id: "bin", label: "Bin", kind: "chooser", rank: "away", primary: false, red: true)] let items = EditingFan.items(of: actions, in: definition) XCTAssertEqual(items.filter(\.primary).map(\.id), ["command-book"]) XCTAssertEqual(items.filter(\.red).map(\.id), ["act-bin"]) let none = EditingFan.items(of: [actions[1], actions[3]], in: definition) XCTAssertFalse(none.contains(where: \.primary), "a fan the core marks no primary act on has none") let order = DeckOffer(id: "order", word: "Order", primary: true) { _ in } let cancel = DeckOffer(id: "cancel", word: "Cancel") { _ in } XCTAssertEqual(EditingFan.items(offered: [cancel, order]).filter(\.primary).map(\.id), ["offer-order"]) XCTAssertFalse(EditingFan.items(offered: [cancel]).contains(where: \.primary), "Cancel is never the primary act") } /// RULE: a flat fan tile's name never breaks inside a word — two lines only at a space ("Take an order"), and a word longer than the tile's line ("Membership") stands whole on one line, set smaller until its ink fits. func testAFlatTilesNameNeverBreaksInsideAWord() throws { for flavour in ["kitchen"] { let theme = try look(flavour) let width = CGFloat(theme.scale[.fanTileW]) let inner = width * (1 - 2 * ToolCardMetrics(scale: theme.scale).contentInset) let long = ToolCard.flatName("Membership", theme: theme, width: width) XCTAssertEqual(long.words, "Membership", flavour) XCTAssertTrue(InkFit.fits(long.words, face: theme.type.title, size: long.size, width: inner, lines: long.lines), "\(flavour): Membership at \(long.size) stands inside \(inner)") for name in ["Take an order", "Schedule", "Kitchen"] { let set = ToolCard.flatName(name, theme: theme, width: width) XCTAssertTrue(InkFit.fits(set.words, face: theme.type.title, size: set.size, width: inner, lines: set.lines), "\(flavour): \(name) breaks only at a space") } } } /// RULE: a look's current prints are drawn after every tile's plain print, nearest the settled tile first, so the fan stands whole before any tile is drawn twice. func testCurrentPrintsAreDrawnAfterThePlainOnes() throws { let theme = try look("kitchen") var environment = EnvironmentValues() environment.theme = theme let tiles = ["a", "b", "c"].map { FanTile(key: $0, name: $0.uppercased(), picture: nil, stock: .cream) } let prints = FanTilePrints() let kinds = FanCurrentPrint.kinds(theme) var order: [String] = [] while let next = prints.missing(of: tiles, around: 1, kinds: kinds) { order.append("\(next.tile.key)\(next.current ? "*" : "")") prints.draw(next.tile, current: next.current, width: 60, scale: 1, environment: environment) } XCTAssertEqual(order, ["b", "a", "c", "b*", "a*", "c*"]) XCTAssertNotNil(prints.picture(of: tiles[1], current: true)) prints.keepOnly([tiles[1]]) XCTAssertNotNil(prints.picture(of: tiles[1], current: true), "a tile kept keeps its current print") XCTAssertNil(prints.picture(of: tiles[0], current: true)) }}#endif