SPB Git

Repository Public

ServerTestSupport.swift 7.7 KB 135 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/LocalChatServiceTests/ServerTestSupport.swift3// Description: Test doubles for the Local API server tests: a deterministic InferenceService that streams canned tokens, and HTTP helpers.4import Foundation5import LocalAPIServer6import LocalChatCore7import LocalChatNetworking8import RemoteInference910/// Streams fixed tokens. It stands in for the engine only: the server, the sockets and the client under test are real.11final class CannedInferenceService: InferenceService, @unchecked Sendable {12    private let lock = NSLock()13    private var _cancelled = 014    private var _started = 015    private var _lastRequest: GenerationRequest?16    var cancelledGenerations: Int { lock.withLock { _cancelled } }17    var startedGenerations: Int { lock.withLock { _started } }18    var lastRequest: GenerationRequest? { lock.withLock { _lastRequest } }1920    static let models = [21        ModelDescriptor(id: "fake:chat", name: "Fake Chat", repoID: "org/fake-chat", backend: .mlx, format: .mlx, parameterCount: 3_000_000_000,22                        quantization: "4bit", contextLength: 4096, capabilities: [.chat, .tools, .reasoning], sizeBytes: 1_800_000_000, localPath: "org/fake-chat"),23        ModelDescriptor(id: "fake:slow", name: "Fake Slow", backend: .mlx, format: .mlx, localPath: "x"),24        ModelDescriptor(id: "fake:fail", name: "Fake Fail", backend: .mlx, format: .mlx, localPath: "x"),25        ModelDescriptor(id: "fake:unloadable", name: "Fake Unloadable", backend: .mlx, format: .mlx, localPath: "x"),26        ModelDescriptor(id: "fake:embed", name: "Fake Embed", backend: .mlx, format: .mlx, capabilities: [.embeddings], localPath: "x"),27    ]2829    func listModels() async -> [ModelDescriptor] { Self.models }3031    func countTokens(modelID: String, text: String) async -> Int? { modelID == "fake:embed" ? text.split(separator: " ").count : nil }3233    func embed(modelID: String, texts: [String]) async throws -> [[Float]] {34        guard modelID == "fake:embed" else { throw InferenceError.unsupported("This model does not produce embeddings.") }35        return texts.map { [Float($0.count), Float($0.utf8.reduce(0) { ($0 + Int($1)) % 97 }), 1] }36    }3738    func generate(modelID: String, request: GenerationRequest) -> AsyncThrowingStream<GenerationEvent, Error> {39        lock.withLock { _started += 1; _lastRequest = request }40        return AsyncThrowingStream { continuation in41            let task = Task {42                do {43                    switch modelID {44                    case "fake:unloadable": throw InferenceError.insufficientMemory(needed: 64 << 30, available: 8 << 30)45                    case "fake:slow":46                        for i in 0..<2000 { try await Task.sleep(nanoseconds: 20_000_000); continuation.yield(.text("t\(i) ")) }47                    case "fake:fail":48                        continuation.yield(.text("partial ")); try await Task.sleep(nanoseconds: 20_000_000)49                        throw InferenceError.generationFailed("the canned model exploded")50                    default:51                        if !request.tools.isEmpty {52                            continuation.yield(.toolCall(ToolInvocation(id: "call_1", name: request.tools[0].name, arguments: ["city": "Québec", "days": 3])))53                            continuation.yield(.finished(GenerationMetrics(promptTokens: 20, completionTokens: 8, stopReason: .toolCall)))54                        } else {55                            let last = request.messages.last?.content ?? ""56                            continuation.yield(.reasoning("thinking… "))57                            for word in ["Hello", ", ", "wörld", " — ", "échö: ", last] { try await Task.sleep(nanoseconds: 2_000_000); continuation.yield(.text(word)) }58                            continuation.yield(.finished(GenerationMetrics(promptTokens: 11, cachedPromptTokens: 4, completionTokens: 6, reasoningTokens: 2, timeToFirstToken: 0.01,59                                                                           promptTime: 0.02, generationTime: 0.5, totalTime: 0.6, peakMemoryBytes: 123_456,60                                                                           stopReason: request.settings.maxTokens == 1 ? .maxTokens : .endOfTurn)))61                        }62                    }63                    continuation.finish()64                } catch { continuation.finish(throwing: error) }65            }66            continuation.onTermination = { [weak self] reason in67                if case .cancelled = reason { self?.lock.withLock { self?._cancelled += 1 } }68                task.cancel()69            }70        }71    }72}7374struct PingRoute: HTTPRouteHandler {75    func handle(_ request: HTTPRequestMessage) async -> HTTPResponseMessage? {76        guard request.path == "/lc/v1/ping" else { return nil }77        return .json(["pong": request.query["name"] ?? "?", "method": request.method, "bytes": String(request.body.count)])78    }79}8081struct RunningServer {82    let server: LocalAPIServer83    let service: CannedInferenceService84    let keys: FileAPIKeyStore85    let port: UInt1686    var root: String { "http://127.0.0.1:\(port)" }8788    static func start(_ configuration: ServerConfiguration = ServerConfiguration(port: 0), routes: [any HTTPRouteHandler] = [PingRoute()]) async throws -> RunningServer {89        let service = CannedInferenceService(); let keys = FileAPIKeyStore()90        let server = LocalAPIServer(configuration: configuration, service: service, keys: keys, routes: routes)91        let port = try await server.start()92        return RunningServer(server: server, service: service, keys: keys, port: port)93    }9495    func node(insecure: Bool = false) -> RemoteNode { RemoteNode(name: "Test Mac", baseURL: "\(root)/v1", allowInsecureHTTP: insecure) }9697    func runtime(model: String = "fake:chat", key: String? = nil) -> RemoteRuntime {98        let node = node()99        return RemoteRuntime(client: RemoteNodeClient(node: node, apiKey: key), remoteModel: model,100                             info: RuntimeInfo(modelID: RemoteNodeClient.modelID(node: node.id, remoteModel: model), backend: .remote, loadTime: 0))101    }102103    func http(_ method: String, _ path: String, body: String? = nil, headers: [String: String] = [:]) async throws -> (status: Int, text: String, headers: [String: String]) {104        var request = URLRequest(url: URL(string: root + path)!); request.httpMethod = method; request.timeoutInterval = 15105        if let body { request.httpBody = Data(body.utf8); request.setValue("application/json", forHTTPHeaderField: "Content-Type") }106        for (k, v) in headers { request.setValue(v, forHTTPHeaderField: k) }107        let (data, response) = try await URLSession(configuration: .ephemeral).data(for: request)108        let http = response as! HTTPURLResponse109        var out: [String: String] = [:]110        for (k, v) in http.allHeaderFields { out["\(k)".lowercased()] = "\(v)" }111        return (http.statusCode, String(decoding: data, as: UTF8.self), out)112    }113}114115func waitUntil(timeout: TimeInterval = 5, _ condition: @Sendable () async -> Bool) async -> Bool {116    let deadline = Date().addingTimeInterval(timeout)117    while Date() < deadline { if await condition() { return true }; try? await Task.sleep(nanoseconds: 25_000_000) }118    return await condition()119}120121struct Collected { var text = ""; var reasoning = ""; var calls: [ToolInvocation] = []; var metrics: GenerationMetrics? }122123func collect(_ stream: AsyncThrowingStream<GenerationEvent, Error>) async throws -> Collected {124    var out = Collected()125    for try await event in stream {126        switch event {127        case .text(let t): out.text += t128        case .reasoning(let t): out.reasoning += t129        case .toolCall(let c): out.calls.append(c)130        case .finished(let m): out.metrics = m131        }132    }133    return out134}135
Files main
Menu

Theme