Repository navigation
Expand file tree
/
Copy pathsettings_test.go
More file actions
175 lines (158 loc) · 5.63 KB
/
Copy pathsettings_test.go
File metadata and controls
175 lines (158 loc) · 5.63 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
package main
import (
"net/http/httptest"
"testing"
)
func TestReadUserSettingsDefaultsWithNoCookie(t *testing.T) {
r := httptest.NewRequest("GET", "/settings", nil)
settings := readUserSettings(r)
if settings.TierSource != "boris" {
t.Errorf("expected default tier source 'boris', got %q", settings.TierSource)
}
if settings.ActionFeedLimit != MAX_ACTIONS {
t.Errorf("expected default action feed limit %d, got %d", MAX_ACTIONS, settings.ActionFeedLimit)
}
if settings.OffseasonOverride != "auto" {
t.Errorf("expected default offseason override 'auto', got %q", settings.OffseasonOverride)
}
}
func TestWriteReadUserSettingsRoundTrip(t *testing.T) {
w := httptest.NewRecorder()
original := UserSettings{
ScoringOverrides: map[string]string{"league123": "Half PPR"},
TierSource: "both",
ActionCategories: map[string]bool{"trade": false},
ActionFeedLimit: 3,
OffseasonOverride: "force_on",
FavoriteLeagues: []string{"league123"},
HiddenLeagues: []string{"league456"},
}
writeUserSettings(w, original)
r := httptest.NewRequest("GET", "/settings", nil)
for _, c := range w.Result().Cookies() {
r.AddCookie(c)
}
got := readUserSettings(r)
if got.ScoringOverrides["league123"] != "Half PPR" {
t.Errorf("expected scoring override to round-trip, got %+v", got.ScoringOverrides)
}
if got.TierSource != "both" {
t.Errorf("expected tier source 'both', got %q", got.TierSource)
}
if got.ActionFeedLimit != 3 {
t.Errorf("expected action feed limit 3, got %d", got.ActionFeedLimit)
}
if got.OffseasonOverride != "force_on" {
t.Errorf("expected offseason override 'force_on', got %q", got.OffseasonOverride)
}
if !isLeagueFavorite("league123", got) {
t.Errorf("expected league123 to be a favorite")
}
if !isLeagueHidden("league456", got) {
t.Errorf("expected league456 to be hidden")
}
if isActionCategoryEnabled("trade", got) {
t.Errorf("expected 'trade' category to be disabled")
}
if !isActionCategoryEnabled("swap", got) {
t.Errorf("expected 'swap' category to default to enabled")
}
}
func TestResolveScoringFormatOverridesAutoDetection(t *testing.T) {
league := map[string]interface{}{
"scoring_settings": map[string]interface{}{"rec": 1.0}, // auto-detects as PPR
}
settings := UserSettings{ScoringOverrides: map[string]string{"lg1": "Standard"}}
if got := resolveScoringFormat(league, "lg1", settings); got != "Standard" {
t.Errorf("expected override 'Standard' to win, got %q", got)
}
if got := resolveScoringFormat(league, "lg-other", settings); got != "PPR" {
t.Errorf("expected auto-detection for a league with no override, got %q", got)
}
}
func TestResolveOffseasonModeOverride(t *testing.T) {
regularSeasonState := map[string]interface{}{"season_type": "regular", "week": 5.0}
if !resolveOffseasonMode(regularSeasonState, UserSettings{OffseasonOverride: "force_on"}) {
t.Errorf("expected force_on to report offseason even mid-regular-season")
}
if resolveOffseasonMode(regularSeasonState, UserSettings{OffseasonOverride: "force_off"}) {
t.Errorf("expected force_off to never report offseason")
}
if resolveOffseasonMode(regularSeasonState, UserSettings{OffseasonOverride: "auto"}) {
t.Errorf("expected auto to defer to isOffseasonMode (false for regular season)")
}
}
func TestApplyActionSettingsFiltersAndLimits(t *testing.T) {
actions := []Action{
{Category: "swap", Description: "a"},
{Category: "waiver", Description: "b"},
{Category: "bye", Description: "c"},
{Category: "injury", Description: "d"},
}
settings := UserSettings{
ActionCategories: map[string]bool{"waiver": false},
ActionFeedLimit: 2,
}
got := applyActionSettings(actions, settings)
if len(got) != 2 {
t.Fatalf("expected limit of 2 actions, got %d: %+v", len(got), got)
}
for _, a := range got {
if a.Category == "waiver" {
t.Errorf("expected waiver category to be filtered out, got %+v", got)
}
}
}
func TestApplyActionSettingsZeroLimitFallsBackToDefault(t *testing.T) {
actions := make([]Action, MAX_ACTIONS+3)
for i := range actions {
actions[i] = Action{Category: "swap", Description: "x"}
}
got := applyActionSettings(actions, UserSettings{})
if len(got) != MAX_ACTIONS {
t.Fatalf("expected fallback to MAX_ACTIONS (%d), got %d", MAX_ACTIONS, len(got))
}
}
func TestSaveSettingsFormPersistsPerLeagueFields(t *testing.T) {
form := httptest.NewRequest("POST", "/settings", nil)
form.Form = map[string][]string{
"tier_source": {"adp"},
"offseason_override": {"force_off"},
"action_feed_limit": {"4"},
"cat_bye": {"on"},
"cat_injury": {"on"},
"cat_swap": {"on"},
"cat_waiver": {"on"},
"cat_handcuff": {"on"},
// "cat_trade" intentionally omitted -- should end up disabled
"league_ids": {"lg1,lg2"},
"scoring_lg1": {"Standard"},
"favorite_lg1": {"on"},
"hidden_lg2": {"on"},
}
w := httptest.NewRecorder()
saveSettingsForm(w, form)
r2 := httptest.NewRequest("GET", "/settings", nil)
for _, c := range w.Result().Cookies() {
r2.AddCookie(c)
}
saved := readUserSettings(r2)
if saved.TierSource != "adp" {
t.Errorf("expected tier_source 'adp', got %q", saved.TierSource)
}
if saved.ScoringOverrides["lg1"] != "Standard" {
t.Errorf("expected lg1 scoring override 'Standard', got %+v", saved.ScoringOverrides)
}
if !isLeagueFavorite("lg1", saved) {
t.Errorf("expected lg1 to be favorited")
}
if !isLeagueHidden("lg2", saved) {
t.Errorf("expected lg2 to be hidden")
}
if isActionCategoryEnabled("trade", saved) {
t.Errorf("expected 'trade' to be disabled since its checkbox was omitted")
}
if !isActionCategoryEnabled("bye", saved) {
t.Errorf("expected 'bye' to stay enabled since its checkbox was present")
}
}