xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / config / config_test.go

13.1 KB
1package config
2
3import (
4	"fmt"
5	"os"
6	"path/filepath"
7	"strings"
8	"testing"
9)
10
11// TestDefaultConfig removed - config file is now required
12
13func TestLoadFromFile(t *testing.T) {
14	// Create temp config file
15	tmpDir := t.TempDir()
16	configPath := filepath.Join(tmpDir, "config.toml")
17
18	content := `
19[swarm]
20max_myses = 32
21
22[providers.ollama]
23endpoint = "http://custom:11434"
24model = "mistral"
25temperature = 0.7
26
27[mcp]
28upstream = "https://custom.mcp/endpoint"
29`
30	if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
31		t.Fatalf("failed to write test config: %v", err)
32	}
33
34	cfg, err := Load(configPath)
35	if err != nil {
36		t.Fatalf("Load() error: %v", err)
37	}
38
39	if cfg.Swarm.MaxMyses != 32 {
40		t.Errorf("expected max_myses=32, got %d", cfg.Swarm.MaxMyses)
41	}
42	if cfg.Providers["ollama"].Endpoint != "http://custom:11434" {
43		t.Errorf("expected custom ollama endpoint, got %s", cfg.Providers["ollama"].Endpoint)
44	}
45	if cfg.MCP.Upstream != "https://custom.mcp/endpoint" {
46		t.Errorf("expected custom mcp upstream, got %s", cfg.MCP.Upstream)
47	}
48}
49
50func TestLoadWithTemperature(t *testing.T) {
51	tmpDir := t.TempDir()
52	configPath := filepath.Join(tmpDir, "config.toml")
53
54	content := `
55[swarm]
56max_myses = 16
57
58[providers.ollama]
59endpoint = "http://localhost:11434"
60model = "qwen3:4b"
61temperature = 0.5
62`
63	if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
64		t.Fatalf("failed to write test config: %v", err)
65	}
66
67	cfg, err := Load(configPath)
68	if err != nil {
69		t.Fatalf("Load() error: %v", err)
70	}
71
72	if cfg.Providers["ollama"].Temperature != 0.5 {
73		t.Errorf("expected temperature=0.5, got %v", cfg.Providers["ollama"].Temperature)
74	}
75}
76
77func TestLoadInvalidTemperature(t *testing.T) {
78	tmpDir := t.TempDir()
79	configPath := filepath.Join(tmpDir, "config.toml")
80
81	content := `
82[providers.ollama]
83endpoint = "http://localhost:11434"
84model = "qwen3:4b"
85temperature = 3.5
86`
87	if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
88		t.Fatalf("failed to write test config: %v", err)
89	}
90
91	_, err := Load(configPath)
92	if err == nil {
93		t.Fatal("expected validation error for invalid temperature")
94	}
95	if !strings.Contains(err.Error(), "temperature") {
96		t.Fatalf("expected temperature validation error, got %v", err)
97	}
98}
99
100func TestLoadMissingProviderFields(t *testing.T) {
101	tmpDir := t.TempDir()
102	configPath := filepath.Join(tmpDir, "config.toml")
103
104	content := `
105[providers.ollama]
106endpoint = ""
107model = ""
108`
109	if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
110		t.Fatalf("failed to write test config: %v", err)
111	}
112
113	_, err := Load(configPath)
114	if err == nil {
115		t.Fatal("expected validation error for missing provider fields")
116	}
117	if !strings.Contains(err.Error(), "providers.ollama.endpoint") {
118		t.Fatalf("expected endpoint validation error, got %v", err)
119	}
120	if !strings.Contains(err.Error(), "providers.ollama.model") {
121		t.Fatalf("expected model validation error, got %v", err)
122	}
123}
124
125func TestLoadWithEnvOverrides(t *testing.T) {
126	tmpDir := t.TempDir()
127	configPath := filepath.Join(tmpDir, "config.toml")
128
129	content := `
130[swarm]
131max_myses = 16
132
133[providers.ollama]
134endpoint = "http://localhost:11434"
135model = "qwen3:4b"
136temperature = 0.7
137
138[mcp]
139upstream = "https://default.mcp/endpoint"
140`
141	if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
142		t.Fatalf("failed to write test config: %v", err)
143	}
144
145	// Set env vars
146	os.Setenv("ZOEA_MAX_MYSES", "10")
147	os.Setenv("ZOEA_MCP_ENDPOINT", "https://env.mcp/endpoint")
148	defer func() {
149		os.Unsetenv("ZOEA_MAX_MYSES")
150		os.Unsetenv("ZOEA_MCP_ENDPOINT")
151	}()
152
153	cfg, err := Load(configPath)
154	if err != nil {
155		t.Fatalf("Load() error: %v", err)
156	}
157
158	if cfg.Swarm.MaxMyses != 10 {
159		t.Errorf("expected env override max_myses=10, got %d", cfg.Swarm.MaxMyses)
160	}
161	if cfg.MCP.Upstream != "https://env.mcp/endpoint" {
162		t.Errorf("expected env override mcp endpoint, got %s", cfg.MCP.Upstream)
163	}
164}
165
166func TestLoadNonExistentFile(t *testing.T) {
167	_, err := Load("/nonexistent/path/config.toml")
168	if err == nil {
169		t.Fatal("Load() should error for non-existent file")
170	}
171	if !strings.Contains(err.Error(), "config file not found") {
172		t.Errorf("expected 'config file not found' error, got %v", err)
173	}
174}
175
176func TestCredentials(t *testing.T) {
177	creds := &Credentials{
178		Providers: make(map[string]ProviderCredentials),
179	}
180
181	// Test GetAPIKey on empty
182	if key := creds.GetAPIKey("opencode_zen"); key != "" {
183		t.Errorf("expected empty key, got %s", key)
184	}
185
186	// Test SetAPIKey
187	creds.SetAPIKey("opencode_zen", "test-api-key")
188	if key := creds.GetAPIKey("opencode_zen"); key != "test-api-key" {
189		t.Errorf("expected test-api-key, got %s", key)
190	}
191}
192
193func TestCredentialsSaveLoad(t *testing.T) {
194	// Override home dir for test
195	tmpDir := t.TempDir()
196	originalHome := os.Getenv("HOME")
197	os.Setenv("HOME", tmpDir)
198	defer os.Setenv("HOME", originalHome)
199
200	// Create and save credentials
201	creds := &Credentials{}
202	creds.SetAPIKey("opencode_zen", "secret-key-123")
203
204	if err := SaveCredentials(creds); err != nil {
205		t.Fatalf("SaveCredentials() error: %v", err)
206	}
207
208	// Verify file permissions
209	path := filepath.Join(tmpDir, ".zoea-nova", "credentials.json")
210	info, err := os.Stat(path)
211	if err != nil {
212		t.Fatalf("credentials file not created: %v", err)
213	}
214	if info.Mode().Perm() != 0600 {
215		t.Errorf("expected permissions 0600, got %o", info.Mode().Perm())
216	}
217
218	// Load credentials back
219	loaded, err := LoadCredentials()
220	if err != nil {
221		t.Fatalf("LoadCredentials() error: %v", err)
222	}
223	if key := loaded.GetAPIKey("opencode_zen"); key != "secret-key-123" {
224		t.Errorf("expected secret-key-123, got %s", key)
225	}
226}
227
228func TestLoadCredentialsNonExistent(t *testing.T) {
229	tmpDir := t.TempDir()
230	originalHome := os.Getenv("HOME")
231	os.Setenv("HOME", tmpDir)
232	defer os.Setenv("HOME", originalHome)
233
234	creds, err := LoadCredentials()
235	if err != nil {
236		t.Fatalf("LoadCredentials() should not error for non-existent file: %v", err)
237	}
238	if creds == nil {
239		t.Fatal("expected non-nil credentials")
240	}
241}
242
243func TestLoadEnvOverridePrecedence(t *testing.T) {
244	tmpDir := t.TempDir()
245	configPath := filepath.Join(tmpDir, "config.toml")
246
247	content := `
248[swarm]
249max_myses = 16
250
251[providers.ollama-qwen]
252endpoint = "http://file-endpoint"
253model = "file-model"
254temperature = 0.7
255`
256	if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
257		t.Fatalf("failed to write test config: %v", err)
258	}
259
260	os.Setenv("ZOEA_MAX_MYSES", "32")
261	defer func() {
262		os.Unsetenv("ZOEA_MAX_MYSES")
263	}()
264
265	cfg, err := Load(configPath)
266	if err != nil {
267		t.Fatalf("Load() error: %v", err)
268	}
269
270	if cfg.Swarm.MaxMyses != 32 {
271		t.Errorf("expected env max_myses override=32, got %d", cfg.Swarm.MaxMyses)
272	}
273	// Verify file values remain unchanged
274	if cfg.Providers["ollama-qwen"].Model != "file-model" {
275		t.Errorf("expected file model, got %s", cfg.Providers["ollama-qwen"].Model)
276	}
277}
278
279func TestLoadIgnoresInvalidEnvOverrides(t *testing.T) {
280	tmpDir := t.TempDir()
281	configPath := filepath.Join(tmpDir, "config.toml")
282
283	content := `
284[swarm]
285max_myses = 16
286
287[providers.ollama-qwen]
288endpoint = "http://localhost:11434"
289model = "qwen3:8b"
290temperature = 0.7
291`
292	if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
293		t.Fatalf("failed to write test config: %v", err)
294	}
295
296	os.Setenv("ZOEA_MAX_MYSES", "not-a-number")
297	defer func() {
298		os.Unsetenv("ZOEA_MAX_MYSES")
299	}()
300
301	cfg, err := Load(configPath)
302	if err != nil {
303		t.Fatalf("Load() error: %v", err)
304	}
305
306	// Invalid env overrides should be ignored, file values should remain
307	if cfg.Swarm.MaxMyses != 16 {
308		t.Errorf("expected max_myses=16, got %d", cfg.Swarm.MaxMyses)
309	}
310	if cfg.Providers["ollama-qwen"].Temperature != 0.7 {
311		t.Errorf("expected temperature=0.7, got %v", cfg.Providers["ollama-qwen"].Temperature)
312	}
313}
314
315func TestLoadInvalidURLFormat(t *testing.T) {
316	tmpDir := t.TempDir()
317	configPath := filepath.Join(tmpDir, "config.toml")
318
319	tests := []struct {
320		name     string
321		content  string
322		expected string
323	}{
324		{
325			name: "missing_scheme",
326			content: `
327[providers.ollama]
328endpoint = "localhost:11434"
329model = "qwen3:4b"
330`,
331			expected: "missing scheme or host",
332		},
333		{
334			name: "missing_host",
335			content: `
336[providers.ollama]
337endpoint = "http://"
338model = "qwen3:4b"
339`,
340			expected: "missing scheme or host",
341		},
342		{
343			name: "invalid_url",
344			content: `
345[providers.ollama]
346endpoint = "ht!tp://localhost:11434"
347model = "qwen3:4b"
348`,
349			expected: "invalid",
350		},
351	}
352
353	for _, tt := range tests {
354		t.Run(tt.name, func(t *testing.T) {
355			if err := os.WriteFile(configPath, []byte(tt.content), 0644); err != nil {
356				t.Fatalf("failed to write test config: %v", err)
357			}
358
359			_, err := Load(configPath)
360			if err == nil {
361				t.Fatal("expected validation error for invalid URL format")
362			}
363			if !strings.Contains(err.Error(), tt.expected) {
364				t.Fatalf("expected error containing %q, got %v", tt.expected, err)
365			}
366		})
367	}
368}
369
370func TestLoadMaxMysesBounds(t *testing.T) {
371	tmpDir := t.TempDir()
372	configPath := filepath.Join(tmpDir, "config.toml")
373
374	tests := []struct {
375		name     string
376		maxMyses int
377	}{
378		{"below_minimum", 0},
379		{"negative", -1},
380		{"above_maximum", 101},
381	}
382
383	for _, tt := range tests {
384		t.Run(tt.name, func(t *testing.T) {
385			content := fmt.Sprintf(`
386[swarm]
387max_myses = %d
388
389[providers.ollama]
390endpoint = "http://localhost:11434"
391model = "qwen3:4b"
392`, tt.maxMyses)
393
394			if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
395				t.Fatalf("failed to write test config: %v", err)
396			}
397
398			_, err := Load(configPath)
399			if err == nil {
400				t.Fatal("expected validation error for max_myses out of bounds")
401			}
402			if !strings.Contains(err.Error(), "max_myses") {
403				t.Fatalf("expected max_myses validation error, got %v", err)
404			}
405			if !strings.Contains(err.Error(), "between 1 and 100") {
406				t.Fatalf("expected bounds message, got %v", err)
407			}
408		})
409	}
410}
411
412func TestLoadDefaultProvider(t *testing.T) {
413	tmpDir := t.TempDir()
414	configPath := filepath.Join(tmpDir, "config.toml")
415
416	configContent := `
417[swarm]
418max_myses = 16
419default_provider = "ollama-qwen"
420
421[providers.ollama-qwen]
422endpoint = "http://localhost:11434"
423model = "qwen3:8b"
424temperature = 0.7
425`
426	if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil {
427		t.Fatalf("failed to write test config: %v", err)
428	}
429
430	cfg, err := Load(configPath)
431	if err != nil {
432		t.Fatalf("Load failed: %v", err)
433	}
434
435	if cfg.Swarm.DefaultProvider != "ollama-qwen" {
436		t.Errorf("expected default_provider=ollama-qwen, got %s", cfg.Swarm.DefaultProvider)
437	}
438}
439
440func TestLoadAllEnvOverrides(t *testing.T) {
441	tmpDir := t.TempDir()
442	configPath := filepath.Join(tmpDir, "config.toml")
443
444	content := `
445[swarm]
446max_myses = 16
447
448[providers.ollama-qwen]
449endpoint = "http://localhost:11434"
450model = "qwen3:8b"
451temperature = 0.7
452
453[mcp]
454upstream = "https://mcp.example.com"
455`
456	if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
457		t.Fatalf("failed to write test config: %v", err)
458	}
459
460	// Set env vars
461	os.Setenv("ZOEA_MAX_MYSES", "32")
462	os.Setenv("ZOEA_MCP_ENDPOINT", "https://env-mcp.example.com")
463	defer func() {
464		os.Unsetenv("ZOEA_MAX_MYSES")
465		os.Unsetenv("ZOEA_MCP_ENDPOINT")
466	}()
467
468	cfg, err := Load(configPath)
469	if err != nil {
470		t.Fatalf("Load() error: %v", err)
471	}
472
473	// Verify overrides
474	if cfg.Swarm.MaxMyses != 32 {
475		t.Errorf("expected max_myses=32, got %d", cfg.Swarm.MaxMyses)
476	}
477	if cfg.MCP.Upstream != "https://env-mcp.example.com" {
478		t.Errorf("expected env mcp endpoint, got %s", cfg.MCP.Upstream)
479	}
480}
481
482func TestLoadPartialEnvOverrides(t *testing.T) {
483	tmpDir := t.TempDir()
484	configPath := filepath.Join(tmpDir, "config.toml")
485
486	content := `
487[swarm]
488max_myses = 16
489
490[providers.ollama-qwen]
491endpoint = "http://localhost:11434"
492model = "qwen3:8b"
493temperature = 0.7
494`
495	if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
496		t.Fatalf("failed to write test config: %v", err)
497	}
498
499	// Override only max_myses
500	os.Setenv("ZOEA_MAX_MYSES", "8")
501	defer func() {
502		os.Unsetenv("ZOEA_MAX_MYSES")
503	}()
504
505	cfg, err := Load(configPath)
506	if err != nil {
507		t.Fatalf("Load() error: %v", err)
508	}
509
510	// Verify partial override
511	if cfg.Swarm.MaxMyses != 8 {
512		t.Errorf("expected max_myses override=8, got %d", cfg.Swarm.MaxMyses)
513	}
514	// Verify file values remain
515	if cfg.Providers["ollama-qwen"].Endpoint != "http://localhost:11434" {
516		t.Errorf("expected file endpoint, got %s", cfg.Providers["ollama-qwen"].Endpoint)
517	}
518	if cfg.Providers["ollama-qwen"].Model != "qwen3:8b" {
519		t.Errorf("expected file model, got %s", cfg.Providers["ollama-qwen"].Model)
520	}
521}
522
523func TestLoadInvalidTOMLSyntax(t *testing.T) {
524	tmpDir := t.TempDir()
525	configPath := filepath.Join(tmpDir, "config.toml")
526
527	tests := []struct {
528		name    string
529		content string
530	}{
531		{
532			name: "unclosed_bracket",
533			content: `
534[swarm
535max_myses = 16
536`,
537		},
538		{
539			name: "invalid_assignment",
540			content: `
541[swarm]
542max_myses = = 16
543`,
544		},
545		{
546			name: "unclosed_string",
547			content: `
548[providers.ollama]
549endpoint = "http://localhost:11434
550model = "qwen3:4b"
551`,
552		},
553	}
554
555	for _, tt := range tests {
556		t.Run(tt.name, func(t *testing.T) {
557			if err := os.WriteFile(configPath, []byte(tt.content), 0644); err != nil {
558				t.Fatalf("failed to write test config: %v", err)
559			}
560
561			_, err := Load(configPath)
562			if err == nil {
563				t.Fatal("expected parse error for invalid TOML syntax")
564			}
565			if !strings.Contains(err.Error(), "failed to parse config") {
566				t.Errorf("expected parse error message, got %v", err)
567			}
568		})
569	}
570}