-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
218 lines (176 loc) · 5.16 KB
/
config_test.go
File metadata and controls
218 lines (176 loc) · 5.16 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2025 Matthew Gall <me@matthewgall.dev>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"os"
"path/filepath"
"testing"
)
func TestLoadConfig(t *testing.T) {
// Create temporary directory for test
tempDir := t.TempDir()
configFile := filepath.Join(tempDir, "test_config.yaml")
// Create test config file
configContent := `account_id: test-account-123
api_key: test-api-key-456
daemon: true
min_points: 100
check_interval_minutes: 5
web_ui: true
web_port: 9090
debug: true
`
err := os.WriteFile(configFile, []byte(configContent), 0644)
if err != nil {
t.Fatalf("Failed to write test config file: %v", err)
}
// Test loading config
config, err := LoadConfig(configFile)
if err != nil {
t.Errorf("Expected no error loading config, got %v", err)
}
if config.AccountID != "test-account-123" {
t.Errorf("Expected AccountID 'test-account-123', got %s", config.AccountID)
}
if config.APIKey != "test-api-key-456" {
t.Errorf("Expected APIKey 'test-api-key-456', got %s", config.APIKey)
}
if !config.Daemon {
t.Error("Expected Daemon to be true")
}
if config.MinPoints != 100 {
t.Errorf("Expected MinPoints 100, got %d", config.MinPoints)
}
if config.CheckInterval != 5 {
t.Errorf("Expected CheckInterval 5, got %d", config.CheckInterval)
}
if !config.WebUI {
t.Error("Expected WebUI to be true")
}
if config.WebPort != 9090 {
t.Errorf("Expected WebPort 9090, got %d", config.WebPort)
}
if !config.Debug {
t.Error("Expected Debug to be true")
}
}
func TestLoadConfigDefaults(t *testing.T) {
// Test loading with empty config path to get defaults
config, err := LoadConfig("")
if err != nil {
t.Errorf("Expected no error loading with empty config path, got %v", err)
}
// Check default values
if config.CheckInterval != 10 {
t.Errorf("Expected default CheckInterval 10, got %d", config.CheckInterval)
}
if config.WebUI != false {
t.Error("Expected default WebUI to be false")
}
if config.WebPort != 8080 {
t.Errorf("Expected default WebPort 8080, got %d", config.WebPort)
}
if config.Daemon != false {
t.Error("Expected default Daemon to be false")
}
if config.MinPoints != 0 {
t.Errorf("Expected default MinPoints 0, got %d", config.MinPoints)
}
if config.Debug != false {
t.Error("Expected default Debug to be false")
}
}
func TestLoadConfigInvalidYAML(t *testing.T) {
// Create temporary directory for test
tempDir := t.TempDir()
configFile := filepath.Join(tempDir, "invalid_config.yaml")
// Create invalid YAML file
invalidYAML := `account_id: test
api_key: [invalid: yaml: content
debug: true`
err := os.WriteFile(configFile, []byte(invalidYAML), 0644)
if err != nil {
t.Fatalf("Failed to write invalid config file: %v", err)
}
// Test loading invalid config
_, err = LoadConfig(configFile)
if err == nil {
t.Error("Expected error loading invalid YAML config, got nil")
}
}
func TestConfigStruct(t *testing.T) {
config := Config{
AccountID: "test-account",
APIKey: "test-key",
Daemon: true,
MinPoints: 50,
CheckInterval: 15,
WebUI: true,
WebPort: 4000,
Debug: true,
}
if config.AccountID != "test-account" {
t.Errorf("Expected AccountID 'test-account', got %s", config.AccountID)
}
if config.APIKey != "test-key" {
t.Errorf("Expected APIKey 'test-key', got %s", config.APIKey)
}
if !config.Daemon {
t.Error("Expected Daemon to be true")
}
if config.MinPoints != 50 {
t.Errorf("Expected MinPoints 50, got %d", config.MinPoints)
}
if config.CheckInterval != 15 {
t.Errorf("Expected CheckInterval 15, got %d", config.CheckInterval)
}
if !config.WebUI {
t.Error("Expected WebUI to be true")
}
if config.WebPort != 4000 {
t.Errorf("Expected WebPort 4000, got %d", config.WebPort)
}
if !config.Debug {
t.Error("Expected Debug to be true")
}
}
func TestConfigApplyDefaults(t *testing.T) {
config := Config{
AccountID: "test",
APIKey: "test",
CheckInterval: 0, // Should be set to default
WebPort: 0, // Should be set to default
}
config.ApplyDefaults()
if config.CheckInterval != 10 {
t.Errorf("Expected CheckInterval to default to 10, got %d", config.CheckInterval)
}
if config.WebPort != 8080 {
t.Errorf("Expected WebPort to default to 8080, got %d", config.WebPort)
}
// Test with valid values (should not change)
config2 := Config{
AccountID: "test",
APIKey: "test",
CheckInterval: 5,
WebPort: 3000,
}
config2.ApplyDefaults()
if config2.CheckInterval != 5 {
t.Errorf("Expected CheckInterval to remain 5, got %d", config2.CheckInterval)
}
if config2.WebPort != 3000 {
t.Errorf("Expected WebPort to remain 3000, got %d", config2.WebPort)
}
}