Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: SPChat/Tests/AppTests/PanelTests.swift3// Description: Hosted tests of the contextual panel: each tab's view model on a scripted server (context, timeline + replay, lens run / error / cancel / delete, branches rename / variant / fork) and light + dark snapshots of the four tabs.4import SwiftUI5import XCTest6@testable import SPChat7@testable import SPChatCore89@MainActor final class PanelTests: XCTestCase {10 private let cid = "conv_3nqb6uaejng8x5f2"1112 /// Catalogue with the two models of the captured conversation (direct Anthropic + an OpenRouter catalogue model).13 private static let models = #"{"models":[{"id":"claude-haiku-5-5","provider":"anthropic","displayName":"Claude Haiku 5.5","kind":"chat","status":"verified","available":true,"tested":true,"capabilities":{"text":true,"tools":true},"contextWindow":1000000,"maxOutputTokens":128000,"pricing":{"input":0.1,"output":0.5},"hidden":false,"favorite":true,"route":"direct","creator":"anthropic","creatorName":"Anthropic","logo":"anthropic"},{"id":"or:google/gemini-4-neon","provider":"openrouter","displayName":"Gemini 4 Neon","kind":"chat","status":"catalog","available":true,"tested":false,"capabilities":{"text":true},"contextWindow":1048576,"pricing":{"input":3,"output":15},"hidden":false,"favorite":false,"route":"openrouter","creator":"google","creatorName":"Google","logo":"google"}],"defaultModel":"claude-haiku-5-5","registry":{"refreshedAt":"2026-11-19T14:00:00.000Z"}}"#1415 /// The dev conversation the panel payloads were captured from: two turns, the second answer regenerated with another model.16 private func detail() -> String {17 func msg(_ id: String, _ parent: String?, _ role: String, _ text: String, active: Bool = true, extra: String = "", at: String) -> String {18 #"{"id":"\#(id)","conversation_id":"\#(cid)","parent_id":\#(parent.map { "\"\($0)\"" } ?? "null"),"role":"\#(role)","parts":[{"type":"text","text":"\#(text)"}],"status":"complete","active":\#(active),"created_at":"\#(at)"\#(extra)}"#19 }20 let haiku = #","model_id":"claude-haiku-5-5","provider":"anthropic","usage":{"input":2237,"cached":2132,"output":121},"cost_usd":0.000092,"latency_ms":725,"ttft_ms":384,"routing":{"mode":"manual","reason":"Modèle choisi explicitement.","selected":"claude-haiku-5-5"}"#21 let gemini = #","model_id":"or:google/gemini-4-neon","provider":"openrouter","usage":{"input":895,"cached":0,"output":413,"costUsd":0.00888,"reasoning":322},"cost_usd":0.00888,"latency_ms":7302,"ttft_ms":6471,"routing":{"mode":"manual","reason":"Modèle choisi explicitement.","selected":"or:google/gemini-4-neon","servedBy":"OpenRouter · Google"},"settings":{"applied":{"applied":{"tools":false},"ignored":[{"param":"temperature","requested":0.2,"reason":"Paramètre non accepté par ce modèle."}]}}"#22 let messages = [23 msg("msg_2hyhe2dqx50mczhh", nil, "user", "Compte de 1 à 60, un nombre par ligne, sans rien d'autre.", at: "2026-11-17T19:01:57.373Z"),24 msg("msg_ady2mo6uy1e1ukp5", "msg_2hyhe2dqx50mczhh", "assistant", "1 2 3 4 5 6 7 8 9 10", extra: haiku, at: "2026-11-17T19:01:57.656Z"),25 msg("msg_memnqrz1s9kt59hu", "msg_ady2mo6uy1e1ukp5", "user", "Donne trois faits sur la rivière des Outaouais.", at: "2026-11-17T19:02:00.777Z"),26 msg("msg_adeoywx4qo0vvzzn", "msg_memnqrz1s9kt59hu", "assistant", "1. **Longueur et bassin** : la rivière des Outaouais mesure environ 1 271 km.", active: false, extra: haiku, at: "2026-11-17T19:02:01.090Z"),27 msg("msg_nanm671m20jr2uhy", "msg_memnqrz1s9kt59hu", "assistant", "1. Longue d'environ 1 271 km, la rivière des Outaouais est le principal affluent du fleuve Saint-Laurent. 2. Elle forme la majeure partie de la frontière entre le Québec et l'Ontario.", extra: gemini, at: "2026-11-19T14:40:11.114Z"),28 ].joined(separator: ",")29 return #"{"conversation":\#(Fixture.conversationRow(cid, "Comptage de 1 à 60")),"messages":[\#(messages)],"siblings":[],"attachments":[{"id":"att_1","filename":"bassin-versant.pdf","mime":"application/pdf","size_bytes":482133}],"artifacts":[],"draft":null,"liveRun":null}"#30 }3132 private func setUp(_ t: MockTransport, transport: HTTPTransport? = nil) async -> (AppEnvironment, ChatViewModel) {33 t.on("GET", "/api/models", .json(200, Self.models))34 var env = Fixture.env(t)35 if let transport {36 var o = LaunchOptions(); o.inMemory = true; o.injectedSessionCookie = "__Host-csa_session=" + String(repeating: "a", count: 43)37 env = AppEnvironment(options: o, transport: transport, authenticator: SoftwareAuthenticator(origin: "https://chat.spboucher.ai"))38 }39 t.on("GET", "/api/conversations/\(cid)", .json(200, detail())); t.on("GET", "/api/conversations", .json(200, "{\"conversations\":[\(Fixture.conversationRow(cid, "Comptage de 1 à 60"))]}"))40 await env.catalog.load()41 let chat = env.chat(conversationId: cid, draftKey: "w1")42 await chat.loadIfNeeded()43 XCTAssertEqual(chat.messages.count, 4)44 return (env, chat)45 }4647 // MARK: Context Inspector4849 func testInspectorLoadsTheReportForTheSelectedModelAndResetsBetweenConversations() async {50 let t = MockTransport(); let (env, _) = await setUp(t)51 t.on("GET", "/api/conversations/\(cid)/context", .json(200, PanelFixture.context)); t.on("GET", "/api/conversations/other/context", .json(404, #"{"error":"Introuvable"}"#))52 let m = ContextInspectorModel()53 await m.load(conversationId: cid, modelId: "or:google/gemini-4-neon", api: env.api)54 XCTAssertEqual(m.report?.estimate?.nextRequestTokens, 1363); XCTAssertEqual(m.report?.measured?.inputTokens, 895); XCTAssertNil(m.error); XCTAssertFalse(m.isLoading)55 XCTAssertEqual(t.requests.last { $0.path.hasSuffix("/context") }?.query, "model=or:google/gemini-4-neon")56 // another conversation: the previous report never stays on screen, the server's error is shown57 await m.load(conversationId: "other", modelId: nil, api: env.api)58 XCTAssertNil(m.report); XCTAssertEqual(m.error, "Introuvable")59 // unsaved conversation: nothing is requested60 let before = t.requests.count61 await m.load(conversationId: nil, modelId: nil, api: env.api)62 XCTAssertNil(m.report); XCTAssertNil(m.error); XCTAssertEqual(t.requests.count, before)63 }6465 // MARK: Timeline + Replay6667 func testTimelineLoadsAndReplayStepsThroughTheRecordedEvents() async {68 let t = MockTransport(); let (env, _) = await setUp(t)69 t.on("GET", "/api/conversations/\(cid)/timeline", .json(200, PanelFixture.timeline))70 let m = TimelineModel()71 await m.load(conversationId: cid, api: env.api)72 XCTAssertEqual(m.events.count, 7); XCTAssertEqual(m.cursor.count, 7); XCTAssertNil(m.cursor.index); XCTAssertNil(m.currentEvent)73 m.next(); XCTAssertEqual(m.currentEvent?.messageId, "msg_2hyhe2dqx50mczhh")74 m.next(); m.next(); m.previous(); XCTAssertEqual(m.cursor.index, 1); XCTAssertEqual(m.currentEvent?.kind, .response)75 m.select(m.events[4]); XCTAssertEqual(m.currentEvent?.kind, .branch); XCTAssertEqual(m.currentEvent?.conversationId, "conv_ax86wojh31sghee7")76 m.scrub(0); XCTAssertEqual(m.cursor.index, 0)77 // play at 4×: every recorded event is visited in order, then playback stops on the last one78 m.cycleSpeed(); m.cycleSpeed(); XCTAssertEqual(m.cursor.speed, .x4)79 var visited: [Int] = []80 m.togglePlay(); XCTAssertTrue(m.cursor.isPlaying)81 let deadline = ContinuousClock.now + .seconds(8)82 while m.cursor.isPlaying, ContinuousClock.now < deadline { if let i = m.cursor.index, visited.last != i { visited.append(i) }; try? await Task.sleep(for: .milliseconds(5)) }83 if let i = m.cursor.index, visited.last != i { visited.append(i) }84 XCTAssertEqual(visited, [0, 1, 2, 3, 4, 5, 6]); XCTAssertFalse(m.cursor.isPlaying); XCTAssertTrue(m.cursor.isAtEnd)85 // pause really stops the clock86 m.togglePlay(); XCTAssertEqual(m.cursor.index, 0); m.pause()87 try? await Task.sleep(for: .milliseconds(400)); XCTAssertEqual(m.cursor.index, 0)88 // a reload keeps the position; another conversation resets the replay89 m.select(m.events[3]); await m.load(conversationId: cid, api: env.api); XCTAssertEqual(m.cursor.index, 3)90 t.on("GET", "/api/conversations/c2/timeline", .json(200, PanelFixture.timelineToolAndArtifact))91 await m.load(conversationId: "c2", api: env.api); XCTAssertNil(m.cursor.index); XCTAssertEqual(m.events.map(\.kind), [.prompt, .response, .tool, .artifact])92 }9394 // MARK: Response Lens9596 func testLensRunStreamsThenIsStoredAndSelected() async throws {97 let t = MockTransport(); let (env, _) = await setUp(t)98 t.on("GET", "/api/lens", .json(200, PanelFixture.lensListEmpty), .json(200, PanelFixture.lensList)); t.on("POST", "/api/lens", .sse(PanelFixture.lensRunFrames))99 let m = LensModel()100 await m.load(messageId: "msg_nanm671m20jr2uhy", api: env.api)101 XCTAssertEqual(m.kinds.map(\.id), ["summary", "explain", "keypoints", "table", "verify"]); XCTAssertTrue(m.lenses.isEmpty); XCTAssertNil(m.selected)102 m.modelId = "claude-haiku-5-5"103 m.start("summary", api: env.api)104 XCTAssertTrue(m.isRunning); XCTAssertEqual(m.run?.lens, "summary")105 m.start("explain", api: env.api); XCTAssertEqual(m.run?.lens, "summary", "a second lens cannot start while one runs")106 await Fixture.wait("lens run") { !m.isRunning && m.run == nil }107 XCTAssertEqual(m.lenses.map(\.id), ["lens_lkb7q8lk9whq0eg1"]); XCTAssertEqual(m.selected?.content, PanelFixture.lensContent)108 XCTAssertEqual(m.selected?.exactCost, false, "the cost basis sent by `done` survives the list refresh"); XCTAssertEqual(m.selected?.latencyMs, 815); XCTAssertEqual(m.selected?.costUsd, 0.00008)109 let post = try XCTUnwrap(t.requests.first { $0.method == "POST" && $0.path == "/api/lens" }); let body = try XCTUnwrap(JSONValue.parse(try XCTUnwrap(post.body)))110 XCTAssertEqual(body["messageId"]?.stringValue, "msg_nanm671m20jr2uhy"); XCTAssertEqual(body["lens"]?.stringValue, "summary"); XCTAssertEqual(body["modelId"]?.stringValue, "claude-haiku-5-5")111 XCTAssertEqual(t.requests.filter { $0.method == "POST" && $0.path == "/api/lens" }.count, 1)112 }113114 func testLensServerErrorStaysReadableAndDeleteRemovesAStoredLens() async throws {115 let t = MockTransport(); let (env, _) = await setUp(t)116 t.on("GET", "/api/lens", .json(200, PanelFixture.lensList)); t.on("POST", "/api/lens", .sse(PanelFixture.lensErrorFrames), .json(402, #"{"error":"Limite quotidienne atteinte.","code":"budget"}"#))117 t.on("DELETE", "/api/lens", .json(500, #"{"error":"Base indisponible"}"#), .json(200, #"{"ok":true}"#))118 let m = LensModel()119 await m.load(messageId: "msg_a", api: env.api)120 XCTAssertEqual(m.selectedId, "lens_lkb7q8lk9whq0eg1")121 m.start("summary", api: env.api)122 await Fixture.wait("lens error") { !m.isRunning }123 XCTAssertEqual(m.run?.error, "Réponse introuvable ou vide"); XCTAssertEqual(m.lenses.count, 1, "nothing was stored")124 m.dismissRun(); XCTAssertNil(m.run)125 // refused before the stream opens (budget): the server's message is shown126 m.start("table", api: env.api)127 await Fixture.wait("lens refusal") { !m.isRunning }128 XCTAssertEqual(m.run?.error, "Limite quotidienne atteinte."); m.dismissRun()129 // delete: a failure keeps the lens, a success removes it130 do { try await m.delete("lens_lkb7q8lk9whq0eg1", api: env.api); XCTFail("expected a failure") } catch { XCTAssertEqual(m.lenses.count, 1) }131 try await m.delete("lens_lkb7q8lk9whq0eg1", api: env.api)132 XCTAssertTrue(m.lenses.isEmpty); XCTAssertNil(m.selectedId)133 XCTAssertEqual(t.requests.last { $0.method == "DELETE" }?.query, "id=lens_lkb7q8lk9whq0eg1")134 }135136 func testLensCancelTellsTheServerAndRemovesTheEmptyResult() async throws {137 // 1. the server confirms the cancellation with an empty stored lens: it is deleted138 let t = MockTransport(); let (env, _) = await setUp(t)139 t.on("GET", "/api/lens", .json(200, PanelFixture.lensListEmpty)); t.on("POST", "/api/chat/cancel", .json(200, #"{"ok":true,"cancelled":true}"#)); t.on("DELETE", "/api/lens", .json(200, #"{"ok":true}"#))140 t.on("POST", "/api/lens", .sse(Array(PanelFixture.lensVerifyFrames.prefix(4)), hang: true))141 let m = LensModel(); m.cancelGrace = .milliseconds(150)142 await m.load(messageId: "msg_a", api: env.api)143 m.start("verify", api: env.api)144 await Fixture.wait("steps") { m.run?.steps.count == 2 }145 XCTAssertEqual(m.run?.steps.map(\.state), [.done, .running]); XCTAssertEqual(m.run?.runId, "run_verify")146 m.cancel(api: env.api); XCTAssertTrue(m.isCancelling)147 await Fixture.wait("cancel") { m.run == nil && !m.isCancelling }148 let cancel = try XCTUnwrap(t.requests.first { $0.path == "/api/chat/cancel" }); XCTAssertEqual(JSONValue.parse(try XCTUnwrap(cancel.body))?["runId"]?.stringValue, "run_verify")149 XCTAssertTrue(m.lenses.isEmpty)150 // 2. real frames of a cancelled run: after the cancel request the server ends the stream with an empty stored lens → DELETE151 let gate = GatedLensTransport(); let t2 = gate.base; let (env2, _) = await setUp(t2, transport: gate)152 t2.on("GET", "/api/lens", .json(200, PanelFixture.lensListEmpty)); t2.on("POST", "/api/chat/cancel", .json(200, #"{"ok":true,"cancelled":true}"#)); t2.on("DELETE", "/api/lens", .json(200, #"{"ok":true}"#))153 let m2 = LensModel(); m2.cancelGrace = .seconds(20)154 await m2.load(messageId: "msg_a", api: env2.api)155 m2.start("explain", api: env2.api)156 await Fixture.wait("stream open") { gate.isOpen }157 for f in PanelFixture.lensCancelledFrames.prefix(3) { gate.send(f) }158 await Fixture.wait("run id") { m2.run?.runId == "run_50fjc8v8uc8p3b0d" && m2.run?.started == true }159 m2.cancel(api: env2.api)160 XCTAssertTrue(m2.isRunning, "the stream is followed until the server confirms")161 await Fixture.wait("cancel request") { t2.requests.contains { $0.path == "/api/chat/cancel" } }162 for f in PanelFixture.lensCancelledFrames.dropFirst(3) { gate.send(f) }163 gate.finish()164 await Fixture.wait("empty lens removed") { m2.run == nil && !m2.isCancelling && t2.requests.contains { $0.method == "DELETE" } }165 XCTAssertEqual(t2.requests.last { $0.method == "DELETE" }?.query, "id=lens_iaqtwqanwrt8s21j"); XCTAssertTrue(m2.lenses.isEmpty)166 }167168 // MARK: Branches169170 func testBranchesLoadRenameActivateVariantAndFork() async throws {171 let t = MockTransport(); let (env, chat) = await setUp(t)172 t.on("GET", "/api/conversations/\(cid)/tree", .json(200, PanelFixture.tree))173 t.on("PATCH", "/api/conversations/conv_ax86wojh31sghee7", .json(200, "{\"conversation\":\(Fixture.conversationRow("conv_ax86wojh31sghee7", "Comptage de 1 à 60 — Variante courte"))}"))174 t.on("POST", "/api/conversations/\(cid)/branch", .json(200, #"{"ok":true}"#), .json(200, "{\"conversation\":\(Fixture.conversationRow("conv_new", "Comptage de 1 à 60 — Essai"))}"))175 let m = BranchesModel()176 await m.load(conversationId: cid, api: env.api)177 let tree = try XCTUnwrap(m.tree)178 XCTAssertEqual(tree.family.map(\.depth), [0, 1]); XCTAssertEqual(tree.variants.first?.options.count, 2)179 // rename the branch label180 try await m.rename("conv_ax86wojh31sghee7", label: " Version brève ", env: env)181 var patch = try XCTUnwrap(JSONValue.parse(try XCTUnwrap(t.requests.last { $0.method == "PATCH" }?.body)))182 XCTAssertEqual(patch["branchLabel"]?.stringValue, "Version brève"); XCTAssertNil(patch["title"]); XCTAssertNil(m.busyId)183 try await m.rename("conv_ax86wojh31sghee7", label: " ", env: env)184 patch = try XCTUnwrap(JSONValue.parse(try XCTUnwrap(t.requests.last { $0.method == "PATCH" }?.body)))185 XCTAssertEqual(patch["branchLabel"], .null, "an empty name clears the label")186 // activate the other variant: the message is known locally → ChatViewModel.showVersion187 let inactive = try XCTUnwrap(tree.variants.first?.options.first { !$0.active })188 await m.activate(inactive, chat: chat, env: env)189 var post = try XCTUnwrap(JSONValue.parse(try XCTUnwrap(t.requests.last { $0.method == "POST" && $0.path.hasSuffix("/branch") }?.body)))190 XCTAssertEqual(post["activate"]?.stringValue, "msg_adeoywx4qo0vvzzn")191 // the active one is never re-activated192 let n = t.requests.count193 await m.activate(try XCTUnwrap(tree.variants.first?.options.first { $0.active }), chat: chat, env: env); XCTAssertEqual(t.requests.count, n)194 // fork from the last message with a label195 let last = try XCTUnwrap(chat.messages.last)196 let newId = try await m.fork(conversationId: cid, at: last.id, label: "Essai", env: env)197 XCTAssertEqual(newId, "conv_new")198 post = try XCTUnwrap(JSONValue.parse(try XCTUnwrap(t.requests.last { $0.method == "POST" && $0.path.hasSuffix("/branch") }?.body)))199 XCTAssertEqual(post["messageId"]?.stringValue, "msg_nanm671m20jr2uhy"); XCTAssertEqual(post["label"]?.stringValue, "Essai")200 }201202 // MARK: Snapshots (light + dark, 320 × 720)203204 func testSnapshotsOfTheFourTabs() async throws {205 let t = MockTransport(); let (env, chat) = await setUp(t)206 let verify = #"{"id":"lens_verify","lens":"verify","model_id":"or:google/gemini-4-neon","content":"**Confirmé** : la longueur de 1 271 km et le rôle d'affluent principal du Saint-Laurent sont attestés par les deux sources.\n\n- Frontière Québec–Ontario : confirmé\n- *Kichi Sibi* : confirmé","sources":[{"url":"https://www.thecanadianencyclopedia.ca/fr/article/riviere-des-outaouais","title":"Rivière des Outaouais | l'Encyclopédie canadienne","claim":"Longue d'environ 1 271 km, principal affluent du Saint-Laurent."},{"url":"https://fr.wikipedia.org/wiki/Rivi%C3%A8re_des_Outaouais","title":"Rivière des Outaouais — Wikipédia","claim":"Elle forme la majeure partie de la frontière entre le Québec et l'Ontario."}],"usage":{"input":5200,"output":310,"costUsd":0.0204},"cost_usd":0.0204,"status":"complete","created_at":"2026-11-19T14:52:03.120Z"}"#207 let stored = PanelFixture.lensList.replacingOccurrences(of: #""lenses":["#, with: #""lenses":[\#(verify),"#)208 t.on("GET", "/api/conversations/\(cid)/context", .json(200, PanelFixture.context)); t.on("GET", "/api/conversations/\(cid)/timeline", .json(200, PanelFixture.timeline))209 t.on("GET", "/api/conversations/\(cid)/tree", .json(200, PanelFixture.tree)); t.on("GET", "/api/lens", .json(200, stored))210 t.on("POST", "/api/lens", .sse(Array(PanelFixture.lensVerifyFrames.prefix(8)), hang: true))211 chat.settings.modelId = "or:google/gemini-4-neon"212 let stores = PanelStores()213 await stores.inspector.load(conversationId: cid, modelId: chat.settings.modelId, api: env.api)214 await stores.timeline.load(conversationId: cid, api: env.api); stores.timeline.select(stores.timeline.events[6])215 await stores.lens.load(messageId: "msg_nanm671m20jr2uhy", api: env.api)216 await stores.branches.load(conversationId: cid, api: env.api)217 XCTAssertEqual(stores.lens.lenses.count, 2)218 let size = CGSize(width: 320, height: 720)219 for tab in PanelTab.allCases {220 for dark in [false, true] {221 let window = WindowModel(); window.destination = .chat(cid); window.panelTab = tab222 let r = Snapshot.render(ContextPanel(chat: chat, window: window, stores: stores).environment(env), name: "panel-\(tab.rawValue)-\(dark ? "dark" : "light")", size: size, dark: dark)223 XCTAssertGreaterThan(r.opaqueFraction, 0.95, "\(tab.rawValue) paints its whole frame")224 }225 }226 // a lens in progress: steps with their state, sources found so far, streamed text227 stores.lens.start("verify", api: env.api)228 await Fixture.wait("running lens") { stores.lens.run?.sources.count == 2 && stores.lens.run?.text.isEmpty == false }229 for dark in [false, true] {230 let window = WindowModel(); window.destination = .chat(cid); window.panelTab = .lens231 Snapshot.render(ContextPanel(chat: chat, window: window, stores: stores).environment(env), name: "panel-lens-running-\(dark ? "dark" : "light")", size: size, dark: dark)232 }233 // an unsaved conversation: quiet empty state234 let draft = env.chat(conversationId: nil, draftKey: "draft"); let w = WindowModel()235 Snapshot.render(ContextPanel(chat: draft, window: w, stores: PanelStores()).environment(env), name: "panel-inspector-empty-light", size: size)236 }237}238239/// Scripted transport whose `POST /api/lens` stream is fed by the test, frame by frame (everything else goes to the mock).240final class GatedLensTransport: HTTPTransport, @unchecked Sendable {241 let base = MockTransport()242 private let lock = NSLock()243 private var continuation: AsyncThrowingStream<Data, Error>.Continuation?244 var isOpen: Bool { lock.withLock { continuation != nil } }245 func send(_ frame: String) { _ = lock.withLock { continuation?.yield(Data(frame.utf8)) } }246 func finish() { lock.withLock { continuation?.finish() } }247248 func send(_ request: URLRequest) async throws -> (Data, HTTPResponse) { try await base.send(request) }249 func upload(_ request: URLRequest, body: Data, progress: @escaping @Sendable (Double) -> Void) async throws -> (Data, HTTPResponse) { try await base.upload(request, body: body, progress: progress) }250 func stream(_ request: URLRequest) async throws -> (HTTPResponse, AsyncThrowingStream<Data, Error>) {251 guard request.httpMethod == "POST", request.url?.path == "/api/lens" else { return try await base.stream(request) }252 let stream = AsyncThrowingStream<Data, Error> { c in lock.withLock { continuation = c } }253 return (HTTPResponse(status: 200, headers: ["Content-Type": "text/event-stream; charset=utf-8"]), stream)254 }255}256