All checks were successful
Tests / unit-tests (push) Successful in 51s
- Login sa session cookie autentifikacijom - Lista projekata iz filesystem-a - Chat sa Claude CLI preko WebSocket-a - Streaming NDJSON parsiranje iz CLI stdout-a - Sesija zivi nezavisno od browsera (reconnect replay) - Sidebar sa .md fajlovima i markdown renderovanjem - Dark tema, htmx + Go templates - 47 unit testova Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
132 lines
3.0 KiB
Go
132 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoadConfig(t *testing.T) {
|
|
dir := t.TempDir()
|
|
cfgPath := filepath.Join(dir, "config.json")
|
|
|
|
t.Run("valid config", func(t *testing.T) {
|
|
data := `{
|
|
"port": 9100,
|
|
"mode": "cli",
|
|
"projects_path": "/tmp/projects",
|
|
"username": "admin",
|
|
"password": "secret",
|
|
"session_secret": "abc123"
|
|
}`
|
|
os.WriteFile(cfgPath, []byte(data), 0644)
|
|
|
|
cfg, err := LoadConfig(cfgPath)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if cfg.Port != 9100 {
|
|
t.Errorf("port = %d, want 9100", cfg.Port)
|
|
}
|
|
if cfg.Mode != "cli" {
|
|
t.Errorf("mode = %q, want cli", cfg.Mode)
|
|
}
|
|
if cfg.ProjectsPath != "/tmp/projects" {
|
|
t.Errorf("projects_path = %q, want /tmp/projects", cfg.ProjectsPath)
|
|
}
|
|
})
|
|
|
|
t.Run("defaults applied", func(t *testing.T) {
|
|
data := `{
|
|
"username": "admin",
|
|
"password": "secret",
|
|
"session_secret": "abc123"
|
|
}`
|
|
os.WriteFile(cfgPath, []byte(data), 0644)
|
|
|
|
cfg, err := LoadConfig(cfgPath)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if cfg.Port != 9100 {
|
|
t.Errorf("port = %d, want 9100", cfg.Port)
|
|
}
|
|
if cfg.Mode != "cli" {
|
|
t.Errorf("mode = %q, want cli", cfg.Mode)
|
|
}
|
|
if cfg.ProjectsPath != "/root/projects" {
|
|
t.Errorf("projects_path = %q, want /root/projects", cfg.ProjectsPath)
|
|
}
|
|
})
|
|
|
|
t.Run("missing username", func(t *testing.T) {
|
|
data := `{"password": "secret", "session_secret": "abc"}`
|
|
os.WriteFile(cfgPath, []byte(data), 0644)
|
|
|
|
_, err := LoadConfig(cfgPath)
|
|
if err == nil {
|
|
t.Fatal("expected error for missing username")
|
|
}
|
|
})
|
|
|
|
t.Run("missing password", func(t *testing.T) {
|
|
data := `{"username": "admin", "session_secret": "abc"}`
|
|
os.WriteFile(cfgPath, []byte(data), 0644)
|
|
|
|
_, err := LoadConfig(cfgPath)
|
|
if err == nil {
|
|
t.Fatal("expected error for missing password")
|
|
}
|
|
})
|
|
|
|
t.Run("missing session_secret", func(t *testing.T) {
|
|
data := `{"username": "admin", "password": "secret"}`
|
|
os.WriteFile(cfgPath, []byte(data), 0644)
|
|
|
|
_, err := LoadConfig(cfgPath)
|
|
if err == nil {
|
|
t.Fatal("expected error for missing session_secret")
|
|
}
|
|
})
|
|
|
|
t.Run("file not found", func(t *testing.T) {
|
|
_, err := LoadConfig("/nonexistent/config.json")
|
|
if err == nil {
|
|
t.Fatal("expected error for missing file")
|
|
}
|
|
})
|
|
|
|
t.Run("invalid json", func(t *testing.T) {
|
|
os.WriteFile(cfgPath, []byte("{invalid"), 0644)
|
|
|
|
_, err := LoadConfig(cfgPath)
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid JSON")
|
|
}
|
|
})
|
|
|
|
t.Run("api config", func(t *testing.T) {
|
|
data := `{
|
|
"username": "admin",
|
|
"password": "secret",
|
|
"session_secret": "abc123",
|
|
"api": {
|
|
"key": "sk-ant-xxx",
|
|
"model": "claude-sonnet-4-20250514"
|
|
}
|
|
}`
|
|
os.WriteFile(cfgPath, []byte(data), 0644)
|
|
|
|
cfg, err := LoadConfig(cfgPath)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if cfg.API.Key != "sk-ant-xxx" {
|
|
t.Errorf("api.key = %q, want sk-ant-xxx", cfg.API.Key)
|
|
}
|
|
if cfg.API.Model != "claude-sonnet-4-20250514" {
|
|
t.Errorf("api.model = %q", cfg.API.Model)
|
|
}
|
|
})
|
|
}
|