SPB Git

Repository Public MIT

DownloadTests.swift 23.0 KB 426 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Astra9// File             : Tests/AstraLocalTests/DownloadTests.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 Foundation90import Testing91@testable import AstraLocal9293@Suite struct DownloadCoordinatorTests {94    let dir = TempDirectory()95    let server = FakeServer()9697    private func fileData(_ store: ModelStore, _ path: String, id: String = "tiny") throws -> Data {98        try Data(contentsOf: store.fileURL(modelID: id, path: path))99    }100    private func partials(_ store: ModelStore) -> [String] {101        let walker = FileManager.default.enumerator(at: store.root, includingPropertiesForKeys: nil)102        return (walker?.allObjects as? [URL] ?? []).map(\.lastPathComponent).filter { $0.hasSuffix(".partial") }103    }104105    private func waitForPartial(_ store: ModelStore, _ size: Int64) async throws {106        let url = URL(fileURLWithPath: try store.fileURL(modelID: "tiny", path: "model.safetensors").path + ".partial")107        try await waitFor { (ModelStore.fileSize(url) ?? 0) >= size }108    }109110    @Test func downloadsFromMirrorVerifiesAndInstalls() async throws {111        Fixture.publish(server, base: Fixture.mirror)112        let (coordinator, store) = Fixture.coordinator(dir, server)113        let card = Fixture.card()114        try await coordinator.start(DownloadPlan(card: card))115        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))116        #expect(status.phase == .completed)117        #expect(status.bytesDone == card.totalBytes && status.filesDone == 3 && !status.usedFallback)118        #expect(status.sha256["model.safetensors"] == card.files[2].sha256)119        #expect(store.isInstalled("tiny"))120        #expect(try fileData(store, "model.safetensors") == Fixture.contents[2].1)121        #expect(partials(store).isEmpty)122        #expect(server.requests.allSatisfy { $0.url.hasPrefix(Fixture.mirror) })123        #expect(store.isExcludedFromBackup())124    }125126    @Test func resumesWithRangeAfterTruncatedResponse() async throws {127        Fixture.publish(server, base: Fixture.mirror)128        let url = Fixture.mirror + "model.safetensors"129        server.script(url, [.truncate(1_200_000), .dropAfter(500_000)])130        let (coordinator, store) = Fixture.coordinator(dir, server)131        try await coordinator.start(DownloadPlan(card: Fixture.card()))132        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))133        #expect(status.phase == .completed)134        let ranges = server.requests(to: url).map(\.range)135        #expect(ranges == [nil, "bytes=1200000-", "bytes=1700000-"])136        #expect(try fileData(store, "model.safetensors") == Fixture.contents[2].1)137        #expect(status.retries == 2 && !status.usedFallback)138    }139140    @Test func serverErrorsThenSuccess() async throws {141        Fixture.publish(server, base: Fixture.mirror)142        server.script(Fixture.mirror + "tokenizer.json", [.status(503), .status(500)])143        let (coordinator, store) = Fixture.coordinator(dir, server)144        try await coordinator.start(DownloadPlan(card: Fixture.card()))145        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))146        #expect(status.phase == .completed && !status.usedFallback)147        #expect(server.requests(to: Fixture.mirror + "tokenizer.json").count == 3)148        #expect(store.isInstalled("tiny"))149    }150151    @Test func serverIgnoringRangeRestartsTheFile() async throws {152        Fixture.publish(server, base: Fixture.mirror)153        let url = Fixture.mirror + "model.safetensors"154        server.script(url, [.truncate(900_000), .ignoreRange])155        let (coordinator, store) = Fixture.coordinator(dir, server)156        try await coordinator.start(DownloadPlan(card: Fixture.card()))157        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))158        #expect(status.phase == .completed)159        #expect(try fileData(store, "model.safetensors") == Fixture.contents[2].1)160    }161162    @Test func mirrorDownFallsBackToHub() async throws {163        Fixture.publish(server, base: Fixture.hub)   // nothing on the mirror: 404164        let (coordinator, store) = Fixture.coordinator(dir, server)165        try await coordinator.start(DownloadPlan(card: Fixture.card()))166        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))167        #expect(status.phase == .completed && status.usedFallback)168        #expect(store.isInstalled("tiny"))169        #expect(server.requests.first?.url.hasPrefix(Fixture.mirror) == true)170    }171172    @Test func mirrorFailingRepeatedlyFallsBack() async throws {173        Fixture.publish(server, base: Fixture.mirror); Fixture.publish(server, base: Fixture.hub)174        server.script(Fixture.mirror + "model.safetensors", Array(repeating: .status(502), count: 10))175        let (coordinator, _) = Fixture.coordinator(dir, server, attempts: 3)176        try await coordinator.start(DownloadPlan(card: Fixture.card()))177        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))178        #expect(status.phase == .completed && status.usedFallback)179        #expect(server.requests(to: Fixture.mirror + "model.safetensors").count == 3)180        #expect(server.requests(to: Fixture.hub + "model.safetensors").count == 1)181    }182183    @Test func nonRedistributableModelNeverTouchesTheMirror() async throws {184        Fixture.publish(server, base: Fixture.mirror); Fixture.publish(server, base: Fixture.hub)185        let (coordinator, _) = Fixture.coordinator(dir, server)186        var card = Fixture.card(redistributable: false); card.baseURL = Fixture.mirror187        try await coordinator.start(DownloadPlan(card: card))188        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))189        #expect(status.phase == .completed)190        #expect(server.requests.allSatisfy { $0.url.hasPrefix(Fixture.hub) })191    }192193    @Test func hashMismatchDeletesTheFileAndFails() async throws {194        var wrong = Fixture.contents195        wrong[2].1 = bytes(3_000_000, seed: 77)   // same size, different bytes196        Fixture.publish(server, base: Fixture.mirror, files: wrong)197        Fixture.publish(server, base: Fixture.hub, files: wrong)198        let (coordinator, store) = Fixture.coordinator(dir, server)199        try await coordinator.start(DownloadPlan(card: Fixture.card()))200        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))201        #expect(status.phase == .failed(.hashMismatch(file: "model.safetensors")))202        #expect(!store.isInstalled("tiny"))203        let model = try store.fileURL(modelID: "tiny", path: "model.safetensors")204        #expect(!FileManager.default.fileExists(atPath: model.path))205        #expect(partials(store).isEmpty)206        #expect(store.loadableDirectory(for: "tiny") == nil)207    }208209    @Test func corruptMirrorIsReplacedByFallbackWhenHashesMatchThere() async throws {210        var wrong = Fixture.contents; wrong[1].1 = bytes(40_000, seed: 50)211        Fixture.publish(server, base: Fixture.mirror, files: wrong)212        Fixture.publish(server, base: Fixture.hub)213        let (coordinator, store) = Fixture.coordinator(dir, server)214        try await coordinator.start(DownloadPlan(card: Fixture.card()))215        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))216        #expect(status.phase == .completed && status.usedFallback)217        #expect(try fileData(store, "tokenizer.json") == Fixture.contents[1].1)218    }219220    @Test func diskFullPreflightRefusesBeforeAnyRequest() async throws {221        Fixture.publish(server, base: Fixture.mirror)222        let (coordinator, store) = Fixture.coordinator(dir, server, free: 2_000_000)   // 3 MB needed + 1 MB margin223        try await coordinator.start(DownloadPlan(card: Fixture.card()))224        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))225        guard case .failed(.insufficientDisk(let needed, let available)) = status.phase else { Issue.record("expected insufficientDisk, got \(status.phase)"); return }226        #expect(available == 2_000_000 && needed == Fixture.card().totalBytes + (1 << 20))227        #expect(server.requests.isEmpty)228        #expect(!store.isInstalled("tiny"))229    }230231    @Test func traversalAndAbsolutePathsAreRejected() async throws {232        let (coordinator, store) = Fixture.coordinator(dir, server)233        let hash = String(repeating: "a", count: 64)234        for path in ["../evil.bin", "a/../../evil.bin", "/etc/passwd", "~/x", "a//b", "a\\b", ".astra/card.json", "weights.partial", "./x", ""] {235            let plan = DownloadPlan(id: "tiny", files: [.init(path: path, size: 4, sha256: hash)], sources: [URL(string: Fixture.mirror)!])236            await #expect(throws: DownloadError.self) { try await coordinator.start(plan) }237        }238        await #expect(throws: DownloadError.self) {239            try await coordinator.start(DownloadPlan(id: "../up", files: [.init(path: "a", size: 1, sha256: hash)], sources: [URL(string: Fixture.mirror)!]))240        }241        #expect(server.requests.isEmpty)242        #expect(!FileManager.default.fileExists(atPath: dir.url.appendingPathComponent("evil.bin").path))243        #expect(throws: (any Error).self) { try store.fileURL(modelID: "tiny", path: "../x") }244    }245246    @Test func symlinkInsideTheModelFolderIsNotFollowed() async throws {247        Fixture.publish(server, base: Fixture.mirror)248        let (coordinator, store) = Fixture.coordinator(dir, server)249        let outside = dir.url.appendingPathComponent("outside"); try FileManager.default.createDirectory(at: outside, withIntermediateDirectories: true)250        let modelDir = try store.directory(for: "tiny"); try FileManager.default.createDirectory(at: modelDir, withIntermediateDirectories: true)251        try FileManager.default.createSymbolicLink(at: modelDir.appendingPathComponent("sub"), withDestinationURL: outside)252        let data = bytes(1000, seed: 5); server.serve(Fixture.mirror + "sub/x.bin", data)253        let plan = DownloadPlan(id: "tiny", files: [.init(path: "sub/x.bin", size: 1000, sha256: sha256Hex(data))], sources: [URL(string: Fixture.mirror)!])254        try await coordinator.start(plan)255        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))256        #expect(status.phase == .failed(.unsafePath("sub/x.bin")))257        #expect((try? FileManager.default.contentsOfDirectory(atPath: outside.path))?.isEmpty == true)258    }259260    @Test func oversizeBodyIsCutAndDeleted() async throws {261        Fixture.publish(server, base: Fixture.mirror)262        server.script(Fixture.mirror + "model.safetensors", [.extra(500_000)])263        let (coordinator, store) = Fixture.coordinator(dir, server)264        var card = Fixture.card(); card.fallbackBaseURL = Fixture.mirror   // single source265        try await coordinator.start(DownloadPlan(card: card))266        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))267        #expect(status.phase == .failed(.oversize(file: "model.safetensors")))268        #expect(partials(store).isEmpty)269        #expect(store.totalBytesOnDisk() < 3_000_000)   // nothing beyond the declared size was kept270        #expect(!store.isInstalled("tiny"))271    }272273    @Test func oversizeDeclaredByContentLengthWritesNothing() async throws {274        var bigger = Fixture.contents; bigger[2].1 = bytes(3_500_000, seed: 3)275        Fixture.publish(server, base: Fixture.mirror, files: bigger)276        let (coordinator, store) = Fixture.coordinator(dir, server)277        var card = Fixture.card(); card.fallbackBaseURL = Fixture.mirror278        try await coordinator.start(DownloadPlan(card: card))279        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))280        #expect(status.phase == .failed(.oversize(file: "model.safetensors")))281        #expect(partials(store).isEmpty)282    }283284    @Test func duplicateStartIsRefusedWhileActive() async throws {285        Fixture.publish(server, base: Fixture.mirror)286        server.script(Fixture.mirror + "model.safetensors", [.stall(100_000)])287        let (coordinator, _) = Fixture.coordinator(dir, server)288        let plan = DownloadPlan(card: Fixture.card())289        try await coordinator.start(plan)290        await #expect(throws: DownloadError.alreadyInProgress("tiny")) { try await coordinator.start(plan) }291        await #expect(throws: DownloadError.alreadyInProgress("tiny")) { try await coordinator.resume(id: "tiny") }292        await coordinator.cancel(id: "tiny")293    }294295    @Test func pauseKeepsPartialAndResumeFinishes() async throws {296        Fixture.publish(server, base: Fixture.mirror)297        let url = Fixture.mirror + "model.safetensors"298        server.script(url, [.stall(1_000_000)])299        let (coordinator, store) = Fixture.coordinator(dir, server)300        try await coordinator.start(DownloadPlan(card: Fixture.card()))301        try await waitForPartial(store, 1_000_000)302        await coordinator.pause(id: "tiny")303        #expect(await coordinator.status(id: "tiny")?.phase == .paused)304        #expect(partials(store) == ["model.safetensors.partial"])305        try await coordinator.resume(id: "tiny")306        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))307        #expect(status.phase == .completed)308        #expect(server.requests(to: url).last?.range?.hasPrefix("bytes=") == true)309        #expect(try fileData(store, "model.safetensors") == Fixture.contents[2].1)310    }311312    @Test func cancelRemovesEverything() async throws {313        Fixture.publish(server, base: Fixture.mirror)314        server.script(Fixture.mirror + "model.safetensors", [.stall(800_000)])315        let (coordinator, store) = Fixture.coordinator(dir, server)316        try await coordinator.start(DownloadPlan(card: Fixture.card()))317        try await waitForPartial(store, 800_000)318        await coordinator.cancel(id: "tiny")319        #expect(await coordinator.status(id: "tiny")?.phase == .cancelled)320        #expect(!FileManager.default.fileExists(atPath: try store.directory(for: "tiny").path))321        #expect(store.totalBytesOnDisk() == 0)322    }323324    @Test func interruptedDownloadIsRecoveredAfterRelaunch() async throws {325        Fixture.publish(server, base: Fixture.mirror)326        let url = Fixture.mirror + "model.safetensors"327        server.script(url, [.stall(700_000)])328        let (first, store) = Fixture.coordinator(dir, server)329        try await first.start(DownloadPlan(card: Fixture.card()))330        try await waitForPartial(store, 700_000)331        await first.pause(id: "tiny")   // stands for the process dying: the files and the state file stay332333        let (second, _) = Fixture.coordinator(dir, server)   // a new process334        let restored = await second.restore()335        #expect(restored.count == 1 && restored[0].phase == .paused)336        #expect(restored[0].bytesDone >= 700_000 && restored[0].filesDone == 2)337        try await second.resume(id: "tiny")338        let status = try #require(await second.waitUntilSettled(id: "tiny"))339        #expect(status.phase == .completed)340        #expect(store.isInstalled("tiny"))341        // config.json and tokenizer.json were verified by the first run and are not fetched again342        #expect(server.requests(to: Fixture.mirror + "config.json").count == 1)343        #expect(server.requests(to: url).last?.range == "bytes=700000-")344    }345346    @Test func tamperedCompleteFileIsDetectedOnResume() async throws {347        Fixture.publish(server, base: Fixture.mirror)348        let (coordinator, store) = Fixture.coordinator(dir, server)349        // a file of the right size but wrong content sits where the model file should be, with no record that it was verified350        let target = try store.fileURL(modelID: "tiny", path: "tokenizer.json")351        try FileManager.default.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)352        try bytes(40_000, seed: 123).write(to: target)353        try await coordinator.start(DownloadPlan(card: Fixture.card()))354        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))355        #expect(status.phase == .completed)356        #expect(try fileData(store, "tokenizer.json") == Fixture.contents[1].1)357    }358359    @Test func gitBlobChecksumIsAcceptedForSmallFiles() async throws {360        Fixture.publish(server, base: Fixture.hub)361        let (coordinator, _) = Fixture.coordinator(dir, server)362        let files = Fixture.contents.map { DownloadPlan.File(path: $0.0, size: Int64($0.1.count), gitBlobSHA1: gitBlobHex($0.1)) }363        try await coordinator.start(DownloadPlan(id: "tiny", files: files, sources: [URL(string: Fixture.hub)!]))364        let status = try #require(await coordinator.waitUntilSettled(id: "tiny"))365        #expect(status.phase == .completed)366        #expect(status.sha256["config.json"] == sha256Hex(Fixture.contents[0].1))367        var bad = files; bad[0].gitBlobSHA1 = String(repeating: "0", count: 40)368        let otherDir = TempDirectory()369        let (other, _) = Fixture.coordinator(otherDir, server)370        try await other.start(DownloadPlan(id: "tiny", files: bad, sources: [URL(string: Fixture.hub)!]))371        #expect(await other.waitUntilSettled(id: "tiny")?.phase == .failed(.hashMismatch(file: "config.json")))372    }373374    @Test func planWithoutChecksumOrWithPlainHTTPIsRefused() async throws {375        let (coordinator, _) = Fixture.coordinator(dir, server)376        await #expect(throws: DownloadError.self) {377            try await coordinator.start(DownloadPlan(id: "tiny", files: [.init(path: "a.bin", size: 1)], sources: [URL(string: Fixture.mirror)!]))378        }379        await #expect(throws: DownloadError.self) {380            try await coordinator.start(DownloadPlan(id: "tiny", files: [.init(path: "a.bin", size: 1, sha256: String(repeating: "a", count: 64))],381                                                     sources: [URL(string: "http://example.com/m/")!]))382        }383    }384385    @Test func newVersionUnderSameIDDoesNotMixFiles() async throws {386        Fixture.publish(server, base: Fixture.mirror)387        let (coordinator, store) = Fixture.coordinator(dir, server)388        try await coordinator.start(DownloadPlan(card: Fixture.card()))389        _ = await coordinator.waitUntilSettled(id: "tiny")390        var next = Fixture.contents; next[2] = ("model-v2.safetensors", bytes(1_000_000, seed: 9))391        Fixture.publish(server, base: Fixture.mirror, files: next)392        try await coordinator.start(DownloadPlan(card: Fixture.card(files: next)))393        #expect(await coordinator.waitUntilSettled(id: "tiny")?.phase == .completed)394        #expect(!FileManager.default.fileExists(atPath: try store.fileURL(modelID: "tiny", path: "model.safetensors").path))395        #expect(store.isInstalled("tiny"))396    }397398    @Test func progressReportsSpeedAndETA() async throws {399        Fixture.publish(server, base: Fixture.mirror)400        let (coordinator, _) = Fixture.coordinator(dir, server)401        let seen = Seen()402        let watcher = Task { for await list in await coordinator.updates() { if let s = list.first { seen.add(s) }; if list.first?.phase == .completed { break } } }403        try await coordinator.start(DownloadPlan(card: Fixture.card()))404        _ = await coordinator.waitUntilSettled(id: "tiny")405        await watcher.value406        let all = seen.all407        #expect(all.last?.fraction == 1)408        #expect(all.map(\.bytesDone) == all.map(\.bytesDone).sorted())   // monotonic when nothing is discarded409        #expect(all.allSatisfy { $0.bytesTotal == Fixture.card().totalBytes })410    }411}412413final class Seen: @unchecked Sendable {414    private let lock = NSLock(); private var items: [DownloadStatus] = []415    func add(_ s: DownloadStatus) { lock.withLock { items.append(s) } }416    var all: [DownloadStatus] { lock.withLock { items } }417}418419func waitFor(timeout: TimeInterval = 10, _ condition: @Sendable () async -> Bool) async throws {420    let deadline = Date().addingTimeInterval(timeout)421    while !(await condition()) {422        if Date() > deadline { throw AstraError.timeout }423        try await Task.sleep(nanoseconds: 5_000_000)424    }425}426
Files main
Menu

Theme