Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Tests/SPGlassRegistryTests/PublishedRegistryTests.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/// Checks the registry committed in `Registry/`: what users download must validate and match its checksums.46@Suite("Published registry")47struct PublishedRegistryTests {48 static let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("Registry")4950 func index() throws -> RegistryIndex {51 let index = try RegistryJSON.decode(RegistryIndex.self, from: Data(contentsOf: Self.root.appendingPathComponent("registry.json")), what: "index")52 try RegistryValidator.validate(index)53 return index54 }5556 @Test func indexListsTheFiftyComponentsTheirSupportItemsAndTheThemes() throws {57 let index = try index()58 #expect(index.items.filter { $0.kind != "primitive" }.count == 50)59 #expect(index.item("glass-foundation") != nil)60 #expect(index.themes.count == 13 && index.theme("aurora") != nil && index.theme("system") != nil)61 #expect(index.license == "MIT")62 }6364 @Test func everyManifestAndFileMatchesItsChecksum() throws {65 let index = try index()66 var files = 067 for entry in index.items {68 for version in entry.versions {69 let data = try Data(contentsOf: Self.root.appendingPathComponent(version.path))70 #expect(RegistryHash.sha256(data) == version.sha256, "manifest of \(entry.id) \(version.version)")71 let manifest = try RegistryJSON.decode(ComponentManifest.self, from: data, what: entry.id)72 try RegistryValidator.validate(manifest)73 for file in manifest.files {74 let bytes = try Data(contentsOf: Self.root.appendingPathComponent(manifest.registryPath(of: file)))75 #expect(throws: Never.self, "\(entry.id): \(file.path)") { try RegistryHash.verify(bytes, against: file) }76 files += 177 }78 }79 }80 for theme in index.themes {81 for version in theme.versions {82 #expect(RegistryHash.sha256(try Data(contentsOf: Self.root.appendingPathComponent(version.path))) == version.sha256, "theme \(theme.id)")83 }84 }85 #expect(files >= 80)86 }8788 @Test func dependenciesResolveWithoutCycleAndEveryComponentNeedsTheFoundation() throws {89 let index = try index()90 var graph: [String: [String]] = [:]91 for entry in index.items {92 let version = try #require(entry.version(entry.latest))93 let manifest = try RegistryJSON.decode(ComponentManifest.self, from: Data(contentsOf: Self.root.appendingPathComponent(version.path)), what: entry.id)94 graph[entry.id] = manifest.dependencies.map(\.id)95 for dependency in manifest.dependencies { #expect(index.item(dependency.id)?.version(dependency.version) != nil, "\(entry.id) → \(dependency.id)") }96 if entry.id != "glass-foundation" { #expect(graph[entry.id]?.contains("glass-foundation") == true, "\(entry.id)") }97 #expect(manifest.license == "MIT")98 if entry.kind != "primitive" {99 #expect(!manifest.examples.isEmpty, "\(entry.id) has no example")100 #expect(!manifest.accessibility.isEmpty, "\(entry.id) has no accessibility note")101 #expect(!manifest.symbols.isEmpty, "\(entry.id) has no symbol")102 }103 }104 let order = try DependencyGraph.installationOrder(roots: graph.keys.sorted()) { graph[$0] ?? [] }105 #expect(order.count == graph.count && order.first == "glass-foundation")106 }107108 @Test func distributedSourcesCarryTheLicenceAndNoPackageImport() throws {109 let index = try index()110 let foundation = try #require(index.item("glass-foundation")?.version("1.0.0"))111 let manifest = try RegistryJSON.decode(ComponentManifest.self, from: Data(contentsOf: Self.root.appendingPathComponent(foundation.path)), what: "foundation")112 #expect(manifest.files.contains { $0.path == "LICENSE-SPGlassKit.txt" })113 for entry in index.items {114 let version = try #require(entry.version(entry.latest))115 let item = try RegistryJSON.decode(ComponentManifest.self, from: Data(contentsOf: Self.root.appendingPathComponent(version.path)), what: entry.id)116 for file in item.files where file.path.hasSuffix(".swift") {117 let text = try String(contentsOf: Self.root.appendingPathComponent(item.registryPath(of: file)), encoding: .utf8)118 #expect(SwiftSourceScanner.imports(in: text).allSatisfy { !$0.hasPrefix("SPGlass") }, "\(file.path)")119 #expect(text.contains("License") && text.contains("MIT"), "\(file.path) does not state its licence")120 #expect(text.contains("File : Sources/"), "\(file.path): the header must name the original source path")121 }122 }123 }124}125126extension PublishedRegistryTests {127 /// When the registry is signed, the signature must match the public key published in `docs/`.128 @Test func theIndexSignatureMatchesThePublishedPublicKey() throws {129 let signatureURL = Self.root.appendingPathComponent(RegistrySchema.signatureFileName)130 let keyURL = Self.root.deletingLastPathComponent().appendingPathComponent("docs/REGISTRY_PUBLIC_KEY.txt")131 let hasSignature = FileManager.default.fileExists(atPath: signatureURL.path), hasKey = FileManager.default.fileExists(atPath: keyURL.path)132 #expect(hasSignature == hasKey, "a signature and its public key are published together")133 guard hasSignature, hasKey else { return }134 let index = try Data(contentsOf: Self.root.appendingPathComponent("registry.json"))135 try RegistrySignature.verify(index, signature: String(contentsOf: signatureURL, encoding: .utf8), publicKey: String(contentsOf: keyURL, encoding: .utf8))136 #expect(throws: RegistryError.signatureInvalid) {137 try RegistrySignature.verify(index + Data(" ".utf8), signature: String(contentsOf: signatureURL, encoding: .utf8), publicKey: String(contentsOf: keyURL, encoding: .utf8))138 }139 }140}141