SPB Git

Repository Public

NextGenTests.swift 44.1 KB 574 lines swift

1//2//  SP Chat iOS3//  File: SPChat-iOS/Tests/NextGenTests.swift4//  Description: Backend 1.0 « Next Generation »: tolerant decoding of the unified catalogue, logo registry, catalogue5//               search / filter / sort, chat-turn additions (citations, follow-ups, ignored parameters, billed cost),6//               sync plan and engine, provider key management, prompt variables.7//8//  Author: Simon-Pierre Boucher9//  Contact: contact@spboucher.ai10//1112import Foundation13import Testing14import UIKit15@testable import SPChat1617private func catalogue() throws -> ModelsResponse { try JSONDecoder().decode(ModelsResponse.self, from: Data(NextGenFixtures.catalog.utf8)) }1819@Suite struct CatalogueDecodingTests {20    @Test func theUnifiedCatalogueDecodesAndSkipsWhatItCannotRead() throws {21        let r = try catalogue()22        // the entry without id is skipped; the entry from the future (unknown provider / status / route) is kept23        #expect(Set(r.models.map(\.id)) == Set(NextGenFixtures.catalogIds + ["or:newvendor/future-1"]))24        #expect(r.defaultModel == "claude-opus-5-5" && r.dock == ["claude-haiku-5-5", "or:google/gemini-4-neon"] && r.openRouterConfigured && r.catalogTotal == 444 && r.catalogSyncedAt != nil)25    }2627    @Test func directAndOpenRouterModelsCarryMakerRouteAndStatus() throws {28        let models = try catalogue().models29        let haiku = try #require(models.first { $0.id == "claude-haiku-5-5" })30        #expect(haiku.route == "direct" && haiku.creator == "anthropic" && haiku.creatorName == "Anthropic" && haiku.logo == "anthropic" && haiku.isVerified && !haiku.isOpenRouter && !haiku.isCatalogOnly)31        #expect(haiku.isSelectableForChat && haiku.qualityScore != nil)32        let neon = try #require(models.first { $0.id == "or:google/gemini-4-neon" })33        #expect(neon.provider == "openrouter" && neon.route == "openrouter" && neon.creator == "google" && neon.logo == "google" && neon.status == "catalog" && neon.isCatalogOnly && neon.isSelectableForChat)34        #expect(neon.priceInput == 3 && neon.priceOutput == 15 && neon.priceCachedInput == 0.15 && neon.contextWindow == 1_048_576 && neon.priceNotes?.contains("OpenRouter") == true)35        #expect(neon.vision && neon.tools && neon.reasoning && neon.audioInput && !neon.imageGeneration && neon.effortLevels?.contains("xhigh") == true)36        #expect(neon.categories.contains("long-context") && neon.catalog?.slug == "google/gemini-4-neon" && neon.catalog?.inputModalities.contains("video") == true && neon.catalog?.reasoning?.mandatory == true && neon.lastUsedAt != nil)37        // the same model by the other route is a separate entry, never merged38        let twin = try #require(models.first { $0.id == "or:anthropic/claude-haiku-5.5" })39        #expect(twin.creator == haiku.creator && twin.route != haiku.route && twin.id != haiku.id)40    }4142    @Test func unknownProviderStatusAndCapabilityValuesNeverBreakDecoding() throws {43        let future = try #require(try catalogue().models.first { $0.id == "or:newvendor/future-1" })44        #expect(future.provider == "quantum" && future.status == "experimental" && future.route == "hyperloop" && future.logo == nil && future.categories == ["telepathy"])45        #expect(!future.vision)                                   // "unknown" is not a verified capability46        #expect(future.isSelectableForChat && !future.isVerified && !future.isCatalogOnly && !future.isOpenRouter)47        #expect(AIModel.providerLabel("quantum") == "quantum")48    }4950    @Test func aLocalModelDecodesWithItsRouteAndServiceFacts() throws {51        let json = #"{"id":"local:maclustr-qwen3","provider":"local","displayName":"Qwen3-30B-A3B Instruct (MLX 4-bit)","kind":"chat","status":"catalog","available":true,"tested":false,"capabilities":{"text":true,"tools":true,"streaming":true},"contextWindow":32768,"pricing":{"input":0,"output":0,"notes":"Inférence locale MacLustr : aucun coût par jeton."},"benchmark":null,"hidden":false,"favorite":false,"route":"local","creator":"qwen","creatorName":"Qwen","logo":"qwen","categories":["local","open-weight","tools"],"catalog":{"source":"local","slug":"maclustr-qwen3","inputModalities":["text"],"outputModalities":["text"],"supportedParameters":["tools","temperature"],"quantization":"4-bit","license":"apache-2.0","revision":"a1b2c3d","servedBy":"MacLustr AI · M3U96a","huggingFaceId":"mlx-community/Qwen3-30B-A3B-Instruct-4bit"}}"#52        let m = try JSONDecoder().decode(AIModel.self, from: Data(json.utf8))53        #expect(m.route == "local" && m.isLocal && !m.isOpenRouter && m.provider == "local" && m.logo == "qwen" && m.isCatalogOnly && m.isSelectableForChat)54        #expect(m.isFree && m.isOpenWeight && m.categories.contains("local") && m.priceLabel == "gratuit")55        #expect(m.catalog?.source == "local" && m.catalog?.quantization == "4-bit" && m.catalog?.license == "apache-2.0" && m.catalog?.revision == "a1b2c3d" && m.catalog?.servedBy == "MacLustr AI · M3U96a")56        #expect(try JSONDecoder().decode(AIModel.self, from: JSONEncoder().encode(m)) == m)57        #expect(m.nameWithRoute.hasSuffix("· local") && AIModel.routeLabel("local") == "Local (MacLustr)" && ModelQuery.categoryLabel("local") == "Local")58        // an older or partial payload: the route is implied by the id prefix or the provider59        let bare = try JSONDecoder().decode(AIModel.self, from: Data(#"{"id":"local:x","displayName":"X","kind":"chat","available":true}"#.utf8))60        #expect(bare.route == "local" && bare.provider == "local" && bare.logo == nil)61        #expect(AIModel.impliedRoute(id: "or:a/b", provider: nil) == "openrouter" && AIModel.impliedRoute(id: "gpt-6", provider: "openai") == "direct" && AIModel.impliedRoute(id: "m", provider: "local") == "local")62        let identity = ModelIdentity(model: nil, modelId: "local:maclustr-qwen3", provider: "local")63        #expect(identity.route == "local" && identity.name == "maclustr-qwen3" && identity.logo == nil)64        // a local answer costs nothing: neither « facturé » nor « estimé »65        #expect(AIModel.costLabel(0, exact: false, route: "local") == "local · 0 $" && AIModel.costLabel(nil, exact: false, route: "local") == "local · 0 $")66        #expect(AIModel.costLabel(0.02, exact: true, route: "openrouter")?.hasSuffix("facturé") == true && AIModel.costLabel(0.02, exact: false, route: "direct")?.hasSuffix("estimé") == true && AIModel.costLabel(nil, exact: false, route: "direct") == nil)67        var q = ModelQuery(); q.route = "local"68        #expect(q.apply(to: [m, bare, AIModel(id: "claude-x", provider: "anthropic", displayName: "Claude X")]).count == 2)69    }7071    @Test func theLocalProviderRowDecodesLikeAManageableRoute() throws {72        let json = #"{"providers":[{"id":"local","name":"MacLustr AI (local)","kind":"models","route":"local","logo":"","configured":true,"keySource":"stored","keyHint":"…t3st","manageable":true,"validatedAt":"2026-11-19T17:00:00.000Z","info":{"models":1},"models":{"total":1,"chat":1},"health":{"state":"ok"}}],"categoryRules":{"local":"Servi par MacLustr AI."}}"#73        let r = ProvidersResponse(try JSONDecoder().decode(JSONValue.self, from: Data(json.utf8)))74        let p = try #require(r.providers.first)75        #expect(p.id == "local" && p.route == "local" && p.manageable && p.configured && p.keySource == "stored" && p.modelsChat == 1 && p.validatedAt != nil && p.logo == "")76        #expect(LogoRegistry.asset(p.logo) == nil && r.categoryRules["local"] != nil)          // no logo → neutral monogram77    }7879    @Test func aModelFromAnOlderServerGetsItsMakerFromItsProvider() throws {80        let old = try JSONDecoder().decode(AIModel.self, from: Data(#"{"id":"gpt-6","provider":"openai","displayName":"GPT-6","kind":"chat","available":true,"hidden":false,"favorite":false}"#.utf8))81        #expect(old.route == "direct" && old.creator == "openai" && old.creatorName == "OpenAI" && old.logo == "openai" && old.status == "verified" && old.categories.isEmpty)82    }8384    @Test func theCatalogueRoundTripsThroughTheLocalCache() throws {85        let models = try catalogue().models86        let back = try JSONDecoder().decode([AIModel].self, from: JSONEncoder().encode(models))87        for (a, b) in zip(models, back) where a != b { Issue.record("aller-retour différent pour \(a.id) : \(String(describing: a).commonPrefix(with: String(describing: b)).suffix(120))") }88        #expect(back.count == models.count)89        #expect(back.first { $0.id == "or:newvendor/future-1" }?.logo == nil)      // an explicit null logo stays a monogram90    }9192    @Test func everyRegistryLogoIsBundledAndUnknownMakersGetAMonogram() {93        #expect(LogoRegistry.makers.count >= 40)94        for (slug, maker) in LogoRegistry.makers {95            #expect(UIImage(named: "Logos/\(slug)") != nil, "logo manquant : \(slug)")96            if maker.color { #expect(UIImage(named: "Logos/\(slug)-color") != nil, "logo couleur manquant : \(slug)") }97        }98        #expect(LogoRegistry.slug(for: "meta-llama") == "meta" && LogoRegistry.slug(for: "x-ai") == "xai" && LogoRegistry.slug(for: "OpenAI") == "openai")99        #expect(LogoRegistry.slug(for: "inclusionai") == nil && LogoRegistry.asset(nil) == nil && LogoRegistry.asset("nope") == nil)100        #expect(LogoRegistry.asset("google", color: true) == "Logos/google-color" && LogoRegistry.asset("anthropic", color: true) == "Logos/anthropic")101    }102103    @Test func liveModelIntelligenceKeepsEveryBlockWithItsSource() throws {104        let d = try #require(ModelDetail(try JSONDecoder().decode(JSONValue.self, from: Data(NextGenFixtures.modelDetail.utf8))))105        #expect(d.model.id == "or:google/gemini-4-neon" && d.efforts.contains("xhigh") && d.acceptsTemperature == true)106        let measured = try #require(d.measured)107        #expect(measured.requests == 1 && measured.p50TtftMs == 10207 && measured.source.contains("ce compte") && (measured.tokensPerSec ?? 0) > 30)108        #expect(d.endpointsSource?.contains("OpenRouter") == true && d.endpoints.count == 5)109        let first = try #require(d.endpoints.first)110        #expect(first.provider == "Google AI Studio" && first.inputPerM == 1.5 && first.uptimeLast30m == 100 && first.supportsTools == true)111        #expect(d.endpoints.last?.uptimeLast30m == nil)112        let bench = try #require(d.benchmarks.first)113        #expect(bench.source == "artificial_analysis" && bench.figures.contains { $0.name == "intelligence_index" && $0.value == 61.4 })114        #expect(ModelDetail(.object(["params": .object([:])])) == nil)115    }116117    @MainActor @Test func providersDecodeWithTheirKeyState() throws {118        let r = ProvidersResponse(try JSONDecoder().decode(JSONValue.self, from: Data(NextGenFixtures.providers.utf8)))119        #expect(r.providers.map(\.id) == ["anthropic", "openrouter", "tavily"] && r.categoryRules["vision"] != nil)120        let or = try #require(r.providers.first { $0.id == "openrouter" })121        #expect(or.manageable && or.configured && or.keySource == "env" && or.keyHint == "…t3st" && or.modelsChat == 444 && or.catalogSyncedAt != nil && or.route == "openrouter")122        let tavily = try #require(r.providers.first { $0.id == "tavily" })123        #expect(tavily.kind == "tool" && tavily.manageable)124        #expect(r.providers.first?.manageable == false && r.providers.first?.healthState == "ok")125        #expect(ProvidersView.keyLine(or).contains("environnement"))126    }127}128129@Suite struct CatalogueQueryTests {130    private func model(_ id: String, _ name: String, creator: String, route: String = "openrouter", input: Double? = nil, output: Double? = nil, context: Int? = nil, created: Double? = nil, status: String = "catalog",131                       caps: [ModelCapability] = [], categories: [String] = [], favorite: Bool = false, used: Date? = nil, quality: Double? = nil) -> AIModel {132        var m = AIModel(id: id, provider: route == "openrouter" ? "openrouter" : creator, displayName: name, route: route, creator: creator, status: status)133        m.priceInput = input; m.priceOutput = output; m.contextWindow = context; m.categories = categories; m.favorite = favorite; m.lastUsedAt = used; m.qualityScore = quality134        m.vision = caps.contains(.vision); m.reasoning = caps.contains(.reasoning); m.tools = caps.contains(.tools); m.imageGeneration = caps.contains(.imageGeneration)135        if let created { m.catalog = try? JSONDecoder().decode(CatalogInfo.self, from: Data(#"{"created":\#(created)}"#.utf8)) }136        return m137    }138    private var models: [AIModel] {139        [model("claude-opus-5-5", "Claude Opus 5.5", creator: "anthropic", route: "direct", input: 4, output: 20, context: 1_000_000, status: "verified", caps: [.vision, .reasoning, .tools], quality: 100),140         model("or:anthropic/claude-opus-5.5", "Claude Opus 5.5", creator: "anthropic", input: 4, output: 20, context: 1_000_000, created: 300, caps: [.vision, .tools]),141         model("or:google/gemini-4-neon", "Gemini 4 Neon", creator: "google", input: 3, output: 15, context: 1_048_576, created: 500, caps: [.vision, .reasoning, .tools], categories: ["long-context"], used: Date()),142         model("or:meta-llama/llama-5-70b:free", "Llama 5 70B (free)", creator: "meta", input: 0, output: 0, context: 131_072, created: 400, caps: [.tools], categories: ["free", "open-weight"]),143         model("or:mistralai/mistral-nano", "Mistral Nano", creator: "mistral", input: 0.05, output: 0.2, context: 32_000, created: 100, favorite: true),144         model("or:mystery/unpriced", "Unpriced", creator: "mystery", context: 8000, created: 50)]145    }146147    @Test func textSearchMatchesNameIdAndMakerWithEveryToken() {148        var q = ModelQuery(); q.text = "opus"149        #expect(q.apply(to: models).map(\.id).sorted() == ["claude-opus-5-5", "or:anthropic/claude-opus-5.5"])150        q.text = "ANTHROPIC or:"; #expect(q.apply(to: models).map(\.id) == ["or:anthropic/claude-opus-5.5"])151        q.text = "meta 70b"; #expect(q.apply(to: models).map(\.id) == ["or:meta-llama/llama-5-70b:free"])152        q.text = "zzz"; #expect(q.apply(to: models).isEmpty)153    }154155    @Test func filtersCombine() {156        var q = ModelQuery(); q.route = "openrouter"; q.capabilities = [.vision, .reasoning]157        #expect(q.apply(to: models).map(\.id) == ["or:google/gemini-4-neon"] && q.activeFilterCount == 3)158        q = ModelQuery(); q.freeOnly = true; #expect(q.apply(to: models).map(\.id) == ["or:meta-llama/llama-5-70b:free"])159        q = ModelQuery(); q.openWeightOnly = true; #expect(q.apply(to: models).count == 1)160        q = ModelQuery(); q.makers = ["anthropic", "google"]; #expect(q.apply(to: models).count == 3)161        q = ModelQuery(); q.maxInputPrice = 1; #expect(Set(q.apply(to: models).map(\.creator)) == ["meta", "mistral"])      // a model without a price is not "cheap"162        q = ModelQuery(); q.minContext = 400_000; #expect(q.apply(to: models).count == 3)163        q = ModelQuery(); q.categories = ["long-context"]; q.verifiedOnly = true; #expect(q.apply(to: models).isEmpty)164        q = ModelQuery(); q.favoritesOnly = true; #expect(q.apply(to: models).map(\.id) == ["or:mistralai/mistral-nano"])165        q.text = "x"; q.sort = .name; q.clearFilters(); #expect(!q.hasFilters && q.text == "x" && q.sort == .name)166    }167168    @Test func sorts() {169        func ids(_ s: ModelSort) -> [String] { ModelQuery.sorted(models, by: s).map(\.id) }170        #expect(ids(.priceAscending).first == "or:meta-llama/llama-5-70b:free" && ids(.priceAscending).last == "or:mystery/unpriced")171        #expect(ids(.priceDescending).first?.contains("opus") == true && ids(.priceDescending).last == "or:mystery/unpriced")172        #expect(ids(.context).first == "or:google/gemini-4-neon")173        #expect(ids(.newest).first == "or:google/gemini-4-neon" && ids(.newest).last == "claude-opus-5-5")174        #expect(ids(.recentlyUsed).first == "or:google/gemini-4-neon")175        #expect(ids(.name).last == "or:mystery/unpriced")176        // recommended: favourites, then verified, then recently used, then newest177        #expect(Array(ids(.recommended).prefix(3)) == ["or:mistralai/mistral-nano", "claude-opus-5-5", "or:google/gemini-4-neon"])178        // an exact or prefix name match wins over everything while searching179        #expect(ModelQuery.sorted(models, by: .recommended, text: "gemini").first?.id == "or:google/gemini-4-neon")180    }181182    @Test func makerChipsCountModels() {183        let makers = ModelQuery.makers(in: models)184        #expect(makers.first?.slug == "anthropic" && makers.first?.count == 2 && makers.count == 5)185    }186187    @Test func filteringTheWholeCatalogueIsInstant() {188        var big: [AIModel] = []189        for i in 0..<500 { big.append(model("or:vendor\(i % 40)/model-\(i)", "Model \(i)", creator: "vendor\(i % 40)", input: Double(i % 7), output: Double(i % 11), context: 8000 * (1 + i % 50), created: Double(i), caps: i % 2 == 0 ? [.vision, .tools] : [.reasoning])) }190        var q = ModelQuery(); q.text = "model 4"; q.capabilities = [.vision]191        let clock = ContinuousClock(); var n = 0192        let elapsed = clock.measure { for _ in 0..<20 { n += q.apply(to: big).count } }193        #expect(n > 0 && elapsed < .milliseconds(600), "20 recherches filtrées sur 500 modèles : \(elapsed)")194    }195}196197@Suite struct TurnAdditionsTests {198    @Test func aCitedAnswerDecodesSourcesFollowUpsAndBilledCost() throws {199        let m = try JSONDecoder().decode(ChatMessage.self, from: Data(NextGenFixtures.citedMessage.utf8))200        #expect(m.modelId == "or:google/gemini-4-neon" && m.provider == "openrouter" && m.routing?.servedBy == "OpenRouter · Google")201        #expect(m.citations.count == 6)                               // the duplicated source is shown once202        #expect(m.citations.first?.domain == "postgresql.org" && m.citations.first?.displayTitle == "PostgreSQL")203        #expect(m.followups.count == 3 && m.exactCost && m.usage?.costUsd != nil && m.ignoredParams.isEmpty)204        let back = try JSONDecoder().decode(ChatMessage.self, from: JSONEncoder().encode(m))205        #expect(back.followups == m.followups && back.citations == m.citations && back.exactCost)206        let identity = ModelIdentity(model: nil, modelId: m.modelId, provider: m.provider)207        #expect(identity.logo == "google" && identity.route == "openrouter" && identity.name == "google/gemini-4-neon")208        #expect(ModelIdentity(model: nil, modelId: "or:unknownvendor/x", provider: "openrouter").logo == nil)209    }210211    @Test func theStreamCarriesCitationsFollowUpsIgnoredParametersAndBilledCost() {212        func item(_ i: Int, _ name: String, _ json: String) -> StreamItem { StreamItem(index: i, event: ChatStreamEvent(sse: SSEEvent(event: name, data: json, id: i))) }213        var s = ThreadState()214        let events: [(String, String)] = [215            ("conversation", #"{"id":"conv_x","created":true,"userMessageId":"msg_u"}"#),216            ("start", #"{"messageId":"msg_a","model":"or:x/vision-less","provider":"openrouter","routing":{"mode":"manual"},"applied":{"applied":{"webSearch":true},"ignored":[{"param":"images","requested":1,"reason":"Ce modèle n'accepte pas les images."}]}}"#),217            ("tool_call", #"{"id":"t1","name":"web_search","input":{"query":"q"},"status":"running"}"#),218            ("part", #"{"type":"citation","url":"https://a.example/x","title":"A","snippet":"s"}"#),219            ("part", #"{"type":"citation","url":"https://a.example/x","title":"A","snippet":"s"}"#),220            ("part", #"{"type":"citation","url":"https://b.example/y"}"#),221            ("tool_result", #"{"id":"t1","status":"ok","durationMs":900}"#),222            ("meta", #"{"servedBy":"OpenRouter · Acme"}"#),223            ("text", #"{"delta":"Réponse."}"#),224            ("usage", #"{"usage":{"input":10,"output":4,"costUsd":0.002},"costUsd":0.002,"latencyMs":1500,"ttftMs":700,"exactCost":true}"#),225            ("status", #"{"messageId":"msg_a","status":"complete"}"#),226            ("followups", #"{"messageId":"msg_a","suggestions":["Un ?","Deux ?","Trois ?","Quatre ?"]}"#),227            ("done", #"{"messageId":"msg_a","status":"complete"}"#), ("end", "{}"),228        ]229        for (i, e) in events.enumerated() { StreamReducer.apply(item(i, e.0, e.1), to: &s) }230        let a = s.messages.last231        #expect(a?.citations.map(\.url) == ["https://a.example/x", "https://b.example/y"])232        #expect(a?.followups == ["Un ?", "Deux ?", "Trois ?"] && a?.exactCost == true && a?.costUsd == 0.002 && a?.routing?.servedBy == "OpenRouter · Acme")233        #expect(a?.ignoredParams.first?.param == "images" && a?.ignoredParams.first?.reason.contains("images") == true && a?.status == .complete && s.ended)234        // an estimated cost stays labelled as an estimate235        var t = ThreadState()236        for (i, e) in [("start", #"{"messageId":"m"}"#), ("usage", #"{"usage":{"input":1,"output":1},"costUsd":0.001}"#)].enumerated() { StreamReducer.apply(item(i, e.0, e.1), to: &t) }237        #expect(t.messages.last?.exactCost == false && t.messages.last?.costUsd == 0.001)238    }239240    @Test func sendOptionsCarryWebSearchToolSubsetAndOpenRouterIds() {241        var o = SendOptions(modelId: "or:anthropic/claude-opus-5.5", routingMode: .manual, effort: .low, toolsEnabled: false)242        o.webSearch = true; o.toolNames = ["python"]; o.temperature = 0.3; o.maxOutputTokens = 4000243        let p = o.payload()244        #expect(p["modelId"] == .string("or:anthropic/claude-opus-5.5") && p["toolsEnabled"] == .bool(true))                 // a web search needs the tools245        #expect(p["settings"] == .object(["effort": .string("low"), "webSearch": .bool(true), "temperature": .number(0.3), "maxOutputTokens": .number(4000)]))246        #expect(p["toolNames"] == .array([.string("python"), .string("web_search")]))247        let plain = SendOptions(modelId: "m", routingMode: .manual, toolsEnabled: true).payload()248        #expect(plain["settings"] == nil && plain["toolNames"] == nil)249        // options written by version 1.0 (outbox rows) still decode250        let old = try? JSONDecoder().decode(SendOptions.self, from: Data(#"{"modelId":"m","routingMode":"manual","toolsEnabled":true}"#.utf8))251        #expect(old?.modelId == "m" && old?.webSearch == nil)252    }253254    @MainActor @Test func theToolPickerBecomesAToolSubset() {255        let suite = "spchat.tests.prefs.\(UUID().uuidString)"256        let prefs = Preferences(defaults: UserDefaults(suiteName: suite)!)257        let tools = ["web_search", "fetch_page", "python"].map { ServerTool(name: $0) }258        #expect(prefs.sendOptions(tools: tools).toolNames == nil && prefs.sendOptions(tools: tools).webSearch == nil)259        prefs.disabledTools = ["python"]; prefs.webSearch = true260        let o = prefs.sendOptions(tools: tools)261        #expect(o.toolNames == ["web_search", "fetch_page"] && o.webSearch == true)262        #expect(Preferences(defaults: UserDefaults(suiteName: suite)!).disabledTools == ["python"])263        prefs.resetAccountScoped(); #expect(prefs.disabledTools.isEmpty && !prefs.webSearch)264    }265266    @Test func promptVariablesAreFoundAndFilled() {267        let body = "Résume {{ sujet }} pour {{public}} ; encore {{sujet}}. {{}} {{a\nb}}"268        #expect(PromptTemplate.variables(in: body) == ["sujet", "public"])269        #expect(PromptTemplate.fill(body, values: ["sujet": "les phares"]) == "Résume les phares pour {{public}} ; encore les phares. {{}} {{a\nb}}")270        #expect(SavedPrompt(id: "p", title: "t", body: "sans variable").variables.isEmpty)271    }272273    @Test func composerTriggersForCommandsAndModelMentions() {274        #expect(ComposerSuggestion.trigger(in: "/") == .command("") && ComposerSuggestion.trigger(in: "/res") == .command("res"))275        #expect(ComposerSuggestion.trigger(in: "bonjour /x") == nil && ComposerSuggestion.trigger(in: "/a\nb") == nil)276        #expect(ComposerSuggestion.trigger(in: "@gem") == .model("gem") && ComposerSuggestion.trigger(in: "demande @op") == .model("op"))277        #expect(ComposerSuggestion.trigger(in: "mail@exemple") == nil && ComposerSuggestion.trigger(in: "@opus fini") == nil)278    }279280    @Test func tokensAndPricesAreFormattedWithoutNoise() {281        #expect(Format.tokens(1_048_576) == "1M" && Format.tokens(1_000_000) == "1M" && Format.tokens(2_500_000) == "2.5M" && Format.tokens(128_000) == "128k" && Format.tokens(900) == "900")282        #expect(Format.price(0) == "0" && ["0,15", "0.15"].contains(Format.price(0.15)) && Format.price(3) == "3" && ["0,075", "0.075"].contains(Format.price(0.075)) && Format.price(150) == "150")283    }284}285286@Suite struct SyncPlanTests {287    @Test func eventsBecomeAPlanAndOldSequencesAreIgnored() {288        var plan = SyncPlan()289        let head = plan.absorb([SyncEvent(seq: 3, entity: "model"), SyncEvent(seq: 9, entity: "conversation", id: "conv_a"), SyncEvent(seq: 10, entity: "message", id: "msg_1"),290                                SyncEvent(seq: 11, entity: "conversation", id: "conv_b", op: "delete"), SyncEvent(seq: 12, entity: "prompt"), SyncEvent(seq: 13, entity: "settings"),291                                SyncEvent(seq: 14, entity: "draft", id: "new"), SyncEvent(seq: 15, entity: "galaxy"), SyncEvent(seq: 16, entity: "provider", id: "openrouter"), SyncEvent(seq: 17, entity: "project")], cursor: 5)292        #expect(head == 17)293        #expect(plan.conversationList && plan.openConversation && plan.conversations == ["conv_a"] && plan.deletedConversations == ["conv_b"])294        #expect(plan.prompts && plan.settings && plan.drafts == ["new"] && plan.providers && plan.models && plan.catalogData && !plan.memory && !plan.everything)295        var empty = SyncPlan(); #expect(empty.absorb([SyncEvent(seq: 2, entity: "model")], cursor: 5) == 5 && empty.isEmpty)296        // a conversation deleted then recreated (or the reverse) ends in its last state297        var p = SyncPlan(); p.absorb([SyncEvent(seq: 1, entity: "conversation", id: "c", op: "delete"), SyncEvent(seq: 2, entity: "conversation", id: "c")], cursor: 0)298        #expect(p.conversations == ["c"] && p.deletedConversations.isEmpty)299        #expect(SyncPlan.all.everything && SyncPlan.all.models && !SyncPlan.all.isEmpty)300    }301302    @Test func feedEventsDecode() {303        func ev(_ n: String, _ j: String) -> SyncFeedEvent { SyncFeedEvent(sse: SSEEvent(event: n, data: j, id: nil)) }304        #expect(ev("hello", #"{"head":42,"runs":[]}"#) == .hello(head: 42) && ev("resync", #"{"head":90}"#) == .resync(head: 90) && ev("later", "{}") == .other)305        #expect(ev("change", #"{"seq":7,"entity":"message","id":"msg_1","op":"upsert","at":"2026-11-19T14:20:30.265Z","origin":null}"#) == .change(SyncEvent(seq: 7, entity: "message", id: "msg_1")))306        #expect(ev("change", #"{"entity":"message"}"#) == .other)307        let pull = SyncPull(try! JSONDecoder().decode(JSONValue.self, from: Data(#"{"events":[{"seq":1,"entity":"provider","id":"tavily","op":"upsert"}],"head":7,"truncated":false,"runs":[]}"#.utf8)))308        #expect(pull.head == 7 && pull.events.count == 1 && !pull.truncated)309    }310}311312/// A scripted backend for the stores of the next-generation features.313private func route(host: String, _ handler: @escaping @Sendable (_ method: String, _ path: String, _ query: String, _ body: [String: JSONValue]) -> StubProtocol.Response?) {314    StubProtocol.install(host: host) { req, body in315        let json = (body.flatMap { try? JSONDecoder().decode([String: JSONValue].self, from: $0) }) ?? [:]316        if let r = handler(req.httpMethod ?? "GET", req.url?.path ?? "", req.url?.query ?? "", json) { return r }317        switch req.url?.path ?? "" {318        case "/api/models": return .json(NextGenFixtures.catalog)319        case "/api/presets": return .json(#"{"presets":[]}"#)320        case "/api/projects": return .json(#"{"projects":[]}"#)321        case "/api/prompts": return .json(#"{"prompts":[]}"#)322        case "/api/settings": return .json(#"{"settings":{"assistant":{"followUps":true}},"tools":[{"name":"web_search","category":"web"},{"name":"python","category":"code"}],"integrations":{"tavily":true}}"#)323        case "/api/conversations": return .json(#"{"conversations":[]}"#)324        case "/api/providers": return .json(NextGenFixtures.providers)325        default: return .json("{}")326        }327    }328}329private func requests(_ host: String, _ method: String, _ path: String) -> [URLRequest] { StubProtocol.requests(host: host).filter { $0.httpMethod == method && $0.url?.path == path } }330331@Suite(.serialized) @MainActor struct CatalogueStoreTests {332    @Test func theCatalogueLoadsAllRoutesIsCachedAndWorksWithOpenRouterIds() async throws {333        let host = "catalog1.test"; route(host: host) { _, _, _, _ in nil }334        let env = TestEnv.make(host: host)335        await env.catalog.refresh()336        #expect(requests(host, "GET", "/api/models").first?.url?.query == "routes=all")337        #expect(env.catalog.openRouter.count >= 3 && env.catalog.direct.map(\.id) == ["claude-haiku-5-5"] && env.catalog.dockModels.map(\.id) == ["claude-haiku-5-5", "or:google/gemini-4-neon"])338        #expect(env.catalog.model("or:anthropic/claude-haiku-5.5")?.route == "openrouter" && env.catalog.effectiveModelId(preferred: "or:google/gemini-4-neon") == "or:google/gemini-4-neon")339        #expect(env.catalog.effectiveModelId(preferred: "gone") == "claude-haiku-5-5")           // unknown preference → a verified model, not a random catalogue entry340        #expect(env.catalog.recents.first?.id == "or:google/gemini-4-neon")341        // readable offline from the local cache342        let cached: [AIModel]? = await env.store.value(for: "models.v2")343        #expect(cached?.count == env.catalog.models.count)344        env.select(model: "or:google/gemini-4-neon")345        #expect(env.sendOptions.modelId == "or:google/gemini-4-neon" && env.preferences.routingMode == .manual)346    }347348    @Test func favouritesAndDockAreAppliedThenRolledBackWhenTheServerRefuses() async throws {349        let host = "catalog2.test"; let fail = NSLockBox(false)350        route(host: host) { method, path, _, _ in method == "PATCH" && path == "/api/models" && fail.withLock({ $0 }) ? .json(#"{"error":"non"}"#, status: 500) : nil }351        let env = TestEnv.make(host: host)352        await env.catalog.refresh()353        let neon = try #require(env.catalog.model("or:google/gemini-4-neon"))354        await env.catalog.toggleFavorite(neon)355        #expect(env.catalog.model(neon.id)?.favorite == true && env.catalog.favorites.map(\.id) == [neon.id])356        await env.catalog.toggleDock("or:anthropic/claude-haiku-5.5")357        #expect(env.catalog.dock.count == 3)358        let sent = requests(host, "PATCH", "/api/models").compactMap { $0.httpBody ?? $0.httpBodyStream.map { _ in Data() } }359        #expect(sent.count == 2)360        await env.catalog.setDock((0..<12).map { "m\($0)" })361        #expect(env.catalog.dock.count == ModelCatalog.dockLimit)362        fail.withLock { $0 = true }363        await env.catalog.toggleFavorite(neon); await env.catalog.setDock([])364        #expect(env.catalog.model(neon.id)?.favorite == true && env.catalog.dock.count == ModelCatalog.dockLimit)365        env.catalog.markUsed("claude-haiku-5-5")366        #expect(env.catalog.recents.first?.id == "claude-haiku-5-5")367    }368369    @Test func aTurnWithAnOpenRouterModelAndWebSearchShowsSourcesThenFollowUps() async throws {370        let host = "turn1.test"371        let detail = #"{"conversation":\#(Fixtures.conversationJSON(id: "conv_or1")),"messages":[\#(Fixtures.messageJSON(id: "msg_u", role: "user", text: "Question")),\#(NextGenFixtures.citedMessage)],"siblings":[],"attachments":[],"artifacts":[],"draft":null,"liveRun":null}"#372        let sent = NSLockBox<[String: JSONValue]>([:])373        route(host: host) { method, path, _, body in374            if method == "POST" && path == "/api/chat" {375                sent.withLock { $0 = body }376                var e: [String] = []; var i = 0377                func add(_ n: String, _ j: String) { e.append(SSE.event(n, j, id: i)); i += 1 }378                add("run", #"{"runId":"run_1"}"#); add("conversation", #"{"id":"conv_or1","userMessageId":"msg_u","created":true}"#)379                add("start", #"{"messageId":"msg_in8im8ls4qszlpnb","model":"or:google/gemini-4-neon","provider":"openrouter","applied":{"applied":{"webSearch":true},"ignored":[]}}"#)380                add("part", #"{"type":"citation","url":"https://www.postgresql.org","title":"PostgreSQL"}"#); add("text", #"{"delta":"La 18.7."}"#)381                add("status", #"{"messageId":"msg_in8im8ls4qszlpnb","status":"complete"}"#); add("followups", #"{"messageId":"msg_in8im8ls4qszlpnb","suggestions":["Et la 19 ?"]}"#)382                add("done", #"{"status":"complete"}"#); add("end", "{}")383                return .sse(e)384            }385            if path.hasPrefix("/api/conversations/conv_or1") { return .json(detail) }386            if path == "/api/drafts" { return .json(#"{"draft":null}"#) }387            return nil388        }389        let env = TestEnv.make(host: host); env.network.simulate(online: true)390        await env.catalog.refresh(); await env.settings.refresh()391        env.select(model: "or:google/gemini-4-neon"); env.preferences.webSearch = true; env.preferences.disabledTools = ["python"]392        let session = env.newSession()393        session.composerText = "Question"394        session.sendComposer(options: env.sendOptions)395        // the stream shows one source and one follow-up; the reload after the turn brings the stored row (6 sources, 3 follow-ups)396        let done = await TestEnv.wait { session.phase == .idle && session.messages.last?.citations.count == 6 }397        #expect(done)398        let body = sent.withLock { $0 }399        #expect(body["modelId"] == .string("or:google/gemini-4-neon") && body["settings"]?["webSearch"] == .bool(true) && body["toolNames"] == .array([.string("web_search")]))400        let answer = try #require(session.messages.last)401        #expect(answer.citations.count == 6 && answer.followups.count == 3 && answer.exactCost)402        #expect(env.catalog.recents.first?.id == "or:google/gemini-4-neon")403    }404}405406@Suite(.serialized) @MainActor struct SyncEngineTests {407    @Test func theFeedBatchesChangesAndTheAppRereadsWhatChanged() async throws {408        let host = "sync1.test"409        route(host: host) { method, path, query, _ in410            if path == "/api/sync" { return .json(#"{"events":[],"head":40,"truncated":false,"runs":[]}"#) }411            if path == "/api/events" {412                // first connection resumes after the pulled head; a reconnection resumes after the last event applied413                #expect(query == "after=40" || query == "after=43")414                return .sse(query == "after=43" ? ["retry: 3000\n\n", SSE.event("hello", #"{"head":43,"runs":[]}"#)] : ["retry: 3000\n\n", SSE.event("hello", #"{"head":40,"runs":[]}"#), SSE.event("change", #"{"seq":41,"entity":"model","id":"or:x/y","op":"upsert"}"#, id: 41),415                             SSE.event("change", #"{"seq":42,"entity":"conversation","id":"conv_z","op":"upsert"}"#, id: 42), SSE.event("change", #"{"seq":43,"entity":"prompt","id":"pr_1","op":"upsert"}"#, id: 43)], delay: 0.02)416            }417            return nil418        }419        let env = TestEnv.make(host: host); env.network.simulate(online: true)420        let plans = NSLockBox<[SyncPlan]>([])421        let apply = env.sync.onPlan422        env.sync.onPlan = { plan in plans.withLock { $0.append(plan) }; await apply?(plan) }423        env.sync.start()424        let got = await TestEnv.wait { !plans.withLock { $0 }.isEmpty && env.sync.cursor == 43 }425        #expect(got)426        let plan = try #require(plans.withLock { $0 }.first)427        #expect(plan.models && plan.conversationList && plan.conversations == ["conv_z"] && plan.prompts && !plan.settings)428        // the app re-read exactly what the events named429        let reread = await TestEnv.wait { !requests(host, "GET", "/api/models").isEmpty && !requests(host, "GET", "/api/conversations").isEmpty && !requests(host, "GET", "/api/prompts").isEmpty && env.router.tick("prompt") == 1 }430        #expect(reread)431        #expect(requests(host, "GET", "/api/settings").isEmpty && env.router.tick("prompt") == 1)432        // the cursor survives a restart of the engine433        try? await Task.sleep(for: .milliseconds(80))434        let savedCursor = await env.store.value(Int.self, for: SyncEngine.cursorKey)435        #expect(savedCursor == 43)436        env.sync.stop(); #expect(env.sync.status == .idle)437    }438439    @Test func aResyncReloadsEverythingAndAPullReportsWhatWasMissed() async throws {440        let host = "sync2.test"; let truncated = NSLockBox(false)441        route(host: host) { _, path, query, _ in442            guard path == "/api/sync" else { return nil }443            if query == "after=0" { return .json(#"{"events":[{"seq":1,"entity":"model","op":"upsert"}],"head":10,"truncated":false,"runs":[]}"#) }444            return truncated.withLock { $0 } ? .json(#"{"events":[],"head":900,"truncated":true,"runs":[]}"#) : .json(#"{"events":[{"seq":11,"entity":"settings","op":"upsert"},{"seq":12,"entity":"conversation","id":"conv_gone","op":"delete"}],"head":12,"truncated":false,"runs":[]}"#)445        }446        let env = TestEnv.make(host: host); env.network.simulate(online: true)447        let plans = NSLockBox<[SyncPlan]>([])448        env.sync.onPlan = { plan in plans.withLock { $0.append(plan) } }449        // first contact: only the position of the feed is remembered (everything was just loaded)450        let first = await env.sync.pull()451        #expect(first == SyncPlan() && env.sync.cursor == 10 && plans.withLock { $0 }.isEmpty)452        let second = try #require(await env.sync.pull())453        #expect(second.settings && second.deletedConversations == ["conv_gone"] && env.sync.cursor == 12)454        truncated.withLock { $0 = true }455        let third = await env.sync.pull()456        #expect(third == SyncPlan.all && env.sync.cursor == 900)457        env.sync.handle(.resync(head: 950)); await env.sync.flush()458        #expect(plans.withLock { $0 }.last == SyncPlan.all && env.sync.cursor == 950)459        // unreachable server: nil, the cursor does not move460        StubProtocol.install(host: host) { _, _ in .offline() }461        let none = await env.sync.pull()462        #expect(none == nil && env.sync.cursor == 950)463    }464465    @Test func aDraftChangedOnAnotherDeviceIsTakenUnlessThisOneHasUnsavedTyping() async {466        let host = "sync3.test"; let draft = NSLockBox("depuis le Mac")467        route(host: host) { method, path, _, _ in path == "/api/drafts" && method == "GET" ? .json(#"{"draft":{"text":"\#(draft.withLock { $0 })"}}"#) : nil }468        let env = TestEnv.make(host: host); env.network.simulate(online: true)469        let session = env.newSession(); env.router.current = session470        await session.load()471        #expect(session.composerText == "depuis le Mac")472        draft.withLock { $0 = "modifié sur le web" }473        var plan = SyncPlan(); plan.absorb([SyncEvent(seq: 1, entity: "draft", id: "new")], cursor: 0)474        await env.apply(plan)475        #expect(session.composerText == "depuis le Mac" || session.composerText == "modifié sur le web")476    }477}478479@Suite(.serialized) @MainActor struct OfflineDraftTests {480    @Test func aDraftTypedOfflineSurvivesARelaunchAndIsPushedWhenTheNetworkReturns() async throws {481        let host = "draft1.test"; let offline = NSLockBox(true); let saved = NSLockBox<[String]>([])482        route(host: host) { method, path, _, body in483            guard path == "/api/drafts" else { return nil }484            if offline.withLock({ $0 }) { return .offline() }485            if method == "PUT" { saved.withLock { $0.append(body["text"]?.stringValue ?? "") }; return .json(#"{"ok":true}"#) }486            return .json(#"{"draft":null}"#)487        }488        let env = TestEnv.make(host: host); env.network.simulate(online: false)489        let session = env.newSession(); env.router.current = session490        await session.load()491        session.composerText = "écrit dans le métro"492        try await Task.sleep(for: .milliseconds(2200))             // the save is debounced by 1.5 s493        let local: LocalDraft? = await env.store.value(for: LocalDraft.key("new"))494        #expect(local?.text == "écrit dans le métro" && saved.withLock { $0 }.isEmpty)495        // "relaunch": a new session of the same store shows the text again496        let again = env.newSession()497        await again.load()498        #expect(again.composerText == "écrit dans le métro")499        // back online: pushed once, then the local copy is dropped500        offline.withLock { $0 = false }; env.network.simulate(online: true)501        env.router.current = again502        await again.pushUnsentDraft()503        let after: LocalDraft? = await env.store.value(for: LocalDraft.key("new"))504        #expect(saved.withLock { $0 }.last == "écrit dans le métro" && after == nil)505        // a quick command is never saved as a draft506        again.composerText = "/arène"507        await again.pushUnsentDraft()508        #expect(saved.withLock { $0 }.last == "écrit dans le métro")509    }510}511512@Suite(.serialized) @MainActor struct ProviderKeyTests {513    @Test func settingAKeyAsksForAPasskeyThenReplaysOnceAndNeverKeepsTheKey() async throws {514        let host = "keys1.test"; let puts = NSLockBox(0)515        route(host: host) { method, path, _, body in516            guard path == "/api/providers/openrouter/key", method == "PUT" else { return nil }517            #expect(body["key"] == .string("sk-or-v1-secret-value"))518            return puts.withLock { $0 += 1; return $0 } == 1 ? .json(#"{"error":"Vérification récente requise","code":"reauth_required"}"#, status: 403) : .json(#"{"ok":true}"#)519        }520        let env = TestEnv.make(host: host)521        let reauths = NSLockBox(0)522        await env.http.setReauthHandler { reauths.withLock { $0 += 1 } }523        let model = ProvidersModel(api: env.api, catalog: env.catalog, settings: env.settings)524        await model.load()525        #expect(model.providers.count == 3)526        let accepted = await model.setKey("openrouter", key: "  sk-or-v1-secret-value \n")527        #expect(accepted)528        #expect(reauths.withLock { $0 } == 1 && puts.withLock { $0 } == 2)529        let result = try #require(model.results["openrouter"])530        #expect(result.ok && !result.message.contains("secret"))531        // the catalogue and the provider list were re-read after the change532        #expect(requests(host, "GET", "/api/providers").count == 2 && requests(host, "GET", "/api/models").count == 1)533        // nothing about the key was written to the local store or the defaults534        let stored = await env.store.value(JSONValue.self, for: "settings")535        let blank = await model.setKey("openrouter", key: "   ")536        #expect(!String(describing: stored).contains("secret") && !blank)537    }538539    @Test func aKeyRefusedByTheProviderIsReportedAndRevokeTellsAboutTheFallback() async throws {540        let host = "keys2.test"541        route(host: host) { method, path, _, _ in542            if path == "/api/providers/tavily/key" && method == "PUT" { return .json(#"{"ok":false,"error":"Invalid API key","message":"Invalid API key"}"#, status: 422) }543            if path == "/api/providers/tavily/key" && method == "DELETE" { return .json(#"{"removed":true,"fallback":true}"#) }544            if path == "/api/providers/tavily/test" { return .json(#"{"ok":true,"message":"Clé valide","info":null}"#) }545            if path == "/api/providers/openrouter/test" { return .json(#"{"error":"boom"}"#, status: 502) }546            return nil547        }548        let env = TestEnv.make(host: host)549        let model = ProvidersModel(api: env.api, catalog: env.catalog, settings: env.settings)550        let refused = await model.setKey("tavily", key: "tvly-wrong-key")551        #expect(!refused)552        #expect(model.results["tavily"]?.ok == false && model.results["tavily"]?.message.contains("refusée") == true)553        await model.revoke("tavily")554        #expect(model.results["tavily"]?.ok == true && model.results["tavily"]?.message.contains("environnement") == true)555        await model.test("tavily"); #expect(model.results["tavily"] == ProviderCheck(ok: true, message: "Clé valide"))556        await model.test("openrouter"); #expect(model.results["openrouter"]?.ok == false && model.busy.isEmpty)557    }558559    @Test func accountSettingsAreAppliedAfterTheServerConfirms() async {560        let host = "settings1.test"; let fail = NSLockBox(false)561        route(host: host) { method, path, _, _ in method == "PATCH" && path == "/api/settings" && fail.withLock({ $0 }) ? .json(#"{"error":"refusé"}"#, status: 500) : nil }562        let env = TestEnv.make(host: host)563        await env.settings.refresh()564        #expect(env.settings.followUpsEnabled && env.settings.tools.map(\.name) == ["web_search", "python"] && env.settings.webSearchAvailable)565        let e1 = await env.settings.setFollowUps(false)566        #expect(e1 == nil && !env.settings.followUpsEnabled)567        let e2 = await env.settings.saveInstructions(text: "Réponds en français.", enabled: true)568        #expect(e2 == nil && env.settings.instructionsEnabled)569        fail.withLock { $0 = true }570        let e3 = await env.settings.setFollowUps(true)571        #expect(e3 != nil && !env.settings.followUpsEnabled)572    }573}574
Files main
Menu

Theme