Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: SPChat/Tests/CoreTests/GenerationEngineTests.swift3// Description: Turn lifecycle: happy path, idempotent retries, text never lost when offline, mid-stream drop and resume, vanished run, silence watchdog, cancellation.4import Foundation5import Testing6@testable import SPChatCore78@Suite struct GenerationEngineTests {9 private func fastTiming() -> GenerationEngine.Timing {10 var t = GenerationEngine.Timing(); t.postBackoff = .milliseconds(1); t.reconnectBase = .milliseconds(1); t.reconnectCap = .milliseconds(2); t.onlinePoll = .milliseconds(1); t.silenceTimeout = .seconds(5)11 return t12 }13 private func collect(_ s: AsyncStream<GenerationUpdate>) async -> [GenerationUpdate] { var out: [GenerationUpdate] = []; for await u in s { out.append(u) }; return out }14 private func text(_ updates: [GenerationUpdate]) -> String { updates.compactMap { if case .event(.text(let t)) = $0 { return t }; return nil }.joined() }15 private let noSleep: @Sendable (Duration) async throws -> Void = { _ in await Task.yield() }1617 @Test func happyPath() async {18 let t = MockTransport(); t.on("POST", "/api/chat", .sse(happyRun()))19 let engine = GenerationEngine(api: makeClient(t), timing: fastTiming(), sleep: noSleep)20 let u = await collect(engine.start(.send, payload: ["text": "Salut"], requestId: "req_0123456789abcdef"))21 #expect(u.first == .phase(.sending)); #expect(u.contains(.phase(.streaming))); #expect(text(u) == "Bonjour"); #expect(u.last == .ended(.completed))22 #expect(u.contains(.event(.title("Salutation"))))23 let body = JSONValue.parse(t.requests[0].body!)!24 #expect(body["clientRequestId"]?.stringValue == "req_0123456789abcdef"); #expect(body["text"]?.stringValue == "Salut"); #expect(t.requests[0].headers["Accept"] == "text/event-stream")25 }2627 @Test func requestIdsMatchTheServerPattern() {28 let ids = Set((0..<200).map { _ in GenerationEngine.requestId() })29 #expect(ids.count == 200)30 for id in ids { #expect(id.range(of: #"^[A-Za-z0-9_-]{16,64}$"#, options: .regularExpression) != nil) }31 }3233 @Test func postIsRetriedWithTheSameIdempotencyKey() async {34 let t = MockTransport(); t.on("POST", "/api/chat", .fail(.networkConnectionLost), .fail(.timedOut), .sse(happyRun()))35 let engine = GenerationEngine(api: makeClient(t), timing: fastTiming(), sleep: noSleep)36 let u = await collect(engine.start(.send, payload: ["text": "x"]))37 #expect(u.last == .ended(.completed)); #expect(t.requests.count == 3)38 let keys = Set(t.requests.map { JSONValue.parse($0.body!)!["clientRequestId"]!.stringValue! })39 #expect(keys.count == 1) // one message, one generation, whatever the number of attempts40 }4142 @Test func nothingSentWhenOfflineTheTextComesBack() async {43 let t = MockTransport(); t.on("POST", "/api/chat", .fail(.notConnectedToInternet))44 let engine = GenerationEngine(api: makeClient(t), timing: fastTiming(), isOnline: { false }, sleep: noSleep)45 let u = await collect(engine.start(.send, payload: ["text": "x"]))46 guard case .ended(.notSent(let e)) = u.last else { Issue.record("expected notSent, got \(String(describing: u.last))"); return }47 #expect(e.message.hasPrefix("Hors ligne")); #expect(t.requests.count == 4)48 }4950 @Test func deduplicatedReplay() async {51 let t = MockTransport(); t.on("POST", "/api/chat", .json(200, #"{"deduplicated":true,"conversationId":"conv_7","userMessageId":"u"}"#))52 let u = await collect(GenerationEngine(api: makeClient(t), timing: fastTiming(), sleep: noSleep).start(.send, payload: ["text": "x"]))53 #expect(u.last == .ended(.deduplicated(conversationId: "conv_7")))54 }5556 @Test func refusalsAreNotRetried() async {57 let t = MockTransport(); t.on("POST", "/api/chat", .json(409, #"{"error":"Une réponse est déjà en cours dans cette conversation.","code":"busy"}"#))58 let u = await collect(GenerationEngine(api: makeClient(t), timing: fastTiming(), sleep: noSleep).start(.send, payload: ["text": "x"]))59 guard case .ended(.refused(let e)) = u.last else { Issue.record("expected refused"); return }60 #expect(e.code == "busy"); #expect(t.requests.count == 1)61 let t2 = MockTransport(); t2.on("POST", "/api/chat", .json(402, #"{"error":"Limite quotidienne atteinte","code":"budget"}"#))62 let u2 = await collect(GenerationEngine(api: makeClient(t2), timing: fastTiming(), sleep: noSleep).start(.send, payload: ["text": "x"]))63 if case .ended(.refused(let e2)) = u2.last { #expect(e2.status == 402) } else { Issue.record("expected refused") }64 }6566 @Test func sessionEndedDuringSend() async {67 let t = MockTransport(); t.on("POST", "/api/chat", .json(401, #"{"error":"Session requise","code":"locked"}"#, headers: ["x-csa-locked": "1"]))68 let u = await collect(GenerationEngine(api: makeClient(t), timing: fastTiming(), sleep: noSleep).start(.send, payload: ["text": "x"]))69 #expect(u.last == .ended(.sessionEnded))70 }7172 @Test func dropMidAnswerResumesFromTheLastEventSeen() async {73 let all = happyRun(text: ["Un ", "deux ", "trois ", "quatre"])74 // the connection dies after "deux " (frames: retry, run#0, conversation#1, start#2, text#3, text#4)75 let t = MockTransport()76 t.on("POST", "/api/chat", .sse(Array(all[..<6]), closes: false))77 t.on("GET", "/api/chat/stream", .fail(.networkConnectionLost), .sse(Array(all[6...])))78 let u = await collect(GenerationEngine(api: makeClient(t), timing: fastTiming(), sleep: noSleep).start(.send, payload: ["text": "x"]))79 #expect(text(u) == "Un deux trois quatre") // nothing lost, nothing duplicated80 #expect(u.contains(.phase(.reconnecting))); #expect(u.last == .ended(.completed))81 let resume = t.requests.filter { $0.path == "/api/chat/stream" }82 #expect(resume.count == 2); #expect(resume[0].query == "after=4&run=run_1"); #expect(resume[0].headers["Origin"] == nil)83 }8485 @Test func runGoneAfterAServerRestart() async {86 let all = happyRun()87 let t = MockTransport(); t.on("POST", "/api/chat", .sse(Array(all[..<5]), closes: false))88 t.on("GET", "/api/chat/stream", .json(410, #"{"error":"Cette génération n'est plus en mémoire.","code":"run_gone"}"#))89 let u = await collect(GenerationEngine(api: makeClient(t), timing: fastTiming(), sleep: noSleep).start(.send, payload: ["text": "x"]))90 #expect(u.last == .ended(.runGone))91 }9293 @Test func followReattachesToARunStartedElsewhere() async {94 let t = MockTransport(); t.on("GET", "/api/chat/stream", .sse(happyRun(created: false)))95 let u = await collect(GenerationEngine(api: makeClient(t), timing: fastTiming(), sleep: noSleep).follow(runId: "run_1"))96 #expect(text(u) == "Bonjour"); #expect(u.last == .ended(.completed)); #expect(t.requests[0].query == "after=-1&run=run_1")97 }9899 @Test func silentConnectionIsAbandonedAndResumed() async {100 let all = happyRun(text: ["a", "b"])101 let t = MockTransport(); t.on("POST", "/api/chat", .sse(Array(all[..<5]), hang: true)) // no bytes, no error: a dead socket102 t.on("GET", "/api/chat/stream", .sse(Array(all[5...])))103 var timing = fastTiming(); timing.silenceTimeout = .milliseconds(150)104 let clock = ContinuousClock(); var u: [GenerationUpdate] = []105 let elapsed = await clock.measure { u = await collect(GenerationEngine(api: makeClient(t), timing: timing, sleep: noSleep).start(.send, payload: ["text": "x"])) }106 #expect(text(u) == "ab"); #expect(u.last == .ended(.completed)); #expect(elapsed < .seconds(10))107 }108109 @Test func cancellingTheConsumerStopsTheEngine() async {110 let t = MockTransport(); t.on("POST", "/api/chat", .sse(Array(happyRun()[..<5]), hang: true))111 let engine = GenerationEngine(api: makeClient(t), timing: fastTiming(), sleep: noSleep)112 let task = Task { () -> Int in var n = 0; for await u in engine.start(.send, payload: ["text": "x"]) { n += 1; if case .event(.text) = u { break } }; return n }113 let n = await task.value114 #expect(n > 0)115 try? await Task.sleep(for: .milliseconds(100))116 #expect(t.requests.filter { $0.path == "/api/chat/stream" }.isEmpty) // no re-attachment after an explicit stop117 }118119 @Test func regenerateAndEditUseTheirEndpoints() async {120 let t = MockTransport(); t.on("POST", "/api/chat/regenerate", .sse(happyRun(created: false))); t.on("POST", "/api/chat/edit", .sse(happyRun(created: false)))121 let engine = GenerationEngine(api: makeClient(t), timing: fastTiming(), sleep: noSleep)122 _ = await collect(engine.start(.regenerate, payload: ["messageId": "msg_a0"].merging(TurnOptions(modelId: "m2", routingMode: .manual).payload) { $1 }))123 _ = await collect(engine.start(.edit, payload: ["messageId": "msg_u0", "text": "corrigé"]))124 #expect(JSONValue.parse(t.requests[0].body!)!["modelId"]?.stringValue == "m2"); #expect(JSONValue.parse(t.requests[1].body!)!["text"]?.stringValue == "corrigé")125 }126}127