Cotton code
72 lines · 5.2 KB · SwiftRaw
import Foundationimport XCTest@testable import CottonUI/// The fan reads its first tier from the core (`shell.fan`): a Books fan/// opens on its one tool's views, a Cotton fan on its tools — the shell/// builds the tier from the answer and decides none of it.@MainActor final class FanRootTests: XCTestCase {    private static let books = #"""    {"opens": {"tier": "views", "tool": "books", "view": "reading"},     "root": {"id": "views-books", "cards": ["reading", "want", "group:g1", "all"], "raised": "reading"},     "tiles": [{"key": "books", "name": "Books", "kind": "tool", "tool": "books", "picture": {"object": "tool-books", "photo": "photo:books"},                "stock": "tan", "ringed": true, "poster": false, "views_table": "book", "counted": true, "current": "reading",                "views": [{"id": "reading", "label": "Reading", "count": 0, "default": true, "picture": "open_book", "read": {"table": "book"}},                          {"id": "want", "label": "Want to read", "count": 2, "default": false, "picture": "ribbon", "read": {"table": "book"}},                          {"id": "group:g1", "label": "Synthetic shelf", "count": 1, "default": false, "stack": ["db/photos/a.jpg"], "read": {"table": "book"}},                          {"id": "all", "label": "All", "count": 2, "default": false, "picture": "book_pile", "read": {"table": "book"}}]}]}    """#    private static let cotton = #"""    {"opens": {"tier": "tools", "tool": "home"},     "root": {"id": "tools", "cards": ["calendar", "home", "todos", "tools", "settings"], "raised": "home"},     "tiles": [{"key": "calendar", "name": "Calendar", "kind": "tool", "tool": "calendar", "ringed": true, "poster": false, "counted": false, "views": []},               {"key": "home", "name": "Home", "kind": "home", "ringed": false, "poster": false, "counted": false, "views": []},               {"key": "todos", "name": "Todos", "kind": "tool", "tool": "todos", "ringed": true, "poster": false, "counted": false, "views": []},               {"key": "tools", "name": "Tools", "kind": "tools", "ringed": true, "poster": false, "counted": false, "current": "shown",                "views": [{"id": "shown", "label": "Shown", "default": true, "read": {"table": ""}}, {"id": "hidden", "label": "Hidden", "default": false, "read": {"table": ""}}]},               {"key": "settings", "name": "Settings", "kind": "settings", "ringed": true, "poster": false, "counted": false, "views": []}]}    """#    private func answer(_ json: String) throws -> FanAnswer { try JSONDecoder().decode(FanAnswer.self, from: Data(json.utf8)) }    /// RULE (the owner on Cotton Books 1.0.0 (1), 5 Nov 2026): the core says a one-tool kit opens on that tool's views; the root tier is those views in the answered order, the current raised, with no tools tier under it.    func testABooksFanStandsOnTheAnsweredViews() throws {        let views = ToolViews()        views.take(try answer(Self.books))        let answer = try XCTUnwrap(views.answer)        XCTAssertTrue(answer.opensOnViews)        XCTAssertEqual(answer.tiles.first?.viewsTable, "book")        XCTAssertEqual(answer.tiles.first?.picture, .init(object: "tool-books", photo: "photo:books"))        XCTAssertEqual(answer.tiles.first?.stock, .tan)        let tiers = FanTiers()        tiers.setRoot(ViewTier.make(tool: "books", views: views.views(of: "books"), current: views.current(of: "books")?.id))        XCTAssertEqual(tiers.open?.id, answer.root.id)        XCTAssertEqual(tiers.open?.cards.map(\.id), answer.root.cards)        XCTAssertEqual(tiers.open?.cards.map(\.painted), ["view-open_book", "view-ribbon", nil, "view-book_pile"])        XCTAssertEqual(tiers.open?.cards[2].list, ListTilePictures(cover: nil, stack: ["db/photos/a.jpg"]))        XCTAssertEqual(tiers.open?.raised, "reading")        XCTAssertFalse(tiers.closes, "no tools tier stands under the views to go back to")        views.choose("all", of: "books")        XCTAssertEqual(views.current(of: "books")?.id, "all", "a view chosen here stands until the core answers again")    }    /// RULE: a Cotton fan opens on its tools at the tile the core names; a tool's views stand over them and close back to them.    func testACottonFanStandsOnItsTools() throws {        let views = ToolViews()        views.take(try answer(Self.cotton))        let answer = try XCTUnwrap(views.answer)        XCTAssertFalse(answer.opensOnViews)        XCTAssertEqual(answer.tiles.map(\.key), answer.root.cards)        XCTAssertEqual(answer.tiles.map(\.kind), [.tool, .home, .tool, .tools, .settings])        XCTAssertEqual(answer.opens.tool, "home")        let tiers = FanTiers()        tiers.setRoot(FanTier(id: answer.root.id, cards: answer.tiles.map { .init(id: $0.key, label: $0.name, style: .object) }, raised: answer.root.raised))        XCTAssertEqual(tiers.open?.id, FanTiers.toolsTier)        tiers.push(ViewTier.make(tool: "tools", views: views.views(of: "tools"), current: views.current(of: "tools")?.id)) { _ in }        XCTAssertEqual(tiers.open?.cards.map(\.label), ["Shown", "Hidden"])        XCTAssertEqual(tiers.open?.raised, "shown")        XCTAssertEqual(tiers.pop()?.id, "views-tools")        XCTAssertEqual(tiers.open?.id, FanTiers.toolsTier)    }}