Skip to content

Commit d81e9a5

Browse files
authored
feat: [add tests, debug logs and progress bar]
feat: [add tests, debug logs and progress bar]
2 parents 7bf091e + 1134f46 commit d81e9a5

File tree

9 files changed

+1577
-74
lines changed

9 files changed

+1577
-74
lines changed

internal/app/app_test.go

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package app
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"faceit-cli/internal/config"
8+
"faceit-cli/internal/logger"
9+
)
10+
11+
// createTestLogger creates a test logger
12+
func createTestLogger() *logger.Logger {
13+
config := logger.Config{
14+
Level: logger.LogLevelInfo,
15+
KafkaEnabled: false,
16+
ServiceName: "test-service",
17+
ProductionMode: false,
18+
LogToStdout: false, // Disable stdout for tests
19+
}
20+
logger, _ := logger.New(config)
21+
return logger
22+
}
23+
24+
func TestNewApp(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
config *config.Config
28+
logger *logger.Logger
29+
wantErr bool
30+
}{
31+
{
32+
name: "valid config with cache disabled",
33+
config: &config.Config{
34+
FaceitAPIKey: "test-api-key",
35+
CacheEnabled: false,
36+
},
37+
logger: createTestLogger(),
38+
wantErr: false,
39+
},
40+
{
41+
name: "valid config with cache enabled",
42+
config: &config.Config{
43+
FaceitAPIKey: "test-api-key",
44+
CacheEnabled: true,
45+
CacheTTL: 30,
46+
},
47+
logger: createTestLogger(),
48+
wantErr: false,
49+
},
50+
{
51+
name: "nil config",
52+
config: nil,
53+
logger: createTestLogger(),
54+
wantErr: true,
55+
},
56+
}
57+
58+
for _, tt := range tests {
59+
t.Run(tt.name, func(t *testing.T) {
60+
defer func() {
61+
if r := recover(); r != nil && !tt.wantErr {
62+
t.Errorf("NewApp() panicked unexpectedly: %v", r)
63+
}
64+
}()
65+
66+
app := NewApp(tt.config, tt.logger)
67+
68+
if tt.wantErr {
69+
if app != nil {
70+
t.Errorf("NewApp() should have failed but returned app: %v", app)
71+
}
72+
return
73+
}
74+
75+
if app == nil {
76+
t.Errorf("NewApp() returned nil app")
77+
return
78+
}
79+
80+
if app.config != tt.config {
81+
t.Errorf("NewApp() config = %v, want %v", app.config, tt.config)
82+
}
83+
84+
if app.logger != tt.logger {
85+
t.Errorf("NewApp() logger = %v, want %v", app.logger, tt.logger)
86+
}
87+
88+
if app.repo == nil {
89+
t.Errorf("NewApp() repo is nil")
90+
}
91+
92+
// Test repository type based on cache setting
93+
if tt.config != nil && tt.config.CacheEnabled {
94+
// With cache enabled, we expect a cached repository
95+
// We can't easily test the exact type without exposing internal types
96+
// So we just verify the repository is not nil
97+
if app.repo == nil {
98+
t.Errorf("NewApp() with cache enabled should return a repository")
99+
}
100+
} else if tt.config != nil {
101+
// Without cache, we expect a direct repository
102+
if app.repo == nil {
103+
t.Errorf("NewApp() with cache disabled should return a repository")
104+
}
105+
}
106+
})
107+
}
108+
}
109+
110+
func TestApp_Run(t *testing.T) {
111+
// This test is limited because we can't easily test the TUI without mocking
112+
// We'll test the initialization and error handling parts
113+
114+
config := &config.Config{
115+
FaceitAPIKey: "test-api-key",
116+
CacheEnabled: false,
117+
}
118+
appLogger := createTestLogger()
119+
120+
app := NewApp(config, appLogger)
121+
122+
// Test with a cancelled context to avoid hanging
123+
ctx, cancel := context.WithCancel(context.Background())
124+
cancel() // Cancel immediately
125+
126+
err := app.Run(ctx)
127+
128+
// The error should be related to context cancellation or TUI initialization
129+
// We can't easily test the full TUI flow without complex mocking
130+
if err == nil {
131+
t.Log("Run() completed without error (this might be expected in test environment)")
132+
} else {
133+
t.Logf("Run() returned error (expected in test environment): %v", err)
134+
}
135+
}
136+
137+
func TestApp_Fields(t *testing.T) {
138+
config := &config.Config{
139+
FaceitAPIKey: "test-api-key",
140+
CacheEnabled: true,
141+
CacheTTL: 60,
142+
}
143+
appLogger := createTestLogger()
144+
145+
app := NewApp(config, appLogger)
146+
147+
// Test that all fields are properly set
148+
if app.config == nil {
149+
t.Error("App.config is nil")
150+
}
151+
152+
if app.repo == nil {
153+
t.Error("App.repo is nil")
154+
}
155+
156+
if app.logger == nil {
157+
t.Error("App.logger is nil")
158+
}
159+
160+
// Test config values
161+
if app.config.FaceitAPIKey != "test-api-key" {
162+
t.Errorf("App.config.FaceitAPIKey = %s, want test-api-key", app.config.FaceitAPIKey)
163+
}
164+
165+
if !app.config.CacheEnabled {
166+
t.Error("App.config.CacheEnabled should be true")
167+
}
168+
169+
if app.config.CacheTTL != 60 {
170+
t.Errorf("App.config.CacheTTL = %d, want 60", app.config.CacheTTL)
171+
}
172+
}
173+
174+
// Benchmark tests
175+
func BenchmarkNewApp(b *testing.B) {
176+
config := &config.Config{
177+
FaceitAPIKey: "test-api-key",
178+
CacheEnabled: true,
179+
CacheTTL: 30,
180+
}
181+
appLogger := createTestLogger()
182+
183+
b.ResetTimer()
184+
for i := 0; i < b.N; i++ {
185+
_ = NewApp(config, appLogger)
186+
}
187+
}
188+
189+
func BenchmarkNewApp_NoCache(b *testing.B) {
190+
config := &config.Config{
191+
FaceitAPIKey: "test-api-key",
192+
CacheEnabled: false,
193+
}
194+
appLogger := createTestLogger()
195+
196+
b.ResetTimer()
197+
for i := 0; i < b.N; i++ {
198+
_ = NewApp(config, appLogger)
199+
}
200+
}

0 commit comments

Comments
 (0)