▍ humdrum codex / sportsball v0.1.0
license AGPL-3.0
1.1 KB raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package config

import (
	"os"
	"path/filepath"
	"testing"
)

func TestLoadMissingReturnsDefaults(t *testing.T) {
	t.Setenv("XDG_CONFIG_HOME", t.TempDir())
	c := Load()
	if len(c.Favorites) != 0 || c.Theme != "" {
		t.Errorf("missing config not default: %+v", c)
	}
}

func TestSaveLoadRoundTrip(t *testing.T) {
	t.Setenv("XDG_CONFIG_HOME", t.TempDir())
	want := Config{
		Favorites: []FavTeam{{League: "mlb", ID: "22", Abbr: "PHI", Name: "Phillies"}},
		Theme:     "uchu",
	}
	if err := Save(want); err != nil {
		t.Fatal(err)
	}
	got := Load()
	if len(got.Favorites) != 1 || got.Favorites[0].ID != "22" || got.Theme != "uchu" {
		t.Errorf("round-trip mismatch: %+v", got)
	}
}

func TestLoadCorruptReturnsDefaults(t *testing.T) {
	tmp := t.TempDir()
	t.Setenv("XDG_CONFIG_HOME", tmp)
	dir := filepath.Join(tmp, "sportsball")
	if err := os.MkdirAll(dir, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(dir, "config.json"), []byte("{not json"), 0o644); err != nil {
		t.Fatal(err)
	}
	if c := Load(); len(c.Favorites) != 0 {
		t.Errorf("corrupt config not default: %+v", c)
	}
}