Skip to content

Commit ac0c0d6

Browse files
committed
test(chaintracks): add comprehensive unit tests for ChainManager and Client
Implemented unit tests for ChainManager methods including IsValidRootForHeight and CurrentHeight. Added tests for Client methods such as GetTip, GetHeight, GetHeaderByHeight, GetHeaderByHash, GetNetwork, and IsValidRootForHeight. These tests ensure correct functionality and error handling for various scenarios, improving overall code reliability and maintainability.
1 parent c93139f commit ac0c0d6

File tree

5 files changed

+1694
-0
lines changed

5 files changed

+1694
-0
lines changed

cmd/server/config_test.go

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestLoadConfig(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
setupEnv func() func()
16+
expectedPort int
17+
expectedNet string
18+
expectedPath string
19+
expectedBootst string
20+
}{
21+
{
22+
name: "LoadsDefaultValues",
23+
setupEnv: func() func() {
24+
// Clear all environment variables
25+
oldPort := os.Getenv("PORT")
26+
oldChain := os.Getenv("CHAIN")
27+
oldStorage := os.Getenv("STORAGE_PATH")
28+
oldBootstrap := os.Getenv("BOOTSTRAP_URL")
29+
30+
_ = os.Unsetenv("PORT")
31+
_ = os.Unsetenv("CHAIN")
32+
_ = os.Unsetenv("STORAGE_PATH")
33+
_ = os.Unsetenv("BOOTSTRAP_URL")
34+
35+
return func() {
36+
_ = os.Setenv("PORT", oldPort)
37+
_ = os.Setenv("CHAIN", oldChain)
38+
_ = os.Setenv("STORAGE_PATH", oldStorage)
39+
_ = os.Setenv("BOOTSTRAP_URL", oldBootstrap)
40+
}
41+
},
42+
expectedPort: 3011,
43+
expectedNet: "main",
44+
expectedPath: getDefaultStoragePath(),
45+
expectedBootst: "",
46+
},
47+
{
48+
name: "LoadsPortFromEnvironment",
49+
setupEnv: func() func() {
50+
oldPort := os.Getenv("PORT")
51+
oldChain := os.Getenv("CHAIN")
52+
oldStorage := os.Getenv("STORAGE_PATH")
53+
oldBootstrap := os.Getenv("BOOTSTRAP_URL")
54+
55+
_ = os.Setenv("PORT", "8080")
56+
_ = os.Unsetenv("CHAIN")
57+
_ = os.Unsetenv("STORAGE_PATH")
58+
_ = os.Unsetenv("BOOTSTRAP_URL")
59+
60+
return func() {
61+
_ = os.Setenv("PORT", oldPort)
62+
_ = os.Setenv("CHAIN", oldChain)
63+
_ = os.Setenv("STORAGE_PATH", oldStorage)
64+
_ = os.Setenv("BOOTSTRAP_URL", oldBootstrap)
65+
}
66+
},
67+
expectedPort: 8080,
68+
expectedNet: "main",
69+
expectedPath: getDefaultStoragePath(),
70+
expectedBootst: "",
71+
},
72+
{
73+
name: "LoadsNetworkFromEnvironment",
74+
setupEnv: func() func() {
75+
oldPort := os.Getenv("PORT")
76+
oldChain := os.Getenv("CHAIN")
77+
oldStorage := os.Getenv("STORAGE_PATH")
78+
oldBootstrap := os.Getenv("BOOTSTRAP_URL")
79+
80+
_ = os.Unsetenv("PORT")
81+
_ = os.Setenv("CHAIN", "testnet")
82+
_ = os.Unsetenv("STORAGE_PATH")
83+
_ = os.Unsetenv("BOOTSTRAP_URL")
84+
85+
return func() {
86+
_ = os.Setenv("PORT", oldPort)
87+
_ = os.Setenv("CHAIN", oldChain)
88+
_ = os.Setenv("STORAGE_PATH", oldStorage)
89+
_ = os.Setenv("BOOTSTRAP_URL", oldBootstrap)
90+
}
91+
},
92+
expectedPort: 3011,
93+
expectedNet: "testnet",
94+
expectedPath: getDefaultStoragePath(),
95+
expectedBootst: "",
96+
},
97+
{
98+
name: "LoadsStoragePathFromEnvironment",
99+
setupEnv: func() func() {
100+
oldPort := os.Getenv("PORT")
101+
oldChain := os.Getenv("CHAIN")
102+
oldStorage := os.Getenv("STORAGE_PATH")
103+
oldBootstrap := os.Getenv("BOOTSTRAP_URL")
104+
105+
_ = os.Unsetenv("PORT")
106+
_ = os.Unsetenv("CHAIN")
107+
_ = os.Setenv("STORAGE_PATH", "/custom/path")
108+
_ = os.Unsetenv("BOOTSTRAP_URL")
109+
110+
return func() {
111+
_ = os.Setenv("PORT", oldPort)
112+
_ = os.Setenv("CHAIN", oldChain)
113+
_ = os.Setenv("STORAGE_PATH", oldStorage)
114+
_ = os.Setenv("BOOTSTRAP_URL", oldBootstrap)
115+
}
116+
},
117+
expectedPort: 3011,
118+
expectedNet: "main",
119+
expectedPath: "/custom/path",
120+
expectedBootst: "",
121+
},
122+
{
123+
name: "LoadsBootstrapURLFromEnvironment",
124+
setupEnv: func() func() {
125+
oldPort := os.Getenv("PORT")
126+
oldChain := os.Getenv("CHAIN")
127+
oldStorage := os.Getenv("STORAGE_PATH")
128+
oldBootstrap := os.Getenv("BOOTSTRAP_URL")
129+
130+
_ = os.Unsetenv("PORT")
131+
_ = os.Unsetenv("CHAIN")
132+
_ = os.Unsetenv("STORAGE_PATH")
133+
_ = os.Setenv("BOOTSTRAP_URL", "http://example.com")
134+
135+
return func() {
136+
_ = os.Setenv("PORT", oldPort)
137+
_ = os.Setenv("CHAIN", oldChain)
138+
_ = os.Setenv("STORAGE_PATH", oldStorage)
139+
_ = os.Setenv("BOOTSTRAP_URL", oldBootstrap)
140+
}
141+
},
142+
expectedPort: 3011,
143+
expectedNet: "main",
144+
expectedPath: getDefaultStoragePath(),
145+
expectedBootst: "http://example.com",
146+
},
147+
{
148+
name: "LoadsAllValuesFromEnvironment",
149+
setupEnv: func() func() {
150+
oldPort := os.Getenv("PORT")
151+
oldChain := os.Getenv("CHAIN")
152+
oldStorage := os.Getenv("STORAGE_PATH")
153+
oldBootstrap := os.Getenv("BOOTSTRAP_URL")
154+
155+
_ = os.Setenv("PORT", "9999")
156+
_ = os.Setenv("CHAIN", "regtest")
157+
_ = os.Setenv("STORAGE_PATH", "/full/custom/path")
158+
_ = os.Setenv("BOOTSTRAP_URL", "http://bootstrap.example.com")
159+
160+
return func() {
161+
_ = os.Setenv("PORT", oldPort)
162+
_ = os.Setenv("CHAIN", oldChain)
163+
_ = os.Setenv("STORAGE_PATH", oldStorage)
164+
_ = os.Setenv("BOOTSTRAP_URL", oldBootstrap)
165+
}
166+
},
167+
expectedPort: 9999,
168+
expectedNet: "regtest",
169+
expectedPath: "/full/custom/path",
170+
expectedBootst: "http://bootstrap.example.com",
171+
},
172+
{
173+
name: "UsesDefaultPortWhenPortIsInvalid",
174+
setupEnv: func() func() {
175+
oldPort := os.Getenv("PORT")
176+
oldChain := os.Getenv("CHAIN")
177+
oldStorage := os.Getenv("STORAGE_PATH")
178+
oldBootstrap := os.Getenv("BOOTSTRAP_URL")
179+
180+
_ = os.Setenv("PORT", "invalid")
181+
_ = os.Unsetenv("CHAIN")
182+
_ = os.Unsetenv("STORAGE_PATH")
183+
_ = os.Unsetenv("BOOTSTRAP_URL")
184+
185+
return func() {
186+
_ = os.Setenv("PORT", oldPort)
187+
_ = os.Setenv("CHAIN", oldChain)
188+
_ = os.Setenv("STORAGE_PATH", oldStorage)
189+
_ = os.Setenv("BOOTSTRAP_URL", oldBootstrap)
190+
}
191+
},
192+
expectedPort: 3011,
193+
expectedNet: "main",
194+
expectedPath: getDefaultStoragePath(),
195+
expectedBootst: "",
196+
},
197+
{
198+
name: "UsesDefaultPortWhenPortIsEmpty",
199+
setupEnv: func() func() {
200+
oldPort := os.Getenv("PORT")
201+
oldChain := os.Getenv("CHAIN")
202+
oldStorage := os.Getenv("STORAGE_PATH")
203+
oldBootstrap := os.Getenv("BOOTSTRAP_URL")
204+
205+
_ = os.Setenv("PORT", "")
206+
_ = os.Unsetenv("CHAIN")
207+
_ = os.Unsetenv("STORAGE_PATH")
208+
_ = os.Unsetenv("BOOTSTRAP_URL")
209+
210+
return func() {
211+
_ = os.Setenv("PORT", oldPort)
212+
_ = os.Setenv("CHAIN", oldChain)
213+
_ = os.Setenv("STORAGE_PATH", oldStorage)
214+
_ = os.Setenv("BOOTSTRAP_URL", oldBootstrap)
215+
}
216+
},
217+
expectedPort: 3011,
218+
expectedNet: "main",
219+
expectedPath: getDefaultStoragePath(),
220+
expectedBootst: "",
221+
},
222+
}
223+
224+
for _, tt := range tests {
225+
t.Run(tt.name, func(t *testing.T) {
226+
cleanup := tt.setupEnv()
227+
defer cleanup()
228+
229+
config := LoadConfig()
230+
231+
require.NotNil(t, config)
232+
assert.Equal(t, tt.expectedPort, config.Port)
233+
assert.Equal(t, tt.expectedNet, config.Network)
234+
assert.Equal(t, tt.expectedPath, config.StoragePath)
235+
assert.Equal(t, tt.expectedBootst, config.BootstrapURL)
236+
})
237+
}
238+
}
239+
240+
func TestGetDefaultStoragePath(t *testing.T) {
241+
tests := []struct {
242+
name string
243+
expectedPath string
244+
}{
245+
{
246+
name: "ReturnsHomeDirectoryPlusChaintracks",
247+
expectedPath: func() string {
248+
home, err := os.UserHomeDir()
249+
if err != nil {
250+
return "./data/headers"
251+
}
252+
return filepath.Join(home, ".chaintracks")
253+
}(),
254+
},
255+
}
256+
257+
for _, tt := range tests {
258+
t.Run(tt.name, func(t *testing.T) {
259+
result := getDefaultStoragePath()
260+
assert.Equal(t, tt.expectedPath, result)
261+
})
262+
}
263+
}

0 commit comments

Comments
 (0)