Cotton code
61 lines · 4.1 KB · SwiftRaw
import Foundationimport XCTest@testable import CottonUI/// Scan guards over the record page's cards and tray (`CottonUI/Cards/`), and/// the no-literal guard over every generic surface.final class CardGuardTests: XCTestCase {    private static let cards = "CottonUI/Cards/"    private func cardSources() throws -> [(name: String, text: String)] {        let files = try SourceSweep.swiftSources().filter { $0.name.hasPrefix(Self.cards) }        XCTAssertGreaterThan(files.count, 5, "the sweep found no card sources — did Cards/ move?")        return files.map { ($0.name, SourceSweep.withoutComments($0.text)) }    }    /// RULE: nothing in CottonUI raises a system alert or confirmation dialog, and nothing under Cards/ presents a sheet, cover, popover or confirm — an act is a card (tap fires, tap asks, hold confirms) and a pick is the pick sheet, so a popup is a second way to act (owner, 12 Oct 2026; architect, 13 Oct 2026).    func testNoAlertAnywhereAndNoModalInTheCardsOrTheTray() throws {        let everywhere = try SourceSweep.swiftSources().filter { $0.name.hasPrefix("CottonUI/") }        XCTAssertFalse(everywhere.isEmpty)        for file in everywhere {            let hits = try SourceSweep.matches(#"\.(alert|confirmationDialog)\s*\("#, in: SourceSweep.withoutComments(file.text))            XCTAssertTrue(hits.isEmpty, "\(file.name) raises \(hits)")        }        // A presentation is told by its binding: `theme.sheet(.cream)` is a paper's colour, not a sheet.        let modal = #"\.(sheet|fullScreenCover|popover|cottonSheet|confirmDecision)\s*\(\s*(isPresented|item)\s*:"#        XCTAssertFalse(try SourceSweep.matches(modal, in: "x.sheet(isPresented: $on) {}").isEmpty)        XCTAssertFalse(try SourceSweep.matches(modal, in: "x.confirmDecision(\n  item: $it)").isEmpty)        for file in try cardSources() {            let hits = try SourceSweep.matches(modal, in: file.text)            XCTAssertTrue(hits.isEmpty, "\(file.name) presents \(hits) — the pick sheet is the only place a card asks")        }    }    /// The generic surfaces: everything a definition draws through.    private static let generic = ["CottonUI/Cards/", "CottonUI/Surfaces/", "CottonUI/Faces/", "CottonUI/Definitions/", "CottonUI/Editors/"]    /// RULE: no generic surface (Cards/, Surfaces/, Faces/, Definitions/, Editors/) reads a definition's `builtin` flag or branches on a table's identity — a table id, a record's table or kind, an engine compared to a literal, switched over, looked up in a literal list, or turned into a `RecordKind` — so a shipped tool and an imported one are the same thing to everything a person sees, and what differs is a definition key (owner's amendment, 12 Oct 2026; the no-literal guard, architect 14 Oct 2026). No allow-list: a table that needs its own behaviour names it in its definition.    func testNoGenericSurfaceReadsBuiltinOrNamesATable() throws {        let patterns = [            #"(?<!stored)(?<!self)\.builtin\b"#,            #"\b(\w+\.)?(table|tableId|tableID|engine)\s*[!=]=\s*""#,            #""\s*[!=]=\s*(\w+\.)?(table|tableId|tableID|engine)\b"#,            #"\b(definition|def|linked|target|table|attachment)\.(id|engine|kind)\s*[!=]=\s*""#,            #"switch\s+(\w+\.)*(table|tableId|tableID|engine)\s*\{"#,            #"switch\s+(definition|def|linked|target|attachment)\.(id|kind)\s*\{"#,            #"RecordKind\(rawValue:\s*(\w+\.)*(table|tableId|tableID|id)\b"#,            #"\]\s*\.contains\(\s*(\w+\.)*(table|tableId|tableID|engine)\s*\)"#,            #"\]\s*\.contains\(\s*(definition|def|linked|target)\.id\s*\)"#,        ]        let files = try SourceSweep.swiftSources().filter { file in Self.generic.contains { file.name.hasPrefix($0) } }        XCTAssertFalse(files.isEmpty, "the sweep found no generic sources — did a folder move?")        for file in files {            let text = SourceSweep.withoutComments(file.text)            for pattern in patterns {                let found = try SourceSweep.matches(pattern, in: text)                XCTAssertTrue(found.isEmpty, "\(file.name) names a table or reads builtin: \(found)")            }        }    }}