Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Make notable-model cache tests independent of the public network

pulse-triage[bot]670974c6e4f0
diff --git a/internal/ai/providers/notable_models_test.go b/internal/ai/providers/notable_models_test.go
index 25a80919e0..6bb28c9b60 100644
--- a/internal/ai/providers/notable_models_test.go
+++ b/internal/ai/providers/notable_models_test.go
@@ -2,74 +2,109 @@ package providers
import (
"context"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "sync/atomic"
"testing"
"time"
)
+// Exercise the real HTTP decoder without depending on external availability,
+// today's catalogue, or the singleton's state from a preceding test.
+func notableModelsTestCache(t *testing.T) (*NotableModelsCache, map[string]ModelsDevInfo, *atomic.Int32) {
+ t.Helper()
+ recent := time.Now().UTC().AddDate(0, -1, 0).Format("2006-01-02")
+ old := time.Now().UTC().AddDate(-1, 0, 0).Format("2006-01-02")
+ providers := map[string]ModelsDevProvider{
+ "anthropic": {ID: "anthropic", Models: map[string]ModelsDevInfo{
+ "claude-opus-4-5": {ID: "claude-opus-4-5", ReleaseDate: recent, LastUpdated: recent},
+ "claude-sonnet-4-5": {ID: "claude-sonnet-4-5", ReleaseDate: recent, LastUpdated: recent},
+ "claude-3-5-sonnet-20241022": {ID: "claude-3-5-sonnet-20241022", ReleaseDate: old, LastUpdated: old},
+ }},
+ "openai": {ID: "openai", Models: map[string]ModelsDevInfo{
+ "gpt-4o": {ID: "gpt-4o", ReleaseDate: recent, LastUpdated: recent},
+ }},
+ }
+ want := make(map[string]ModelsDevInfo)
+ for provider, data := range providers {
+ for model, info := range data.Models {
+ want[normalizeKey(provider, model)] = info
+ want[normalizeModelID(model)] = info
+ }
+ }
+ var calls atomic.Int32
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ calls.Add(1)
+ if r.Method != http.MethodGet || r.URL.RequestURI() != "/api.json" {
+ t.Errorf("request = %s %s, want GET /api.json", r.Method, r.URL.RequestURI())
+ }
+ if got := r.Header.Get("User-Agent"); got != "Pulse/1.0" {
+ t.Errorf("User-Agent = %q, want Pulse/1.0", got)
+ }
+ w.Header().Set("Content-Type", "application/json")
+ if err := json.NewEncoder(w).Encode(providers); err != nil {
+ t.Errorf("encode fixture: %v", err)
+ }
+ }))
+ t.Cleanup(server.Close)
+ return NewNotableModelsCache(server.URL+"/api.json", time.Hour), want, &calls
+}
+
func TestNotableModelsCache_Refresh(t *testing.T) {
- // Test that we can successfully fetch and parse models.dev API
- cache := NewNotableModelsCache(modelsDevAPIURL, 1*time.Hour)
+ cache, want, calls := notableModelsTestCache(t)
- ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
- err := cache.Refresh(ctx)
- if err != nil {
+ if err := cache.Refresh(ctx); err != nil {
t.Fatalf("Failed to refresh cache: %v", err)
}
-
- if len(cache.data) == 0 {
- t.Fatal("Cache is empty after refresh")
- }
-
- t.Logf("Cache contains %d models", len(cache.data))
-
- // Check for some known models
- testCases := []struct {
- provider string
- modelID string
- }{
- {"anthropic", "claude-opus-4-5"},
- {"anthropic", "claude-sonnet-4-5"},
- {"openai", "gpt-4o"},
+ if len(cache.data) != len(want) {
+ t.Fatalf("cache contains %d entries, want %d", len(cache.data), len(want))
}
-
- for _, tc := range testCases {
- key := normalizeKey(tc.provider, tc.modelID)
- if info, found := cache.data[key]; found {
- t.Logf("Found %s: release_date=%s, last_updated=%s", key, info.ReleaseDate, info.LastUpdated)
- } else {
- t.Logf("Model %s not found in cache (key=%s)", tc.modelID, key)
- // Try fuzzy match
- fuzzyKey := normalizeModelID(tc.modelID)
- if info, found := cache.data[fuzzyKey]; found {
- t.Logf("Found via fuzzy match %s: release_date=%s", fuzzyKey, info.ReleaseDate)
- }
+ for key, info := range want {
+ if got, found := cache.data[key]; !found || got != info {
+ t.Errorf("cache[%q] = %+v (found=%t), want %+v", key, got, found, info)
}
}
+ if cache.lastFetched.IsZero() {
+ t.Fatal("successful refresh did not record freshness")
+ }
+ if err := cache.Refresh(ctx); err != nil {
+ t.Fatalf("fresh cached refresh: %v", err)
+ }
+ if got := calls.Load(); got != 1 {
+ t.Errorf("fresh cache sent %d HTTP requests, want one", got)
+ }
}
func TestIsNotable_Integration(t *testing.T) {
- cache := GetNotableCache()
+ cache, _, calls := notableModelsTestCache(t)
// Test Ollama - should always be notable
if !cache.IsNotable("ollama", "llama3", 0) {
t.Error("Ollama models should always be notable")
}
-
- // Test Anthropic models
- // claude-opus-4-5 was released 2025-11-21, should be notable
- notable := cache.IsNotable("anthropic", "claude-opus-4-5", 0)
- t.Logf("claude-opus-4-5 notable: %v", notable)
-
- // claude-3-5-sonnet-20241022 was released 2024-10-22, should NOT be notable (>6 months old)
- notable = cache.IsNotable("anthropic", "claude-3-5-sonnet-20241022", 0)
- t.Logf("claude-3-5-sonnet-20241022 notable: %v", notable)
-
- // Print cache size
- cache.mu.RLock()
- t.Logf("Cache size: %d", len(cache.data))
- cache.mu.RUnlock()
+ if got := calls.Load(); got != 0 {
+ t.Errorf("Ollama fetched metadata %d times, want none", got)
+ }
+ for _, tc := range []struct {
+ provider string
+ modelID string
+ want bool
+ }{
+ {"anthropic", "claude-opus-4-5", true},
+ {"anthropic", "claude-3-5-sonnet-20241022", false},
+ {"openai", "gpt-4o", true},
+ } {
+ if got := cache.IsNotable(tc.provider, tc.modelID, 0); got != tc.want {
+ t.Errorf("IsNotable(%q, %q) = %t, want %t", tc.provider, tc.modelID, got, tc.want)
+ }
+ }
+ if got := calls.Load(); got != 1 {
+ t.Errorf("cached notable checks sent %d HTTP requests, want one", got)
+ }
}
func TestGetModelFamilyAliases(t *testing.T) {