Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/LocalChatDataTests/ModelHubDeviceCatalogTests.swift3// Description: Device classes and the per-device MLX catalog: memory rule, size estimates, architecture filtering; plus a live listing of mlx-community gated by LC_LIVE=1.4import Foundation5import Testing6import LocalChatCore7@testable import LocalChatModelHub89@Suite struct ModelHubDeviceCatalogTests {10 func model(_ repo: String, _ arch: String?, params: Int64, bits: Double, downloads: Int = 1000, task: CatalogModel.Task = .chat) -> CatalogModel {11 CatalogModel(repoID: repo, task: task, architecture: arch, parameterCount: params, quantization: "\(Int(bits))bit", bitsPerWeight: bits + 0.5,12 estimatedSizeBytes: Int64(Double(params) * (bits + 0.5) / 8), downloads: downloads, likes: 0, lastModified: nil, isGated: false, license: nil,13 capabilities: [.chat], unsupportedReason: arch == "qwen3" ? nil : "unsupported")14 }1516 @Test func memoryBudgetsFollowTheDeviceRule() {17 let iphone8 = AppleDeviceClass.all.first { $0.id == "iPhone-8" }!18 let mac48 = AppleDeviceClass.all.first { $0.id == "mac-48" }!19 #expect(iphone8.usableModelMemoryBytes == Int64(8.0 * 1_073_741_824 * 0.55))20 #expect(mac48.usableModelMemoryBytes == Int64(48.0 * 1_073_741_824 * 0.75))21 #expect(iphone8.fit(estimatedMemoryBytes: nil) == .unknown)22 #expect(iphone8.fit(estimatedMemoryBytes: 1_000_000_000) == .comfortable)23 #expect(iphone8.fit(estimatedMemoryBytes: 4_000_000_000) == .tight)24 #expect(iphone8.fit(estimatedMemoryBytes: 6_000_000_000) == .tooLarge)25 }2627 @Test func entriesKeepOnlyLoadableModelsThatFit() {28 let models = [model("mlx-community/Qwen3-4B-4bit", "qwen3", params: 4_000_000_000, bits: 4, downloads: 9000),29 model("mlx-community/Qwen3-30B-4bit", "qwen3", params: 30_000_000_000, bits: 4, downloads: 8000),30 model("mlx-community/Exotic-1B-4bit", "exotic", params: 1_000_000_000, bits: 4, downloads: 7000)]31 let iphone = AppleDeviceClass.all.first { $0.id == "iPhone-8" }!32 let mac = AppleDeviceClass.all.first { $0.id == "mac-48" }!33 #expect(DeviceModelCatalog.entries(models, for: iphone).map(\.model.name) == ["Qwen3-4B-4bit"])34 #expect(DeviceModelCatalog.entries(models, for: mac).map(\.model.name) == ["Qwen3-4B-4bit", "Qwen3-30B-4bit"])35 #expect(DeviceModelCatalog.bestPicks(models, for: mac, task: .chat).first?.model.name == "Qwen3-30B-4bit")36 }3738 @Test func bestPicksKeepOneQuantizationPerModel() {39 let models = [model("mlx-community/Qwen3-4B-4bit", "qwen3", params: 4_000_000_000, bits: 4, downloads: 9000),40 model("mlx-community/Qwen3-4B-8bit", "qwen3", params: 4_000_000_000, bits: 8, downloads: 3000)]41 let mac = AppleDeviceClass.all.first { $0.id == "mac-48" }!42 #expect(DeviceModelCatalog.bestPicks(models, for: mac, task: .chat).map(\.model.name) == ["Qwen3-4B-4bit"])43 }4445 @Test func registryOfLoadableArchitecturesIsPopulated() {46 #expect(MLXArchitectures.text.contains("qwen3") && MLXArchitectures.text.contains("llama"))47 #expect(MLXArchitectures.vision.contains("gemma3"))48 #expect(MLXArchitectures.embedding.contains("bert"))49 }5051 @Test func liveListingOfMLXCommunity() async throws {52 guard ProcessInfo.processInfo.environment["LC_LIVE"] == "1" else { return }53 let catalog = DeviceModelCatalog(api: HuggingFaceAPI(), cacheURL: nil)54 let snapshot = try await catalog.models(forceRefresh: true, pagesPerTask: 1)55 print("LIVE catalog:", snapshot.models.count, "models,", snapshot.models.filter(\.isSupported).count, "loadable,", snapshot.models.filter { $0.estimatedSizeBytes != nil }.count, "with a size estimate,", snapshot.models.filter(\.parametersAreDeclared).count, "with declared parameters")56 #expect(snapshot.models.count > 50)57 for device in AppleDeviceClass.all {58 let picks = DeviceModelCatalog.bestPicks(snapshot.models, for: device, task: .chat, count: 3)59 print("LIVE", device.title, "→", picks.map { "\($0.model.name) (\(ByteFormat.memory($0.estimatedMemoryBytes ?? 0)))" })60 #expect(!picks.isEmpty)61 }62 }63}64