64 lines
2.6 KiB
Swift
64 lines
2.6 KiB
Swift
import Foundation
|
|
import Testing
|
|
@testable import SstWhisper
|
|
|
|
struct ModelStorageTests {
|
|
func makeStorage() -> ModelStorage {
|
|
ModelStorage(base: FileManager.default.temporaryDirectory.appending(path: "sst-models-\(UUID().uuidString)"))
|
|
}
|
|
|
|
/// Creates `<repo>/<folder>/<model>.mlmodelc/coremldata.bin` for the given models.
|
|
func install(_ storage: ModelStorage, folder: String, models: [String] = ModelStorage.requiredModels) throws {
|
|
for model in models {
|
|
let dir = storage.repoDirectory.appending(path: "\(folder)/\(model).mlmodelc", directoryHint: .isDirectory)
|
|
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
|
try Data([1]).write(to: dir.appending(path: "coremldata.bin"))
|
|
}
|
|
}
|
|
|
|
@Test func emptyStorageHasNothingInstalled() {
|
|
let storage = makeStorage()
|
|
#expect(storage.folder(for: "small") == nil)
|
|
#expect(!storage.isInstalled("small"))
|
|
}
|
|
|
|
@Test func completeInstallIsFound() throws {
|
|
let storage = makeStorage()
|
|
try install(storage, folder: "openai_whisper-large-v3-v20240930_turbo")
|
|
#expect(storage.isInstalled("large-v3-v20240930_turbo"))
|
|
#expect(storage.folder(for: "large-v3-v20240930_turbo")?.lastPathComponent == "openai_whisper-large-v3-v20240930_turbo")
|
|
}
|
|
|
|
@Test func incompleteInstallDoesNotCount() throws {
|
|
let storage = makeStorage()
|
|
try install(storage, folder: "openai_whisper-small", models: ["MelSpectrogram", "AudioEncoder"])
|
|
#expect(!storage.isInstalled("small"))
|
|
}
|
|
|
|
@Test func variantsAreMatchedExactly() throws {
|
|
let storage = makeStorage()
|
|
try install(storage, folder: "openai_whisper-large-v3-v20240930_turbo_632MB")
|
|
#expect(!storage.isInstalled("large-v3-v20240930_turbo"))
|
|
#expect(storage.isInstalled("large-v3-v20240930_turbo_632MB"))
|
|
}
|
|
|
|
@Test func deleteRemovesOnlyThatVariant() throws {
|
|
let storage = makeStorage()
|
|
try install(storage, folder: "openai_whisper-small")
|
|
try install(storage, folder: "openai_whisper-base")
|
|
try storage.delete("small")
|
|
#expect(!storage.isInstalled("small"))
|
|
#expect(storage.isInstalled("base"))
|
|
}
|
|
|
|
@Test func deletingSomethingNotInstalledIsHarmless() throws {
|
|
try makeStorage().delete("small")
|
|
}
|
|
|
|
@Test func catalogStartsWithTheDefaultAndHasUniqueIds() {
|
|
#expect(ModelCatalog.all.first?.id == ModelCatalog.defaultVariant)
|
|
#expect(Set(ModelCatalog.all.map(\.id)).count == ModelCatalog.all.count)
|
|
#expect(ModelCatalog.all.allSatisfy { $0.approxSizeMB > 0 })
|
|
}
|
|
}
|