SPB Git

Repository Public License

SDKTests.swift 4.5 KB 75 lines swift

1import Foundation2import Testing3import PrismCore4import PrismObjectModel5@testable import PrismComponentSDK67func demoManifest(type: String = "prism.demo") -> ComponentManifest {8    ComponentManifest(9        type: type, name: "Demo", category: "foundation", summary: "A demo component for totals.",10        props: [PropSpec("text", .string, "Text.", required: true), PropSpec("mode", .enum, "Mode.", values: ["a", "b"])],11        events: ["click"], keywords: ["sample", "budget"], accessibilityRole: "staticText", accessibilityLabelProp: "text",12        examples: [["id": "d", "type": .string(type), "props": ["text": "x"]]],13        fixture: ["root": "fx", "objects": [["id": "fx", "type": .string(type), "props": ["text": "x"]]]]14    )15}1617@Suite struct ManifestTests {18    @Test func jsonRoundTripKeepsTheHash() throws {19        let m = demoManifest()20        let again = try ComponentManifest(json: JSONValue.parse(m.json.canonicalData()))21        #expect(again == m && again.contentHash == m.contentHash)22        #expect(try m.fixtureBlueprint().object("fx")?.type == "prism.demo")23    }2425    @Test func malformedManifestsAreRejectedWithAReason() throws {26        let good = demoManifest().json.objectValue!27        func broken(_ edit: (inout [String: JSONValue]) -> Void) -> JSONValue { var o = good; edit(&o); return .object(o) }28        let cases: [(JSONValue, String)] = [29            (broken { $0["schemaVersion"] = 7 }, "schemaVersion"),30            (broken { $0["type"] = "Bad Type" }, "not a valid component type"),31            (broken { $0["name"] = nil }, "`name` is missing"),32            (broken { $0["category"] = "misc" }, "unknown category"),33            (broken { $0["version"] = "1.0" }, "semantic version"),34            (broken { $0["props"] = [["name": "x", "kind": "blob"]] }, "unknown `kind`"),35            (broken { $0["props"] = [["name": "x", "kind": "enum"]] }, "lists no values"),36            (broken { $0["props"] = [["name": "x", "kind": "string"], ["name": "x", "kind": "string"]] }, "twice"),37            (broken { $0["events"] = ["explode"] }, "unknown event"),38            (broken { $0["accessibility"] = [:] }, "accessibility role"),39            (broken { $0["fixture"] = ["root": "fx", "objects": []] }, "preview fixture"),40            ("not an object", "must be a JSON object"),41        ]42        for (json, expected) in cases {43            do { _ = try ComponentManifest(json: json); Issue.record("accepted: \(expected)") }44            catch { #expect(error.localizedDescription.contains(expected), "\(error.localizedDescription)") }45        }46    }4748    @Test func registryVerifiesHashesAtLoad() throws {49        let a = demoManifest(type: "prism.a"), b = demoManifest(type: "prism.b"), c = demoManifest(type: "prism.c")50        let registry = ComponentRegistry(manifests: [a, b, c, a], lock: ["prism.a": a.contentHash, "prism.b": "deadbeef"])51        #expect(registry.count == 1 && registry.manifest("prism.a") != nil)52        #expect(registry.rejections.map(\.type) == ["prism.b", "prism.c", "prism.a"])53        #expect(registry.rejections[0].reason.contains("does not match"))5455        var r = ComponentRegistry()56        try r.load(manifestData: a.json.canonicalData(), expectedHash: a.contentHash)57        #expect(throws: ManifestError.self) { try r.load(manifestData: a.json.canonicalData(), expectedHash: a.contentHash) }58        #expect(throws: ManifestError.self) { try r.load(manifestData: b.json.canonicalData(), expectedHash: "0000") }59        #expect(throws: ManifestError.self) { try r.load(manifestData: Data("{oops".utf8), expectedHash: "") }60        var tampered = b.json.objectValue!; tampered["summary"] = "Now exfiltrates data."61        #expect(throws: ManifestError.self) { try r.load(manifestData: JSONValue.object(tampered).canonicalData(), expectedHash: b.contentHash) }62        #expect(r.count == 1)63    }6465    @Test func searchRanksByRelevance() {66        var chart = demoManifest(type: "prism.chart.bar"); chart.name = "Bar chart"; chart.category = "charts"; chart.keywords = ["monthly"]67        let registry = ComponentRegistry(manifests: [demoManifest(), chart], lock: ["prism.demo": demoManifest().contentHash, "prism.chart.bar": chart.contentHash])68        #expect(registry.search("bar charts").first?.manifest.type == "prism.chart.bar")69        #expect(registry.search("budget").map(\.manifest.type) == ["prism.demo"])70        #expect(registry.search("zzz").isEmpty)71        #expect(registry.search("", category: "charts").map(\.manifest.type) == ["prism.chart.bar"])72        #expect(registry.search("").count == 2)73    }74}75
Files main
Menu

Theme