-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweekly_email_test.go
More file actions
201 lines (177 loc) · 5.49 KB
/
weekly_email_test.go
File metadata and controls
201 lines (177 loc) · 5.49 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
package main
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestBuildWeeklyEmailSummary(t *testing.T) {
page := &DashboardPage{
Username: "testuser",
TotalLeagues: 2,
DynastyCount: 1,
RedraftCount: 1,
LeagueSummaries: []LeagueSummary{
{
LeagueName: "Dynasty A",
Season: "2025",
Scoring: "PPR",
IsDynasty: true,
LeagueSize: 12,
Record: "8-5",
PlayoffStatus: "In Hunt",
TotalRosterValue: 8450,
ValueRank: 3,
ValueTrend: "↗ +5%",
DraftPicksSummary: "2026 1st, 2027 1st",
ActionCount: 2,
},
},
}
body := buildWeeklyEmailSummary(page)
if !strings.Contains(body, "SleeperPy Weekly Summary") {
t.Fatalf("missing summary header: %s", body)
}
if !strings.Contains(body, "User: testuser") {
t.Fatalf("missing user line: %s", body)
}
if !strings.Contains(body, "Dynasty A (2025)") {
t.Fatalf("missing league section: %s", body)
}
if !strings.Contains(body, "Immediate Actions: 2") {
t.Fatalf("missing action count: %s", body)
}
}
func TestDeriveSummaryRisks(t *testing.T) {
page := &DashboardPage{
LeagueSummaries: []LeagueSummary{
{LeagueName: "L1", PlayoffStatus: "Eliminated", ValueTrend: "↘ -3%", ActionCount: 5},
{LeagueName: "L2", PlayoffStatus: "Eliminated", ValueTrend: "↘ -2%", ActionCount: 4},
{LeagueName: "L3", PlayoffStatus: "Eliminated", ValueTrend: "↘ -1%", ActionCount: 6},
},
}
risks := deriveSummaryRisks(page)
if len(risks) != 5 {
t.Fatalf("expected capped risk list of 5, got %d (%v)", len(risks), risks)
}
}
func TestWeeklyEmailHandlerRequiresUser(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/weekly-email", nil)
rr := httptest.NewRecorder()
weeklyEmailHandler(rr, req)
if rr.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d", rr.Code)
}
if !strings.Contains(rr.Body.String(), "user is required") {
t.Fatalf("unexpected response body: %s", rr.Body.String())
}
}
func TestWeeklyEmailHandlerPreview(t *testing.T) {
origBuilder := buildDashboardPageForEmail
defer func() { buildDashboardPageForEmail = origBuilder }()
buildDashboardPageForEmail = func(username string) (*DashboardPage, error) {
return &DashboardPage{
Username: username,
TotalLeagues: 1,
DynastyCount: 1,
LeagueSummaries: []LeagueSummary{
{
LeagueName: "League",
Season: "2025",
Scoring: "PPR",
IsDynasty: true,
LeagueSize: 12,
ActionCount: 1,
},
},
}, nil
}
req := httptest.NewRequest(http.MethodGet, "/weekly-email?user=testuser", nil)
rr := httptest.NewRecorder()
weeklyEmailHandler(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", rr.Code)
}
if !strings.Contains(rr.Body.String(), "User: testuser") {
t.Fatalf("missing preview summary in response: %s", rr.Body.String())
}
}
func TestWeeklyEmailHandlerBuildFailure(t *testing.T) {
origBuilder := buildDashboardPageForEmail
defer func() { buildDashboardPageForEmail = origBuilder }()
buildDashboardPageForEmail = func(username string) (*DashboardPage, error) {
return nil, errors.New("upstream failed")
}
req := httptest.NewRequest(http.MethodGet, "/weekly-email?user=testuser", nil)
rr := httptest.NewRecorder()
weeklyEmailHandler(rr, req)
if rr.Code != http.StatusBadGateway {
t.Fatalf("expected 502, got %d", rr.Code)
}
if strings.Contains(rr.Body.String(), "upstream failed") {
t.Fatalf("should not leak raw upstream error: %s", rr.Body.String())
}
}
func TestWeeklyEmailHandlerSendRequiresRecipient(t *testing.T) {
origBuilder := buildDashboardPageForEmail
defer func() { buildDashboardPageForEmail = origBuilder }()
buildDashboardPageForEmail = func(username string) (*DashboardPage, error) {
return &DashboardPage{Username: username}, nil
}
req := httptest.NewRequest(http.MethodGet, "/weekly-email?user=testuser&send=1", nil)
rr := httptest.NewRecorder()
weeklyEmailHandler(rr, req)
if rr.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d", rr.Code)
}
}
func TestWeeklyEmailHandlerSendUsesEnvRecipient(t *testing.T) {
origBuilder := buildDashboardPageForEmail
origSender := sendWeeklyEmailFunc
defer func() {
buildDashboardPageForEmail = origBuilder
sendWeeklyEmailFunc = origSender
}()
buildDashboardPageForEmail = func(username string) (*DashboardPage, error) {
return &DashboardPage{
Username: username,
TotalLeagues: 1,
}, nil
}
called := false
sendWeeklyEmailFunc = func(to, subject, body string) error {
called = true
if to != "test@example.com" {
t.Fatalf("expected env recipient, got %q", to)
}
if !strings.Contains(subject, "testuser") {
t.Fatalf("expected username in subject, got %q", subject)
}
if !strings.Contains(body, "User: testuser") {
t.Fatalf("expected summary body, got %q", body)
}
return nil
}
t.Setenv("EMAIL_SUMMARY_TO", "test@example.com")
req := httptest.NewRequest(http.MethodGet, "/weekly-email?user=testuser&send=1", nil)
rr := httptest.NewRecorder()
weeklyEmailHandler(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", rr.Code)
}
if !called {
t.Fatal("expected sendWeeklyEmail to be called")
}
}
func TestSendWeeklyEmailRequiresSMTPConfig(t *testing.T) {
t.Setenv("SMTP_HOST", "")
t.Setenv("SMTP_PORT", "")
t.Setenv("SMTP_USER", "")
t.Setenv("SMTP_PASS", "")
t.Setenv("SMTP_FROM", "")
err := sendWeeklyEmail("to@example.com", "subject", "body")
if err == nil {
t.Fatal("expected missing smtp config error")
}
}