aboutsummaryrefslogtreecommitdiff
path: root/internal/config/config_test.go
blob: 616f0cd856d1bc4c993f4710e07abd7ea797a08d (plain) (blame)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package config

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

func TestLoadSeeds(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("XDG_CONFIG_HOME", dir)
	cfg, err := Load()
	if err != nil {
		t.Fatal(err)
	}
	if cfg.DefaultVersion != "pl" || cfg.Offline {
		t.Errorf("defaults wrong: %+v", cfg)
	}
	if cfg.Lectionary != "new" {
		t.Errorf("lectionary default wrong: %+v", cfg)
	}
	if _, err := os.Stat(filepath.Join(dir, "lectio", "config.toml")); err != nil {
		t.Error("config not seeded")
	}
}

func TestLoadOverride(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("XDG_CONFIG_HOME", dir)
	os.MkdirAll(filepath.Join(dir, "lectio"), 0o755)
	os.WriteFile(filepath.Join(dir, "lectio", "config.toml"),
		[]byte("offline = true\ndefault_version = \"wuj\"\n"), 0o644)
	cfg, _ := Load()
	if !cfg.Offline || cfg.DefaultVersion != "wuj" {
		t.Errorf("override not applied: %+v", cfg)
	}
}

func TestWebDefaults(t *testing.T) {
	def := Default()
	if def.WebMono {
		t.Errorf("WebMono default should be false")
	}
	if len(def.WebVersions) != 0 {
		t.Errorf("WebVersions default should be empty, got %v", def.WebVersions)
	}
	if def.WebTheme != "transfiguration" {
		t.Errorf("WebTheme default wrong: got %q, want %q", def.WebTheme, "transfiguration")
	}
	if def.WebPort != 0 {
		t.Errorf("WebPort default wrong: got %d, want 0", def.WebPort)
	}
}

func TestWebLoadsFromSeed(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("XDG_CONFIG_HOME", dir)
	cfg, err := Load()
	if err != nil {
		t.Fatal(err)
	}
	if cfg.WebTheme != "transfiguration" {
		t.Errorf("WebTheme from seed wrong: got %q, want %q", cfg.WebTheme, "transfiguration")
	}
	if cfg.WebPort != 0 {
		t.Errorf("WebPort from seed wrong: got %d, want 0", cfg.WebPort)
	}
	if cfg.WebDisplay != "horizontal" {
		t.Errorf("WebDisplay from seed wrong: got %q, want %q", cfg.WebDisplay, "horizontal")
	}
}

func TestWebDisplayDefault(t *testing.T) {
	def := Default()
	if def.WebDisplay != "horizontal" {
		t.Errorf("WebDisplay default wrong: got %q, want %q", def.WebDisplay, "horizontal")
	}
}

func TestWebDisplayLoadsSetting(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("XDG_CONFIG_HOME", dir)
	os.MkdirAll(filepath.Join(dir, "lectio"), 0o755)
	os.WriteFile(filepath.Join(dir, "lectio", "config.toml"),
		[]byte("web_display = \"vertical\"\n"), 0o644)
	cfg, err := Load()
	if err != nil {
		t.Fatal(err)
	}
	if cfg.WebDisplay != "vertical" {
		t.Errorf("WebDisplay = %q, want %q", cfg.WebDisplay, "vertical")
	}
}

func TestWebDisplayNormalizesUnknown(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("XDG_CONFIG_HOME", dir)
	os.MkdirAll(filepath.Join(dir, "lectio"), 0o755)
	os.WriteFile(filepath.Join(dir, "lectio", "config.toml"),
		[]byte("web_display = \"bogus\"\n"), 0o644)
	cfg, err := Load()
	if err != nil {
		t.Fatal(err)
	}
	if cfg.WebDisplay != "horizontal" {
		t.Errorf("WebDisplay = %q, want %q (normalized from bogus)", cfg.WebDisplay, "horizontal")
	}
}

func TestUILanguageDefault(t *testing.T) {
	def := Default()
	if def.UILanguage != "en" {
		t.Errorf("UILanguage default wrong: got %q, want %q", def.UILanguage, "en")
	}
}

func TestUILanguageLoadsPL(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("XDG_CONFIG_HOME", dir)
	os.MkdirAll(filepath.Join(dir, "lectio"), 0o755)
	os.WriteFile(filepath.Join(dir, "lectio", "config.toml"),
		[]byte("ui_language = \"pl\"\n"), 0o644)
	cfg, err := Load()
	if err != nil {
		t.Fatal(err)
	}
	if cfg.UILanguage != "pl" {
		t.Errorf("UILanguage = %q, want %q", cfg.UILanguage, "pl")
	}
}

func TestUILanguageNormalizesUnknown(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("XDG_CONFIG_HOME", dir)
	os.MkdirAll(filepath.Join(dir, "lectio"), 0o755)
	os.WriteFile(filepath.Join(dir, "lectio", "config.toml"),
		[]byte("ui_language = \"bogus\"\n"), 0o644)
	cfg, err := Load()
	if err != nil {
		t.Fatal(err)
	}
	if cfg.UILanguage != "en" {
		t.Errorf("UILanguage = %q, want %q (normalized from bogus)", cfg.UILanguage, "en")
	}
}

func TestPartShown(t *testing.T) {
	var empty Config
	if !empty.PartShown("new", "psalm") {
		t.Error("nil Parts: psalm should be shown")
	}
	if !empty.PartShown("new", "ewangelia") {
		t.Error("nil Parts: ewangelia should be shown")
	}

	hidden := Config{Parts: map[string]map[string]bool{
		"new": {"psalm": false},
	}}
	if hidden.PartShown("new", "psalm") {
		t.Error("psalm explicitly false should be hidden")
	}
	if !hidden.PartShown("new", "ewangelia") {
		t.Error("ewangelia not mentioned should still be shown")
	}
}