Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: SPChat/Tests/CoreTests/IntegrationTests.swift3// Description: End-to-end tests against a real (development) backend: WebAuthn enrolment and sign-in, conversations, real streaming, cancel, regenerate, search, uploads, exports.4import Foundation5import Testing6@testable import SPChatCore78/// Runs only when `SPCHAT_IT_URL` (a development server, never production) and `SPCHAT_IT_CODE` (a one-time enrolment9/// code created on the node) are set — see Scripts/integration-test.sh. Real model calls are made (a fraction of a cent).10private let itURL = ProcessInfo.processInfo.environment["SPCHAT_IT_URL"].flatMap(URL.init(string:))11private let itCode = ProcessInfo.processInfo.environment["SPCHAT_IT_CODE"]1213@Suite(.serialized, .enabled(if: itURL != nil && itCode != nil)) struct BackendIntegrationTests {14 @Test func fullJourney() async throws {15 let server = try #require(ServerConfiguration(url: itURL!))16 #expect(server.host == "localhost" || server.host == "127.0.0.1", "integration tests must never target production")17 let secrets = InMemorySecretStore()18 let api = APIClient(server: server, transport: URLSessionTransport(), secrets: secrets)19 let device = SoftwareAuthenticator(origin: server.origin)20 let auth = AuthService(api: api, authenticator: device)21 let ended = Flag(); api.onSessionEnded = { ended.set() }2223 // --- without a session: neutral state, locked API24 let before = try await auth.state()25 #expect(before.configOk); #expect(!before.signedIn); #expect(before.rpId == server.host)26 do { _ = try await api.conversations(); Issue.record("API must be locked without a session") } catch let e as APIError { #expect(e.kind == .locked) }2728 // --- sign-in without any passkey on this device is refused by the server (forged-free path)29 do { try await auth.signIn(); Issue.record("no passkey yet") } catch let e as PasskeyError { #expect(e.code == .cancelled) }3031 // --- wrong code, then the real one-time code → enrolment → session32 do { try await auth.redeemSetupCode("AAAA-AAAA-AAAA-AAAA-AAAA"); Issue.record("wrong code accepted") } catch let e as PasskeyError { #expect(e.code == .refused || e.code == .rateLimited) }33 try await auth.redeemSetupCode(itCode!)34 try await auth.registerPasskey(label: "SP Chat · tests natifs")35 #expect(api.hasSession); #expect(secrets.read("session@\(server.host)") != nil)36 #expect(try await auth.state().signedIn)3738 // --- sign out really revokes on the server; the same passkey signs back in (real signature verification)39 let oldCookie = api.cookies.header40 await auth.signOut()41 #expect(!api.hasSession)42 let replay = APIClient(server: server, transport: URLSessionTransport(), secrets: InMemorySecretStore())43 replay.cookies.ingest(setCookie: (oldCookie ?? "") + "; Path=/; Max-Age=600")44 do { _ = try await replay.models(); Issue.record("revoked session still works") } catch let e as APIError { #expect(e.kind == .locked) }45 try await auth.signIn()46 #expect(api.hasSession); #expect(device.assertions == 1)4748 // --- account49 let overview = try await auth.passkeys()50 #expect(overview.passkeys.contains { $0.label == "SP Chat · tests natifs" && $0.current }); #expect(overview.recentAuth)51 #expect(try await auth.sessions().contains { $0.current })52 let codes = try await auth.generateRecoveryCodes(); #expect(codes.count == 8)53 #expect(try await auth.events().contains { $0.event == "login.ok" })5455 // --- catalogue56 let catalog = try await api.models()57 #expect(!catalog.chatModels.isEmpty)58 let settings = try await api.serverSettings(); #expect(settings.limits.maxUploadMb > 0); #expect(!settings.tools.isEmpty)59 #expect(!(try await api.presets()).isEmpty)60 let model = try #require(catalog.model("claude-haiku-5-5") ?? catalog.chatModels.min { ($0.outputPrice ?? 99) < ($1.outputPrice ?? 99) })6162 // --- upload a text file, then a real streamed turn that uses it63 let note = "Le mot de passe du coffre de test est: mangue-42."64 #expect(FileValidator.validate(data: Data(note.utf8), filename: "note.txt", maxUploadMb: settings.limits.maxUploadMb) == .success("text/plain"))65 let atts = try await api.uploadAttachments([UploadFile(filename: "note.txt", mime: "text/plain", data: Data(note.utf8))])66 #expect(atts.count == 1)67 do { _ = try await api.uploadAttachments([UploadFile(filename: "x.bin", mime: "application/octet-stream", data: Data([0, 1, 2, 3]))]); Issue.record("binary accepted") } catch let e as APIError { #expect(e.status == 415) }6869 let engine = GenerationEngine(api: api)70 var acc = MessageAccumulator(conversationId: ""); var conversationId = ""; var userMessageId: String?; var title: String?; var end: GenerationEnd?; var deltas = 071 let opts = TurnOptions(modelId: model.id, routingMode: .manual, maxOutputTokens: 400, toolsEnabled: false)72 for await u in engine.start(.send, payload: ["text": "Quel est le mot de passe du coffre de test indiqué dans le fichier joint ? Réponds en une phrase.", "attachmentIds": [.string(atts[0].id)]].merging(opts.payload) { $1 }) {73 switch u {74 case .event(.conversation(let id, let created, let uid, _, _, _)): conversationId = id; userMessageId = uid; acc.conversationId = id; acc.parentId = uid; #expect(created)75 case .event(.title(let t)): title = t76 case .event(let e): if case .text = e { deltas += 1 }; acc.apply(e)77 case .ended(let e): end = e78 default: break79 }80 }81 #expect(end == .completed)82 let answer = try #require(acc.message)83 #expect(answer.status == .complete); #expect(answer.visibleText.lowercased().contains("mangue-42")); #expect(deltas >= 1)84 #expect(answer.modelId == model.id); #expect((answer.usage?.output ?? 0) > 0); #expect(answer.parentId == userMessageId)8586 // --- the stored conversation matches what was streamed87 var detail = try await api.conversation(conversationId)88 #expect(detail.activeMessages.count == 2); #expect(detail.activeMessages.last?.id == answer.id); #expect(detail.activeMessages.last?.visibleText == answer.visibleText)89 #expect(detail.attachments.first?.id == atts[0].id); #expect(detail.liveRun == nil)90 if let title { #expect(detail.conversation.title == title) }9192 // --- regenerate → a second version; switch back to the first93 var acc2 = MessageAccumulator(conversationId: conversationId, parentId: userMessageId); var end2: GenerationEnd?94 for await u in engine.start(.regenerate, payload: ["messageId": .string(answer.id)].merging(opts.payload) { $1 }) {95 if case .event(let e) = u { acc2.apply(e) }; if case .ended(let e) = u { end2 = e }96 }97 #expect(end2 == .completed); let second = try #require(acc2.message); #expect(second.id != answer.id)98 detail = try await api.conversation(conversationId)99 #expect(detail.versions(of: second).map(\.id) == [answer.id, second.id]); #expect(detail.activeMessages.last?.id == second.id)100 try await api.activateBranch(conversationId: conversationId, messageId: answer.id)101 #expect(try await api.conversation(conversationId).activeMessages.last?.id == answer.id)102103 // --- edit the question → new user version, new answer104 var end3: GenerationEnd?; var edited: String?105 for await u in engine.start(.edit, payload: ["messageId": .string(userMessageId!), "text": "Réponds uniquement par le mot OK."].merging(opts.payload) { $1 }) {106 if case .event(.conversation(_, _, let uid, _, let ed, _)) = u { #expect(ed == userMessageId); edited = uid }; if case .ended(let e) = u { end3 = e }107 }108 #expect(end3 == .completed); #expect(edited != nil && edited != userMessageId)109110 // --- cancel a long answer: the server stops, the partial text is kept as cancelled111 var acc4 = MessageAccumulator(conversationId: conversationId); var runId: String?; var stopped = false112 let longOpts = TurnOptions(modelId: model.id, routingMode: .manual, maxOutputTokens: 4000, toolsEnabled: false)113 for await u in engine.start(.send, payload: ["conversationId": .string(conversationId), "text": "Écris un texte de 1500 mots sur l'histoire des phares."].merging(longOpts.payload) { $1 }) {114 if case .event(.run(let id)) = u { runId = id }115 if case .event(let e) = u { acc4.apply(e); if case .text = e, (acc4.message?.visibleText.count ?? 0) > 200, !stopped { stopped = true; try await api.cancelGeneration(runId: runId, messageId: acc4.message?.id) } }116 }117 #expect(stopped)118 let cancelled = try #require(try await api.conversation(conversationId).messages.first { $0.id == acc4.message?.id })119 #expect(cancelled.status == .cancelled); #expect(!cancelled.visibleText.isEmpty); #expect(cancelled.visibleText.count < 9000)120121 // --- list, rename, favourite, pin, search, draft, export, cache122 let renamed = try await api.updateConversation(conversationId, ConversationPatch(title: "Coffre de test natif", pinned: true, favorite: true))123 #expect(renamed.title == "Coffre de test natif"); #expect(renamed.pinned)124 #expect(try await api.conversations(filter: .favorites).contains { $0.id == conversationId })125 #expect(try await api.conversations(query: "Coffre de test natif").first?.id == conversationId)126 #expect(try await api.search("phares", semantic: false).contains { $0.conversationId == conversationId })127 try await api.saveDraft(scope: conversationId, text: "à finir"); #expect(try await api.draft(scope: conversationId)?.text == "à finir")128 try await api.saveDraft(scope: conversationId, text: ""); #expect(try await api.draft(scope: conversationId) == nil)129 let md = try await api.exportConversation(conversationId, format: "md")130 #expect(md.filename?.hasSuffix(".md") == true); #expect(String(decoding: md.data, as: UTF8.self).contains("# Coffre de test natif"))131 let file = try await api.attachmentData(atts[0].id); #expect(String(decoding: file.data, as: UTF8.self) == note)132133 let cache = try CacheStore(url: nil)134 detail = try await api.conversation(conversationId)135 try await cache.saveDetail(detail)136 #expect(try await cache.search("phares").first?.conversationId == conversationId) // readable offline137 #expect(ConversationExporter.markdown(detail).contains("phares"))138139 // --- fork, archive, delete140 let fork = try await api.forkConversation(conversationId, at: detail.activeMessages[0].id)141 #expect(fork.id != conversationId); #expect(try await api.conversation(fork.id).messages.count == 1)142 try await api.updateConversation(fork.id, ConversationPatch(archived: true))143 #expect(try await api.conversations(filter: .archived).contains { $0.id == fork.id })144 try await api.deleteConversation(fork.id); try await api.deleteConversation(conversationId)145 do { _ = try await api.conversation(conversationId); Issue.record("deleted conversation still there") } catch let e as APIError { #expect(e.status == 404) }146147 // --- revoking this device elsewhere ends the session here148 let me = try #require(try await auth.sessions().first { $0.current })149 #expect(try await auth.revokeSession(me.id))150 do { _ = try await api.conversations(); Issue.record("session should be gone") } catch let e as APIError { #expect(e.kind == .locked) }151 #expect(ended.value); #expect(!api.hasSession); #expect(secrets.isEmpty)152 }153}154