Repository Public
1// SP Notes Core2// File: Tests/SPNotesFeaturesTests/SupportTests.swift3// Description: The shared support of the feature screens: failure mapping, banner text, layout resolution, the sample4// world, the string catalogue (every literal has English, the committed catalogue is current), and the5// package rule "no WebKit / JavaScriptCore".6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import SPNotesAPI11import SPNotesDesign12import SPNotesSync13import SwiftUI14import Testing15@testable import SPNotesFeatures16@testable import SPNotesFeaturesCatalog1718enum SourceTree {19 static func root(file: String = #filePath) -> URL {20 URL(fileURLWithPath: file).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()21 }2223 static func swiftFiles(_ relative: String) -> [URL] {24 let base = root().appendingPathComponent(relative)25 let enumerator = FileManager.default.enumerator(at: base, includingPropertiesForKeys: nil)26 return (enumerator?.allObjects as? [URL] ?? []).filter { $0.pathExtension == "swift" }27 }28}2930@Suite("Features support")31struct SupportTests {32 @Test("a repository error becomes a calm, typed failure")33 func failures() {34 #expect(FeatureFailure(RepositoryError.offline).kind == .offline)35 #expect(FeatureFailure(RepositoryError.offline).isOffline)36 #expect(FeatureFailure(RepositoryError.notCached).isOffline)37 #expect(FeatureFailure(RepositoryError.unsupported(feature: "canvas")).kind == .unsupported)38 #expect(FeatureFailure(RepositoryError.stale).kind == .stale)39 #expect(FeatureFailure(APIError.offline).kind == .offline)40 #expect(FeatureFailure(RepositoryError.invalid("Titre vide")).message == "Titre vide")41 let again = FeatureFailure(FeatureFailure(kind: .server, message: "non", code: "relation_exists"))42 #expect(again.code == "relation_exists")43 }4445 @Test("the banner says nothing when fresh, the date of the copy when cached, and stays calm offline")46 func banner() {47 let now = FeatureSamples.now48 let fresh = Freshness(fetchedAt: now, fromCache: false)49 let cached = Freshness(fetchedAt: now.addingTimeInterval(-3 * 3600), fromCache: true)50 #expect(FeatureStatusBanner.text(failure: nil, freshness: fresh, now: now) == nil)51 #expect(FeatureStatusBanner.text(failure: nil, freshness: nil, now: now) == nil)52 let copy = FeatureStatusBanner.text(failure: nil, freshness: cached, now: now)53 #expect(copy?.contains("Copie de l'appareil") == true)54 let offline = FeatureStatusBanner.text(failure: FeatureFailure(RepositoryError.offline), freshness: cached, now: now)55 #expect(offline?.hasPrefix("Hors ligne") == true)56 #expect(FeatureStatusBanner.text(failure: FeatureFailure(RepositoryError.stale), freshness: nil, now: now)?.isEmpty == false)57 #expect(FeatureFormat.relative(now.addingTimeInterval(-5), now: now) == "à l'instant")58 #expect(FeatureFormat.clock(75) == "01:15")59 #expect(FeatureFormat.clock(3723) == "1:02:03")60 }6162 @Test("layout: the override wins; macOS falls back on the container width")63 func layout() {64 #expect(FeatureLayout.resolve(override: .compact, horizontal: .regular, width: 1200) == .compact)65 #expect(FeatureLayout.resolve(override: .regular, horizontal: .compact, width: 300) == .regular)66 #if os(macOS)67 #expect(FeatureLayout.resolve(override: nil, horizontal: nil, width: 390) == .compact)68 #expect(FeatureLayout.resolve(override: nil, horizontal: nil, width: 900) == .regular)69 #endif70 }7172 @Test("the sample world syncs with the fake backend and goes offline on demand")73 func world() async throws {74 let world = try await FeatureWorld()75 let meta = await world.repositories.features()76 #expect(meta?.api == 3)77 world.setOffline(true)78 await #expect(throws: RepositoryError.offline) { _ = try await world.repositories.inbox.count() }79 }8081 @Test("every L(\"…\") literal has an English translation and the committed catalogue is current")82 func stringCatalogue() throws {83 let process = Process()84 process.executableURL = URL(fileURLWithPath: "/usr/bin/env")85 process.arguments = ["python3", SourceTree.root().appendingPathComponent("Scripts/build-string-catalog.py").path, "--check"]86 let pipe = Pipe()87 process.standardOutput = pipe88 process.standardError = pipe89 try process.run()90 process.waitUntilExit()91 let output = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""92 #expect(process.terminationStatus == 0, "\(output)")93 // The compiled catalogue really carries both languages.94 #expect(L("Réessayer") == "Réessayer")95 #expect(FeatureStrings.string("Réessayer", language: "en") == "Retry")96 }9798 @Test("no WebKit, no JavaScriptCore, no raw withAnimation in the feature sources (SPEC §0.4, §0.9)")99 func forbiddenAPIs() throws {100 for file in SourceTree.swiftFiles("Sources/SPNotesFeatures") + SourceTree.swiftFiles("Sources/SPNotesFeaturesCatalog") {101 let text = try String(contentsOf: file, encoding: .utf8)102 #expect(!text.contains("import WebKit"), "\(file.lastPathComponent)")103 #expect(!text.contains("import JavaScriptCore"), "\(file.lastPathComponent)")104 #expect(!text.contains("WKWebView"), "\(file.lastPathComponent)")105 for line in text.split(separator: "\n") where !line.trimmingCharacters(in: .whitespaces).hasPrefix("//") {106 #expect(!line.contains("withAnimation(") || line.contains("withNVAnimation("), "raw withAnimation in \(file.lastPathComponent): \(line)")107 #expect(!line.contains(".animation(") || line.contains(".nvAnimation("), "raw .animation in \(file.lastPathComponent): \(line)")108 }109 }110 }111112 #if os(macOS)113 @Test("hosted snapshot of the shared chrome in the four variants")114 @MainActor func chromeSnapshot() {115 let offline = FeatureFailure(RepositoryError.offline)116 let urls = Snapshot.render("support-chrome") {117 FeatureScaffold(title: "Écran de démonstration", subtitle: "Socle commun des écrans", phase: .failed(offline),118 freshness: Freshness(fetchedAt: FeatureSamples.now.addingTimeInterval(-7200), fromCache: true), retry: {},119 actions: { FeatureIconButton("arrow.clockwise", label: "Actualiser") {} }) { sizeClass in120 ScrollView {121 VStack(alignment: .leading, spacing: NVSpace.lg) {122 NVCard { Text(sizeClass.isCompact ? "Disposition compacte" : "Disposition régulière").nvFont(.body).foregroundStyle(NVColor.fg) }123 FeatureAICard(model: "claude-haiku", title: "Suggestion") {124 Text("Un contenu produit par un modèle, jamais appliqué sans action.").nvFont(.body).foregroundStyle(NVColor.fg)125 FeatureButton("Appliquer", systemImage: "checkmark", kind: .ai) {}126 }127 HStack { FeatureButton("Principal", kind: .primary) {}; FeatureButton("Secondaire") {}; FeatureButton("Supprimer", kind: .destructive) {} }128 FeatureFailureView(failure: FeatureFailure(RepositoryError.unsupported(feature: "x")), retry: {}).frame(height: 220)129 }130 .padding(sizeClass.isCompact ? NVSpace.lg : NVSpace.xl)131 }132 }133 }134 #expect(urls.count == 4)135 }136 #endif137}138