SPB Git

Repository Public

ModelDecodingTests.swift 9.8 KB 91 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: SPChat/Tests/CoreTests/ModelDecodingTests.swift3// Description: Decoding of real backend payload shapes (captured from the development server) and formatting helpers.4import Foundation5import Testing6@testable import SPChatCore78@Suite struct ModelDecodingTests {9    @Test func conversationListRow() throws {10        let json = #"{"id":"conv_3nqb6uaejng8x5f2","project_id":null,"title":"Comptage de 1 à 60","model_id":"claude-haiku-5-5","settings":{"routingMode":"manual","toolsEnabled":true},"pinned":false,"favorite":true,"archived_at":null,"parent_id":null,"branch_message_id":null,"mode":"chat","message_count":4,"total_cost_usd":0.000556,"last_message_at":"2026-11-17T19:02:01.090Z","created_at":"2026-11-17T19:01:57.373Z","updated_at":"2026-11-17T19:02:05.983Z","project_name":null,"project_color":null,"has_draft":false,"streaming":false}"#11        let c = try JSONDecoder.api().decode(Conversation.self, from: Data(json.utf8))12        #expect(c.id == "conv_3nqb6uaejng8x5f2"); #expect(c.favorite); #expect(c.messageCount == 4)13        #expect(abs(c.totalCostUsd - 0.000556) < 1e-9); #expect(c.settings["routingMode"]?.stringValue == "manual")14        #expect(c.lastMessageAt != nil); #expect(!c.isArchived)15        // round trip through the cache encoding16        let back = try JSONDecoder.api().decode(Conversation.self, from: JSONEncoder.api().encode(c))17        #expect(back == c)18    }1920    @Test func numericColumnsSentAsStrings() throws {21        let c = try JSONDecoder.api().decode(Conversation.self, from: Data(#"{"id":"c","title":"t","message_count":"12","total_cost_usd":"0.25","created_at":"2026-11-17T19:01:57Z"}"#.utf8))22        #expect(c.messageCount == 12); #expect(c.totalCostUsd == 0.25)23    }2425    @Test func assistantMessageWithUnknownColumnsAndParts() throws {26        let json = #"{"id":"msg_a","conversation_id":"conv_1","parent_id":"msg_u","role":"assistant","parts":[{"type":"reasoning","text":"hmm","summarized":true},{"text":"Bonjour","type":"text"},{"type":"tool_call","id":"t1","name":"web_search","input":{"query":"x"},"status":"ok","output":{"results":[1,2]},"durationMs":812},{"type":"citation","url":"https://example.org","title":"Ex"},{"type":"hologram","payload":{"a":1}},{"type":"generated_image","attachmentId":"att_1","prompt":"chat","model":"gpt-image-1.5"}],"text":"Bonjour","model_id":"claude-haiku-5-5","provider":"anthropic","settings":{"applied":{"applied":{"tools":true,"effort":"low"},"ignored":[{"param":"temperature","requested":0.2,"reason":"non accepté"}]},"presetId":null},"routing":{"mode":"manual","reason":"Modèle choisi explicitement.","selected":"claude-haiku-5-5"},"usage":{"input":2237,"cached":2132,"output":121,"reasoning":0},"cost_usd":9.2e-05,"latency_ms":725,"ttft_ms":384,"status":"complete","error":null,"active":true,"created_at":"2026-11-17T19:01:57.656Z","tsv":"'1':1","client_request_id":null,"error_code":null,"stop_reason":null,"run_id":null}"#27        let m = try JSONDecoder.api().decode(Message.self, from: Data(json.utf8))28        #expect(m.role == .assistant); #expect(m.parts.count == 6); #expect(m.visibleText == "Bonjour"); #expect(m.reasoningText == "hmm")29        #expect(m.usage?.cached == 2132); #expect(m.latencyMs == 725); #expect(m.status == .complete)30        #expect(m.appliedParameters?.ignored.first?.param == "temperature")31        if case .unknown(let v) = m.parts[4] { #expect(v["type"]?.stringValue == "hologram") } else { Issue.record("unknown part must be preserved") }32        // unknown parts survive re-encoding (cache) untouched33        let back = try JSONDecoder.api().decode(Message.self, from: JSONEncoder.api().encode(m))34        #expect(back.parts == m.parts)35    }3637    @Test func unknownStatusIsKept() throws {38        let m = try JSONDecoder.api().decode(Message.self, from: Data(#"{"id":"m","role":"assistant","parts":[],"status":"paused"}"#.utf8))39        #expect(m.status == .other("paused")); #expect(m.status.rawValue == "paused")40    }4142    @Test func modelCatalog() throws {43        let json = #"{"models":[{"id":"claude-fable-5","provider":"anthropic","displayName":"Claude Fable 5","kind":"chat","api":null,"status":"verified","available":true,"tested":true,"unavailableReason":null,"capabilities":{"text":true,"tools":true,"vision":true,"reasoning":true,"effortLevels":["low","high"]},"contextWindow":1000000,"maxOutputTokens":128000,"pricing":{"input":10,"output":50,"cachedInput":1},"benchmark":{"tests":11,"errors":[],"passed":11,"perTest":{"code":"pass"},"p50TtftMs":1862,"p50LatencyMs":2691,"qualityScore":100,"totalCostUsd":0.20264,"avgOutputTokens":77},"hidden":false,"favorite":false,"lastVerifiedAt":"2026-11-01T07:18:11.433Z"},{"id":"gpt-image-1.5","provider":"openai","displayName":"GPT Image","kind":"image","status":"verified","available":true,"tested":true,"capabilities":{},"hidden":false,"favorite":false},{"id":"old","provider":"openai","displayName":"Old","kind":"chat","status":"unavailable","available":false,"tested":true,"capabilities":{},"hidden":false,"favorite":false}],"defaultModel":"claude-fable-5","registry":{"live":121,"refreshedAt":"2026-11-17T17:03:00.449Z"}}"#44        let cat = try JSONDecoder.api().decode(ModelCatalog.self, from: Data(json.utf8))45        #expect(cat.models.count == 3); #expect(cat.chatModels.map(\.id) == ["claude-fable-5"]) // only verified, available, conversational models are selectable46        let m = try #require(cat.model("claude-fable-5"))47        #expect(m.shortName == "Fable 5"); #expect(m.has("vision")); #expect(m.effortLevels == ["low", "high"]); #expect(m.inputPrice == 10); #expect(m.benchmark?.passed == 11)48        #expect(cat.refreshedAt != nil)49    }5051    @Test func usageReportWithBigintStrings() throws {52        let json = #"{"totals":{"requests":766,"cost":0.323154,"cost_confirmed":0,"input_tokens":"522324","output_tokens":"176205","cached_tokens":"215981","reasoning_tokens":"2945","avg_latency":2948,"avg_ttft":4897,"errors":2},"daily":[{"day":"2026-11-01T00:00:00.000Z","provider":"anthropic","cost":0.030296,"requests":5,"input_tokens":"4290","output_tokens":"498"}],"byModel":[{"model_id":"claude-haiku-5-5","provider":"anthropic","requests":3,"cost":0.01,"input_tokens":"10","output_tokens":"20","cached_tokens":"0","avg_latency":100,"avg_ttft":50,"errors":0}],"byKind":[{"kind":"chat","requests":3,"cost":0.01}],"byProject":[],"monthly":[],"spending":{"today":0.1,"month":0.3,"limits":{"dailyLimitUsd":10,"alertAtPercent":80,"monthlyLimitUsd":150}},"range":{}}"#53        let u = try JSONDecoder.api().decode(UsageReport.self, from: Data(json.utf8))54        #expect(u.totals.inputTokens == 522_324); #expect(u.daily.count == 1); #expect(u.spending.limits.dailyLimitUsd == 10); #expect(u.byModel[0].outputTokens == 20)55    }5657    @Test func conversationDetailAndVersions() throws {58        let json = #"{"conversation":{"id":"c1","title":"T","created_at":"2026-11-17T19:00:00.000Z"},"messages":[{"id":"u1","conversation_id":"c1","parent_id":null,"role":"user","parts":[{"type":"text","text":"q"}],"active":true,"created_at":"2026-11-17T19:00:01.000Z"},{"id":"a1","conversation_id":"c1","parent_id":"u1","role":"assistant","parts":[{"type":"text","text":"v1"}],"active":false,"created_at":"2026-11-17T19:00:02.000Z"},{"id":"a2","conversation_id":"c1","parent_id":"u1","role":"assistant","parts":[{"type":"text","text":"v2"}],"active":true,"created_at":"2026-11-17T19:00:03.000Z"}],"siblings":[{"parent_id":"u1","role":"assistant","n":2}],"attachments":[{"id":"att_1","filename":"a.pdf","mime":"application/pdf","size_bytes":1234,"page_count":3}],"artifacts":[],"draft":"brouillon","liveRun":null}"#59        let d = try JSONDecoder.api().decode(ConversationDetail.self, from: Data(json.utf8))60        #expect(d.activeMessages.map(\.id) == ["u1", "a2"]); #expect(d.draft == "brouillon")61        #expect(d.versions(of: d.messages[2]).map(\.id) == ["a1", "a2"])62        #expect(d.attachments[0].sizeBytes == 1234); #expect(d.attachments[0].pageCount == 3)63        if case .file(let id, _, let name, let size, let pages) = d.attachments[0].asPart { #expect(id == "att_1"); #expect(name == "a.pdf"); #expect(size == 1234); #expect(pages == 3) } else { Issue.record("expected a file part") }64    }6566    @Test func formatting() {67        #expect(Format.tokens(950) == "950"); #expect(Format.tokens(1500) == "1.5k"); #expect(Format.tokens(25_000) == "25k"); #expect(Format.tokens(nil) == "—")68        #expect(Format.usd(0) == "0 $"); #expect(Format.usd(0.000556) == "0.0006 $"); #expect(Format.usd(1.5) == "1.50 $")69        #expect(Format.duration(ms: 725) == "725 ms"); #expect(Format.duration(ms: 2691) == "2.7 s")70        #expect(Format.bytes(512) == "512 o"); #expect(Format.bytes(2048) == "2 Ko"); #expect(Format.bytes(5_242_880) == "5.0 Mo")71        let now = Date(timeIntervalSince1970: 1_800_000_000)72        #expect(Format.relative(now.addingTimeInterval(-20), now: now) == "à l'instant"); #expect(Format.relative(now.addingTimeInterval(-7200), now: now) == "2 h")73    }7475    @Test func searchSnippetSegmentsNeverInterpretHTML() {76        let hit = SearchHit(id: "m", conversationId: "c", role: "user", title: "t", snippet: "avant <b>mot</b> après <script>x</script>")77        let seg = hit.snippetSegments78        #expect(seg.map(\.text) == ["avant ", "mot", " après <script>x</script>"]); #expect(seg.map(\.highlighted) == [false, true, false])79    }8081    @Test func turnOptionsPayload() {82        let o = TurnOptions(modelId: "m1", routingMode: .manual, effort: .high, maxOutputTokens: 9_999_999, temperature: 3, toolsEnabled: false)83        let p = o.payload84        #expect(p["modelId"]?.stringValue == "m1"); #expect(p["settings"]?["maxOutputTokens"]?.intValue == 128_000); #expect(p["settings"]?["temperature"]?.doubleValue == 2)85        #expect(p["toolsEnabled"]?.boolValue == false)86        // a model id is not sent when the router decides87        #expect(TurnOptions(modelId: "m1", routingMode: .economy).payload["modelId"] == nil)88        #expect(TurnOptions().payload.isEmpty)89    }90}91
Files main
Menu

Theme