Cotton code
78 lines · 5.5 KB · SwiftRaw
import XCTest@testable import CottonUIfinal class NoTableInGenericSurfacesTests: XCTestCase {    /// RULE: The app never decides behaviour by whether a table shipped: `builtin` is decoded and read nowhere — whether a record's commands run comes from its engine commands or typed package acts, so an imported tool's records behave as a built-in's (architect, 13 Oct 2026).    func testNothingReadsBuiltin() throws {        let root = SourceSweep.iosSources.appendingPathComponent("CottonUI")        let files = try FileManager.default.subpathsOfDirectory(atPath: root.path).filter { $0.hasSuffix(".swift") }        let read = try NSRegularExpression(pattern: #"(?<!")\bbuiltin\b(?!")"#)        var readers: [String] = []        for path in files where path != "Definitions/TableDefinition.swift" {            let code = SourceSweep.withoutComments(try String(contentsOf: root.appendingPathComponent(path), encoding: .utf8))            if read.firstMatch(in: code, range: NSRange(code.startIndex..., in: code)) != nil { readers.append(path) }        }        XCTAssertEqual(readers, [], "read whether a table shipped")        let commanded = TableDefinition(            id: "imported", name: "Imported", plural: "Imported", builtin: false, engine: "an-engine",            columns: [ColumnDefinition(key: "title", type: .text)], title: "title",            slip: SlipLayout(title: "title"), tile: TileLayout(face: .slipList), page: [])        XCTAssertFalse(commanded.canRunCommands, "an engine with no commands runs none")    }    /// RULE: A package's typed acts need no native engine, and filtering commands must remove those acts too, so the tray can offer them and the Bin can suppress them.    func testPackageActsNeedNoNativeEngine() throws {        var table = TableDefinition(            id: "synthetic_package", name: "Package", plural: "Packages", builtin: false, engine: nil,            columns: [ColumnDefinition(key: "title", type: .text)], title: "title",            slip: SlipLayout(title: "title"), tile: TileLayout(face: .slipList), page: [])        let data = Data(#"{"id":"attend","label":"Attend","kind":"update","verb":"set","data":{"column":"title","value":"Attended"}}"#.utf8)        table.acts = [try JSONDecoder().decode(RecordCommand.self, from: data)]        XCTAssertTrue(table.canRunCommands)        XCTAssertEqual(table.commands?.first?.id, "attend")        table.commands = []        XCTAssertFalse(table.canRunCommands)        XCTAssertNil(table.acts)    }    /// RULE: No generic surface names a table or an engine: Surfaces/, Faces/, Definitions/ and Editors/ never compare a table, an id, a link or an engine with a shipped table's or engine's name, index the definitions by one, pass one as a `table:`, list several, or key a map by one — what a table does is a definition key (`role`, `record_route`, `sealed`, `binned`…), so an imported tool behaves as a shipped one. No allow-list (architect ruling 3, 13 Oct 2026).    func testNoGenericSurfaceNamesATable() throws {        let definitions = FixtureDefinitions.load().definitions        let tables = Set(definitions.map(\.id))        let engines = Set(definitions.compactMap(\.engine))        XCTAssertFalse(tables.isEmpty, "the built-in definitions are bundled")        let word = #""([a-z_]+)""#        let named: [(NSRegularExpression, Set<String>)] = [            (try NSRegularExpression(pattern: #"(?:\btable|\.id|\blinkTo|\bto)\s*[!=]=\s*"# + word), tables),            (try NSRegularExpression(pattern: #"\bengine\s*[!=]=\s*"# + word), engines),            (try NSRegularExpression(pattern: #"definitions\[\s*"# + word + #"\s*\]"#), tables),            (try NSRegularExpression(pattern: #"\btable:\s*"# + word), tables),            (try NSRegularExpression(pattern: #"^\s*"# + word + #":\s*(?:\.[a-z]|[A-Z])"#, options: .anchorsMatchLines), tables),        ]        let list = try NSRegularExpression(pattern: #"\[\s*"[a-z_]+"(?:\s*,\s*"[a-z_]+")+\s*\]"#)        let quoted = try NSRegularExpression(pattern: word)        // A multi-line string is data a sampler or a preview decodes, not code.        let block = try NSRegularExpression(pattern: #"""(?s).*?""""#)        let folders = ["CottonUI/Surfaces/", "CottonUI/Faces/", "CottonUI/Definitions/", "CottonUI/Editors/"]        var found: [String] = []        for source in try SourceSweep.swiftSources() where folders.contains(where: source.name.hasPrefix) {            let stripped = SourceSweep.withoutComments(source.text)            let code = block.stringByReplacingMatches(                in: stripped, range: NSRange(stripped.startIndex..., in: stripped), withTemplate: "\"\"") as NSString            let all = NSRange(location: 0, length: code.length)            for (pattern, words) in named {                for match in pattern.matches(in: code as String, range: all) where words.contains(code.substring(with: match.range(at: 1))) {                    found.append("\(source.name): \(code.substring(with: match.range))")                }            }            for match in list.matches(in: code as String, range: all) {                let inside = code.substring(with: match.range) as NSString                let ids = quoted.matches(in: inside as String, range: NSRange(location: 0, length: inside.length))                    .map { inside.substring(with: $0.range(at: 1)) }.filter(tables.contains)                if ids.count >= 2 { found.append("\(source.name): \(inside)") }            }        }        XCTAssertEqual(found, [], "a generic surface reads what a table does from its definition, never from its name")    }}