SPB Git

Repository Public License

CatalogTests.swift 8.7 KB 121 lines swift

1import Foundation2import Testing3import PrismComponentSDK4import PrismCore5import PrismObjectModel6@testable import PrismComponentCatalog78@Suite struct CatalogTests {9    @Test func everyManifestLoadsWithAVerifiedHash() {10        #expect(Catalog.registry.rejections.isEmpty, "Run scripts/update-catalog-lock.sh after an intentional manifest change.")11        #expect(Catalog.registry.count == Catalog.manifests.count)12        #expect(Set(Catalog.manifests.map(\.type)).count == Catalog.manifests.count, "a component type is declared twice")13        #expect(Catalog.registry.count >= 200, "the catalog promises at least 200 working components")14        #expect(Set(CatalogLock.hashes.keys) == Set(Catalog.manifests.map(\.type)))15    }1617    @Test func manifestsSurviveStrictDecoding() throws {18        for m in Catalog.manifests {19            #expect(try ComponentManifest(json: JSONValue.parse(m.json.canonicalData())) == m, "\(m.type)")20            #expect(!m.summary.isEmpty && !m.keywords.isEmpty && !m.examples.isEmpty, "\(m.type)")21        }22    }2324    @Test(arguments: Catalog.manifests.map(\.type))25    func fixtureIsAValidApp(type: String) throws {26        let m = try #require(Catalog.registry.manifest(type))27        let issues = BlueprintValidator.validate(try m.fixtureBlueprint(), registry: Catalog.registry)28        #expect(issues.errors.isEmpty, "\(type): \(issues.errors)")29    }3031    @Test func examplesUseOnlyDeclaredPropsAndEvents() throws {32        for m in Catalog.manifests {33            for example in m.examples {34                let o = try PrismObject(json: example)35                #expect(o.type == m.type)36                for key in o.props.keys { #expect(m.prop(key) != nil, "\(m.type) example uses unknown prop \(key)") }37                for event in o.events.keys { #expect(m.events.contains(event), "\(m.type) example uses unknown event \(event)") }38                for spec in m.props where spec.required { #expect(o.props[spec.name] != nil, "\(m.type) example misses \(spec.name)") }39            }40        }41    }4243    @Test func searchFindsWhatAnExpenseTrackerNeeds() {44        func top(_ q: String) -> [String] { Catalog.registry.search(q, limit: 4).map(\.manifest.type) }45        #expect(top("csv import").first == "prism.csvImport")46        #expect(top("category filter").contains("prism.dropdown"))47        #expect(top("monthly chart").contains("prism.chart.bar") && top("monthly chart").contains("prism.chart.line"))48        #expect(top("budget alert").first == "prism.alert")49        #expect(top("sortable table").first == "prism.table")50        #expect(top("edit record form").contains("prism.recordEditor"))51        #expect(top("kanban").first == "prism.kanban")52        #expect(top("zxqvhologram").isEmpty)53        #expect(Catalog.registry.search("", category: "charts", limit: 50).count >= 3)54    }55}5657@Suite struct ValidatorTests {58    func app(_ extra: [PrismObject], state: [String: JSONValue] = ["category": "All"], permissions: [String] = [Permission.storageLocal]) -> AppBlueprint {59        var bp = AppBlueprint(id: "app_v", name: "V", entrypoint: "home")60        bp.state = state61        bp.permissions = permissions62        bp.objects = [63            PrismObject(id: "home", type: ObjectType.page, name: "Home", children: ["root"]),64            PrismObject(id: "root", type: "prism.stack", children: extra.filter(\.isComponent).map(\.id)),65            PrismObject(id: "tx", type: ObjectType.dataTable, props: ["columns": [["name": "date", "type": "date"], ["name": "category", "type": "text"], ["name": "amount", "type": "currency"]]]),66        ] + extra67        return bp68    }6970    func codes(_ bp: AppBlueprint) -> Set<String> { Set(BlueprintValidator.validate(bp, registry: Catalog.registry).errors.map(\.code)) }7172    @Test func aCorrectAppPasses() {73        let bp = app([74            PrismObject(id: "list", type: "prism.table", props: [75                "query": ["table": "tx", "filters": [["column": "category", "value": ["$bind": "category"], "unless": ["$expr": "state.category == 'All'"]]]],76                "columns": [["column": "date"], ["column": "amount", "format": "currency"]],77            ]),78            PrismObject(id: "totals", type: ObjectType.dataQuery, props: ["query": ["table": "tx", "aggregates": [["fn": "sum", "column": "amount", "as": "total"]]]]),79            PrismObject(id: "warn", type: "prism.alert", props: ["title": "Over", "visible": ["$expr": "sum(queries.totals, 'total') > 100"]]),80            PrismObject(id: "go", type: "prism.button", props: ["title": "Reset"], events: ["click": [["type": "setState", "key": "category", "value": "All"]]]),81        ])82        #expect(BlueprintValidator.validate(bp, registry: Catalog.registry).errors.isEmpty)83        #expect(Catalog.registry.coverage(of: bp) == 1)84    }8586    @Test func eachKindOfMistakeIsNamed() {87        #expect(codes(app([PrismObject(id: "x", type: "prism.hologram")])).contains("component.unknown"))88        #expect(codes(app([PrismObject(id: "x", type: "prism.text")])).contains("prop.required"))89        #expect(codes(app([PrismObject(id: "x", type: "prism.text", props: ["text": "a", "colour": "red"])])).contains("prop.unknown"))90        #expect(codes(app([PrismObject(id: "x", type: "prism.text", props: ["text": 5])])).contains("prop.type"))91        #expect(codes(app([PrismObject(id: "x", type: "prism.text", props: ["text": "a", "style": "huge"])])).contains("prop.enum"))92        #expect(codes(app([PrismObject(id: "x", type: "prism.text", props: ["text": ["$expr": "1 +"]])])).contains("expression.syntax"))93        #expect(codes(app([PrismObject(id: "x", type: "prism.text", props: ["text": ["$expr": "launchMissiles()"]])])).contains("expression.function"))94        #expect(codes(app([PrismObject(id: "x", type: "prism.text", props: ["text": ["$expr": "process.env"]])])).contains("expression.name"))95        #expect(codes(app([PrismObject(id: "x", type: "prism.text", props: ["text": ["$expr": "state.nope"]])])).contains("state.undeclared"))96        #expect(codes(app([PrismObject(id: "x", type: "prism.text", props: ["text": ["$expr": "count(queries.ghost)"]])])).contains("expression.query"))97        #expect(codes(app([PrismObject(id: "x", type: "prism.toggle", props: ["label": "a", "value": true])])).contains("prop.binding"))98        #expect(codes(app([PrismObject(id: "x", type: "prism.text", props: ["text": ["$bind": "category"]])])).contains("prop.binding"))99        #expect(codes(app([PrismObject(id: "x", type: "prism.table", props: ["query": ["table": "ghost"]])])).contains("query.table"))100        #expect(codes(app([PrismObject(id: "x", type: "prism.table", props: ["query": ["table": "tx", "sort": [["column": "nope"]]]])])).contains("query.invalid"))101        #expect(codes(app([PrismObject(id: "x", type: "prism.table", props: ["query": ["table": "tx"], "columns": [["column": "ghost"]]])])).contains("column.unknown"))102        #expect(codes(app([PrismObject(id: "x", type: "prism.chart.bar", props: ["query": ["table": "tx"], "x": "month", "y": "amount"])])).contains("column.unknown"))103        #expect(codes(app([PrismObject(id: "x", type: "prism.text", props: ["text": "a"], children: ["y"]), PrismObject(id: "y", type: "prism.divider")])).contains("children.notAllowed"))104        #expect(codes(app([PrismObject(id: "x", type: "prism.text", props: ["text": "a"], events: ["click": []])])).contains("event.unknown"))105        #expect(codes(app([PrismObject(id: "x", type: "prism.button", props: ["title": "a"], events: ["click": [["type": "runShell", "command": "rm -rf /"]]])])).contains("action.invalid"))106        #expect(codes(app([PrismObject(id: "x", type: "prism.button", props: ["title": "a"], events: ["click": [["type": "createRecord", "table": "tx", "values": ["ghost": 1]]]])])).contains("action.invalid"))107        #expect(codes(app([PrismObject(id: "x", type: "prism.button", props: ["title": "a"], events: ["click": [["type": "navigate", "page": "nowhere"]]])])).contains("action.invalid"))108        #expect(codes(app([PrismObject(id: "x", type: "prism.csvImport", props: ["table": "tx"])])).contains("permission.missing"))109        #expect(codes(app([], permissions: [])).contains("permission.missing"))110        #expect(codes(app([], permissions: [Permission.storageLocal, "network.client"])).contains("permission.unavailable"))111        #expect(codes(app([], permissions: [Permission.storageLocal, "root.everything"])).contains("permission.unknown"))112        #expect(codes(app([PrismObject(id: "bad", type: ObjectType.dataTable, props: ["columns": [["name": "id"]]])])).contains("table.schema"))113    }114115    @Test func accessibilityGapsAreWarnings() {116        let bp = app([PrismObject(id: "x", type: "prism.button", props: ["icon": "plus"])])117        let issues = BlueprintValidator.validate(bp, registry: Catalog.registry)118        #expect(issues.errors.isEmpty && issues.warnings.contains { $0.code == "accessibility.label" })119    }120}121
Files main
Menu

Theme