feat: add config and credential storage

This commit is contained in:
2026-05-13 16:17:30 +08:00
parent cb01999a77
commit 187c14122e
5 changed files with 254 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
package config
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestLoadMissingConfigReturnsDefaults(t *testing.T) {
dir := t.TempDir()
cfg, err := Load(filepath.Join(dir, "config.json"))
if err != nil {
t.Fatalf("Load returned error: %v", err)
}
if cfg.Gitea.AuthType != "password" {
t.Fatalf("default auth type = %q, want password", cfg.Gitea.AuthType)
}
if !cfg.Update.AutoUpdate || !cfg.Update.CheckOnStartup {
t.Fatalf("update defaults should be enabled: %+v", cfg.Update)
}
if cfg.Update.IntervalMinutes != 60 {
t.Fatalf("interval = %d, want 60", cfg.Update.IntervalMinutes)
}
}
func TestSaveDoesNotPersistSecrets(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.json")
cfg := Default()
cfg.Gitea.BaseURL = "https://gitea.example.com"
cfg.Gitea.Org = "skills"
cfg.Gitea.Username = "alice"
cfg.Gitea.CredentialKey = "sgg-ai-skill-manager:gitea"
if err := Save(path, cfg); err != nil {
t.Fatalf("Save returned error: %v", err)
}
raw, err := os.ReadFile(path)
if err != nil {
t.Fatalf("ReadFile returned error: %v", err)
}
text := string(raw)
if containsAny(text, []string{"password-secret", "token-secret"}) {
t.Fatalf("config persisted a secret: %s", text)
}
}
func TestValidateRejectsMissingConnectionFields(t *testing.T) {
cfg := Default()
if err := Validate(cfg); err == nil {
t.Fatal("Validate returned nil error for empty config")
}
cfg.Gitea.BaseURL = "https://gitea.example.com"
if err := Validate(cfg); err == nil {
t.Fatal("Validate returned nil error without org")
}
cfg.Gitea.Org = "skills"
if err := Validate(cfg); err != nil {
t.Fatalf("Validate returned error for valid config: %v", err)
}
}
func containsAny(text string, needles []string) bool {
for _, needle := range needles {
if needle != "" && strings.Contains(text, needle) {
return true
}
}
return false
}