yao/openapi/setting/cloud_model_cache_test.go
Max 756ff95d3f feat(llm): enhance capabilities management with embedding and image generation support
- Added support for embedding and image generation capabilities in the capabilitiesFromMap function.
- Updated the ToMap method to delegate to the canonical Capabilities.ToMap() method, simplifying the conversion process.
- Enhanced the capabilities handling in various components, including filters for non-chat models in OpenAPI settings.
- Introduced utility functions for filter checking and non-chat model identification, improving overall capabilities management.
2026-05-02 14:45:31 +08:00

92 lines
2.4 KiB
Go

package setting
import (
"encoding/json"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
)
func TestFetchCloudModels_CachesAfterFirstCall(t *testing.T) {
var hits int64
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt64(&hits, 1)
json.NewEncoder(w).Encode(map[string]interface{}{
"data": []map[string]interface{}{
{"id": "gpt-4o", "object": "model"},
},
})
}))
defer srv.Close()
invalidateCloudModelCache()
models := fetchCloudModels(srv.URL, "test-key")
if len(models) == 0 {
t.Fatal("expected models from first fetch, got none")
}
if atomic.LoadInt64(&hits) != 1 {
t.Fatalf("expected 1 HTTP hit after first fetch, got %d", atomic.LoadInt64(&hits))
}
models2 := fetchCloudModels(srv.URL, "test-key")
if len(models2) == 0 {
t.Fatal("expected models from cached fetch, got none")
}
if atomic.LoadInt64(&hits) != 1 {
t.Fatalf("expected still 1 HTTP hit after second fetch (cache), got %d", atomic.LoadInt64(&hits))
}
}
func TestFetchCloudModels_InvalidateForcesRefetch(t *testing.T) {
var hits int64
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt64(&hits, 1)
json.NewEncoder(w).Encode(map[string]interface{}{
"data": []map[string]interface{}{
{"id": "gpt-4o", "object": "model"},
},
})
}))
defer srv.Close()
invalidateCloudModelCache()
fetchCloudModels(srv.URL, "test-key")
if atomic.LoadInt64(&hits) != 1 {
t.Fatalf("expected 1 HTTP hit, got %d", atomic.LoadInt64(&hits))
}
invalidateCloudModelCache()
fetchCloudModels(srv.URL, "test-key")
if atomic.LoadInt64(&hits) != 2 {
t.Fatalf("expected 2 HTTP hits after invalidation, got %d", atomic.LoadInt64(&hits))
}
}
func TestFetchCloudModels_URLChangeForcesRefetch(t *testing.T) {
var hits int64
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt64(&hits, 1)
json.NewEncoder(w).Encode(map[string]interface{}{
"data": []map[string]interface{}{
{"id": "gpt-4o", "object": "model"},
},
})
}))
defer srv.Close()
invalidateCloudModelCache()
fetchCloudModels(srv.URL, "test-key")
if atomic.LoadInt64(&hits) != 1 {
t.Fatalf("expected 1 HTTP hit, got %d", atomic.LoadInt64(&hits))
}
fetchCloudModels(srv.URL+"/other", "test-key")
if atomic.LoadInt64(&hits) != 2 {
t.Fatalf("expected 2 HTTP hits after URL change, got %d", atomic.LoadInt64(&hits))
}
}