package llm import ( "context" "net/http" "net/http/httptest" "testing" ) // The point of these tests: a wrong-but-plausible model label is the bug, so // every path that cannot learn the real name must return an error instead of a // guess. No llama-server needed — a stub server stands in. func TestModelID(t *testing.T) { cases := []struct { name string body string code int want string // "" ⇒ expect an error }{ {"full path", `{"data":[{"id":"/mnt/hdd1/llms/qwen3.5/Qwen3.5-0.8B.Q4_K_M.gguf"}]}`, 200, "Qwen3.5-0.8B.Q4_K_M"}, {"bare name", `{"data":[{"id":"LFM2.5-1.2B"}]}`, 200, "LFM2.5-1.2B"}, {"empty list", `{"data":[]}`, 200, ""}, {"id missing", `{"data":[{}]}`, 200, ""}, {"id blank", `{"data":[{"id":" "}]}`, 200, ""}, {"server error", `nope`, 500, ""}, {"not json", ``, 200, ""}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/v1/models" { t.Errorf("asked for %s, want /v1/models", r.URL.Path) } w.WriteHeader(c.code) _, _ = w.Write([]byte(c.body)) })) defer srv.Close() got, err := ModelID(context.Background(), srv.URL+"/") if c.want == "" { if err == nil { t.Fatalf("want an error, got label %q", got) } return } if err != nil { t.Fatalf("ModelID: %v", err) } if got != c.want { t.Errorf("got %q, want %q", got, c.want) } }) } } func TestModelIDUnreachable(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) url := srv.URL srv.Close() // nothing listening now if got, err := ModelID(context.Background(), url); err == nil { t.Fatalf("want an error from a dead server, got label %q", got) } }