Cotton code
105 lines · 8.2 KB · SwiftRaw
// Rollover (door row 5): a swipe past a view's last card goes on to the next// chip's first, back past its first to the previous chip's last — Late →// Today → Upcoming → All — and no further at either end of the ring.import XCTest@testable import CottonUI@MainActorfinal class DeckRolloverTests: XCTestCase {    /// The ring as the core answers it (`shell.fan`): the tool's views in its order, the default the one it stands on.    private static func ring(_ tool: String, _ views: [(id: String, table: String, section: String?)], standing current: String) throws -> ToolViews {        let listed = views.map { view in            let section = view.section.map { #","section":"\#($0)""# } ?? ""            return #"{"id":"\#(view.id)","label":"\#(view.id)","default":\#(view.id == current),"read":{"table":"\#(view.table)"\#(section)}}"#        }.joined(separator: ",")        let answer = #"{"opens":{"tier":"tools","tool":"\#(tool)"},"root":{"id":"tools","cards":["\#(tool)"],"raised":"\#(tool)"},"tiles":[{"key":"\#(tool)","name":"Synthetic","kind":"tool","tool":"\#(tool)","ringed":true,"poster":false,"counted":false,"current":"\#(current)","views":[\#(listed)]}]}"#        let toolViews = ToolViews()        toolViews.take(try JSONDecoder().decode(FanAnswer.self, from: Data(answer.utf8)))        return toolViews    }    private func todos() throws -> ToolViews {        try Self.ring("todo", [("late", "todo", "late"), ("today", "todo", "today"), ("upcoming", "todo", "upcoming"), ("all", "todo", nil)], standing: "today")    }    func testTheNeighboursAreTheRingsOrderFromTheCurrentView() throws {        let toolViews = try todos()        XCTAssertEqual(toolViews.neighbour(of: "todo", step: 1)?.id, "upcoming", "from the default, Today")        XCTAssertEqual(toolViews.neighbour(of: "todo", step: -1)?.id, "late")        toolViews.choose("upcoming", of: "todo")        XCTAssertEqual(toolViews.neighbour(of: "todo", step: 1)?.id, "all")    }    func testNothingPastEitherEnd() throws {        let toolViews = try todos()        toolViews.choose("all", of: "todo")        XCTAssertNil(toolViews.neighbour(of: "todo", step: 1))        toolViews.choose("late", of: "todo")        XCTAssertNil(toolViews.neighbour(of: "todo", step: -1))    }    func testANotesLastPageIsWiredToTheDeck() throws {        let note = try String(contentsOf: SourceSweep.loomSources.appendingPathComponent("CottonFaces/Cotton/NoteFace.swift"), encoding: .utf8)        XCTAssertTrue(note.contains("passOn: { forward in passOn?(forward) }"), "NoteCard hands NoteFace the deck's move")    }    /// The kit's tool manifest `tool`.    private static func kitTool(_ tool: String) throws -> ToolDefinition {        try ToolDefinition.decode(Data(contentsOf: SourceSweep.kits.appendingPathComponent("personal/tools/\(tool)/manifest.json")))    }    /// RULE: A list is keyed by the tool whose view it is: the tool its page handed in; else the table's own tool block; else the tool that lists the table, its home first (Music, Books and Films declare their views in their manifests and their tables carry no tool block); so Music's songs roll back into its artists as Todos' views roll.    func testAListIsKeyedByTheToolItsPageHandedIn() throws {        let song = try TableDefinition.decode(Data(contentsOf: SourceSweep.kitTable("song")))        let todo = try TableDefinition.decode(Data(contentsOf: SourceSweep.kitTable("todo")))        let tools = try ["books", "music", "cinemas"].map(Self.kitTool)        XCTAssertNil(song.tool, "the song table names no tool; Music's manifest declares its views")        XCTAssertEqual(ListTool.key(handed: nil, table: song, tools: tools), "music", "the tool that lists it: Music's deck rolls into its views")        XCTAssertEqual(ListTool.key(handed: "music", table: song, tools: tools), "music")        XCTAssertEqual(ListTool.key(handed: nil, table: todo, tools: tools), todo.tool?.key ?? todo.id)        XCTAssertEqual(ListTool.key(handed: "todos", table: todo, tools: tools), "todos")        XCTAssertNil(ListTool.key(handed: nil, table: nil, tools: tools))        XCTAssertNil(ListTool.key(handed: nil, table: song, tools: []), "a table no tool lists and no block names is no tool's")        let route = try String(contentsOf: SourceSweep.source("CottonUI/Surfaces/TableRoute.swift"), encoding: .utf8)        XCTAssertTrue(route.contains("tool: tool, onOpen: read == table ? onOpen : nil)"), "the tool's page hands its tool to the list")        XCTAssertFalse(route.contains("definitions[table]?.tool.map"), "no list is keyed by its table's block alone")        let toolViews = try Self.ring("music", [("albums", "music_album", nil), ("artists", "artist", nil), ("songs", "song", nil)], standing: "albums")        toolViews.choose("songs", of: "music")        XCTAssertEqual(toolViews.neighbour(of: "music", step: -1)?.id, "artists", "back past the first song")        XCTAssertNil(toolViews.neighbour(of: "music", step: 1))    }    /// RULE (the Cinemas walk, 5 Nov 2026): a deck over a table its tool lists but no tool block names — Films' film, Books' book — reads the view the tier chose, whatever page draws it: the landing's record page and the tool's plain list follow the views tier, never stand whole (The Salt Road, three of three, on On this phone, Films and every view).    func testATablesDeckFollowsTheToolThatListsIt() throws {        let film = try TableDefinition.decode(Data(contentsOf: SourceSweep.kitTable("film")))        let book = try TableDefinition.decode(Data(contentsOf: SourceSweep.kitTable("book")))        let tools = try ["books", "music", "cinemas"].map(Self.kitTool)        XCTAssertNil(film.tool, "the film table names no tool; Films' manifest declares its views")        XCTAssertEqual(ListTool.key(handed: nil, table: film, tools: tools), "cinemas")        XCTAssertEqual(ListTool.key(handed: nil, table: book, tools: tools), "books")        let films = try Self.ring("cinemas", [("films", "film", nil), ("all", "film", nil), ("on_phone", "film", nil)], standing: "films")        films.choose("on_phone", of: "cinemas")        XCTAssertEqual(ListTool.key(handed: nil, table: film, tools: tools).flatMap { films.current(of: $0)?.id }, "on_phone",                       "the record's deck reads the view the tier chose")        let opening = try String(contentsOf: SourceSweep.source("CottonUI/Surfaces/RecordOpening.swift"), encoding: .utf8)        XCTAssertTrue(opening.contains("lands: true"), "a record's deck stands on the view the core names")        XCTAssertFalse(opening.contains("definition.tool.map"), "a record's deck keyed by its table's block alone reads no view")    }    /// RULE (Cotton Cinemas 1.0 (2), 6 Nov 2026: a list opened on "Films could not be read" — its deck was named `film/group:<id>`, which is no tool, and the core refused "No tool film"): a list's deck stands for `<tool>/group:<id>` named by the tool that lists its table — Books' shelves, Music's playlists, Cinemas' lists alike — the ring's tool where the page was handed it, and no view at all where no tool lists the table.    func testAListsDeckIsNamedByTheToolThatListsItsTable() throws {        let tools = try ["books", "music", "cinemas"].map(Self.kitTool)        for (table, tool) in [("book", "books"), ("song", "music"), ("film", "cinemas")] {            let definition = try TableDefinition.decode(Data(contentsOf: SourceSweep.kitTable(table)))            XCTAssertNil(definition.tool, "the \(table) table names no tool block")            let list = RecordGroup(id: "invented-list", name: "Invented list", of: table, count: 1)            XCTAssertEqual(GroupPage.deckName(of: list, in: definition, tool: nil, tools: tools), "\(tool)/group:invented-list")            XCTAssertEqual(GroupPage.deckName(of: list, in: definition, tool: tool, tools: []), "\(tool)/group:invented-list", "the ring's tool, handed")            XCTAssertNil(GroupPage.deckName(of: list, in: definition, tool: nil, tools: []), "no tool lists it: the deck reads its members alone")        }        let page = try String(contentsOf: SourceSweep.source("CottonUI/Surfaces/GroupStacks.swift"), encoding: .utf8)        XCTAssertFalse(page.contains("definition.tool?.key ?? definition.id)/"), "no list's deck is named by its table's id")    }}