1package agent
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 "github.com/patriceckhart/zot/packages/provider"
10)
11
12func TestReadAgentsContextLoadsGlobalAndAncestors(t *testing.T) {
13 root := t.TempDir()
14 zotHome := filepath.Join(root, "zot-home")
15 project := filepath.Join(root, "repo")
16 nested := filepath.Join(project, "packages", "app")
17 if err := os.MkdirAll(zotHome, 0o755); err != nil {
18 t.Fatal(err)
19 }
20 if err := os.MkdirAll(nested, 0o755); err != nil {
21 t.Fatal(err)
22 }
23 if err := os.WriteFile(filepath.Join(zotHome, "AGENTS.md"), []byte("global rule"), 0o644); err != nil {
24 t.Fatal(err)
25 }
26 if err := os.WriteFile(filepath.Join(project, "AGENTS.md"), []byte("repo rule"), 0o644); err != nil {
27 t.Fatal(err)
28 }
29 if err := os.WriteFile(filepath.Join(nested, "AGENTS.md"), []byte("app rule"), 0o644); err != nil {
30 t.Fatal(err)
31 }
32
33 got := readAgentsContext(nested, zotHome)
34 for _, want := range []string{"global rule", "repo rule", "app rule"} {
35 if !strings.Contains(got, want) {
36 t.Fatalf("readAgentsContext missing %q in:\n%s", want, got)
37 }
38 }
39 if strings.Index(got, "global rule") > strings.Index(got, "repo rule") || strings.Index(got, "repo rule") > strings.Index(got, "app rule") {
40 t.Fatalf("AGENTS.md files loaded in wrong order:\n%s", got)
41 }
42}
43
44func TestReadAgentsContextMissingFilesIsEmpty(t *testing.T) {
45 got := readAgentsContext(t.TempDir(), t.TempDir())
46 if got != "" {
47 t.Fatalf("expected no context, got %q", got)
48 }
49}
50
51// TestResolveFallsBackWhenConfiguredModelIsGone reproduces the
52// startup failure caught by the user's screenshot: the persisted
53// config.json points at a model id that's no longer in the active
54// catalogue (because they edited models.json or zot's bundled
55// catalogue changed). Resolve must NOT error — strands the user
56// with no way to fix it from the TUI — and should repair the config
57// so the next launch is silent.
58func TestResolveFallsBackWhenConfiguredModelIsGone(t *testing.T) {
59 t.Setenv("ZOT_HOME", t.TempDir())
60 t.Setenv("OPENAI_API_KEY", "test-key")
61 // Persist a stale model id.
62 stale := "gpt-5.5-pro-not-real"
63 if err := SaveConfig(Config{Provider: "openai", Model: stale}); err != nil {
64 t.Fatal(err)
65 }
66
67 r, err := Resolve(Args{}, false)
68 if err != nil {
69 t.Fatalf("Resolve refused to launch with stale model: %v", err)
70 }
71 if r.Model == stale {
72 t.Fatalf("Resolve kept stale model %q", r.Model)
73 }
74 if r.Provider != "openai" {
75 t.Errorf("provider drifted: got %q; want openai", r.Provider)
76 }
77
78 // Config on disk should now hold the fallback so subsequent
79 // launches don't repeat the warning.
80 cfg, _ := LoadConfig()
81 if cfg.Model == stale {
82 t.Errorf("config.json still pins the stale model %q", cfg.Model)
83 }
84 if cfg.Model == "" {
85 t.Errorf("config.json was emptied; expected the fallback model id")
86 }
87}
88
89// TestResolveExplicitFlagStaleDoesNotRepairConfig confirms the
90// repair-on-disk happens ONLY when the stale id came from the
91// persisted config. If the user passed --model X explicitly and X is
92// unknown, we still fall back, but we don't touch their config.
93func TestResolveExplicitFlagStaleDoesNotRepairConfig(t *testing.T) {
94 t.Setenv("ZOT_HOME", t.TempDir())
95 t.Setenv("OPENAI_API_KEY", "test-key")
96 good := "gpt-5"
97 if err := SaveConfig(Config{Provider: "openai", Model: good}); err != nil {
98 t.Fatal(err)
99 }
100
101 r, err := Resolve(Args{Model: "gpt-totally-fake"}, false)
102 if err != nil {
103 t.Fatalf("Resolve errored on unknown --model: %v", err)
104 }
105 if r.Model == "gpt-totally-fake" {
106 t.Errorf("Resolve kept the bogus --model value")
107 }
108 cfg, _ := LoadConfig()
109 if cfg.Model != good {
110 t.Errorf("config.json was clobbered (was %q; now %q)", good, cfg.Model)
111 }
112}
113
114func TestResolveOllamaUsesModelBaseURLBeforeDefault(t *testing.T) {
115 t.Setenv("ZOT_HOME", t.TempDir())
116 provider.SetLiveModels(nil)
117 defer provider.SetLiveModels(nil)
118 provider.SetUserModels([]provider.Model{{
119 Provider: "ollama",
120 ID: "qwen-local",
121 DisplayName: "Qwen Local",
122 ContextWindow: 32768,
123 MaxOutput: 8192,
124 BaseURL: "http://localhost:8000/v1",
125 }})
126
127 r, err := Resolve(Args{Provider: "ollama", Model: "qwen-local"}, false)
128 if err != nil {
129 t.Fatalf("Resolve failed: %v", err)
130 }
131 if r.BaseURL != "http://localhost:8000/v1" {
132 t.Fatalf("BaseURL = %q, want models.json baseUrl", r.BaseURL)
133 }
134}
135
136func TestResolveOllamaFallsBackToDefaultBaseURL(t *testing.T) {
137 t.Setenv("ZOT_HOME", t.TempDir())
138 provider.SetLiveModels(nil)
139 defer provider.SetLiveModels(nil)
140
141 r, err := Resolve(Args{Provider: "ollama", Model: "any-local-model"}, false)
142 if err != nil {
143 t.Fatalf("Resolve failed: %v", err)
144 }
145 if r.BaseURL != "http://localhost:11434" {
146 t.Fatalf("BaseURL = %q, want ollama default", r.BaseURL)
147 }
148}
149
150func TestCanonicalProviderResolvesAliases(t *testing.T) {
151 cases := map[string]string{
152 "bedrock": "amazon-bedrock",
153 "AWS-Bedrock": "amazon-bedrock",
154 " bedrock ": "amazon-bedrock",
155 "vertex": "google-vertex",
156 "gemini": "google",
157 "azure": "azure-openai-responses",
158 "copilot": "github-copilot",
159 "codex": "openai-codex",
160 "moonshot": "moonshotai",
161 "vercel": "vercel-ai-gateway",
162 "hf": "huggingface",
163 "anthropic": "anthropic", // canonical passes through
164 "amazon-bedrock": "amazon-bedrock", // already canonical
165 "totally-unknown": "totally-unknown", // unknown returned unchanged (lowered)
166 "Totally-UNKNOWN": "totally-unknown",
167 "": "",
168 }
169 for in, want := range cases {
170 if got := canonicalProvider(in); got != want {
171 t.Errorf("canonicalProvider(%q) = %q, want %q", in, got, want)
172 }
173 }
174}
175
176func TestCanonicalProviderAliasesAreKnown(t *testing.T) {
177 for alias, canon := range providerAliases {
178 if !isKnownProvider(canon) {
179 t.Errorf("alias %q maps to %q which is not a known provider", alias, canon)
180 }
181 }
182}