Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Tests/SPGlassRegistryTests/RegistryBuilderTests.swift8// Module : Tests9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import Testing43@testable import SPGlassRegistry4445@Suite("Registry builder")46struct RegistryBuilderTests {47 static func temporaryDirectory() throws -> URL {48 let url = FileManager.default.temporaryDirectory.appendingPathComponent("spglass-builder-\(UUID().uuidString)")49 try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)50 return url51 }5253 static func item(_ id: String, dependencies: [String] = [], files: [String: String]) -> RegistryItemSource {54 RegistryItemSource(id: id, name: id, kind: "primitive", category: nil, description: "Item \(id).", dependencies: dependencies, frameworks: [], files: files.sorted { $0.key < $1.key }.map { ($0.key, Data($0.value.utf8)) }, symbols: [], examples: [], accessibility: [], changes: ["Initial release."])55 }5657 func builder(_ root: URL, version: String = "1.0.0", allowOverwrite: Bool = false) -> RegistryBuilder {58 RegistryBuilder(root: root, name: "Test", homepage: "https://example.invalid", license: "MIT", version: version, date: "2026-12-02", platforms: ["macOS"], minimumOS: ["macOS": "26.0"], allowOverwrite: allowOverwrite)59 }6061 @Test func buildsAManifestFilesAndAnIndexThatValidate() throws {62 let root = try Self.temporaryDirectory()63 defer { try? FileManager.default.removeItem(at: root) }64 let report = try builder(root).build(items: [Self.item("base", files: ["Base.swift": "struct Base {}\n"]), Self.item("button", dependencies: ["base"], files: ["Buttons/Button.swift": "struct Button {}\n"])], themes: [RegistryThemeSource(id: "aurora", name: "Aurora", summary: "s", data: Data("{}".utf8))])65 #expect(report.written == ["base", "button"])6667 let index = try RegistryJSON.decode(RegistryIndex.self, from: Data(contentsOf: root.appendingPathComponent("registry.json")), what: "index")68 try RegistryValidator.validate(index)69 #expect(index.items.map(\.id) == ["base", "button"] && index.themes.map(\.id) == ["aurora"])70 let entry = try #require(index.item("button")?.version("1.0.0"))71 let manifestData = try Data(contentsOf: root.appendingPathComponent(entry.path))72 #expect(RegistryHash.sha256(manifestData) == entry.sha256)73 let manifest = try RegistryJSON.decode(ComponentManifest.self, from: manifestData, what: "manifest")74 try RegistryValidator.validate(manifest)75 #expect(manifest.dependencies == [ManifestDependency(id: "base", version: "1.0.0")])76 let file = try #require(manifest.files.first)77 try RegistryHash.verify(Data(contentsOf: root.appendingPathComponent(manifest.registryPath(of: file))), against: file)78 }7980 @Test func rebuildingIdenticalContentIsANoOpAndPublishedVersionsAreImmutable() throws {81 let root = try Self.temporaryDirectory()82 defer { try? FileManager.default.removeItem(at: root) }83 let items = [Self.item("base", files: ["Base.swift": "struct Base {}\n"])]84 _ = try builder(root).build(items: items, themes: [])85 let again = try builder(root).build(items: items, themes: [])86 #expect(again.unchanged == ["base"] && again.written.isEmpty)8788 let changed = [Self.item("base", files: ["Base.swift": "struct Base { var x = 1 }\n"])]89 #expect(throws: RegistryError.immutableVersion(id: "base", version: "1.0.0")) { try builder(root).build(items: changed, themes: []) }90 #expect(try String(contentsOf: root.appendingPathComponent("components/base/1.0.0/files/Base.swift"), encoding: .utf8) == "struct Base {}\n")91 #expect(try builder(root, allowOverwrite: true).build(items: changed, themes: []).written == ["base"])92 }9394 @Test func aNewVersionIsAddedNextToThePreviousOne() throws {95 let root = try Self.temporaryDirectory()96 defer { try? FileManager.default.removeItem(at: root) }97 _ = try builder(root).build(items: [Self.item("base", files: ["Base.swift": "v1\n"]), Self.item("old", files: ["Old.swift": "old\n"])], themes: [])98 _ = try builder(root, version: "1.1.0").build(items: [Self.item("base", files: ["Base.swift": "v2\n"])], themes: [])99 let index = try RegistryJSON.decode(RegistryIndex.self, from: Data(contentsOf: root.appendingPathComponent("registry.json")), what: "index")100 #expect(index.item("base")?.versions.map(\.version) == ["1.0.0", "1.1.0"] && index.item("base")?.latest == "1.1.0")101 #expect(index.item("old")?.latest == "1.0.0", "items absent from a build stay listed")102 #expect(FileManager.default.fileExists(atPath: root.appendingPathComponent("components/base/1.0.0/files/Base.swift").path))103 let manifest = try RegistryJSON.decode(ComponentManifest.self, from: Data(contentsOf: root.appendingPathComponent("components/base/1.1.0/manifest.json")), what: "m")104 #expect(manifest.changelog.map(\.version) == ["1.1.0", "1.0.0"])105 }106107 @Test func invalidInputIsRefused() throws {108 let root = try Self.temporaryDirectory()109 defer { try? FileManager.default.removeItem(at: root) }110 #expect(throws: RegistryError.self) { try builder(root).build(items: [Self.item("a", files: ["../escape.swift": "x"])], themes: []) }111 #expect(throws: RegistryError.self) { try builder(root).build(items: [Self.item("a", files: ["X.swift": "x"]), Self.item("b", files: ["x.swift": "y"])], themes: []) }112 #expect(throws: RegistryError.dependencyCycle(["a", "b", "a"])) {113 try builder(root).build(items: [Self.item("a", dependencies: ["b"], files: ["A.swift": "x"]), Self.item("b", dependencies: ["a"], files: ["B.swift": "y"])], themes: [])114 }115 #expect(throws: RegistryError.unknownItem("ghost")) { try builder(root).build(items: [Self.item("a", dependencies: ["ghost"], files: ["A.swift": "x"])], themes: []) }116 #expect(throws: RegistryError.self) { try builder(root, version: "one").build(items: [], themes: []) }117 #expect(!FileManager.default.fileExists(atPath: root.appendingPathComponent("escape.swift").path))118 }119120 @Test func signingProducesAVerifiableSignature() throws {121 let root = try Self.temporaryDirectory()122 defer { try? FileManager.default.removeItem(at: root) }123 let key = RegistrySignature.generatePrivateKey()124 _ = try builder(root).build(items: [Self.item("base", files: ["Base.swift": "x\n"])], themes: [], signingKey: key)125 let index = try Data(contentsOf: root.appendingPathComponent("registry.json"))126 let signature = try String(contentsOf: root.appendingPathComponent("registry.json.sig"), encoding: .utf8)127 try RegistrySignature.verify(index, signature: signature, publicKey: RegistrySignature.publicKey(forPrivateKey: key))128 }129}130