Cotton code
129 lines · 10.3 KB · SwiftRaw
import CottonKit@testable import CottonUIimport XCTestfinal class CustomerShellTests: XCTestCase {    private func pages(_ role: String) throws -> BusinessPages {        try JSONDecoder().decode(BusinessPages.self, from: Data(BusinessFake.pages(role: role, booked: false).utf8))    }    /// RULE: A business app opens on its business's door: a manifest naming its business's object (a business app's, as one shipped) opens on the business's shell, a personal app's never does (the core starts its database instead: there is no first-open choice), and the root decides it there and only there, drawing nothing else at the door.    func testABusinessOpensOnItsShell() throws {        for flavour in ["kitchen"] {            let manifest = try JSONDecoder().decode(FlavourManifest.self, from: Data(BusinessFake.manifest(flavour).utf8))            XCTAssertEqual(manifest.genesis, .none, flavour)            XCTAssertEqual(CustomerShell.opens(manifest), BusinessAddress(server: BusinessFake.server, slug: flavour))        }        let development = try JSONDecoder().decode(FlavourManifest.self, from: Data(            try SourceSweep.mustRead("apps/cotton/Tests/Fixtures/Flavours/development.json", ownedBy: "project2").utf8))        XCTAssertNil(CustomerShell.opens(development), "a personal app's object is its database, never a business's shell")        let cotton = try JSONDecoder().decode(FlavourManifest.self, from: Data(            try SourceSweep.mustRead("apps/cotton/App/CottonManifest/flavour.json", ownedBy: "shell3").utf8))        XCTAssertEqual(development.personalObject, BusinessAddress(server: "https://cottondb.com", window: ["message": 90]))        // Cotton's manifest names a server per side; the test process is Cotton Dev's (`AppFlavorTests`). The source        // says `{dev-host}`, which the build fills in from config/dev-server.json.        XCTAssertEqual(cotton.personalObject, BusinessAddress(server: "https://{dev-host}", window: ["message": 90]))        for personal in [development, cotton] {            XCTAssertNil(CustomerShell.opens(personal), personal.flavour)        }        XCTAssertNil(CustomerShell.opens(nil), "a bundle with no manifest opens as Cotton")        let root = SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Boot/RootView.swift", ownedBy: "shell3"))        let door = try XCTUnwrap(root.range(of: "case .door:"))        let next = try XCTUnwrap(root.range(of: "case .gate", range: door.upperBound..<root.endIndex))        let face = root[door.upperBound..<next.lowerBound]        XCTAssertNotNil(face.range(of: "CustomerShell.opens"), "the door does not ask whether this is a business")        XCTAssertTrue(face.contains("BusinessClient(core: core, address: business)"))        XCTAssertFalse(root.contains("StartScreen"), "no Start screen stands anywhere")    }    /// RULE: The fan is one tile per view the object answered — keyed `tool/view`, named as the view, on its tool's picture and stock, grouped by tool in the object's order — then Me; it opens on the default view of the tool the manifest's dial opens on.    func testTheFanIsOneTilePerView() throws {        let pages = try pages("public")        let tiles = CustomerShellTiles.dealt(pages)        XCTAssertEqual(tiles.map(\.key), ["yoga/yoga_session", "yoga/next", "yoga_studio/studio", "me"])        XCTAssertEqual(tiles.map(\.name), ["Today", "Tomorrow", "Studio", "Me"])        XCTAssertEqual(tiles[0].stock, .sage)        XCTAssertEqual(tiles[0].picture, .asset("yoga"))        XCTAssertEqual(CustomerShellTiles.view("yoga/next", in: pages)?.view.label, "Tomorrow")        XCTAssertEqual(CustomerShellTiles.dealt(nil).map(\.key), ["me"], "before the object answers, Me alone")        XCTAssertEqual(CustomerShellTiles.opens(tiles, pages: pages, fan: .init(opens: "yoga")), 0)        XCTAssertEqual(CustomerShellTiles.opens(tiles, pages: pages, fan: .init(opens: "yoga_studio")), 2)        XCTAssertEqual(CustomerShellTiles.opens(tiles, pages: pages, fan: .init(opens: "no_such_tool")), 0, "the first default view")    }    /// RULE: One shell for every role: a member's fan is the stranger's with their own views in it, staff's the member's with the staff views — dealt by the same rule from what the object answered — and no shell source names a role, a hand-built page per role, or anything of the relay's sign-in (consent, the owner's challenge, the handover, deletion).    func testOneShellForEveryRole() throws {        let stranger = Set(CustomerShellTiles.dealt(try pages("public")).map(\.key))        let member = Set(CustomerShellTiles.dealt(try pages("member")).map(\.key))        let staff = Set(CustomerShellTiles.dealt(try pages("staff")).map(\.key))        XCTAssertEqual(member.subtracting(stranger), ["yoga/mine"])        XCTAssertEqual(staff.subtracting(member), ["yoga/members"])        for file in ["Join/CustomerShell.swift", "Join/CustomerDeck.swift", "Surfaces/Board/HomeCustomer.swift"] {            let source = SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/\(file)", ownedBy: "business"))            for role in [".staff", ".owner", ".member", "\"staff\"", "\"owner\""] {                XCTAssertFalse(source.contains(role), "\(file) decides by role (`\(role)`)")            }            for gone in ["consent", "owner:", "deleteAccount", "claimOwnership", "acceptOwner", "VaultKeyring", "BusinessOwnerKeys", "onOwner",                         "StartScreen"] {                XCTAssertFalse(source.contains(gone), "\(file) still reaches \(gone)")            }        }        let client = SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonKit/BusinessClient.swift", ownedBy: "business"))        for gone in ["customers.", "public.pages", "consent", "privateKey", "handover", "deleteAccount"] {            XCTAssertFalse(client.contains(gone), "the client still speaks `\(gone)`")        }        XCTAssertFalse(FileManager.default.fileExists(atPath: SourceSweep.repoRoot            .appendingPathComponent("apps/cotton/Sources/CottonKit/CustomerSignIn.swift").path), "the relay's customer client is back")    }    /// RULE: Every page of the shell is drawn from the object's answer: each view's tile is a deck of that view's rows (`CustomerDeck`), Me alone is the account, and the shell holds no list of its own.    func testEveryPageIsTheObjectsAnswer() throws {        let shell = SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Join/CustomerShell.swift", ownedBy: "business"))        XCTAssertTrue(shell.contains("FanHome("), "the shell's pages are not under the fan")        XCTAssertTrue(shell.contains("CustomerPage(model: model)") && shell.contains("CustomerDeck(model: model, key: tile.key, tile: tile)"))        XCTAssertTrue(shell.contains("CustomerShellTiles.dealt(model.pages)"), "the fan is not the object's views")    }    /// RULE: The sign-in sheet asks the number, the six-digit code and — once, from a stranger the object asks it of — a name, and nothing else: no consent, no owner code — everyone signs in alike.    func testTheSheetAsksTheNumberAndTheCodeOnly() throws {        XCTAssertTrue(HomeCustomerKnobs.codeReady("123456"))        XCTAssertFalse(HomeCustomerKnobs.codeReady("12345"))        XCTAssertFalse(HomeCustomerKnobs.codeReady(String(repeating: "A", count: 43)), "an owner code is not a code")        let customer = SourceSweep.withoutComments(            try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Surfaces/Board/HomeCustomer.swift", ownedBy: "business"))        XCTAssertEqual(customer.components(separatedBy: "PaperTextField(").count - 1, 3, "the sheet's fields are the number, the code and a new account's name")        // NEXT-SIGNIN (28 Nov 2026): the name has its own step, after the code is proved, for a number with no account.        XCTAssertTrue(customer.contains("case .name(let challenge):"), "the name is its own step")        XCTAssertTrue(customer.contains("nameAfter: true"), "the code goes alone; the name only after it")        XCTAssertFalse(customer.contains("asksName"), "the name is never asked beside the code")        XCTAssertFalse(customer.contains("Toggle("), "the sheet asks for consent")        for id in ["home-customer-phone", "home-customer-send", "home-customer-code", "home-customer-confirm"] {            XCTAssertTrue(customer.contains(id), "the sheet lost \(id)")        }    }    /// RULE: A card's acts fan closes: no record button stands under the shell's fan, so the fan draws its own X (`fan-record-close`) just above itself while a card is edited and the fan stands, and its tap puts the fan back with nothing run. Home's own fan keeps its close in the record button, never a second X.    @MainActor    func testACardsActsFanCloses() throws {        XCTAssertTrue(FanEditCloses.stands(closesEdits: true, editing: true, fanDown: false))        XCTAssertFalse(FanEditCloses.stands(closesEdits: true, editing: false, fanDown: false), "an X with nothing edited")        XCTAssertFalse(FanEditCloses.stands(closesEdits: true, editing: true, fanDown: true), "an X over a fan put away")        XCTAssertFalse(FanEditCloses.stands(closesEdits: false, editing: true, fanDown: false), "a second X beside the record button's")        let shell = SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Join/CustomerShell.swift", ownedBy: "door"))        XCTAssertTrue(shell.contains("closesEdits: true"), "the shell's fan draws no close of its own")        let board = SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Surfaces/BoardSurface.swift", ownedBy: "door"))        XCTAssertFalse(board.contains("closesEdits: true"), "Home's fan draws a second close beside the record button's")        XCTAssertFalse(board.contains("HomeCustomer") || board.contains("BusinessClient"), "a database's Home carries a business's account")        var ran = false        let editor = DeckEditor.standing()        editor.offer([DeckOffer(id: "book", word: "Book") { _ in ran = true }],                     for: DeckEditTarget(table: "yoga_session", record: "s1", date: nil))        XCTAssertTrue(editor.isEditing && editor.holdsActs)        editor.close()        XCTAssertNil(editor.editing, "the X leaves the acts standing")        XCTAssertFalse(ran, "the X ran the act")    }}