Files
MuyueWorkspace/internal/config/config_test.go
Augustin 3494f6b40d
All checks were successful
CI / build (push) Successful in 2m37s
feat: security hardening, tests, doctor command, CI update, CHANGELOG
- Add AES-256-GCM encryption for API keys (internal/secret)
- Add dangerous command detection in terminal
- Add muyue doctor command for system health checks
- Add scanner TTL cache, orchestrator history mutex, shared HTTP client
- Deduplicate MCP config generation, refactor skills YAML parser
- Add XDG-compliant config dir with legacy migration
- Add cleanup on all TUI quit paths
- Add 8 test files (config, workflow, skills, orchestrator, version,
  platform, scanner, secret)
- Update CI to actions/setup-go@v5
- Add CHANGELOG.md, update README and Makefile

🤖 Generated with Crush

Assisted-by: GLM-5.1 via Crush <crush@charm.land>
2026-04-20 19:56:07 +02:00

155 lines
3.8 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
)
func TestDefault(t *testing.T) {
cfg := Default()
if cfg.Version != "0.1.0" {
t.Errorf("Expected version 0.1.0, got %s", cfg.Version)
}
if cfg.Profile.Pseudo != "muyue" {
t.Errorf("Expected pseudo muyue, got %s", cfg.Profile.Pseudo)
}
if len(cfg.AI.Providers) == 0 {
t.Error("Expected at least one AI provider")
}
found := false
for _, p := range cfg.AI.Providers {
if p.Name == "minimax" && p.Active {
found = true
}
}
if !found {
t.Error("Expected minimax to be active")
}
}
func TestSaveAndLoad(t *testing.T) {
tmpDir := t.TempDir()
origHome := os.Getenv("HOME")
os.Setenv("HOME", tmpDir)
defer os.Setenv("HOME", origHome)
origConfig := os.Getenv("XDG_CONFIG_HOME")
os.Setenv("XDG_CONFIG_HOME", filepath.Join(tmpDir, ".config"))
defer os.Setenv("XDG_CONFIG_HOME", origConfig)
cfg := Default()
cfg.Profile.Name = "Test User"
cfg.Profile.Email = "test@example.com"
cfg.Profile.Pseudo = "tester"
cfg.Profile.Languages = []string{"go", "python"}
cfg.AI.Providers[0].APIKey = "test-key-123"
if err := Save(cfg); err != nil {
t.Fatalf("Save failed: %v", err)
}
if !Exists() {
t.Error("Exists should return true after Save")
}
loaded, err := Load()
if err != nil {
t.Fatalf("Load failed: %v", err)
}
if loaded.Profile.Name != "Test User" {
t.Errorf("Expected name Test User, got %s", loaded.Profile.Name)
}
if loaded.Profile.Pseudo != "tester" {
t.Errorf("Expected pseudo tester, got %s", loaded.Profile.Pseudo)
}
if loaded.Profile.Email != "test@example.com" {
t.Errorf("Expected email test@example.com, got %s", loaded.Profile.Email)
}
if len(loaded.Profile.Languages) != 2 {
t.Errorf("Expected 2 languages, got %d", len(loaded.Profile.Languages))
}
if loaded.AI.Providers[0].APIKey != "test-key-123" {
t.Error("API key should be decrypted on load")
}
}
func TestExistsFalse(t *testing.T) {
tmpDir := t.TempDir()
origHome := os.Getenv("HOME")
os.Setenv("HOME", tmpDir)
defer os.Setenv("HOME", origHome)
origConfig := os.Getenv("XDG_CONFIG_HOME")
os.Setenv("XDG_CONFIG_HOME", filepath.Join(tmpDir, ".config"))
defer os.Setenv("XDG_CONFIG_HOME", origConfig)
if Exists() {
t.Error("Exists should return false for non-existent config")
}
}
func TestConfigDir(t *testing.T) {
tmpDir := t.TempDir()
origConfig := os.Getenv("XDG_CONFIG_HOME")
os.Setenv("XDG_CONFIG_HOME", filepath.Join(tmpDir, ".config"))
defer os.Setenv("XDG_CONFIG_HOME", origConfig)
dir, err := ConfigDir()
if err != nil {
t.Fatalf("ConfigDir failed: %v", err)
}
expected := filepath.Join(tmpDir, ".config", "muyue")
if dir != expected {
t.Errorf("Expected %s, got %s", expected, dir)
}
}
func TestConfigPath(t *testing.T) {
tmpDir := t.TempDir()
origConfig := os.Getenv("XDG_CONFIG_HOME")
os.Setenv("XDG_CONFIG_HOME", filepath.Join(tmpDir, ".config"))
defer os.Setenv("XDG_CONFIG_HOME", origConfig)
path, err := ConfigPath()
if err != nil {
t.Fatalf("ConfigPath failed: %v", err)
}
expected := filepath.Join(tmpDir, ".config", "muyue", "config.yaml")
if path != expected {
t.Errorf("Expected %s, got %s", expected, path)
}
}
func TestRoundtripEmptyFields(t *testing.T) {
tmpDir := t.TempDir()
origHome := os.Getenv("HOME")
os.Setenv("HOME", tmpDir)
defer os.Setenv("HOME", origHome)
origConfig := os.Getenv("XDG_CONFIG_HOME")
os.Setenv("XDG_CONFIG_HOME", filepath.Join(tmpDir, ".config"))
defer os.Setenv("XDG_CONFIG_HOME", origConfig)
cfg := Default()
cfg.Profile.Name = ""
cfg.AI.Providers[0].APIKey = ""
if err := Save(cfg); err != nil {
t.Fatalf("Save failed: %v", err)
}
loaded, err := Load()
if err != nil {
t.Fatalf("Load failed: %v", err)
}
if loaded.Profile.Name != "" {
t.Errorf("Expected empty name, got %s", loaded.Profile.Name)
}
if loaded.AI.Providers[0].APIKey != "" {
t.Error("Expected empty API key")
}
}