SPB Git

Repository Public

ModelHubLibraryTests.swift 10.0 KB 158 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/LocalChatDataTests/ModelHubLibraryTests.swift3// Description: GGUF header parsing on synthetic files and the installed-model library (import, persistence, delete, reconcile).4import Foundation5import LocalChatCore6import Testing7@testable import LocalChatModelHub89@Suite struct ModelHubGGUFTests {10    private func write(_ data: Data, _ name: String = "m.gguf") throws -> URL {11        let dir = FileManager.default.temporaryDirectory.appendingPathComponent("lc-gguf-\(UUID().uuidString)")12        try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)13        let url = dir.appendingPathComponent(name)14        try data.write(to: url)15        return url16    }1718    @Test func readsMetadataAndSkipsLargeArrays() throws {19        let url = try write(HubFixtures.chatGGUF())20        defer { try? FileManager.default.removeItem(at: url.deletingLastPathComponent()) }21        let header = try GGUFReader.readHeader(at: url)22        #expect(header.version == 3 && header.tensorCount == 0)23        #expect(header.architecture == "qwen3" && header.name == "Tiny Chat" && header.license == "apache-2.0")24        #expect(header.fileType == 15 && header.declaredParameterCount == 1_500_000_000)25        #expect(header.contextLength == 40960 && header.embeddingLength == 2560)26        #expect(header.hasChatTemplate && header.chatTemplate?.contains("tools") == true)27        #expect(header.metadata["tokenizer.ggml.tokens#count"]?.intValue == 5000)28        #expect(header.metadata["tokenizer.ggml.tokens"] == nil)29        #expect(!header.isProjector)30    }3132    @Test func readsVersion2AndScalarTypes() throws {33        let url = try write(HubFixtures.gguf(version: 2, [("general.architecture", .string("llama")), ("llama.context_length", .u64(8192)),34                                                          ("general.quantized", .bool(true)), ("llama.scale", .f32(0.5))]))35        defer { try? FileManager.default.removeItem(at: url.deletingLastPathComponent()) }36        let header = try GGUFReader.readHeader(at: url)37        #expect(header.version == 2 && header.contextLength == 8192)38        #expect(header.metadata["general.quantized"]?.boolValue == true && header.metadata["llama.scale"]?.numberValue == 0.5)39        #expect(!header.hasChatTemplate)40    }4142    @Test func rejectsInvalidFiles() throws {43        let notGGUF = try write(Data("this is not a model".utf8))44        #expect(throws: GGUFError.notGGUF) { try GGUFReader.readHeader(at: notGGUF) }45        var bad = HubFixtures.gguf(version: 9, [])46        let v9 = try write(bad)47        #expect(throws: GGUFError.unsupportedVersion(9)) { try GGUFReader.readHeader(at: v9) }48        bad = HubFixtures.chatGGUF()49        let truncated = try write(bad.prefix(200))50        #expect(throws: GGUFError.truncated) { try GGUFReader.readHeader(at: truncated) }51        let empty = try write(Data())52        #expect(throws: GGUFError.truncated) { try GGUFReader.readHeader(at: empty) }53        for u in [notGGUF, v9, truncated, empty] { try? FileManager.default.removeItem(at: u.deletingLastPathComponent()) }54    }55}5657@Suite struct ModelHubLibraryTests {58    @Test func importsAnMLXFolderAndPersists() async throws {59        let paths = HubFixtures.tempPaths(); defer { HubFixtures.cleanup(paths) }60        let source = try HubFixtures.writeMLXFolder(in: paths.root.appendingPathComponent("src"), name: "Tiny-3B-4bit", weights: 10_000)61        let library = ModelLibrary(paths: paths)62        let d = try await library.importModel(from: source)63        #expect(d.id == "mlx:local/Tiny-3B-4bit" && d.imported && d.backend == .mlx)64        #expect(d.localPath == "imported/Tiny-3B-4bit")65        #expect(d.architecture == "qwen3" && d.quantization == "4bit" && d.contextLength == 32768)66        #expect(d.capabilities == [.chat, .tools])67        #expect(d.parameterCount == 3_000_000_000 && d.parameterProvenance == .inferred)68        #expect((d.sizeBytes ?? 0) > 10_000 && d.addedAt != nil)69        #expect(FileManager.default.fileExists(atPath: source.path), "copy mode leaves the source in place")70        #expect(await library.diskUsage() == d.sizeBytes)7172        // a second instance reads the same index from disk73        let reopened = ModelLibrary(paths: paths)74        #expect(await reopened.all().map(\.id) == ["mlx:local/Tiny-3B-4bit"])75        await reopened.touch(id: d.id)76        #expect(await reopened.model(id: d.id)?.lastUsedAt != nil)77    }7879    @Test func importsAGGUFFileByMoving() async throws {80        let paths = HubFixtures.tempPaths(); defer { HubFixtures.cleanup(paths) }81        let src = paths.root.appendingPathComponent("src"); try FileManager.default.createDirectory(at: src, withIntermediateDirectories: true)82        let file = src.appendingPathComponent("tiny-chat-Q4_K_M.gguf")83        try HubFixtures.chatGGUF().write(to: file)84        let library = ModelLibrary(paths: paths)85        let d = try await library.importModel(from: file, mode: .move)86        #expect(d.id == "gguf:local:tiny-chat-Q4_K_M.gguf" && d.backend == .gguf && d.format == .gguf)87        #expect(d.quantization == "Q4_K_M" && d.architecture == "qwen3" && d.contextLength == 40960)88        #expect(d.parameterCount == 1_500_000_000 && d.parameterProvenance == .declared)89        #expect(d.capabilities == [.chat, .tools, .reasoning] && d.license == "apache-2.0")90        #expect(!FileManager.default.fileExists(atPath: file.path), "move mode removes the source")91        #expect(d.sizeBytes == Int64(HubFixtures.chatGGUF().count))92    }9394    @Test func rejectsWhatIsNotAModel() async throws {95        let paths = HubFixtures.tempPaths(); defer { HubFixtures.cleanup(paths) }96        let src = paths.root.appendingPathComponent("src"); try FileManager.default.createDirectory(at: src, withIntermediateDirectories: true)97        let fake = src.appendingPathComponent("fake.gguf"); try Data("hello".utf8).write(to: fake)98        let text = src.appendingPathComponent("notes.txt"); try Data("hello".utf8).write(to: text)99        let emptyDir = src.appendingPathComponent("empty"); try FileManager.default.createDirectory(at: emptyDir, withIntermediateDirectories: true)100        let library = ModelLibrary(paths: paths)101        for url in [fake, text, emptyDir, src.appendingPathComponent("missing")] {102            await #expect(throws: HubError.self) { try await library.importModel(from: url) }103        }104        #expect(await library.all().isEmpty)105        #expect(((try? FileManager.default.contentsOfDirectory(atPath: paths.models.appendingPathComponent("imported").path)) ?? []).isEmpty)106    }107108    @Test func deleteRemovesFilesAndNotifies() async throws {109        let paths = HubFixtures.tempPaths(); defer { HubFixtures.cleanup(paths) }110        let library = ModelLibrary(paths: paths)111        var changes = await library.changes().makeAsyncIterator()112        #expect(await changes.next()?.isEmpty == true)113        let source = try HubFixtures.writeMLXFolder(in: paths.root.appendingPathComponent("src"), name: "A-1B-8bit")114        let d = try await library.importModel(from: source)115        #expect(await changes.next()?.count == 1)116        let url = try #require(await library.url(for: d))117        #expect(FileManager.default.fileExists(atPath: url.path))118        try await library.delete(id: d.id)119        #expect(!FileManager.default.fileExists(atPath: url.path))120        #expect(await library.all().isEmpty)121        #expect(await library.diskUsage() == 0)122        #expect(await changes.next()?.isEmpty == true)123    }124125    @Test func reconcileDropsMissingAndAdoptsFound() async throws {126        let paths = HubFixtures.tempPaths(); defer { HubFixtures.cleanup(paths) }127        let library = ModelLibrary(paths: paths)128        let kept = try HubFixtures.writeMLXFolder(in: paths.root.appendingPathComponent("src"), name: "Kept-2B-4bit")129        let gone = try HubFixtures.writeMLXFolder(in: paths.root.appendingPathComponent("src"), name: "Gone-2B-4bit")130        _ = try await library.importModel(from: kept)131        let goneDescriptor = try await library.importModel(from: gone)132        try FileManager.default.removeItem(at: #require(await library.url(for: goneDescriptor)))133        // files that appeared outside the app: a downloaded-looking MLX repo, a GGUF repo with a split file and a projector134        _ = try HubFixtures.writeMLXFolder(in: paths.models.appendingPathComponent("acme"), name: "Found-7B-4bit", vision: true)135        let ggufDir = paths.models.appendingPathComponent("acme/Found-GGUF"); try FileManager.default.createDirectory(at: ggufDir, withIntermediateDirectories: true)136        try HubFixtures.chatGGUF().write(to: ggufDir.appendingPathComponent("found-Q8_0-00001-of-00002.gguf"))137        try HubFixtures.bytes(500).write(to: ggufDir.appendingPathComponent("found-Q8_0-00002-of-00002.gguf"))138        try HubFixtures.gguf([("general.architecture", .string("clip"))]).write(to: ggufDir.appendingPathComponent("mmproj-found-f16.gguf"))139140        let report = await library.reconcile()141        #expect(report.removed == [goneDescriptor.id])142        #expect(Set(report.adopted) == ["mlx:acme/Found-7B-4bit", "gguf:acme/Found-GGUF:found-Q8_0-00001-of-00002.gguf"])143        let all = await library.all()144        #expect(all.count == 3)145        let vlm = try #require(all.first { $0.id == "mlx:acme/Found-7B-4bit" })146        #expect(vlm.capabilities.contains(.vision) && vlm.repoID == "acme/Found-7B-4bit" && !vlm.imported)147        let split = try #require(all.first { $0.backend == .gguf })148        #expect(split.sizeBytes == Int64(HubFixtures.chatGGUF().count + 500), "both parts are counted")149        #expect(split.capabilities.contains(.vision), "a projector sits next to the model")150        #expect(await library.reconcile() == ReconcileReport(), "a second pass changes nothing")151152        // deleting the split model removes both parts, the projector and the empty folders153        try await library.delete(id: split.id)154        #expect(!FileManager.default.fileExists(atPath: ggufDir.path))155        #expect(FileManager.default.fileExists(atPath: paths.models.appendingPathComponent("acme/Found-7B-4bit").path))156    }157}158
Files main
Menu

Theme