import CottonKit import CottonValues import XCTest @testable import CottonUI /// A business's views read as the owner reads a tool: each view's page a deck /// of the rows the object answered, each row on its table's face, and a /// hold's acts the row's own, posted as the core sent them (coordinator, /// 23–24 Oct 2026). @MainActor final class CustomerDeckTests: XCTestCase { private let today = CustomerShellTiles.viewKey(tool: "yoga", view: "yoga_session") /// A model over the fake business, signed in or not, loaded. private func model(signedIn: Bool, role: String = "member", booked: Bool = false) async throws -> (HomeCustomerModel, BusinessFake) { let core = BusinessFake() core.booked = booked let store = InMemorySecretStore() let client = BusinessClient(core: core, address: BusinessFake.address, store: store) if signedIn { let challenge = try await client.askCode(phone: "+15550100140") _ = try await client.signIn(phone: "+15550100140", challenge: challenge, code: "synthetic-code") core.role = role } let model = HomeCustomerModel(client: client, business: "Synthetic Studio", zone: "Asia/Kolkata") await model.load() return (model, core) } /// RULE: A view's page is a deck, one row at a time: every page but Me is a `CustomerDeck` over the handed-cards deck (`CardDeck`), dealing the view's rows in the view's order — never a list of slips — and a row's acts are the ones the core sent with it, in their own labels; a row that carries none offers nothing. func testAViewsPageDealsItsRowsWithTheirOwnActs() async throws { let (model, _) = try await model(signedIn: false) let cards = model.cards(today) XCTAssertEqual(cards.map(\.id), ["s1"]) XCTAssertEqual(cards.map(\.title), ["Morning hatha"]) XCTAssertEqual(model.acts(on: cards[0]).map(\.word), ["Book"]) XCTAssertEqual(cards[0].acts.first?.picks.map(\.argument), ["quantity"], "Book asks how many") XCTAssertEqual(model.acts(on: cards[0]).first?.asks, true) let studio = model.cards(CustomerShellTiles.viewKey(tool: "yoga_studio", view: "studio")) XCTAssertTrue(model.acts(on: studio[0]).isEmpty, "the studio is read, not acted on") XCTAssertTrue(model.cards("no/such").isEmpty) let deck = SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Join/CustomerDeck.swift", ownedBy: "door")) XCTAssertTrue(deck.contains("CardDeck("), "a view's page is not the deck") for word in ["HomeCustomerWords.cancel", "HomeCustomerWords.book", "\"cancel\"", "claim"] { XCTAssertFalse(deck.contains(word), "the deck makes an act of its own (`\(word)`)") } } /// RULE: Cancel is just an act a row carries: a member's booking offers the Cancel the core sent with it, drawn last as the act that takes the thing away, and it posts `cancel` with the row's own args — then the pages are read again from the cache, with no second sync. func testCancelIsAnActTheRowCarries() async throws { let (model, core) = try await model(signedIn: true, booked: true) let mine = CustomerShellTiles.viewKey(tool: "yoga", view: "mine") let booking = try XCTUnwrap(model.cards(mine).first) let acts = model.acts(on: booking) XCTAssertEqual(acts.map(\.word), ["Cancel"]) XCTAssertTrue(acts[0].destroys) let syncs = core.requests(CottonMethod.businessSync).count await acts[0].perform([]) let posted = try XCTUnwrap(core.requests(CottonMethod.businessAct).last) XCTAssertEqual(posted["act"], .string("cancel")) XCTAssertEqual(posted["args"], .object(["id": .string("b1")])) XCTAssertEqual(core.asked.last?.method, CottonMethod.businessPages, "the pages are not read again after the act") XCTAssertEqual(core.requests(CottonMethod.businessSync).count, syncs, "an act's answer is in the cache: no sync after it") XCTAssertTrue(model.cards(mine).isEmpty, "the cancelled booking still stands") XCTAssertNil(model.refusal) } /// RULE: Staff and the owner see more views on the same shell, each a deck dealt by the same rule. func testStaffSeeMoreViewsOnTheSameDeck() async throws { let (model, _) = try await model(signedIn: true, role: "staff") XCTAssertEqual(model.cards(CustomerShellTiles.viewKey(tool: "yoga", view: "members")).map(\.title), ["Anita"]) XCTAssertEqual(model.pages?.role, .staff) } #if os(iOS) /// RULE: A row's Book is the long-press fan's primary act, drawn on the look's primary fill; a Cancel never is. func testTheReserveActIsTheFansPrimaryAct() async throws { let (model, _) = try await model(signedIn: true, booked: true) let session = try XCTUnwrap(model.cards(today).first) let book = EditingFan.items(offered: CustomerDeck.offers(model.acts(on: session), on: session, place: model.pickPlace)) XCTAssertEqual(book.filter(\.primary).map(\.id), [book.first?.id].compactMap { $0 }) XCTAssertNotNil(book.first) let booking = try XCTUnwrap(model.cards(CustomerShellTiles.viewKey(tool: "yoga", view: "mine")).first) let cancel = EditingFan.items(offered: CustomerDeck.offers(model.acts(on: booking), on: booking, place: model.pickPlace)) XCTAssertFalse(cancel.contains(where: \.primary), "Cancel is not the primary act") } /// RULE: Book asks its places on the long-press fan before it runs (the core's `shell_input_business`: the stepper at its default, in the pick's own label), posts nothing while it asks, and the count answered posts `book` for the held session with that quantity at its argument. func testBookAsksItsPlacesThenPostsThem() async throws { let (model, core) = try await model(signedIn: true) let card = try XCTUnwrap(model.cards(today).first) let target = DeckEditTarget(table: card.row.table, record: card.id, date: nil) let offers = CustomerDeck.offers(model.acts(on: card), on: card, place: model.pickPlace) let editor = DeckEditor.standing() editor.offer(offers, for: target) XCTAssertEqual(EditingFan.items(offered: editor.offered).map(\.word), ["Book"]) let outcome = await editor.act(offers[0], on: target) XCTAssertEqual(outcome, .asking) let asked = editor.pickOptions let standing = try XCTUnwrap(asked.first { if case .answer = $0.does { true } else { false } }) XCTAssertEqual(standing.word, "1") XCTAssertEqual(core.requests(CottonMethod.shellOpened).last?["business"]?["slug"]?.string, "studio", "the pick is asked of this business's cache") XCTAssertTrue(core.requests(CottonMethod.businessAct).isEmpty, "a booking was posted before its places were asked") _ = await editor.answer(try XCTUnwrap(asked.first { $0.does == .count(2) })) let two = try XCTUnwrap(editor.pickOptions.first { if case .answer = $0.does { true } else { false } }) XCTAssertEqual(two.word, "2") _ = await editor.answer(two) XCTAssertNil(editor.editing, "the fan is back to its tools") let posted = try XCTUnwrap(core.requests(CottonMethod.businessAct).last) XCTAssertEqual(posted["act"], .string("book")) XCTAssertEqual(posted["args"], .object(["slot": .string("s1"), "quantity": .number(2)])) XCTAssertNil(model.refusal) } /// RULE: A staff act's record pick offers the rows the object answered (a member among the members staff read), said as their slips say them, and the answer is posted at the pick's dotted argument. func testARecordPickOffersTheServedRowsAndPostsAtItsArgument() async throws { let (model, core) = try await model(signedIn: true, role: "staff") let act = try JSONDecoder().decode(BusinessAct.self, from: Data(#""" {"id":"book","label":"Book","kind":"command","rank":"main","primary":true,"red":false,"act":"record_act","args":{"table":"yoga_session","id":"s1","act":"book"}, "picks":[{"argument":"data.member","kind":"record","table":"yoga_member","label":"Member"}]} """#.utf8)) core.pickStep = #"{"kind":"pick","label":"Member","index":0,"table":"yoga_member","writes":{"kind":"business_pick","answers":[],"of":1},"rows":[{"value":"m1","words":"Anita","does":"answer"},{"value":"search","words":"\#(ShellLines.line("editing", "command.search"))","does":"search","search":{"editor":"line","words":"","title":"Member","keeps_words":false,"writes":{"kind":"pick_search"}}}]}"# let session = try XCTUnwrap(model.cards(today).first) let card = CustomerCard(row: BusinessRow(table: "yoga_session", id: "s1", data: session.row.data, title: session.title, acts: [act])) let target = DeckEditTarget(table: "yoga_session", record: "s1", date: nil) let offers = CustomerDeck.offers(model.acts(on: card), on: card, place: model.pickPlace) let editor = DeckEditor.standing() editor.offer(offers, for: target) _ = await editor.act(offers[0], on: target) let members = editor.pickOptions XCTAssertEqual(members.map(\.word), ["Anita", ShellLines.line("editing", "command.search")], "served members precede the shell's Search option") guard case .search(let search)? = members.last?.does else { return XCTFail("Search asks in the shell's writer") } XCTAssertEqual(search?.writes["kind"]?.string, "pick_search") _ = await editor.answer(try XCTUnwrap(members.first)) let posted = try XCTUnwrap(core.requests(CottonMethod.businessAct).last) XCTAssertEqual(posted["act"], .string("record_act")) XCTAssertEqual(posted["args"], .object(["table": .string("yoga_session"), "id": .string("s1"), "act": .string("book"), "data": .object(["member": .string("m1")])])) } #endif /// RULE: A row stands on its table's real face, as the owner's deck draws it: the core sends each row whole with its table's card under `@card`, the deck draws that card's face (`DeckFace.view`, on its kind's stock) told which table it stands for — a studio's place in the face its kit names — and the tool's plain face (`ToolFace`) only for a row with no card a build draws. func testARowStandsOnItsTablesFace() async throws { let (model, _) = try await model(signedIn: false) let session = try XCTUnwrap(model.cards(today).first) XCTAssertEqual(session.face?.face, "event") XCTAssertEqual(session.face?.corner, "07:00") XCTAssertEqual(session.record["capacity"], .number(10), "the row comes whole") let plain = CustomerCard(row: BusinessRow(table: "yoga_session", id: "s9", data: ["@card": .object(["face": .string("no_such_face")])], title: "Yin")) XCTAssertNil(plain.face, "an unknown kind stands plain") let place = try XCTUnwrap(model.cards(CustomerShellTiles.viewKey(tool: "yoga_studio", view: "studio")).first) XCTAssertEqual(place.target?.target.table, "yoga_place", "the card names no table to its face") XCTAssertEqual(place.target?.target.record, "p1") XCTAssertEqual(place.face?.face, DefinitionCardKind.sharmilaStudio.rawValue, "the studio's card is not the face its kit names") XCTAssertEqual(DeckFace.registered(try XCTUnwrap(place.face))?.id, DefinitionCardKind.sharmilaStudio.rawValue, "the studio's own card is not drawn by its own face") let deck = SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Join/CustomerDeck.swift", ownedBy: "door")) XCTAssertTrue(deck.contains("DeckFace.view(for:"), "a row is not drawn on its table's face") XCTAssertTrue(deck.contains(".environment(\\.faceChoiceTable, card.row.table)"), "the face is not told its table") XCTAssertTrue(deck.contains(".environment(\\.deckCardTarget, opened == card.id ? card.target : nil)"), "at rest no editor rides down; opened in place, the card's parts are live") // Opened, the card stays where it stands and the fan turns into its // acts (owner, 3 Nov 2026): no page is pushed. XCTAssertFalse(deck.contains("OpenedCardPage(")) XCTAssertFalse(deck.contains(".pageDestination(")) XCTAssertTrue(deck.contains("editor.offer(")) XCTAssertTrue(deck.contains("opened: opened")) } }