v0.3.97 c2267b3

modelsync_test.go RawHistoryFolder

1package agent
2
3import (
4	"context"
5	"encoding/json"
6	"fmt"
7	"net/http"
8	"net/http/httptest"
9	"os"
10	"path/filepath"
11	"testing"
12	"time"
13
14	"github.com/patriceckhart/zot/packages/provider"
15)
16
17func TestRefreshLlamaCPPModelsAddsOnlyLoadedModels(t *testing.T) {
18	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
19		if r.URL.Path != "/models" {
20			http.NotFound(w, r)
21			return
22		}
23		fmt.Fprint(w, `{"data":[{"id":"loaded","status":{"value":"loaded"},"meta":{"n_ctx":32768}},{"id":"offline","status":{"value":"unloaded"}}]}`)
24	}))
25	defer server.Close()
26
27	t.Setenv("ZOT_HOME", t.TempDir())
28	t.Setenv("LLAMA_BASE_URL", "")
29	if err := AuthStoreFor().SetEndpointCredential(provider.LlamaCPPProviderID, server.URL, ""); err != nil {
30		t.Fatal(err)
31	}
32	provider.SetManagedModels(nil)
33	t.Cleanup(func() { provider.SetManagedModels(nil) })
34
35	if err := RefreshLlamaCPPModels(context.Background()); err != nil {
36		t.Fatal(err)
37	}
38	loaded, err := provider.FindModel(provider.LlamaCPPProviderID, "loaded")
39	if err != nil {
40		t.Fatal(err)
41	}
42	if loaded.ContextWindow != 32768 || loaded.BaseURL != server.URL+"/v1" {
43		t.Fatalf("loaded model = %+v", loaded)
44	}
45	if _, err := provider.FindModel(provider.LlamaCPPProviderID, "offline"); err == nil {
46		t.Fatal("unloaded model must not be selectable")
47	}
48}
49
50// TestValidateAndRepairConfig_MismatchedPair simulates the bug from a
51// stale /model switch: provider=anthropic but model=kimi-for-coding
52// (which belongs to provider=kimi). The validator should rewrite the
53// model to anthropic's default and persist.
54func TestValidateAndRepairConfig_MismatchedPair(t *testing.T) {
55	home := t.TempDir()
56	t.Setenv("ZOT_HOME", home)
57
58	must := func(c Config) {
59		t.Helper()
60		b, _ := json.Marshal(c)
61		if err := os.WriteFile(filepath.Join(home, "config.json"), b, 0o644); err != nil {
62			t.Fatal(err)
63		}
64	}
65	must(Config{Provider: "anthropic", Model: "kimi-for-coding"})
66
67	ValidateAndRepairConfig()
68
69	out, err := LoadConfig()
70	if err != nil {
71		t.Fatal(err)
72	}
73	if out.Provider != "anthropic" {
74		t.Errorf("provider not preserved: %q", out.Provider)
75	}
76	if out.Model == "kimi-for-coding" {
77		t.Errorf("model not repaired; still %q", out.Model)
78	}
79	if out.Model == "" {
80		t.Errorf("model not set; expected anthropic default")
81	}
82}
83
84// TestValidateAndRepairConfig_UnknownProvider resets to anthropic and
85// clears the model when the saved provider id isn't recognised
86// (e.g. user removed it from a previous build).
87func TestValidateAndRepairConfig_UnknownProvider(t *testing.T) {
88	home := t.TempDir()
89	t.Setenv("ZOT_HOME", home)
90
91	b, _ := json.Marshal(Config{Provider: "made-up-provider", Model: "some-model"})
92	_ = os.WriteFile(filepath.Join(home, "config.json"), b, 0o644)
93
94	ValidateAndRepairConfig()
95
96	out, _ := LoadConfig()
97	if out.Provider != "anthropic" {
98		t.Errorf("provider not reset: %q", out.Provider)
99	}
100	if out.Model != "" {
101		t.Errorf("model not cleared: %q", out.Model)
102	}
103}
104
105// TestValidateAndRepairConfig_UnknownModel keeps the provider but
106// snaps the model to that provider's default when the saved id is no
107// longer in the catalog.
108func TestValidateAndRepairConfig_UnknownModel(t *testing.T) {
109	home := t.TempDir()
110	t.Setenv("ZOT_HOME", home)
111
112	b, _ := json.Marshal(Config{Provider: "anthropic", Model: "claude-deleted-model"})
113	_ = os.WriteFile(filepath.Join(home, "config.json"), b, 0o644)
114
115	ValidateAndRepairConfig()
116
117	out, _ := LoadConfig()
118	if out.Provider != "anthropic" {
119		t.Errorf("provider changed: %q", out.Provider)
120	}
121	if out.Model == "" || out.Model == "claude-deleted-model" {
122		t.Errorf("model not repaired: %q", out.Model)
123	}
124}
125
126func TestValidateAndRepairConfig_DuplicateModelIDValidForConfiguredProvider(t *testing.T) {
127	home := t.TempDir()
128	t.Setenv("ZOT_HOME", home)
129
130	b, _ := json.Marshal(Config{Provider: "openai-codex", Model: "gpt-5.5"})
131	_ = os.WriteFile(filepath.Join(home, "config.json"), b, 0o644)
132
133	ValidateAndRepairConfig()
134
135	out, _ := LoadConfig()
136	if out.Provider != "openai-codex" {
137		t.Errorf("provider mutated: %q", out.Provider)
138	}
139	if out.Model != "gpt-5.5" {
140		t.Errorf("model mutated: %q", out.Model)
141	}
142}
143
144func TestModelCacheFetchedAtPreservesCatalogFreshness(t *testing.T) {
145	cachedAt := time.Date(2026, time.August, 30, 12, 0, 0, 0, time.UTC)
146	now := cachedAt.Add(time.Hour)
147	cached := provider.ModelCache{FetchedAt: cachedAt}
148
149	if got := modelCacheFetchedAt(cached, false, now); !got.Equal(cachedAt) {
150		t.Fatalf("fresh cache timestamp = %v; want %v", got, cachedAt)
151	}
152	if got := modelCacheFetchedAt(cached, true, now); !got.Equal(now) {
153		t.Fatalf("refreshed cache timestamp = %v; want %v", got, now)
154	}
155}
156
157func TestMergeOpenRouterPresetsReplacesCachedPresets(t *testing.T) {
158	existing := []provider.Model{
159		{Provider: "openrouter", ID: "deepseek/deepseek-v4-flash"},
160		{Provider: "openrouter", ID: "@preset/old"},
161		{Provider: "anthropic", ID: "claude-sonnet-4-5"},
162	}
163	presets := []provider.Model{
164		{Provider: "openrouter", ID: "@preset/flash", DisplayName: "Flash (preset)", ContextWindow: 1000000},
165	}
166	out := mergeOpenRouterPresets(existing, presets)
167	if len(out) != 3 {
168		t.Fatalf("got %d models; want 3", len(out))
169	}
170	var ids []string
171	for _, m := range out {
172		ids = append(ids, m.Provider+"/"+m.ID)
173	}
174	want := []string{"openrouter/deepseek/deepseek-v4-flash", "anthropic/claude-sonnet-4-5", "openrouter/@preset/flash"}
175	for i, id := range want {
176		if ids[i] != id {
177			t.Errorf("ids[%d] = %q; want %q", i, ids[i], id)
178		}
179	}
180}
181
182func TestValidateAndRepairConfig_OpenRouterPreservesRoutedModelID(t *testing.T) {
183	home := t.TempDir()
184	t.Setenv("ZOT_HOME", home)
185
186	want := "deepseek/deepseek-v4-flash"
187	b, _ := json.Marshal(Config{Provider: "openrouter", Model: want})
188	_ = os.WriteFile(filepath.Join(home, "config.json"), b, 0o644)
189
190	ValidateAndRepairConfig()
191
192	out, _ := LoadConfig()
193	if out.Provider != "openrouter" {
194		t.Errorf("provider mutated: %q", out.Provider)
195	}
196	if out.Model != want {
197		t.Errorf("routed model id mutated: got %q, want %q", out.Model, want)
198	}
199}
200
201func TestValidateAndRepairConfig_GatewayPlainUnknownModelStillRepairs(t *testing.T) {
202	home := t.TempDir()
203	t.Setenv("ZOT_HOME", home)
204
205	b, _ := json.Marshal(Config{Provider: "openrouter", Model: "not-a-routed-model"})
206	_ = os.WriteFile(filepath.Join(home, "config.json"), b, 0o644)
207
208	ValidateAndRepairConfig()
209
210	out, _ := LoadConfig()
211	if out.Provider != "openrouter" {
212		t.Errorf("provider mutated: %q", out.Provider)
213	}
214	if out.Model == "not-a-routed-model" || out.Model == "" {
215		t.Errorf("plain unknown gateway model was not repaired: %q", out.Model)
216	}
217}
218
219// TestValidateAndRepairConfig_HappyPath leaves a valid config alone.
220func TestValidateAndRepairConfig_HappyPath(t *testing.T) {
221	home := t.TempDir()
222	t.Setenv("ZOT_HOME", home)
223
224	b, _ := json.Marshal(Config{Provider: "anthropic", Model: "claude-sonnet-4-5"})
225	_ = os.WriteFile(filepath.Join(home, "config.json"), b, 0o644)
226
227	ValidateAndRepairConfig()
228
229	out, _ := LoadConfig()
230	if out.Provider != "anthropic" {
231		t.Errorf("provider mutated: %q", out.Provider)
232	}
233	if out.Model != "claude-sonnet-4-5" {
234		t.Errorf("model mutated: %q", out.Model)
235	}
236}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close