added /models cmd

This commit is contained in:
Luxferre
2026-08-28 09:30:48 +03:00
parent 7b26d8f485
commit 699bb429fb
3 changed files with 132 additions and 2 deletions
+59
View File
@@ -1845,6 +1845,65 @@ func TestFormatUsage(t *testing.T) {
}
}
func TestListModels(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/models" {
http.NotFound(w, r)
return
}
if r.Header.Get("Authorization") != "Bearer secret" {
w.WriteHeader(401)
return
}
w.Write([]byte(`{
"data": [
{"id": "alpha"},
{"id": "my-target-model"},
{"id": "beta"}
]
}`))
}))
defer srv.Close()
cfg := defCfg
cfg.Endpoint = srv.URL
cfg.Model = "my-target-model"
cfg.APIKey = "secret"
out, err := listModels(&cfg)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
lines := strings.Split(strings.TrimRight(out, "\n"), "\n")
var got []string
for _, l := range lines {
if l == "" { continue }
got = append(got, l)
}
want := []string{" alpha", "* my-target-model", " beta"}
if len(got) != len(want) {
t.Fatalf("expected %d lines, got %d: %q", len(want), len(got), out)
}
for i := range want {
if got[i] != want[i] {
t.Errorf("line %d: expected %q, got %q", i, want[i], got[i])
}
}
}
func TestListModelsHTTPError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
}))
defer srv.Close()
cfg := defCfg
cfg.Endpoint = srv.URL
cfg.APIKey = "-"
if _, err := listModels(&cfg); err == nil {
t.Fatalf("expected error on HTTP 500, got nil")
}
}
func TestFetchContextWindowFromModelsAPI(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/models" {