Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/LocalChatServiceTests/RemoteTests.swift3// Description: Remote inference: node registry persistence and policy, backend loading, health and connection diagnostics against a live local server.4import Foundation5import LocalAPIServer6import LocalChatCore7import LocalChatNetworking8import RemoteInference9import Testing1011@Suite("Remote inference")12struct RemoteTests {13 @Test func modelIdentifiers() {14 let id = UUID()15 let full = RemoteNodeClient.modelID(node: id, remoteModel: "mlx:mlx-community/Qwen3-4B-4bit")16 #expect(full == "remote:\(id.uuidString):mlx:mlx-community/Qwen3-4B-4bit")17 #expect(RemoteNodeClient.remoteModelName(fromID: full) == "mlx:mlx-community/Qwen3-4B-4bit")18 #expect(RemoteNodeClient.remoteModelName(fromID: "mlx:foo") == nil)19 #expect(RemoteNodeClient.remoteModelName(fromID: "remote:\(id.uuidString):") == nil)20 }2122 @Test func registryPersistsNodesAndEnforcesTransportPolicy() async throws {23 let url = FileManager.default.temporaryDirectory.appendingPathComponent("lc-nodes-\(UUID().uuidString)/nodes.json")24 defer { try? FileManager.default.removeItem(at: url.deletingLastPathComponent()) }25 let registry = RemoteNodeRegistry(fileURL: url, apiKey: { _ in "secret-key" })26 let studio = try await registry.add(RemoteNode(name: "Studio", baseURL: "http://192.168.2.103:8765/v1/"))27 await #expect(throws: URLPolicy.Violation.self) { try await registry.add(RemoteNode(name: "Public", baseURL: "http://example.com/v1")) }28 try await registry.add(RemoteNode(name: "Public opt-in", baseURL: "http://example.com/v1", allowInsecureHTTP: true))29 try await registry.add(RemoteNode(name: "MacLustr", baseURL: "https://llm.spboucher.ai/v1", kind: .openAICompatible))30 #expect(await registry.allNodes().map(\.name) == ["Studio", "Public opt-in", "MacLustr"])31 #expect(try await registry.client(for: studio).endpoint("models").absoluteString == "http://192.168.2.103:8765/v1/models")32 #expect(await registry.client(for: studio).apiKey == "secret-key")33 #expect(!(try String(contentsOf: url, encoding: .utf8)).contains("secret-key")) // keys never reach the file3435 var renamed = studio; renamed.name = "Mac Studio"; renamed.isEnabled = false36 try await registry.update(renamed)37 try await registry.remove(id: await registry.allNodes()[1].id)38 let reopened = RemoteNodeRegistry(fileURL: url, apiKey: { _ in nil })39 #expect(await reopened.allNodes().map(\.name) == ["Mac Studio", "MacLustr"])40 #expect(await reopened.node(id: studio.id)?.isEnabled == false)41 // a disabled node is never contacted42 #expect(await reopened.refresh(id: studio.id)?.health.state == .unknown)43 }4445 @Test func registryRefreshDiscoversModelsAndHealth() async throws {46 let s = try await RunningServer.start(ServerConfiguration(port: 0, requireAuth: true)); defer { Task { await s.server.stop() } }47 let (_, secret) = try await s.keys.create(label: "phone")48 let good = s.node(); var locked = s.node(); locked.name = "No key"49 let dead = RemoteNode(name: "Dead", baseURL: "http://127.0.0.1:9/v1")50 let registry = RemoteNodeRegistry(fileURL: nil, apiKey: { $0.id == good.id ? secret : nil })51 for n in [good, locked, dead] { try await registry.add(n) }52 let statuses = await registry.refresh()53 #expect(statuses.map(\.node.name) == ["Test Mac", "No key", "Dead"])54 #expect(statuses[0].health.state == .online && (statuses[0].health.latency ?? 0) > 0 && statuses[0].models.count == 5)55 #expect(statuses[0].models.allSatisfy { $0.backend == .remote && $0.remoteNodeID == good.id && $0.id.hasPrefix("remote:\(good.id.uuidString):") })56 #expect(statuses[1].health.state == .unauthorized && statuses[1].models.isEmpty)57 #expect(statuses[2].health.state == .offline && statuses[2].health.detail?.isEmpty == false)58 #expect(await registry.availableModels().count == 5)5960 // the backend loads a runtime for a discovered model and streams through it61 let backend = RemoteBackend(registry: registry, apiKey: { $0.id == good.id ? secret : nil })62 let descriptor = try #require(statuses[0].models.first { $0.name == "Fake Chat" })63 let runtime = try await backend.load(descriptor, modelsRoot: FileManager.default.temporaryDirectory, progress: { _ in })64 #expect(runtime.info.backend == .remote && runtime.info.weightsMemoryBytes == nil && runtime.info.contextLength == 4096)65 #expect(await runtime.countTokens("anything") == nil) // no local tokenizer: never an invented count66 let out = try await collect(runtime.generate(GenerationRequest(messages: [.user("via backend")])))67 #expect(out.text.hasSuffix("via backend") && out.metrics?.completionTokens == 6)6869 try await registry.remove(id: good.id)70 await #expect(throws: InferenceError.self) { _ = try await backend.load(descriptor, modelsRoot: FileManager.default.temporaryDirectory, progress: { _ in }) }71 var orphan = descriptor; orphan.remoteNodeID = nil72 await #expect(throws: InferenceError.modelNotFound("Fake Chat")) { _ = try await backend.load(orphan, modelsRoot: FileManager.default.temporaryDirectory, progress: { _ in }) }73 }7475 @Test func connectionDiagnosticsNameTheFailingStep() async throws {76 let s = try await RunningServer.start(ServerConfiguration(port: 0, requireAuth: true)); defer { Task { await s.server.stop() } }77 let (_, secret) = try await s.keys.create(label: "diag")78 func outcomes(_ checks: [DiagnosticCheck]) -> [String] { checks.map { "\($0.name)=\($0.outcome.rawValue)" } }7980 let ok = await RemoteNodeClient(node: s.node(), apiKey: secret).diagnostics()81 #expect(outcomes(ok) == ["URL policy=passed", "Connection=passed", "TLS=skipped", "Authentication=passed", "Models=passed"])82 #expect(ok.last?.detail.contains("5 models") == true)8384 let noKey = await RemoteNodeClient(node: s.node(), apiKey: nil).diagnostics()85 #expect(outcomes(noKey) == ["URL policy=passed", "Connection=passed", "TLS=skipped", "Authentication=failed", "Models=skipped"])86 #expect(noKey[3].detail.contains("none is set"))8788 let refused = await RemoteNodeClient(node: RemoteNode(name: "x", baseURL: "http://127.0.0.1:9/v1"), apiKey: nil).diagnostics(timeout: 3)89 #expect(outcomes(refused) == ["URL policy=passed", "Connection=failed", "TLS=skipped", "Authentication=skipped", "Models=skipped"])9091 let policy = await RemoteNodeClient(node: RemoteNode(name: "x", baseURL: "http://example.com/v1"), apiKey: nil).diagnostics()92 #expect(outcomes(policy).first == "URL policy=failed" && policy.dropFirst().allSatisfy { $0.outcome == .skipped })9394 // right host, wrong path: the answer is not a model list95 var wrongPath = s.node(); wrongPath.baseURL = s.root + "/v2"96 let notAPI = await RemoteNodeClient(node: wrongPath, apiKey: secret).diagnostics()97 #expect(notAPI.last?.name == "Models" && notAPI.last?.outcome == .failed)98 #expect(await RemoteNodeClient(node: wrongPath, apiKey: secret).health().state == .offline)99 }100}101