Add upstream model list fetching and a Codex-specific model section

This commit is contained in:
kate522 committed 2026-10-18 19:33:32 +08:00
1 parent d759fe5e5d
commit f3bf1472e7
12 files changed
+339 -16

No files matched your search

+60
View File
@@ -23,6 +23,11 @@ type fakeUpstream struct {
}
func (f *fakeUpstream) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && r.URL.Path == "/models" {
w.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(w, `{"object":"list","data":[{"id":"ds-chat"},{"id":"ds-haiku"}]}`)
return
}
if r.URL.Path != "/chat/completions" {
w.WriteHeader(http.StatusNotFound)
return
@@ -683,6 +688,61 @@ func TestApplyRejectsWrongTarget(t *testing.T) {
}
}
func TestFetchModels(t *testing.T) {
env := newTestEnv(t)
resp := env.post(t, "/api/fetch-models", map[string]any{
"base_url": env.upstreamURL,
"api_key": "up-key",
}, "")
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("status = %d", resp.StatusCode)
}
var out struct {
Items []struct {
ID string `json:"id"`
} `json:"items"`
}
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
t.Fatalf("decode: %v", err)
}
if len(out.Items) != 2 || out.Items[0].ID != "ds-chat" {
t.Errorf("items = %+v", out.Items)
}
}
func TestFetchModelsRejectsBadInput(t *testing.T) {
env := newTestEnv(t)
for _, body := range []map[string]any{
{"base_url": env.upstreamURL},
{"api_key": "up-key"},
{"base_url": "ftp://x", "api_key": "k"},
} {
resp := env.post(t, "/api/fetch-models", body, "")
resp.Body.Close()
if resp.StatusCode != http.StatusBadRequest {
t.Errorf("body %v status = %d, want 400", body, resp.StatusCode)
}
}
}
func TestFetchModelsUpstreamFailure(t *testing.T) {
env := newTestEnv(t)
broken := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
_, _ = io.WriteString(w, `{"error":{"message":"bad key"}}`)
}))
defer broken.Close()
resp := env.post(t, "/api/fetch-models", map[string]any{
"base_url": broken.URL,
"api_key": "sk-bad",
}, "")
defer resp.Body.Close()
if resp.StatusCode != http.StatusBadGateway {
t.Errorf("status = %d, want 502", resp.StatusCode)
}
}
func TestListProvidersFiltersByTarget(t *testing.T) {
env := newCodexTestEnv(t)
codexItems := env.get(t, "/api/providers?target=codex")["items"].([]any)