Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Astra9// File : Tests/AstraLocalTests/Support.swift10// Module : AstraLocalTests11// Repository : https://git.spboucher.ai/orix-astra12// Created : 2026-11-2113// Last Modified : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author : Simon-Pierre Boucher20// Position : Professor of Finance21// Institution : Université du Québec en Outaouais (UQO)22// Department : Département des sciences administratives23// Organization : SP Boucher24//25// Website : https://spboucher.ai26// Git : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General : info@spboucher.ai33// Professional : contact@spboucher.ai34// Direct : simon@spboucher.ai35// Research : research@spboucher.ai36// Development : dev@spboucher.ai37// Support : support@spboucher.ai38// Security/Admin : admin@spboucher.ai39// Notifications : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification : Public66// Distribution : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License : Proprietary, all rights reserved (see license file)80// License File : LICENSE81// Maintainer : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import AstraCore89import CryptoKit90import Foundation91@testable import AstraLocal9293func sha256Hex(_ data: Data) -> String { SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined() }9495func gitBlobHex(_ data: Data) -> String {96 var h = Insecure.SHA1(); h.update(data: Data("blob \(data.count)\0".utf8)); h.update(data: data)97 return h.finalize().map { String(format: "%02x", $0) }.joined()98}99100/// Deterministic pseudo-random bytes.101func bytes(_ count: Int, seed: UInt64 = 1) -> Data {102 var state = seed &* 6364136223846793005 &+ 1442695040888963407103 var out = Data(capacity: count)104 for _ in 0..<count { state = state &* 6364136223846793005 &+ 1442695040888963407; out.append(UInt8(truncatingIfNeeded: state >> 33)) }105 return out106}107108final class TempDirectory: @unchecked Sendable {109 let url: URL110 init() {111 url = FileManager.default.temporaryDirectory.appendingPathComponent("astra-local-tests-" + UUID().uuidString, isDirectory: true)112 try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)113 }114 deinit { try? FileManager.default.removeItem(at: url) }115}116117/// A local stand-in for the mirror and for Hugging Face: serves bytes by URL with Range support and scripted misbehaviour.118final class FakeServer: DownloadTransport, @unchecked Sendable {119 enum Behaviour {120 /// Serve correctly, honouring Range.121 case ok122 case status(Int)123 /// Send only the first `n` bytes of what was asked, then end the body normally.124 case truncate(Int)125 /// Send `n` bytes, then fail the body with a network error.126 case dropAfter(Int)127 /// Answer 200 with the whole file even when a Range was requested.128 case ignoreRange129 /// Send the file followed by `n` extra bytes, without a Content-Length.130 case extra(Int)131 /// Send `n` bytes, then stall until the transfer is cancelled.132 case stall(Int)133 }134 struct Request: Sendable { var url: String; var range: String? }135136 private let lock = NSLock()137 private var files: [String: Data] = [:]138 private var scripts: [String: [Behaviour]] = [:]139 private var log: [Request] = []140 var chunkSize = 64 << 10141142 func serve(_ url: String, _ data: Data) { lock.withLock { files[url] = data } }143 /// Behaviours consumed one per request for this URL; once exhausted, `.ok`.144 func script(_ url: String, _ behaviours: [Behaviour]) { lock.withLock { scripts[url] = behaviours } }145 var requests: [Request] { lock.withLock { log } }146 func requests(to url: String) -> [Request] { requests.filter { $0.url == url } }147148 func open(_ request: URLRequest) async throws -> DownloadResponse {149 let url = request.url!.absoluteString150 let range = request.value(forHTTPHeaderField: "Range")151 let (data, behaviour) = lock.withLock { () -> (Data?, Behaviour) in152 log.append(Request(url: url, range: range))153 var b = Behaviour.ok154 if var s = scripts[url], !s.isEmpty { b = s.removeFirst(); scripts[url] = s }155 return (files[url], b)156 }157 if case .status(let code) = behaviour { return DownloadResponse(status: code, body: AsyncThrowingStream { $0.finish() }) }158 guard let data else { return DownloadResponse(status: 404, body: AsyncThrowingStream { $0.finish() }) }159160 var offset = 0161 if let range, range.hasPrefix("bytes="), let start = Int(range.dropFirst(6).split(separator: "-").first ?? "") { offset = start }162 if case .ignoreRange = behaviour { offset = 0 }163 if offset > data.count { return DownloadResponse(status: 416, body: AsyncThrowingStream { $0.finish() }) }164 var payload = data.subdata(in: offset..<data.count)165 var headers: [String: String] = ["Content-Length": String(payload.count)]166 let status = offset > 0 ? 206 : 200167 if offset > 0 { headers["Content-Range"] = "bytes \(offset)-\(data.count - 1)/\(data.count)" }168 var failAtEnd = false, stall = false169 switch behaviour {170 case .truncate(let n): payload = payload.prefix(n)171 case .dropAfter(let n): payload = payload.prefix(n); failAtEnd = true172 case .extra(let n): payload.append(bytes(n, seed: 99)); headers["Content-Length"] = nil173 case .stall(let n): payload = payload.prefix(n); stall = true174 default: break175 }176 let chunk = chunkSize177 let (payloadCopy, stalls, fails) = (payload, stall, failAtEnd)178 let body = AsyncThrowingStream<Data, Error> { continuation in179 let task = Task {180 let payload = payloadCopy181 var i = payload.startIndex182 while i < payload.endIndex {183 if Task.isCancelled { continuation.finish(throwing: CancellationError()); return }184 let end = min(payload.endIndex, i + chunk)185 continuation.yield(payload.subdata(in: i..<end)); i = end186 await Task.yield()187 }188 if stalls {189 while !Task.isCancelled { try? await Task.sleep(nanoseconds: 5_000_000) }190 continuation.finish(throwing: CancellationError()); return191 }192 if fails { continuation.finish(throwing: URLError(.networkConnectionLost)) } else { continuation.finish() }193 }194 continuation.onTermination = { _ in task.cancel() }195 }196 return DownloadResponse(status: status, headers: headers, body: body)197 }198}199200/// HTTP seam for catalog and Hub API tests: canned responses by URL.201final class FakeHTTP: HTTPTransport, @unchecked Sendable {202 private let lock = NSLock()203 private var responses: [String: (Int, Data)] = [:]204 func respond(_ url: String, status: Int = 200, _ data: Data) { lock.withLock { responses[url] = (status, data) } }205 func send(_ request: URLRequest) async throws -> (Data, HTTPURLResponse) {206 let url = request.url!.absoluteString207 guard let (status, data) = lock.withLock({ responses[url] }) else { throw AstraError.network("no route for \(url)") }208 return (data, HTTPURLResponse(url: request.url!, statusCode: status, httpVersion: "HTTP/1.1", headerFields: [:])!)209 }210 func stream(_ request: URLRequest) async throws -> (HTTPURLResponse, AsyncThrowingStream<String, Error>) { throw AstraError.unsupported("not used") }211}212213enum Fixture {214 static let mirror = "https://mirror.test/astra/models/v1/tiny/"215 static let hub = "https://huggingface.co/acme/tiny/resolve/0123456789abcdef0123456789abcdef01234567/"216 static let revision = "0123456789abcdef0123456789abcdef01234567"217218 static let contents: [(String, Data)] = [219 ("config.json", Data(#"{"model_type":"tiny","max_position_embeddings":2048}"#.utf8)),220 ("tokenizer.json", bytes(40_000, seed: 2)),221 ("model.safetensors", bytes(3_000_000, seed: 3)),222 ]223224 static func card(id: String = "tiny", redistributable: Bool = true, files: [(String, Data)] = contents) -> LocalModelCard {225 let list = files.map { LocalModelFile(path: $0.0, size: Int64($0.1.count), sha256: sha256Hex($0.1)) }226 return LocalModelCard(id: id, displayName: "Tiny", family: "Tiny", publisher: "acme", repo: "acme/tiny", revision: revision, architecture: "tiny",227 quantization: "4-bit", parameterCount: 1_000_000, licenseID: "apache-2.0", licenseURL: "https://www.apache.org/licenses/LICENSE-2.0",228 redistributable: redistributable, totalBytes: list.reduce(0) { $0 + $1.size }, files: list, contextLength: 2048,229 minimumDeviceMemoryGB: 4, estimatedRuntimeMemoryBytes: 500 << 20,230 baseURL: redistributable ? mirror : nil, fallbackBaseURL: hub)231 }232233 /// Registers the card's files on the server under `base`.234 static func publish(_ server: FakeServer, base: String, files: [(String, Data)] = contents) {235 for (path, data) in files { server.serve(base + path, data) }236 }237238 static func coordinator(_ dir: TempDirectory, _ server: FakeServer, free: Int64? = 1 << 40, attempts: Int = 4) -> (DownloadCoordinator, ModelStore) {239 let store = ModelStore(root: dir.url.appendingPathComponent("Models"))240 var config = DownloadCoordinator.Configuration()241 config.retryDelay = { _ in 0 }242 config.sleep = { _ in await Task.yield() }243 config.maxAttemptsPerSource = attempts244 config.safetyMarginBytes = 1 << 20245 config.freeSpace = { _ in free }246 return (DownloadCoordinator(store: store, transport: server, configuration: config), store)247 }248}249