package modelsvc import ( "context" "errors" "slices" "strings" "testing" "gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog" "gitea.mynodes.xyz/ilker/llmgw/internal/hf" ) const commit = "0123456789abcdef0123456789abcdef01234567" type fakeRegistry struct{ asked []string } func (f *fakeRegistry) Base() string { return "https://huggingface.co" } func (f *fakeRegistry) RepoAt(_ context.Context, id, rev string) (hf.Repo, error) { f.asked = append(f.asked, id+"@"+rev) if id == "gone/repo" { return hf.Repo{}, errors.New("404") } return hf.Repo{ID: id, Revision: commit, Files: []hf.File{ {Path: "q4.gguf", Bytes: 10, SHA256: "aaaa"}, {Path: "model-00001-of-00002.safetensors", Bytes: 20, SHA256: "bbbb"}, {Path: "config.json", Bytes: 1}, }}, nil } func TestAFileFromTheRegistryIsPinnedToItsCommit(t *testing.T) { for _, src := range []string{"hf:o/r/q4.gguf", "https://huggingface.co/o/r/resolve/main/q4.gguf"} { m := catalog.Model{Name: "q", Source: src, Profiles: []catalog.Profile{{Name: "cpu", Engine: "llamacpp"}}} if err := pin(t.Context(), &fakeRegistry{}, &m); err != nil { t.Fatal(err) } if m.Source != "https://huggingface.co/o/r/resolve/"+commit+"/q4.gguf" { t.Errorf("%s became %s", src, m.Source) } if m.Origin == nil || m.Origin.Revision != commit || m.OriginSHA256(m.Source) != "aaaa" { t.Errorf("%s: origin %+v", src, m.Origin) } } } func TestARepositoryAnEngineDownloadsIsGivenItsRevision(t *testing.T) { m := catalog.Model{Name: "q", Source: "o/r", Profiles: []catalog.Profile{ {Name: "gpu", Engine: "vllm", Args: []string{"--max-model-len", "4096"}}, {Name: "cpu", Engine: "llamacpp", Source: "hf:o/r/q4.gguf"}}} if err := pin(t.Context(), &fakeRegistry{}, &m); err != nil { t.Fatal(err) } if a := m.Profiles[0].Args; !slices.Equal(a, []string{"--max-model-len", "4096", "--revision", commit}) { t.Errorf("vllm args %v", a) } if !strings.Contains(m.Profiles[1].Source, commit) { t.Errorf("the llama.cpp profile's file: %s", m.Profiles[1].Source) } // A revision given is the one looked up, and kept. reg := &fakeRegistry{} m = catalog.Model{Name: "q", Source: "o/r", Profiles: []catalog.Profile{{Name: "gpu", Engine: "vllm", Args: []string{"--revision", "v2"}}}} if err := pin(t.Context(), reg, &m); err != nil { t.Fatal(err) } if !slices.Contains(reg.asked, "o/r@v2") || argValue(m.Profiles[0].Args, "--revision") != "v2" { t.Errorf("asked %v, args %v", reg.asked, m.Profiles[0].Args) } } func TestWhatIsNotTheRegistrysIsLeftAndWhatCannotBePinnedIsRefused(t *testing.T) { m := catalog.Model{Name: "q", Source: "http://10.0.0.146:8081/q/abc/q4.gguf", Profiles: []catalog.Profile{{Name: "cpu", Engine: "llamacpp"}}} reg := &fakeRegistry{} if err := pin(t.Context(), reg, &m); err != nil || len(reg.asked) != 0 || m.Origin != nil { t.Errorf("a mirror's file: %v %v %+v", err, reg.asked, m.Origin) } // Served through the mirror, with its origin: left alone. m = catalog.Model{Name: "q", Source: "hf:o/r/q4.gguf", Origin: &catalog.Origin{Repo: "o/r"}} if err := pin(t.Context(), reg, &m); err != nil || m.Source != "hf:o/r/q4.gguf" { t.Errorf("an origin given was not kept: %v %s", err, m.Source) } for _, src := range []string{"hf:gone/repo/q4.gguf", "hf:o/r/missing.gguf"} { m = catalog.Model{Name: "q", Source: src} if err := pin(t.Context(), reg, &m); err == nil { t.Errorf("%s was not refused", src) } } }