Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/LocalChatDataTests/ModelHubSupport.swift3// Description: Test support for the model hub: a URLProtocol stub keyed by host (with Range support and cut connections), temp folders, synthetic GGUF files.4import CryptoKit5import Foundation6import LocalChatCore7@testable import LocalChatModelHub89struct HubStubResponse: Sendable {10 var status = 20011 var headers: [String: String] = [:]12 var body = Data()13 var cutAfter: Int? // send this many body bytes, then drop the connection14 var delayPerChunk: TimeInterval = 015 var chunkSize = 64 * 102416}1718final class HubStub: URLProtocol, @unchecked Sendable {19 typealias Handler = @Sendable (URLRequest) -> HubStubResponse20 private static let lock = NSLock()21 nonisolated(unsafe) private static var handlers: [String: Handler] = [:]22 nonisolated(unsafe) private static var logs: [String: [URLRequest]] = [:]2324 /// Registers a handler under a fresh host and returns its base URL.25 static func register(_ handler: @escaping Handler) -> URL {26 let host = "stub-\(UUID().uuidString.lowercased()).test"27 lock.lock(); handlers[host] = handler; logs[host] = []; lock.unlock()28 return URL(string: "https://\(host)")!29 }30 static func requests(_ base: URL) -> [URLRequest] { lock.lock(); defer { lock.unlock() }; return logs[base.host ?? ""] ?? [] }3132 /// Serves `files` (path → bytes) under /<repo>/resolve/<rev>/<path> with Range support. `mutate` can alter the answer per request.33 static func registerFiles(_ files: [String: Data], mutate: (@Sendable (URLRequest, String, Int, inout HubStubResponse) -> Void)? = nil) -> URL {34 let counter = Counter()35 return register { request in36 let path = request.url!.path37 guard let marker = path.range(of: "/resolve/"), let slash = path[marker.upperBound...].firstIndex(of: "/"),38 let data = files[String(path[path.index(after: slash)...])] else { return HubStubResponse(status: 404) }39 let name = String(path[path.index(after: slash)...])40 var response = HubStubResponse(headers: ["Accept-Ranges": "bytes"], body: data)41 if let range = request.value(forHTTPHeaderField: "Range"), range.hasPrefix("bytes=") {42 let parts = range.dropFirst(6).split(separator: "-", omittingEmptySubsequences: false)43 let start = Int(parts[0]) ?? 044 let end = parts.count > 1 && !parts[1].isEmpty ? min(Int(parts[1])!, data.count - 1) : data.count - 145 if start >= data.count { response = HubStubResponse(status: 416, headers: ["Content-Range": "bytes */\(data.count)"]) }46 else {47 response.status = 206; response.body = data.subdata(in: start..<(end + 1))48 response.headers["Content-Range"] = "bytes \(start)-\(end)/\(data.count)"49 }50 }51 mutate?(request, name, counter.next(name), &response)52 return response53 }54 }5556 final class Counter: @unchecked Sendable {57 private let lock = NSLock(); private var counts: [String: Int] = [:]58 func next(_ key: String) -> Int { lock.lock(); defer { lock.unlock() }; counts[key, default: 0] += 1; return counts[key]! }59 func value(_ key: String) -> Int { lock.lock(); defer { lock.unlock() }; return counts[key] ?? 0 }60 }6162 override class func canInit(with request: URLRequest) -> Bool { request.url?.host?.hasSuffix(".test") == true }63 override class func canonicalRequest(for request: URLRequest) -> URLRequest { request }64 private var stopped = false6566 override func startLoading() {67 let host = request.url?.host ?? ""68 Self.lock.lock(); let handler = Self.handlers[host]; Self.logs[host, default: []].append(request); Self.lock.unlock()69 guard let handler else { client?.urlProtocol(self, didFailWithError: URLError(.cannotFindHost)); return }70 let answer = handler(request)71 let http = HTTPURLResponse(url: request.url!, statusCode: answer.status, httpVersion: "HTTP/1.1", headerFields: answer.headers)!72 client?.urlProtocol(self, didReceive: http, cacheStoragePolicy: .notAllowed)73 let limit = answer.cutAfter.map { min($0, answer.body.count) } ?? answer.body.count74 var sent = 075 while sent < limit, !stopped {76 let n = min(answer.chunkSize, limit - sent)77 client?.urlProtocol(self, didLoad: answer.body.subdata(in: sent..<(sent + n)))78 sent += n79 if answer.delayPerChunk > 0 { Thread.sleep(forTimeInterval: answer.delayPerChunk) }80 }81 if answer.cutAfter != nil {82 // like a real socket: the bytes already received reach the delegate before the connection error does83 Thread.sleep(forTimeInterval: 0.2)84 client?.urlProtocol(self, didFailWithError: URLError(.networkConnectionLost))85 }86 else { client?.urlProtocolDidFinishLoading(self) }87 }88 override func stopLoading() { stopped = true }8990 static func session() -> URLSession {91 let c = URLSessionConfiguration.ephemeral; c.protocolClasses = [HubStub.self]92 return URLSession(configuration: c)93 }94}9596enum HubFixtures {97 static func tempPaths() -> AppPaths {98 AppPaths(root: FileManager.default.temporaryDirectory.appendingPathComponent("lc-hub-tests-\(UUID().uuidString)", isDirectory: true))99 }100 static func cleanup(_ paths: AppPaths) { try? FileManager.default.removeItem(at: paths.root) }101102 static func sha256(_ data: Data) -> String { SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined() }103104 static func bytes(_ count: Int, seed: UInt8 = 7) -> Data {105 var d = Data(count: count); var x = UInt32(seed) &+ 1106 d.withUnsafeMutableBytes { buffer in107 for i in 0..<count { x = x &* 1_664_525 &+ 1_013_904_223; buffer[i] = UInt8(truncatingIfNeeded: x >> 16) }108 }109 return d110 }111112 static func configuration(_ base: URL) -> DownloadManager.Configuration {113 var c = DownloadManager.Configuration()114 c.baseURL = base; c.protocolClasses = [HubStub.self]; c.retryBaseDelay = 0.01; c.storageMarginBytes = 0; c.publishInterval = 0.05115 return c116 }117118 /// A request for an MLX-style repository made of `files`.119 static func mlxRequest(repo: String = "acme/tiny-4bit", files: [String: Data], revision: String = "abc123", lfs: Set<String> = ["model.safetensors"],120 tamper: [String: String] = [:]) -> DownloadRequest {121 let descriptor = ModelDescriptor(id: "mlx:\(repo)", name: repo.split(separator: "/").last.map(String.init) ?? repo, repoID: repo, backend: .mlx, format: .mlx)122 let list = files.keys.sorted().map { path in123 DownloadFile(path: path, size: Int64(files[path]!.count), sha256: lfs.contains(path) ? (tamper[path] ?? sha256(files[path]!)) : nil)124 }125 return DownloadRequest(descriptor: descriptor, repoID: repo, revision: revision, files: list)126 }127128 static let mlxConfig = Data(#"{"model_type":"qwen3","max_position_embeddings":40960,"quantization":{"bits":4,"group_size":64}}"#.utf8)129130 // MARK: synthetic GGUF131132 enum GGUFValue { case u32(UInt32), u64(UInt64), string(String), bool(Bool), strings([String]), f32(Float), u32s([UInt32]) }133134 static func gguf(version: UInt32 = 3, _ pairs: [(String, GGUFValue)], tensorBytes: Int = 64) -> Data {135 var d = Data()136 func put<T: FixedWidthInteger>(_ v: T) { withUnsafeBytes(of: v.littleEndian) { d.append(contentsOf: $0) } }137 func str(_ s: String) { let u = Array(s.utf8); put(UInt64(u.count)); d.append(contentsOf: u) }138 d.append(contentsOf: Array("GGUF".utf8)); put(version); put(UInt64(0)); put(UInt64(pairs.count))139 for (key, value) in pairs {140 str(key)141 switch value {142 case .u32(let v): put(UInt32(4)); put(v)143 case .u64(let v): put(UInt32(10)); put(v)144 case .f32(let v): put(UInt32(6)); put(v.bitPattern)145 case .bool(let v): put(UInt32(7)); put(UInt8(v ? 1 : 0))146 case .string(let v): put(UInt32(8)); str(v)147 case .strings(let v): put(UInt32(9)); put(UInt32(8)); put(UInt64(v.count)); v.forEach(str)148 case .u32s(let v): put(UInt32(9)); put(UInt32(4)); put(UInt64(v.count)); v.forEach { put($0) }149 }150 }151 d.append(Data(count: tensorBytes))152 return d153 }154155 static func chatGGUF(name: String = "Tiny Chat", parameters: UInt64 = 1_500_000_000) -> Data {156 gguf([157 ("general.architecture", .string("qwen3")), ("general.name", .string(name)), ("general.file_type", .u32(15)),158 ("general.parameter_count", .u64(parameters)), ("general.license", .string("apache-2.0")),159 ("qwen3.context_length", .u32(40960)), ("qwen3.embedding_length", .u32(2560)),160 ("tokenizer.ggml.tokens", .strings((0..<5000).map { "tok\($0)" })), ("tokenizer.ggml.token_type", .u32s(Array(repeating: 1, count: 5000))),161 ("tokenizer.chat_template", .string("{%- if tools %}tools{%- endif %}{{ '<think>' if enable_thinking }}")),162 ])163 }164165 /// Writes an MLX-looking model folder and returns it.166 static func writeMLXFolder(in parent: URL, name: String, weights: Int = 4096, vision: Bool = false, template: String? = "{% if tools %}x{% endif %}") throws -> URL {167 let dir = parent.appendingPathComponent(name, isDirectory: true)168 try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)169 var config: [String: Any] = ["model_type": vision ? "gemma3" : "qwen3", "max_position_embeddings": 32768, "quantization": ["bits": 4, "group_size": 64]]170 if vision { config["vision_config"] = ["hidden_size": 1152] }171 try JSONSerialization.data(withJSONObject: config).write(to: dir.appendingPathComponent("config.json"))172 try bytes(weights).write(to: dir.appendingPathComponent("model.safetensors"))173 var tokenizer: [String: Any] = ["model_max_length": 131072]174 if let template { tokenizer["chat_template"] = template }175 try JSONSerialization.data(withJSONObject: tokenizer).write(to: dir.appendingPathComponent("tokenizer_config.json"))176 return dir177 }178}179