package config import ( "os" "path/filepath" "runtime" "strings" "testing" ) func valid() Config { cfg := Defaults() cfg.Server = "https://clip.example.org" cfg.Token = "lc_abc" return cfg } func TestSaveAndLoad(t *testing.T) { path := filepath.Join(t.TempDir(), "sub", "agent.json") cfg := valid() cfg.Exclude = []string{`^\d{6}$`} if err := Save(path, cfg); err != nil { t.Fatal(err) } got, err := Load(path) if err != nil { t.Fatal(err) } if got.Server != cfg.Server || got.Token != cfg.Token || got.Exclude[0] != cfg.Exclude[0] { t.Fatalf("roundtrip mismatch: %+v", got) } if runtime.GOOS != "windows" { info, _ := os.Stat(path) if info.Mode().Perm() != 0o600 { t.Fatalf("config readable by others: %v", info.Mode()) } } } func TestLoadFillsDefaults(t *testing.T) { path := filepath.Join(t.TempDir(), "agent.json") os.WriteFile(path, []byte(`{"server":"https://x.org","token":"lc_x"}`), 0o600) cfg, err := Load(path) if err != nil { t.Fatal(err) } if cfg.Direction != Both || cfg.Keep != 50 || cfg.Project != "Zwischenablage" { t.Fatalf("defaults missing: %+v", cfg) } } func TestMissingFileExplainsSetup(t *testing.T) { _, err := Load(filepath.Join(t.TempDir(), "nope.json")) if err == nil || !strings.Contains(err.Error(), "lc-agent setup") { t.Fatalf("unexpected error: %v", err) } } func TestValidate(t *testing.T) { cases := map[string]func(*Config){ "http to remote": func(c *Config) { c.Server = "http://clip.example.org" }, "no host": func(c *Config) { c.Server = "https://" }, "bad token": func(c *Config) { c.Token = "secret" }, "bad direction": func(c *Config) { c.Direction = "sideways" }, "keep zero": func(c *Config) { c.Keep = 0 }, "poll too fast": func(c *Config) { c.PollMs = 10 }, "bad exclude": func(c *Config) { c.Exclude = []string{"("} }, } for name, mutate := range cases { cfg := valid() mutate(&cfg) if cfg.Validate() == nil { t.Errorf("%s: expected an error", name) } } local := valid() local.Server = "http://localhost:8000" if err := local.Validate(); err != nil { t.Errorf("localhost over http should be allowed: %v", err) } } func TestDirection(t *testing.T) { if !Both.Sends() || !Both.Receives() || Send.Receives() || Receive.Sends() { t.Fatal("direction helpers wrong") } }