Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/LocalChatServiceTests/ServerTests.swift3// Description: Local API server over real sockets: OpenAI routes, streaming interop with the remote client, auth, rate limits, origins, cancellation, diagnostics.4import Foundation5import LocalAPIServer6import LocalChatCore7import LocalChatNetworking8import RemoteInference9import Testing1011@Suite("Local API server")12struct ServerTests {13 @Test func listsModelsWithLocalChatDetails() async throws {14 let s = try await RunningServer.start(); defer { Task { await s.server.stop() } }15 let node = s.node()16 let client = RemoteNodeClient(node: node, apiKey: nil)17 let models = try await client.listModels()18 #expect(models.map(\.id) == CannedInferenceService.models.map(\.id))19 let chat = client.descriptor(for: models[0])20 #expect(chat.id == "remote:\(node.id.uuidString):fake:chat")21 #expect(chat.backend == .remote && chat.remoteNodeID == node.id && chat.localPath == nil && !chat.isLocal)22 #expect(chat.name == "Fake Chat" && chat.quantization == "4bit" && chat.contextLength == 4096 && chat.parameterCount == 3_000_000_000)23 #expect(chat.capabilities == [.chat, .tools, .reasoning])24 #expect(client.descriptor(for: models[4]).capabilities == [.embeddings])25 }2627 @Test func nonStreamingChatCompletion() async throws {28 let s = try await RunningServer.start(); defer { Task { await s.server.stop() } }29 let r = try await s.http("POST", "/v1/chat/completions", body: #"{"model":"fake:chat","messages":[{"role":"user","content":"ping"}]}"#)30 #expect(r.status == 200)31 #expect(r.headers["content-type"]?.contains("application/json") == true)32 let response = try JSONDecoder().decode(OpenAI.ChatResponse.self, from: Data(r.text.utf8))33 #expect(response.object == "chat.completion" && response.model == "fake:chat" && response.id.hasPrefix("chatcmpl-"))34 #expect(response.choices.first?.message.content?.plainText == "Hello, wörld — échö: ping")35 #expect(response.choices.first?.message.reasoningContent == "thinking… ")36 #expect(response.choices.first?.finishReason == "stop")37 #expect(response.usage?.promptTokens == 11 && response.usage?.completionTokens == 6 && response.usage?.totalTokens == 17)38 #expect(response.usage?.promptTokensDetails?.cachedTokens == 4)39 #expect(response.localChat?.generationTime == 0.5 && response.localChat?.peakMemoryBytes == 123_456)40 // model resolved by display name and by repository too; max_tokens mapped41 let byName = try await s.http("POST", "/v1/chat/completions", body: #"{"model":"fake chat","max_tokens":1,"messages":[{"role":"user","content":"x"}]}"#)42 #expect(byName.status == 200 && byName.text.contains(#""finish_reason":"length""#))43 #expect(s.service.lastRequest?.settings.maxTokens == 1)44 #expect(s.service.lastRequest?.settings.contextLength == 4096) // capped to what the model declares45 let byRepo = try await s.http("POST", "/v1/chat/completions", body: #"{"model":"org/fake-chat","messages":[{"role":"user","content":"x"}]}"#)46 #expect(byRepo.status == 200)47 }4849 @Test func streamingRoundTripThroughRemoteRuntime() async throws {50 let s = try await RunningServer.start(); defer { Task { await s.server.stop() } }51 let request = GenerationRequest(messages: [.system("be brief"), .user("ça va?")], settings: GenerationSettings(temperature: 0.1, maxTokens: 64), stop: ["ZZ"])52 let out = try await collect(s.runtime().generate(request))53 #expect(out.text == "Hello, wörld — échö: ça va?")54 #expect(out.reasoning == "thinking… ")55 #expect(out.calls.isEmpty)56 let m = try #require(out.metrics)57 #expect(m.promptTokens == 11 && m.completionTokens == 6 && m.cachedPromptTokens == 4 && m.reasoningTokens == 2)58 #expect(m.generationTime == 0.5 && m.promptTime == 0.02) // the node's own measurements59 #expect((m.timeToFirstToken ?? 0) > 0 && (m.totalTime ?? 0) >= (m.timeToFirstToken ?? 1)) // measured on the client60 #expect(m.peakMemoryBytes == nil) // a remote node's memory is not this device's61 #expect(m.stopReason == .endOfTurn && m.tokensPerSecond == 12)62 // the server received exactly what the client asked for63 let seen = try #require(s.service.lastRequest)64 #expect(seen.messages == request.messages && seen.stop == ["ZZ"] && seen.settings.temperature == 0.1 && seen.settings.maxTokens == 64)65 }6667 @Test func rawStreamIsValidSSEWithUsageAndDone() async throws {68 let s = try await RunningServer.start(); defer { Task { await s.server.stop() } }69 let r = try await s.http("POST", "/v1/chat/completions", body: #"{"model":"fake:chat","stream":true,"stream_options":{"include_usage":true},"messages":[{"role":"user","content":"x"}]}"#)70 #expect(r.status == 200 && r.headers["content-type"]?.hasPrefix("text/event-stream") == true)71 var parser = SSEParser()72 let events = parser.feed(r.text)73 #expect(events.last?.isDone == true)74 let chunks = events.dropLast().map { try! JSONDecoder().decode(OpenAI.ChatChunk.self, from: Data($0.data.utf8)) }75 #expect(chunks.first?.choices.first?.delta.role == "assistant")76 #expect(chunks.allSatisfy { $0.object == "chat.completion.chunk" && $0.id == chunks[0].id })77 #expect(chunks.last?.choices.isEmpty == true && chunks.last?.usage?.totalTokens == 17)78 #expect(chunks.dropLast().last?.choices.first?.finishReason == "stop")79 // without include_usage no usage chunk is sent80 let plain = try await s.http("POST", "/v1/chat/completions", body: #"{"model":"fake:chat","stream":true,"messages":[{"role":"user","content":"x"}]}"#)81 #expect(!plain.text.contains(#""usage""#) && plain.text.hasSuffix("data: [DONE]\n\n"))82 }8384 @Test func toolCallsRoundTrip() async throws {85 let s = try await RunningServer.start(); defer { Task { await s.server.stop() } }86 let tool = ToolDefinition(name: "weather", summary: "forecast", parameters: ["type": "object", "properties": ["city": ["type": "string"]]])87 let out = try await collect(s.runtime().generate(GenerationRequest(messages: [.user("météo ?")], tools: [tool])))88 #expect(out.calls.count == 1)89 #expect(out.calls[0].id == "call_1" && out.calls[0].name == "weather")90 #expect(out.calls[0].arguments["city"]?.stringValue == "Québec" && out.calls[0].arguments["days"]?.intValue == 3)91 #expect(out.metrics?.stopReason == .toolCall)92 #expect(s.service.lastRequest?.tools == [tool])93 let raw = try await s.http("POST", "/v1/chat/completions", body: #"{"model":"fake:chat","messages":[{"role":"user","content":"x"}],"tools":[{"type":"function","function":{"name":"weather","parameters":{"type":"object"}}}]}"#)94 #expect(raw.text.contains(#""finish_reason":"tool_calls""#) && raw.text.contains(#""name":"weather""#))95 }9697 @Test func authenticationRequiredInvalidRevoked() async throws {98 let s = try await RunningServer.start(ServerConfiguration(port: 0, requireAuth: true)); defer { Task { await s.server.stop() } }99 let (record, secret) = try await s.keys.create(label: "laptop")100 #expect(secret.hasPrefix("lc-") && secret.count > 40 && record.hint.hasPrefix("lc-"))101 let missing = try await s.http("GET", "/v1/models")102 #expect(missing.status == 401 && missing.headers["www-authenticate"]?.contains("Bearer") == true && missing.text.contains("authentication_error"))103 #expect(try await s.http("GET", "/v1/models", headers: ["Authorization": "Bearer lc-wrong-key-000000"]).status == 401)104 #expect(try await s.http("GET", "/v1/models", headers: ["Authorization": "Bearer \(secret)"]).status == 200)105 #expect(try await s.http("GET", "/v1/models", headers: ["X-API-Key": secret]).status == 200)106 #expect(try await s.http("GET", "/lc/v1/ping").status == 401) // extra routes are behind the key too107 #expect(try await s.http("GET", "/health").status == 200) // liveness needs no key108109 let node = s.node()110 #expect(await RemoteNodeClient(node: node, apiKey: nil).health().state == .unauthorized)111 #expect(await RemoteNodeClient(node: node, apiKey: "lc-bad-bad-bad").health().state == .unauthorized)112 let good = await RemoteNodeClient(node: node, apiKey: secret).health()113 #expect(good.state == .online && good.models.contains("fake:chat") && (good.latency ?? 0) > 0)114 await #expect(throws: InferenceError.self) { _ = try await collect(s.runtime(key: "lc-bad-bad-bad").generate(GenerationRequest(messages: [.user("x")]))) }115 #expect(try await collect(s.runtime(key: secret).generate(GenerationRequest(messages: [.user("x")]))).text.hasSuffix("x"))116117 #expect(await s.keys.list().first?.lastUsedAt != nil)118 try await s.keys.revoke(id: record.id)119 #expect(try await s.http("GET", "/v1/models", headers: ["Authorization": "Bearer \(secret)"]).status == 401)120 let labels = await s.server.requestLog().compactMap(\.keyLabel)121 #expect(labels.contains("laptop"))122 }123124 @Test func lanExposureAlwaysRequiresAKey() {125 #expect(ServerConfiguration(exposeOnLAN: true, requireAuth: false).authenticationRequired)126 #expect(!ServerConfiguration().authenticationRequired && !ServerConfiguration().exposeOnLAN)127 #expect(ServerConfiguration(requireAuth: true).authenticationRequired)128 }129130 @Test func rateLimitReturns429WithRetryAfter() async throws {131 let s = try await RunningServer.start(ServerConfiguration(port: 0, rateLimitPerMinute: 3)); defer { Task { await s.server.stop() } }132 var statuses: [Int] = []; var retry: String?133 for _ in 0..<5 { let r = try await s.http("GET", "/v1/models"); statuses.append(r.status); if r.status == 429 { retry = r.headers["retry-after"] } }134 #expect(statuses == [200, 200, 200, 429, 429])135 #expect((Int(retry ?? "") ?? 0) >= 1)136 #expect(try await s.http("GET", "/health").status == 200) // liveness is not rate limited137 let limiter = RateLimiter(limitPerMinute: 60); let t0 = Date()138 for _ in 0..<60 { #expect(await limiter.check("a", now: t0) == nil) }139 #expect(await limiter.check("a", now: t0) == 1)140 #expect(await limiter.check("b", now: t0) == nil) // per client141 #expect(await limiter.check("a", now: t0.addingTimeInterval(1.01)) == nil) // refilled142 #expect(await RateLimiter(limitPerMinute: 0).check("a") == nil)143 }144145 @Test func embeddingsEndpoint() async throws {146 let s = try await RunningServer.start(); defer { Task { await s.server.stop() } }147 let vectors = try await s.runtime(model: "fake:embed").embed(["one", "three words here"])148 #expect(vectors.count == 2 && vectors[0][0] == 3 && vectors[1][0] == 16)149 let raw = try await s.http("POST", "/v1/embeddings", body: #"{"model":"fake:embed","input":"a b c"}"#)150 let response = try JSONDecoder().decode(OpenAI.EmbeddingResponse.self, from: Data(raw.text.utf8))151 #expect(raw.status == 200 && response.data.count == 1 && response.usage?.promptTokens == 3)152 #expect(try await s.http("POST", "/v1/embeddings", body: #"{"model":"fake:chat","input":"x"}"#).status == 400) // not an embedding model153 #expect(try await s.http("POST", "/v1/embeddings", body: #"{"model":"fake:embed","input":[]}"#).status == 400)154 #expect(try await s.http("POST", "/v1/embeddings", body: #"{"model":"fake:embed","input":"x","encoding_format":"base64"}"#).status == 400)155 await #expect(throws: InferenceError.self) { _ = try await s.runtime(model: "fake:chat").embed(["x"]) }156 }157158 @Test func malformedRequestsAndUnknownRoutes() async throws {159 let s = try await RunningServer.start(); defer { Task { await s.server.stop() } }160 let bad = try await s.http("POST", "/v1/chat/completions", body: "{not json")161 #expect(bad.status == 400 && (try? JSONDecoder().decode(OpenAI.ErrorEnvelope.self, from: Data(bad.text.utf8)))?.error.type == "invalid_request_error")162 #expect(try await s.http("POST", "/v1/chat/completions", body: #"{"model":"fake:chat"}"#).status == 400)163 #expect(try await s.http("POST", "/v1/chat/completions", body: #"{"model":"fake:chat","messages":[]}"#).status == 400)164 #expect(try await s.http("POST", "/v1/chat/completions", body: #"{"model":"fake:chat","messages":[{"role":"wizard","content":"x"}]}"#).status == 400)165 #expect(try await s.http("POST", "/v1/chat/completions", body: #"{"model":"fake:chat","messages":[{"role":"user","content":[{"type":"image_url","image_url":{"url":"http://x/y.png"}}]}]}"#).status == 400)166 let unknownModel = try await s.http("POST", "/v1/chat/completions", body: #"{"model":"nope","messages":[{"role":"user","content":"x"}]}"#)167 #expect(unknownModel.status == 404 && unknownModel.text.contains("not_found_error"))168 #expect(try await s.http("GET", "/v1/nope").status == 404)169 #expect(try await s.http("GET", "/lc/v1/other").status == 404)170 #expect(try await s.http("GET", "/../etc/passwd").status != 200)171 let wrongMethod = try await s.http("GET", "/v1/chat/completions")172 #expect(wrongMethod.status == 405 && wrongMethod.headers["allow"] == "POST")173 #expect(try await s.http("DELETE", "/v1/models").status == 405)174 await #expect(throws: InferenceError.modelNotFound("nope on “Test Mac” (The model “nope” is not available on this node. List models with GET /v1/models.)")) {175 _ = try await collect(s.runtime(model: "nope").generate(GenerationRequest(messages: [.user("x")])))176 }177 // a model that cannot load answers with a real HTTP status, even in streaming mode178 let unloadable = try await s.http("POST", "/v1/chat/completions", body: #"{"model":"fake:unloadable","stream":true,"messages":[{"role":"user","content":"x"}]}"#)179 #expect(unloadable.status == 503 && unloadable.text.contains("needs about"))180 }181182 @Test func bodyLargerThanTheLimitIsRefused() async throws {183 let s = try await RunningServer.start(ServerConfiguration(port: 0, maxBodyBytes: 2048)); defer { Task { await s.server.stop() } }184 let big = String(repeating: "x", count: 5000)185 let r = try? await s.http("POST", "/v1/chat/completions", body: #"{"model":"fake:chat","messages":[{"role":"user","content":"\#(big)"}]}"#)186 #expect(r == nil || r?.status == 413) // the server answers 413 and closes; URLSession may report the reset instead187 #expect(s.service.startedGenerations == 0)188 #expect(try await s.http("GET", "/health").status == 200)189 }190191 @Test func midStreamFailureIsReportedInBand() async throws {192 let s = try await RunningServer.start(); defer { Task { await s.server.stop() } }193 var text = ""; var failure: Error?194 do { for try await event in s.runtime(model: "fake:fail").generate(GenerationRequest(messages: [.user("x")])) { if case .text(let t) = event { text += t } } }195 catch { failure = error }196 #expect(text == "partial ")197 #expect((failure as? InferenceError)?.errorDescription?.contains("the canned model exploded") == true)198 let nonStream = try await s.http("POST", "/v1/chat/completions", body: #"{"model":"fake:fail","messages":[{"role":"user","content":"x"}]}"#)199 #expect(nonStream.status == 500 && nonStream.text.contains("exploded"))200 }201202 @Test func clientDisconnectCancelsTheGeneration() async throws {203 let s = try await RunningServer.start(); defer { Task { await s.server.stop() } }204 let consumer = Task { () -> Int in205 var pieces = 0206 do { for try await event in s.runtime(model: "fake:slow").generate(GenerationRequest(messages: [.user("go")])) { if case .text = event { pieces += 1 } } } catch {}207 return pieces208 }209 #expect(await waitUntil { s.service.startedGenerations == 1 })210 try await Task.sleep(nanoseconds: 200_000_000)211 #expect(s.service.cancelledGenerations == 0)212 consumer.cancel()213 let pieces = await consumer.value214 #expect(pieces > 0 && pieces < 2000)215 #expect(await waitUntil { s.service.cancelledGenerations == 1 }, "the server must cancel the engine stream when the client goes away")216 #expect(await waitUntil { await s.server.requestLog().contains { $0.error == "client disconnected" } })217 // stopping the server cancels generations in flight too218 let second = Task { try? await collect(s.runtime(model: "fake:slow").generate(GenerationRequest(messages: [.user("go")]))) }219 #expect(await waitUntil { s.service.startedGenerations == 2 })220 await s.server.stop()221 #expect(await waitUntil { s.service.cancelledGenerations == 2 })222 _ = await second.value223 #expect(await s.server.isRunning == false)224 #expect(await RemoteNodeClient(node: s.node(), apiKey: nil).health(timeout: 2).state == .offline)225 }226227 @Test func extraRouteHandlersUnderLC() async throws {228 let s = try await RunningServer.start(); defer { Task { await s.server.stop() } }229 let r = try await s.http("POST", "/lc/v1/ping?name=sync%20relay", body: "12345")230 #expect(r.status == 200)231 let json = try JSONDecoder().decode([String: String].self, from: Data(r.text.utf8))232 #expect(json == ["pong": "sync relay", "method": "POST", "bytes": "5"])233 }234235 @Test func documentationAndHealth() async throws {236 let s = try await RunningServer.start(); defer { Task { await s.server.stop() } }237 for path in ["/", "/docs"] {238 let r = try await s.http("GET", path)239 #expect(r.status == 200 && r.headers["content-type"]?.hasPrefix("text/html") == true)240 #expect(r.text.contains("http://127.0.0.1:\(s.port)/v1/chat/completions") && r.text.contains("/v1/embeddings") && r.text.contains("/v1/models"))241 #expect(r.text.contains("This Mac only"))242 }243 let health = try await s.http("GET", "/health")244 #expect(health.status == 200 && health.text == #"{"status":"ok"}"#)245 #expect(await s.server.baseURL == "http://127.0.0.1:\(s.port)/v1")246 }247248 @Test func foreignBrowserOriginsAreRefusedUnlessAllowed() async throws {249 let s = try await RunningServer.start(ServerConfiguration(port: 0, allowedOrigins: ["http://localhost:3000"])); defer { Task { await s.server.stop() } }250 let evil = try await s.http("POST", "/v1/chat/completions", body: #"{"model":"fake:chat","messages":[{"role":"user","content":"x"}]}"#, headers: ["Origin": "https://evil.example"])251 #expect(evil.status == 403 && evil.headers["access-control-allow-origin"] == nil)252 #expect(s.service.startedGenerations == 0)253 #expect(try await s.http("OPTIONS", "/v1/chat/completions", headers: ["Origin": "https://evil.example", "Access-Control-Request-Method": "POST"]).status == 403)254 let ok = try await s.http("GET", "/v1/models", headers: ["Origin": "http://localhost:3000"])255 #expect(ok.status == 200 && ok.headers["access-control-allow-origin"] == "http://localhost:3000")256 let preflight = try await s.http("OPTIONS", "/v1/chat/completions", headers: ["Origin": "http://localhost:3000", "Access-Control-Request-Method": "POST"])257 #expect(preflight.status == 204 && preflight.headers["access-control-allow-headers"]?.contains("Authorization") == true)258 // the documentation page of the server itself is same-origin259 #expect(try await s.http("GET", "/v1/models", headers: ["Origin": "http://127.0.0.1:\(s.port)"]).status == 200)260 }261262 @Test func busyPortIsReportedClearly() async throws {263 let s = try await RunningServer.start(); defer { Task { await s.server.stop() } }264 let second = LocalAPIServer(configuration: ServerConfiguration(port: s.port), service: s.service, keys: s.keys)265 await #expect(throws: ServerError.portInUse(s.port)) { try await second.start() }266 #expect(await second.isRunning == false)267 await #expect(throws: ServerError.alreadyRunning) { try await s.server.start() }268 // the port is free again after stop269 await s.server.stop()270 let third = LocalAPIServer(configuration: ServerConfiguration(port: s.port), service: s.service, keys: s.keys)271 #expect(try await third.start() == s.port)272 await third.stop()273 }274275 @Test func requestLogHoldsDiagnosticsButNeverContent() async throws {276 let s = try await RunningServer.start(); defer { Task { await s.server.stop() } }277 let live = await s.server.requestStream()278 let secret = "TOP-SECRET-PROMPT-9f3"279 _ = try await collect(s.runtime().generate(GenerationRequest(messages: [.user(secret)])))280 _ = try await s.http("GET", "/v1/nope")281 #expect(await waitUntil { await s.server.requestLog().count == 2 })282 let entries = await s.server.requestLog()283 let chat = try #require(entries.first { $0.path == "/v1/chat/completions" })284 #expect(chat.status == 200 && chat.method == "POST" && chat.streamed && chat.model == "fake:chat" && chat.clientAddress == "127.0.0.1")285 #expect(chat.promptTokens == 11 && chat.completionTokens == 6 && chat.tokensPerSecond == 12 && chat.duration > 0 && chat.error == nil)286 #expect(entries.last?.status == 404 && entries.last?.error == "unknown route")287 #expect(!String(decoding: try JSONEncoder().encode(entries), as: UTF8.self).contains(secret))288 var streamed: [RequestLogEntry] = []289 for await entry in live { streamed.append(entry); if streamed.count == 2 { break } }290 #expect(streamed.map(\.path) == ["/v1/chat/completions", "/v1/nope"])291 let ring = RequestLog(capacity: 3)292 for i in 0..<10 { await ring.append(RequestLogEntry(method: "GET", path: "/\(i)", status: 200)) }293 #expect(await ring.snapshot().map(\.path) == ["/7", "/8", "/9"])294 #expect(await ring.totalRequests == 10)295 }296297 @Test func keepAliveServesSeveralRequestsOnOneConnection() async throws {298 let s = try await RunningServer.start(); defer { Task { await s.server.stop() } }299 let config = URLSessionConfiguration.ephemeral; config.httpMaximumConnectionsPerHost = 1300 let session = URLSession(configuration: config)301 for _ in 0..<4 {302 let (data, response) = try await session.data(from: URL(string: "\(s.root)/v1/models")!)303 #expect((response as? HTTPURLResponse)?.statusCode == 200 && data.count > 50)304 var post = URLRequest(url: URL(string: "\(s.root)/v1/chat/completions")!); post.httpMethod = "POST"305 post.httpBody = Data(#"{"model":"fake:chat","stream":true,"messages":[{"role":"user","content":"x"}]}"#.utf8)306 let (stream, _) = try await session.data(for: post)307 #expect(String(decoding: stream, as: UTF8.self).hasSuffix("data: [DONE]\n\n"))308 }309 #expect(await s.server.requestLog().count == 8)310 }311312 @Test func apiKeysArePersistedAsHashesOnly() async throws {313 let url = FileManager.default.temporaryDirectory.appendingPathComponent("lc-keys-\(UUID().uuidString).json")314 defer { try? FileManager.default.removeItem(at: url) }315 let store = FileAPIKeyStore(fileURL: url)316 let (record, secret) = try await store.create(label: " ")317 #expect(record.label == "API key")318 let onDisk = try String(contentsOf: url, encoding: .utf8)319 #expect(!onDisk.contains(secret) && onDisk.contains(FileAPIKeyStore.hash(secret)))320 let mode = try FileManager.default.attributesOfItem(atPath: url.path)[.posixPermissions] as? Int321 #expect(mode == 0o600)322 let reopened = FileAPIKeyStore(fileURL: url)323 #expect(await reopened.validate(secret)?.id == record.id)324 #expect(await reopened.validate(secret + "x") == nil)325 #expect(await reopened.validate("") == nil)326 #expect(await reopened.hasActiveKeys)327 try await reopened.delete(id: record.id)328 #expect(await FileAPIKeyStore(fileURL: url).validate(secret) == nil)329 #expect(FileAPIKeyStore.generateSecret() != FileAPIKeyStore.generateSecret())330 }331}332