|
| 1 | +package llm |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestDiscoverModelContext_OpenRouterFormat(t *testing.T) { |
| 11 | + ResetModelCache() |
| 12 | + |
| 13 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 14 | + if r.URL.Path != "/models" { |
| 15 | + t.Errorf("expected /models, got %s", r.URL.Path) |
| 16 | + } |
| 17 | + resp := modelsResponse{ |
| 18 | + Data: []rawModel{ |
| 19 | + {ID: "deepseek-v4-flash", ContextLength: 131072}, |
| 20 | + {ID: "deepseek-v4-pro", ContextLength: 1048576}, |
| 21 | + }, |
| 22 | + } |
| 23 | + json.NewEncoder(w).Encode(resp) |
| 24 | + })) |
| 25 | + defer srv.Close() |
| 26 | + |
| 27 | + ctx := DiscoverModelContext(srv.URL, "test-key", "deepseek-v4-flash") |
| 28 | + if ctx != 131072 { |
| 29 | + t.Errorf("expected 131072, got %d", ctx) |
| 30 | + } |
| 31 | + |
| 32 | + // Second model |
| 33 | + ctx = DiscoverModelContext(srv.URL, "test-key", "deepseek-v4-pro") |
| 34 | + if ctx != 1048576 { |
| 35 | + t.Errorf("expected 1048576, got %d", ctx) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestDiscoverModelContext_UnknownModel(t *testing.T) { |
| 40 | + ResetModelCache() |
| 41 | + |
| 42 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 43 | + resp := modelsResponse{ |
| 44 | + Data: []rawModel{ |
| 45 | + {ID: "gpt-4o", ContextLength: 128000}, |
| 46 | + }, |
| 47 | + } |
| 48 | + json.NewEncoder(w).Encode(resp) |
| 49 | + })) |
| 50 | + defer srv.Close() |
| 51 | + |
| 52 | + ctx := DiscoverModelContext(srv.URL, "test-key", "unknown-model") |
| 53 | + if ctx != 0 { |
| 54 | + t.Errorf("expected 0 for unknown model, got %d", ctx) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestDiscoverModelContext_ServerError(t *testing.T) { |
| 59 | + ResetModelCache() |
| 60 | + |
| 61 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 62 | + w.WriteHeader(http.StatusInternalServerError) |
| 63 | + })) |
| 64 | + defer srv.Close() |
| 65 | + |
| 66 | + ctx := DiscoverModelContext(srv.URL, "test-key", "any-model") |
| 67 | + if ctx != 0 { |
| 68 | + t.Errorf("expected 0 on server error, got %d", ctx) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestDiscoverModelContext_Timeout(t *testing.T) { |
| 73 | + ResetModelCache() |
| 74 | + |
| 75 | + // A port that's very unlikely to be listening — tests connection refused handling |
| 76 | + ctx := DiscoverModelContext("http://127.0.0.1:1", "test-key", "any-model") |
| 77 | + if ctx != 0 { |
| 78 | + t.Errorf("expected 0 on connection error, got %d", ctx) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestDiscoverModelContext_Cached(t *testing.T) { |
| 83 | + ResetModelCache() |
| 84 | + |
| 85 | + callCount := 0 |
| 86 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 87 | + callCount++ |
| 88 | + resp := modelsResponse{ |
| 89 | + Data: []rawModel{ |
| 90 | + {ID: "flash", ContextLength: 131072}, |
| 91 | + }, |
| 92 | + } |
| 93 | + json.NewEncoder(w).Encode(resp) |
| 94 | + })) |
| 95 | + defer srv.Close() |
| 96 | + |
| 97 | + // First call hits the server |
| 98 | + ctx1 := DiscoverModelContext(srv.URL, "test-key", "flash") |
| 99 | + if ctx1 != 131072 { |
| 100 | + t.Errorf("expected 131072, got %d", ctx1) |
| 101 | + } |
| 102 | + |
| 103 | + // Second call should be cached |
| 104 | + ctx2 := DiscoverModelContext(srv.URL, "test-key", "flash") |
| 105 | + if ctx2 != 131072 { |
| 106 | + t.Errorf("expected 131072, got %d", ctx2) |
| 107 | + } |
| 108 | + |
| 109 | + if callCount != 1 { |
| 110 | + t.Errorf("expected 1 server call (cached), got %d", callCount) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func TestDiscoverModelContext_MaxContextFallback(t *testing.T) { |
| 115 | + ResetModelCache() |
| 116 | + |
| 117 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 118 | + resp := modelsResponse{ |
| 119 | + Data: []rawModel{ |
| 120 | + {ID: "my-model", MaxContext: 64000}, |
| 121 | + }, |
| 122 | + } |
| 123 | + json.NewEncoder(w).Encode(resp) |
| 124 | + })) |
| 125 | + defer srv.Close() |
| 126 | + |
| 127 | + ctx := DiscoverModelContext(srv.URL, "test-key", "my-model") |
| 128 | + if ctx != 64000 { |
| 129 | + t.Errorf("expected 64000 from max_context fallback, got %d", ctx) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestDiscoverModelContext_ModelsArrayFallback(t *testing.T) { |
| 134 | + ResetModelCache() |
| 135 | + |
| 136 | + // Some providers use "models" instead of "data" |
| 137 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 138 | + json.NewEncoder(w).Encode(map[string][]rawModel{ |
| 139 | + "models": { |
| 140 | + {ID: "claude-sonnet-4", ContextLength: 200000}, |
| 141 | + }, |
| 142 | + }) |
| 143 | + })) |
| 144 | + defer srv.Close() |
| 145 | + |
| 146 | + ctx := DiscoverModelContext(srv.URL, "test-key", "claude-sonnet-4") |
| 147 | + if ctx != 200000 { |
| 148 | + t.Errorf("expected 200000, got %d", ctx) |
| 149 | + } |
| 150 | +} |
0 commit comments