Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Apps/GlassStudio/KitTests/MediaMapsAIDescriptorTests.swift8// Module : GlassStudio9// 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 SwiftUI43import Testing44import SPGlassKit45@testable import GlassStudioKit46#if os(macOS)47import AppKit48#endif4950@Suite("Media, map and AI descriptors")51@MainActor52struct MediaMapsAIDescriptorTests {53 /// The fifteen components, with whether each one can be placed in a playground composition.54 static let expected: [(id: String, isComposable: Bool)] = [55 ("glass-audio-player", true), ("glass-video-player", false), ("glass-volume-control", true),56 ("glass-gallery", false), ("glass-waveform", true),57 ("glass-map-controls", true), ("glass-compass", true), ("glass-location-button", true),58 ("glass-map-pin", true), ("glass-map-panel", false),59 ("glass-chat-bubble", true), ("glass-prompt-bar", true), ("glass-thinking-indicator", true),60 ("glass-model-picker", true), ("glass-agent-panel", true),61 ]6263 static let hostileTexts = ["\"); fatalError(); (\"", "\\(1)", "first line\nfatalError()"]6465 var descriptors: [ComponentDescriptor] {66 Self.expected.compactMap { ComponentCatalog.standard.descriptor($0.id) }67 }6869 /// Defaults, then each variant.70 func valueSets(_ descriptor: ComponentDescriptor) -> [InspectorValues] {71 [descriptor.defaultValues] + descriptor.variants.map(descriptor.values(for:))72 }7374 /// `values` with every text field replaced by `text` (cut to the field's length by validation).75 func replacingTexts(in values: InspectorValues, with text: String) -> InspectorValues {76 var copy = values77 for field in values.fields {78 if case .text = field.kind { copy[field.id] = .text(text) }79 }80 return copy81 }8283 @Test func everyDescriptorIsInTheCatalogueWithFieldsAndVariants() {84 #expect(descriptors.count == 15)85 for (id, isComposable) in Self.expected {86 guard let descriptor = ComponentCatalog.standard.descriptor(id) else {87 Issue.record("\(id) has no descriptor")88 continue89 }90 #expect((2...7).contains(descriptor.fields.count), "\(id): \(descriptor.fields.count) fields")91 #expect((1...4).contains(descriptor.variants.count), "\(id): \(descriptor.variants.count) variants")92 #expect(Set(descriptor.variants.map(\.id)).count == descriptor.variants.count, "\(id): duplicate variant ids")93 #expect(descriptor.isComposable == isComposable, "\(id): isComposable")94 for variant in descriptor.variants {95 #expect(descriptor.values(for: variant) != descriptor.defaultValues, "\(id) variant \(variant.id) changes nothing")96 }97 }98 }99100 @Test func variantsProduceDistinctSnippets() {101 for descriptor in descriptors {102 let snippets = valueSets(descriptor).map(descriptor.snippet)103 #expect(Set(snippets).count == snippets.count, "\(descriptor.id): two value sets give the same snippet")104 }105 }106107 @Test func hostileTextStaysInsideStringLiterals() {108 for descriptor in descriptors {109 for values in valueSets(descriptor) {110 for text in Self.hostileTexts {111 let snippet = descriptor.snippet(replacingTexts(in: values, with: text))112 let code = SwiftSourceBalance.strippingStrings(snippet)113 #expect(!code.contains("fatalError"), "\(descriptor.id): text escaped its literal in\n\(snippet)")114 #expect(code.filter { $0 == "(" }.count == code.filter { $0 == ")" }.count, "\(descriptor.id): parentheses")115 #expect(code.filter { $0 == "{" }.count == code.filter { $0 == "}" }.count, "\(descriptor.id): braces")116 #expect(snippet.filter { $0 == "\n" }.count == descriptor.snippet(replacingTexts(in: values, with: "x")).filter { $0 == "\n" }.count, "\(descriptor.id): a line break left its literal")117 }118 }119 }120 }121122 @Test func everyTitleIsTranslatedInFrenchAndEnglish() {123 for descriptor in descriptors {124 var titles = descriptor.fields.map(\.title) + descriptor.variants.map(\.title)125 for field in descriptor.fields {126 if case .choice(let options, _) = field.kind { titles += options.map(\.title) }127 }128 for title in titles {129 for language in ["en", "fr"] {130 let found = StudioL10n.tables.contains { StudioL10n.string(title, language: language, table: $0) != nil }131 #expect(found, "\(descriptor.id): \"\(title)\" has no \(language) translation")132 }133 }134 }135 }136137 #if os(macOS)138 @Test func everyPreviewLaysOutOffScreen() {139 for descriptor in descriptors {140 for values in valueSets(descriptor) + [replacingTexts(in: descriptor.defaultValues, with: Self.hostileTexts[0])] {141 let host = NSHostingView(rootView: descriptor.preview(values).frame(width: 420, height: 520))142 host.frame = NSRect(x: 0, y: 0, width: 420, height: 520)143 host.layoutSubtreeIfNeeded()144 #expect(host.fittingSize.width > 0, "\(descriptor.id): the preview has no size")145 }146 }147 }148 #endif149150 @Test func theCompilerHelperWritesOneViewPerSnippet() {151 let unit = SnippetCompilerC.Unit(descriptorID: "glass-x", snippets: ["Text(\"a\")", "// Requires: import AVKit\nText(\"b\")"])152 let source = SnippetCompilerC.source(for: unit, index: 3)153 #expect(source.contains("import SwiftUI\nimport SPGlassKit\nimport AVKit\n"))154 #expect(source.contains("struct Snippet_3_0: View") && source.contains("struct Snippet_3_1: View"))155 #expect(SnippetCompilerC.requiredImports(in: "// Requires: import A B\n// Requires: import ") == [])156 #expect(FileManager.default.fileExists(atPath: SnippetCompilerC.repositoryRoot.appendingPathComponent("Package.swift").path))157 }158159 /// Slow (builds the package from scratch in a temporary directory): run with `GLASS_SNIPPET_COMPILE=1`.160 @Test(.enabled(if: ProcessInfo.processInfo.environment["GLASS_SNIPPET_COMPILE"] == "1"))161 func everySnippetCompiles() throws {162 let combined = Self.hostileTexts.joined(separator: " ")163 let units = descriptors.map { descriptor in164 SnippetCompilerC.Unit(165 descriptorID: descriptor.id,166 snippets: valueSets(descriptor).map(descriptor.snippet)167 + valueSets(descriptor).map { descriptor.snippet(replacingTexts(in: $0, with: combined)) }168 )169 }170 #expect(units.count == 15)171 let outcome = try SnippetCompilerC.compile(units)172 #expect(outcome.succeeded, "swift build failed (\(outcome.status)) for \(outcome.snippetCount) snippets:\n\(outcome.output)")173 #expect(outcome.snippetCount == units.reduce(0) { $0 + $1.snippets.count })174 }175176 /// Negative control: the helper must really invoke the compiler, so a snippet that is not valid Swift177 /// for SPGlassKit has to fail the build.178 @Test(.enabled(if: ProcessInfo.processInfo.environment["GLASS_SNIPPET_COMPILE"] == "1"))179 func aSnippetThatDoesNotCompileFailsTheBuild() throws {180 let unit = SnippetCompilerC.Unit(descriptorID: "negative-control", snippets: ["GlassCompass(heading: \"north\") { }"])181 let outcome = try SnippetCompilerC.compile([unit])182 #expect(!outcome.succeeded, "a snippet with a type error compiled")183 #expect(outcome.output.contains("error:"), "no compiler diagnostic in:\n\(outcome.output)")184 }185}186