package config
import (
"harness/internal/llm"
"os"
"path/filepath"
"strings"
"testing"
)
// isolate points the config at a temp directory, so a test never reads or
// writes the developer's real ~/.config/harness.
func isolate(t *testing.T) string {
t.Helper()
dir := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", dir)
return filepath.Join(dir, "harness", Filename)
}
func TestLoadReportsMissingWithoutError(t *testing.T) {
isolate(t)
cfg, found, err := Load()
if err != nil {
t.Fatalf("a missing config is the first-run signal, not an error: %v", err)
}
if found {
t.Error("reported a config that does not exist")
}
if cfg != (Config{}) {
t.Errorf("a missing config should come back zeroed, got %+v", cfg)
}
}
func TestSaveThenLoadRoundTrips(t *testing.T) {
path := isolate(t)
want := Config{Model: "claude-sonnet-5", Effort: "high", Approvals: ApprovalsAcceptAll, Theme: "iris"}
if err := Save(want); err != nil {
t.Fatalf("Save: %v", err)
}
if _, err := os.Stat(path); err != nil {
t.Fatalf("Save did not write %s: %v", path, err)
}
got, found, err := Load()
if err != nil {
t.Fatalf("Load: %v", err)
}
if !found {
t.Fatal("Load did not find the config Save just wrote")
}
want.Version = Version // stamped on write
if got != want {
t.Errorf("round trip changed the config:\n got %+v\nwant %+v", got, want)
}
}
// A file we cannot parse must not be silently replaced: the user's settings
// are still in there, and the next Save would destroy them without the error
// ever being shown.
func TestLoadRejectsMalformedWithoutDiscardingIt(t *testing.T) {
path := isolate(t)
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte("{not json"), 0o644); err != nil {
t.Fatal(err)
}
cfg, found, err := Load()
if err == nil {
t.Fatal("a malformed config loaded cleanly")
}
if found {
t.Error("a malformed config was reported as usable")
}
if cfg != (Config{}) {
t.Errorf("a malformed config produced values: %+v", cfg)
}
if b, _ := os.ReadFile(path); string(b) != "{not json" {
t.Errorf("Load rewrote the file it could not parse: %q", b)
}
if !strings.Contains(err.Error(), path) {
t.Errorf("the error does not say which file to fix: %v", err)
}
}
func TestSaveCreatesTheDirectory(t *testing.T) {
path := isolate(t)
if err := Save(Config{Model: "claude-opus-5"}); err != nil {
t.Fatalf("Save into a directory that does not exist yet: %v", err)
}
if _, err := os.Stat(path); err != nil {
t.Fatal(err)
}
}
// Unknown values are how a config written by a newer build, or edited by
// hand, reaches an older one. They are dropped rather than passed through to
// the API, which would fail the request with something unhelpful.
func TestLoadDropsValuesItDoesNotRecognise(t *testing.T) {
path := isolate(t)
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
body := `{"version":1,"model":"claude-opus-5","effort":"extreme","approvals":"sometimes","theme":"neon"}`
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
cfg, found, err := Load()
if err != nil || !found {
t.Fatalf("Load(found=%v): %v", found, err)
}
if cfg.Model != "claude-opus-5" {
t.Errorf("a recognised field was dropped: %q", cfg.Model)
}
if cfg.Effort != "" || cfg.Approvals != "" || cfg.Theme != "" {
t.Errorf("unrecognised values survived: %+v", cfg)
}
}
// A level the API would reject must not survive a round trip, however it got
// into the file.
func TestLoadDropsAnEffortTheAPIWouldReject(t *testing.T) {
for _, level := range llm.EffortLevels {
if err := Save(Config{Effort: level}); err != nil {
t.Fatal(err)
}
got, _, err := Load()
if err != nil {
t.Fatal(err)
}
if got.Effort != level {
t.Errorf("%q did not survive a round trip: got %q", level, got.Effort)
}
}
}
// The config is only ever a default: a flag says what this run should do, and
// it has to win.
func TestFlagsOverrideTheConfig(t *testing.T) {
cfg := Config{Model: "claude-sonnet-5", Effort: "low"}
if got := Or("claude-opus-5", cfg.Model); got != "claude-opus-5" {
t.Errorf("the flag lost to the config: %q", got)
}
if got := Or("", cfg.Model); got != "claude-sonnet-5" {
t.Errorf("an unset flag did not fall through to the config: %q", got)
}
if got := Or("", ""); got != "" {
t.Errorf("two empties produced %q", got)
}
}