Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Tests/GlassCLICoreTests/CLICommandTests.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 Testing43import SPGlassRegistry44import SPGlassThemes45@testable import GlassCLICore4647@Suite("spglass commands")48struct CLICommandTests {49 @Test func versionAndHelp() async throws {50 let fixture = try Fixture(); defer { fixture.cleanUp() }51 #expect(await fixture.run("--version").standardOutput == "spglass 1.0.0\n")52 #expect(await fixture.run().standardOutput.contains("Usage: spglass <command>"))53 #expect(await fixture.run("add", "--help").standardOutput.contains("Usage: spglass add"))54 let json = try fixture.json(await fixture.run("--version", "--json"))55 #expect(json["ok"] as? Bool == true)56 }5758 @Test func initDetectsASwiftPackageAndWritesTheConfiguration() async throws {59 let fixture = try Fixture(); defer { fixture.cleanUp() }60 let outcome = await fixture.run("init", "--registry", fixture.registry.path)61 #expect(outcome.exitCode == 0)62 #expect(outcome.standardOutput.contains("Sources/App/GlassKit"))63 let config = try JSONDecoder().decode(ProjectConfig.self, from: Data(contentsOf: fixture.project.appendingPathComponent("spglass.json")))64 #expect(config.sourcesPath == "Sources/App/GlassKit" && config.themesPath == "Sources/App/GlassKit/CustomThemes")6566 let again = await fixture.run("init", "--registry", fixture.registry.path)67 #expect(again.exitCode == ExitCode.conflict.rawValue, "an existing configuration is not replaced silently")68 #expect(await fixture.run("init", "--registry", fixture.registry.path, "--force").exitCode == 0)69 }7071 @Test func initRefusesAmbiguousProjectsAndUnsafePaths() async throws {72 let fixture = try Fixture(); defer { fixture.cleanUp() }73 try FileManager.default.createDirectory(at: fixture.project.appendingPathComponent("Sources/Other"), withIntermediateDirectories: true)74 let ambiguous = await fixture.run("init", "--registry", fixture.registry.path)75 #expect(ambiguous.exitCode == ExitCode.usage.rawValue && ambiguous.standardError.contains("Several targets"))76 #expect(!fixture.exists("spglass.json"))77 for path in ["../outside", "/tmp/abs", "a/../../b"] {78 let outcome = await fixture.run("init", "--registry", fixture.registry.path, "--path", path)79 #expect(outcome.exitCode != 0 && !fixture.exists("spglass.json"), "\(path)")80 }81 #expect(await fixture.run("init", "--registry", "http://insecure.example", "--path", "Sources/App/G").exitCode != 0)82 #expect(await fixture.run("init", "--registry", fixture.registry.path, "--path", "Sources/App/GlassKit").exitCode == 0)83 }8485 @Test func xcodeProjectsAreDetectedButNeverEdited() throws {86 let fixture = try Fixture(); defer { fixture.cleanUp() }87 try FileManager.default.removeItem(at: fixture.project.appendingPathComponent("Package.swift"))88 try FileManager.default.createDirectory(at: fixture.project.appendingPathComponent("Demo.xcodeproj"), withIntermediateDirectories: true)89 try "// project".write(to: fixture.project.appendingPathComponent("Demo.xcodeproj/project.pbxproj"), atomically: true, encoding: .utf8)90 var detection = ProjectDetection.detect(in: fixture.project)91 #expect(detection.kind == .xcodeProject && detection.suggestedSourcesPath == nil)92 try FileManager.default.createDirectory(at: fixture.project.appendingPathComponent("Demo"), withIntermediateDirectories: true)93 detection = ProjectDetection.detect(in: fixture.project)94 #expect(detection.suggestedSourcesPath == "Demo/GlassKit")95 #expect(detection.notes.joined().contains("never edits the project file"))96 #expect(fixture.read("Demo.xcodeproj/project.pbxproj") == "// project")97 }9899 @Test func listAndInfo() async throws {100 let fixture = try Fixture(); defer { fixture.cleanUp() }101 await fixture.initialize()102 let list = await fixture.run("list")103 #expect(list.standardOutput.contains("glass-button") && list.standardOutput.contains("glass-card"))104 #expect(await fixture.run("list", "--kind", "primitive").standardOutput.split(separator: "\n").count == 1)105 #expect(await fixture.run("list", "--category", "nope").exitCode == ExitCode.usage.rawValue)106 #expect(await fixture.run("theme", "list").standardOutput.contains("aurora"))107 let info = await fixture.run("info", "glass-card")108 #expect(info.standardOutput.contains("depends on: glass-button 1.0.0") && info.standardOutput.contains("Containers/Card.swift"))109 #expect(await fixture.run("info", "ghost").exitCode == ExitCode.notFound.rawValue)110 #expect(await fixture.run("info", "glass-card", "--version", "9.9.9").exitCode == ExitCode.notFound.rawValue)111 let json = try fixture.json(await fixture.run("list", "--json"))112 #expect(((json["result"] as? [String: Any])?["items"] as? [[String: Any]])?.count == 3)113 }114115 @Test func addResolvesDependenciesAndWritesTheLock() async throws {116 let fixture = try Fixture(); defer { fixture.cleanUp() }117 await fixture.initialize()118 let dry = await fixture.run("add", "glass-card", "--dry-run")119 #expect(dry.exitCode == 0 && dry.standardOutput.contains("Dry run: nothing was changed."))120 #expect(!fixture.exists("Sources/App/GlassKit") && !fixture.exists("spglass.lock.json"))121122 let outcome = await fixture.run("add", "glass-card")123 #expect(outcome.exitCode == 0, "\(outcome.standardError)")124 #expect(fixture.read("Sources/App/GlassKit/Containers/Card.swift") == "struct Card { var button = Button() }\n")125 #expect(fixture.exists("Sources/App/GlassKit/Buttons/Button+Style.swift") && fixture.exists("Sources/App/GlassKit/Core/Base.swift"))126 #expect(fixture.exists("Sources/App/GlassKit/Resources/en.lproj/GlassKit.strings"))127 let lock = try fixture.lock()128 #expect(lock.items.map(\.id) == ["glass-button", "glass-card", "glass-foundation"])129 #expect(lock.item("glass-card")?.requested == true && lock.item("glass-button")?.requested == false)130 #expect(lock.item("glass-button")?.files.map(\.path) == ["Sources/App/GlassKit/Buttons/Button+Style.swift", "Sources/App/GlassKit/Buttons/Button.swift"])131 #expect(!fixture.exists(".spglass-staging"))132 let order = outcome.standardOutput.split(separator: "\n").filter { $0.hasPrefix("install") }.map(String.init)133 #expect(order.count == 3 && order[0].contains("glass-foundation") && order[2].contains("glass-card"), "dependencies are installed first")134135 let before = fixture.snapshot()136 let again = await fixture.run("add", "glass-card")137 #expect(again.standardOutput.contains("Everything is up to date.") && fixture.snapshot() == before)138 #expect(await fixture.run("verify").exitCode == 0)139 }140141 @Test func addRefusesUnknownItemsAndVersionsWithoutChangingAnything() async throws {142 let fixture = try Fixture(); defer { fixture.cleanUp() }143 await fixture.initialize()144 let before = fixture.snapshot()145 #expect(await fixture.run("add", "ghost").exitCode == ExitCode.notFound.rawValue)146 #expect(await fixture.run("add", "glass-button", "--version", "3.0.0").exitCode == ExitCode.notFound.rawValue)147 #expect(await fixture.run("add", "../etc").exitCode == ExitCode.usage.rawValue)148 #expect(await fixture.run("add", "glass-button", "glass-card", "--version", "1.0.0").exitCode == ExitCode.usage.rawValue)149 #expect(await fixture.run("add", "glass-button", "--overwrite", "--write-incoming").exitCode == ExitCode.usage.rawValue)150 #expect(fixture.snapshot() == before)151 let uninitialised = try Fixture(); defer { uninitialised.cleanUp() }152 #expect(await uninitialised.run("add", "glass-button", "--registry", uninitialised.registry.path).exitCode == ExitCode.notFound.rawValue)153 }154155 @Test func aModifiedFileIsNeverOverwrittenWithoutConsent() async throws {156 let fixture = try Fixture(); defer { fixture.cleanUp() }157 await fixture.initialize()158 #expect(await fixture.run("add", "glass-button").exitCode == 0)159 let mine = "struct Button {\n var title = \"Mine\"\n var tint = 1\n}\n"160 try fixture.write("Sources/App/GlassKit/Buttons/Button.swift", mine)161 try fixture.publish(version: "1.1.0")162163 let before = fixture.snapshot()164 let update = await fixture.run("update", "glass-button")165 #expect(update.exitCode == ExitCode.conflict.rawValue)166 #expect(update.standardError.contains("Nothing was changed") && update.standardError.contains("--write-incoming"))167 #expect(update.standardError.contains("- var title = \"Mine\"") && update.standardError.contains("+ var isLoading = false"), "the diff is shown")168 #expect(fixture.snapshot() == before, "a refused update changes no file, not even unrelated ones")169170 let dry = await fixture.run("update", "glass-button", "--dry-run")171 #expect(dry.exitCode == ExitCode.conflict.rawValue && fixture.snapshot() == before)172173 let incoming = await fixture.run("update", "glass-button", "--write-incoming")174 #expect(incoming.exitCode == 0, "\(incoming.standardError)")175 #expect(fixture.read("Sources/App/GlassKit/Buttons/Button.swift") == mine, "the local version is kept")176 #expect(fixture.read("Sources/App/GlassKit/Buttons/Button.swift.incoming")?.contains("isLoading") == true)177 #expect(fixture.exists("Sources/App/GlassKit/Buttons/Button+Loading.swift"), "new files of the version are added")178 #expect(!fixture.exists("Sources/App/GlassKit/Buttons/Button+Style.swift"), "an unmodified file dropped by the new version is removed")179 #expect(try fixture.lock().item("glass-button")?.version == "1.1.0")180 let verify = await fixture.run("verify")181 #expect(verify.exitCode == 0 && verify.standardOutput.contains("Buttons/Button.swift"))182 #expect(await fixture.run("verify", "--strict").exitCode == ExitCode.integrity.rawValue)183 }184185 @Test func overwriteIsExplicitAndKeepsACopy() async throws {186 let fixture = try Fixture(); defer { fixture.cleanUp() }187 await fixture.initialize()188 #expect(await fixture.run("add", "glass-button").exitCode == 0)189 try fixture.write("Sources/App/GlassKit/Buttons/Button.swift", "// mine\n")190 try fixture.publish(version: "1.1.0")191 let outcome = await fixture.run("update", "--overwrite")192 #expect(outcome.exitCode == 0, "\(outcome.standardError)")193 #expect(fixture.read("Sources/App/GlassKit/Buttons/Button.swift")?.contains("isLoading") == true)194 #expect(fixture.read("Sources/App/GlassKit/Buttons/Button.swift.orig") == "// mine\n")195 }196197 @Test func updateReplacesUnmodifiedFilesAndKeepsModifiedOnesThatLeftTheItem() async throws {198 let fixture = try Fixture(); defer { fixture.cleanUp() }199 await fixture.initialize()200 #expect(await fixture.run("add", "glass-card").exitCode == 0)201 try fixture.write("Sources/App/GlassKit/Buttons/Button+Style.swift", "enum ButtonStyle { case regular, mine }\n")202 try fixture.publish(version: "1.1.0")203204 let dry = await fixture.run("update", "glass-card", "--dry-run")205 #expect(dry.exitCode == 0 && dry.standardOutput.contains("1.0.0 → 1.1.0"))206 #expect(try fixture.lock().item("glass-button")?.version == "1.0.0")207 let outcome = await fixture.run("update", "glass-card")208 #expect(outcome.exitCode == 0, "\(outcome.standardError)")209 #expect(outcome.standardOutput.contains("keep-modified"))210 #expect(fixture.read("Sources/App/GlassKit/Buttons/Button.swift")?.contains("isLoading") == true)211 #expect(fixture.read("Sources/App/GlassKit/Buttons/Button+Style.swift")?.contains("mine") == true, "a modified file is kept even when the new version drops it")212 let lock = try fixture.lock()213 #expect(lock.items.allSatisfy { $0.version == "1.1.0" })214 #expect(lock.item("glass-button")?.files.contains { $0.path.hasSuffix("Button+Style.swift") } == false)215 #expect(await fixture.run("update", "ghost").exitCode == ExitCode.notFound.rawValue)216 }217218 @Test func addKeepsTheInstalledVersionUnlessAskedOtherwise() async throws {219 let fixture = try Fixture(); defer { fixture.cleanUp() }220 await fixture.initialize()221 #expect(await fixture.run("add", "glass-button").exitCode == 0)222 try fixture.publish(version: "1.1.0")223 #expect(await fixture.run("add", "glass-button").standardOutput.contains("Everything is up to date."))224 #expect(try fixture.lock().item("glass-button")?.version == "1.0.0")225 #expect(await fixture.run("add", "glass-button", "--version", "1.1.0").exitCode == 0)226 #expect(try fixture.lock().item("glass-button")?.version == "1.1.0")227 }228229 @Test func removeChecksDependentsAndPreservesLocalChanges() async throws {230 let fixture = try Fixture(); defer { fixture.cleanUp() }231 await fixture.initialize()232 #expect(await fixture.run("add", "glass-card").exitCode == 0)233 let before = fixture.snapshot()234 let blocked = await fixture.run("remove", "glass-button")235 #expect(blocked.exitCode == ExitCode.conflict.rawValue && blocked.standardError.contains("glass-card"))236 #expect(fixture.snapshot() == before)237 #expect(await fixture.run("remove", "ghost").exitCode == ExitCode.notFound.rawValue)238239 try fixture.write("Sources/App/GlassKit/Containers/Card.swift", "// customised card\n")240 #expect(await fixture.run("remove", "glass-card", "--dry-run").exitCode == 0)241 #expect(fixture.exists("Sources/App/GlassKit/Buttons/Button.swift"))242 let outcome = await fixture.run("remove", "glass-card", "--prune")243 #expect(outcome.exitCode == 0, "\(outcome.standardError)")244 #expect(fixture.read("Sources/App/GlassKit/Containers/Card.swift") == "// customised card\n", "a modified file is kept")245 #expect(!fixture.exists("Sources/App/GlassKit/Buttons") && !fixture.exists("Sources/App/GlassKit/Core"), "unmodified files and emptied directories are removed")246 #expect(try fixture.lock().items.isEmpty)247 }248249 @Test func diffShowsLocalChangesAgainstTheInstalledVersion() async throws {250 let fixture = try Fixture(); defer { fixture.cleanUp() }251 await fixture.initialize()252 #expect(await fixture.run("add", "glass-button").exitCode == 0)253 #expect(await fixture.run("diff", "glass-button").standardOutput == "No local change.\n")254 try fixture.write("Sources/App/GlassKit/Buttons/Button.swift", "struct Button {\n var title = \"Hello\"\n}\n")255 try FileManager.default.removeItem(at: fixture.project.appendingPathComponent("Sources/App/GlassKit/Buttons/Button+Style.swift"))256 let diff = await fixture.run("diff").standardOutput257 #expect(diff.contains("- var title = \"\"") && diff.contains("+ var title = \"Hello\""))258 #expect(diff.contains("missing Sources/App/GlassKit/Buttons/Button+Style.swift"))259 let verify = await fixture.run("verify")260 #expect(verify.exitCode == ExitCode.integrity.rawValue && verify.standardOutput.contains("Missing"))261 #expect(await fixture.run("add", "glass-button").exitCode == ExitCode.conflict.rawValue, "repair does not overwrite the modified file")262 #expect(await fixture.run("add", "glass-button", "--write-incoming").exitCode == 0)263 #expect(fixture.exists("Sources/App/GlassKit/Buttons/Button+Style.swift"), "the missing file is restored")264 #expect(await fixture.run("diff", "ghost").exitCode == ExitCode.notFound.rawValue)265 }266267 @Test func themesAreInstalledAndExportedAsCompilableLookingSwift() async throws {268 let fixture = try Fixture(); defer { fixture.cleanUp() }269 await fixture.initialize()270 #expect(await fixture.run("export", "--format", "swift").exitCode == ExitCode.notFound.rawValue)271 let outcome = await fixture.run("theme", "add", "aurora", "crystal")272 #expect(outcome.exitCode == 0, "\(outcome.standardError)")273 let data = try Data(contentsOf: fixture.project.appendingPathComponent("Sources/App/GlassKit/CustomThemes/aurora.json"))274 #expect(try GlassThemeCodec.decode(data) == .aurora)275276 #expect(await fixture.run("export", "--format", "swift", "--theme", "aurora").exitCode == 0)277 let swift = try #require(fixture.read("Sources/App/GlassKit/CustomThemes/GlassTheme+AuroraTheme.swift"))278 #expect(swift.contains("static let auroraTheme = GlassTheme(") && swift.contains("import SPGlassKit"))279 #expect(await fixture.run("export", "--format", "yaml").exitCode == ExitCode.usage.rawValue)280 #expect(await fixture.run("export", "--format", "json", "--output", "../out").exitCode != 0)281282 try fixture.write("Sources/App/GlassKit/CustomThemes/aurora.json", "{\"id\":\"aurora\",\"name\":\"My Aurora\"}")283 #expect(await fixture.run("theme", "add", "aurora").exitCode == ExitCode.conflict.rawValue)284 #expect(fixture.read("Sources/App/GlassKit/CustomThemes/aurora.json")?.contains("My Aurora") == true)285 #expect(await fixture.run("export", "--format", "swift", "--theme", "aurora").exitCode == ExitCode.conflict.rawValue, "an existing export with other content is not replaced silently")286 #expect(await fixture.run("theme", "add", "ghost").exitCode == ExitCode.notFound.rawValue)287288 #expect(await fixture.run("add", "glass-button").exitCode == 0)289 #expect(await fixture.run("export", "--format", "swift", "--theme", "crystal").exitCode == 0)290 #expect(fixture.read("Sources/App/GlassKit/CustomThemes/GlassTheme+CrystalTheme.swift")?.contains("import SPGlassKit") == false, "in source mode the theme lives in the host module")291 }292293 @Test func doctorReportsTheStateOfTheProject() async throws {294 let fixture = try Fixture(); defer { fixture.cleanUp() }295 let fresh = await fixture.run("doctor")296 #expect(fresh.exitCode == 0 && fresh.standardOutput.contains("! configuration"))297 await fixture.initialize()298 #expect(await fixture.run("add", "glass-button").exitCode == 0)299 let healthy = await fixture.run("doctor")300 #expect(healthy.exitCode == 0 && healthy.standardOutput.contains("✓ registry") && healthy.standardOutput.contains("! authenticity"))301 #expect(healthy.standardOutput.contains("! resources"))302 try FileManager.default.removeItem(at: fixture.project.appendingPathComponent("Sources/App/GlassKit/Buttons/Button.swift"))303 let broken = await fixture.run("doctor", "--json")304 #expect(broken.exitCode == ExitCode.failure.rawValue)305 #expect(try fixture.json(broken)["ok"] as? Bool == false)306 }307308 @Test func jsonOutputIsOneObjectForSuccessAndFailure() async throws {309 let fixture = try Fixture(); defer { fixture.cleanUp() }310 await fixture.initialize()311 let success = try fixture.json(await fixture.run("add", "glass-button", "--json"))312 #expect(success["ok"] as? Bool == true && success["command"] as? String == "add")313 #expect((success["result"] as? [String: Any])?["applied"] as? Bool == true)314 let failure = await fixture.run("add", "ghost", "--json")315 let object = try fixture.json(failure)316 #expect(object["ok"] as? Bool == false && failure.standardError.isEmpty)317 #expect((object["error"] as? [String: Any])?["code"] as? String == "unknown_item")318 let usage = try fixture.json(await fixture.run("nope", "--json"))319 #expect((usage["error"] as? [String: Any])?["code"] as? String == "usage")320 }321}322