package weights import ( "path/filepath" "slices" "testing" ) func TestOnlyFilesLLMGWFetchedAreEverOrphans(t *testing.T) { // The invariant the whole feature rests on. Everything without the cache // name was put there by the operator — scp, a shared mount, a copy by hand // — and deleting one could cost a week of someone's bandwidth. It does not // matter how unreferenced it looks. const dir = "/var/lib/llmgw-models" have := []string{ filepath.Join(dir, "qwen2.5-coder-14b-instruct-q8_0-00001-of-00002.gguf"), filepath.Join(dir, "some-model.safetensors"), filepath.Join(dir, "notes.txt"), CachePathFor(dir, "https://models.internal/gone.gguf"), } got := Orphans(have, nil) if len(got) != 1 { t.Fatalf("expected only the fetched file, got %v", got) } if !Owned(got[0]) { t.Errorf("returned a file llmgw did not fetch: %s", got[0]) } } func TestAFetchedFileStillInTheCatalogIsKept(t *testing.T) { const dir = "/var/lib/llmgw-models" const wanted = "hf:Qwen/Qwen2.5-Coder-3B-Instruct-GGUF/q4.gguf" const gone = "https://models.internal/old.gguf" have := []string{CachePathFor(dir, wanted), CachePathFor(dir, gone)} got := Orphans(have, []string{wanted}) if slices.Contains(got, CachePathFor(dir, wanted)) { t.Error("a file the catalog still points at was called an orphan") } if !slices.Contains(got, CachePathFor(dir, gone)) { t.Errorf("a file nothing points at was not found: %v", got) } } func TestTheCacheNameMatchesWhatAFetchActuallyWrites(t *testing.T) { // The control plane proposes deletions by recomputing this name from the // catalog. If it ever drifted from what a fetch writes, the proposal would // name a file that is in use — so the two must be one function, and this // is what says so. f := &Fetcher{CacheDir: "/cache"} const source = "https://models.internal/x.gguf" got, err := f.cachePath(source) if err != nil { t.Fatal(err) } if want := CachePathFor("/cache", source); got != want { t.Errorf("a fetch writes %q but the catalog would look for %q", got, want) } } func TestOneOfTwoLookalikeFilesIsNotMistakenForTheOther(t *testing.T) { // That the two cache to different names is covered where the naming lives. // What matters here is the consequence: deleting the unreferenced one must // not take the referenced one with it, and both end in "model.gguf". a := CachePathFor("/cache", "https://one.example/model.gguf") b := CachePathFor("/cache", "https://two.example/model.gguf") got := Orphans([]string{a, b}, []string{"https://one.example/model.gguf"}) if !slices.Equal(got, []string{b}) { t.Errorf("orphans = %v, want only %s", got, b) } } func TestASourceThatIsNeverFetchedProtectsNothingAndDeletesNothing(t *testing.T) { // A plain path, or a model id an engine resolves itself, puts nothing in // the cache. Computing a cache name for it would reserve a file that does // not exist, and it must not stop a real orphan being found either. const dir = "/models" orphan := CachePathFor(dir, "https://models.internal/gone.gguf") got := Orphans([]string{orphan, filepath.Join(dir, "local.gguf")}, []string{"/models/local.gguf", "Qwen/Qwen2.5-7B-Instruct"}) if !slices.Equal(got, []string{orphan}) { t.Errorf("orphans = %v, want only %s", got, orphan) } } func TestAnEmptyCatalogDoesNotMakeOperatorFilesDisposable(t *testing.T) { // The dangerous case: every model removed from the catalog at once. Files // llmgw fetched become orphans, which is right. Files it did not must stay // untouched, which is the whole point. const dir = "/models" have := []string{ filepath.Join(dir, "hand-placed.gguf"), CachePathFor(dir, "https://models.internal/a.gguf"), } got := Orphans(have, nil) if len(got) != 1 || got[0] != CachePathFor(dir, "https://models.internal/a.gguf") { t.Errorf("orphans = %v", got) } } func TestPartialsAreNotSweptUpWithTheRest(t *testing.T) { // A fetch may be running right now, and only the node itself can tell a // live download from one a killed agent left behind. if !PartialName("/models/abc-model.gguf.part") { t.Error("a .part file was not recognised") } if PartialName("/models/abc-model.gguf") { t.Error("a finished file was called partial") } }