mcp-framework/config/config_test.go
2026-04-13 15:33:48 +02:00

78 lines
1.8 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
)
type testProfile struct {
BaseURL string `json:"base_url"`
StreamID string `json:"stream_id"`
}
func TestLoadMissingReturnsDefault(t *testing.T) {
store := NewStore[testProfile]("mcp-framework-test")
path := filepath.Join(t.TempDir(), "missing.json")
cfg, err := store.Load(path)
if err != nil {
t.Fatalf("Load returned error: %v", err)
}
if cfg.Version != CurrentVersion {
t.Fatalf("Version = %d, want %d", cfg.Version, CurrentVersion)
}
if len(cfg.Profiles) != 0 {
t.Fatalf("Profiles = %v, want empty", cfg.Profiles)
}
}
func TestSaveAndLoadRoundTrip(t *testing.T) {
store := NewStore[testProfile]("mcp-framework-test")
dir := t.TempDir()
path := filepath.Join(dir, "mcp-framework", "config.json")
input := FileConfig[testProfile]{
Version: CurrentVersion,
CurrentProfile: "prod",
Profiles: map[string]testProfile{
"prod": {
BaseURL: "https://graylog.example.com",
StreamID: "stream-1",
},
},
}
if err := store.Save(path, input); err != nil {
t.Fatalf("Save returned error: %v", err)
}
info, err := os.Stat(path)
if err != nil {
t.Fatalf("Stat returned error: %v", err)
}
if info.Mode().Perm() != 0o600 {
t.Fatalf("file mode = %o, want 600", info.Mode().Perm())
}
dirInfo, err := os.Stat(filepath.Dir(path))
if err != nil {
t.Fatalf("Stat dir returned error: %v", err)
}
if dirInfo.Mode().Perm() != 0o700 {
t.Fatalf("dir mode = %o, want 700", dirInfo.Mode().Perm())
}
cfg, err := store.Load(path)
if err != nil {
t.Fatalf("Load returned error: %v", err)
}
if cfg.CurrentProfile != "prod" {
t.Fatalf("CurrentProfile = %q, want prod", cfg.CurrentProfile)
}
if cfg.Profiles["prod"].BaseURL != "https://graylog.example.com" {
t.Fatalf("BaseURL = %q", cfg.Profiles["prod"].BaseURL)
}
}