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
|
package web
import (
"database/sql"
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"strings"
"testing"
"time"
"github.com/martinlehoux/biking_home/config"
"github.com/martinlehoux/biking_home/rides"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newWebTestServer(t *testing.T) (*Server, *sql.DB) {
t.Helper()
db, err := sql.Open("sqlite3", ":memory:")
require.NoError(t, err)
t.Cleanup(func() { db.Close() })
_, err = db.Exec(`
create table rides (
id integer primary key,
external_id text unique not null,
gpx_path text not null,
name text not null,
type text not null,
start_date text not null,
distance_m real not null,
moving_time_s integer not null,
elapsed_time_s integer not null,
total_elevation_gain_m real not null,
average_speed_mps real not null,
created_at text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
updated_at text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now'))
)
`)
require.NoError(t, err)
configPath := filepath.Join(t.TempDir(), "config.yaml")
appConfig := config.Default()
appConfig.Strava.ClientID = "123"
appConfig.Strava.ClientSecret = "secret"
require.NoError(t, config.Save(configPath, appConfig))
return NewServer(db, configPath), db
}
func TestHandlerRendersRidesPage(t *testing.T) {
server, db := newWebTestServer(t)
require.NoError(t, rides.Save(db, rides.Ride{
ExternalID: "strava:1234",
GPXPath: "missing.gpx",
Name: "Long Ride",
Type: "Ride",
StartDate: time.Date(2026, 8, 1, 7, 0, 0, 0, time.UTC),
DistanceM: 20_000,
}))
require.NoError(t, rides.Save(db, rides.Ride{
ExternalID: "strava:5678",
GPXPath: "short.gpx",
Name: "Short Ride",
Type: "Ride",
StartDate: time.Date(2026, 8, 2, 7, 0, 0, 0, time.UTC),
DistanceM: 9_999,
}))
req := httptest.NewRequest(http.MethodGet, "/", nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, req)
assert.Equal(t, http.StatusOK, response.Code)
assert.Contains(t, response.Body.String(), "All rides")
assert.Contains(t, response.Body.String(), "Long Ride")
assert.NotContains(t, response.Body.String(), "Short Ride")
assert.Contains(t, response.Body.String(), "Cotacol")
assert.Contains(t, response.Body.String(), "Cotacol / 100 km")
}
func TestSyncPageRequestsAuthorization(t *testing.T) {
server, _ := newWebTestServer(t)
req := httptest.NewRequest(http.MethodGet, "/sync", nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, req)
assert.Equal(t, http.StatusOK, response.Code)
assert.Contains(t, response.Body.String(), "Authorize Strava")
}
func TestSyncRedirectsToOAuthWhenUnauthenticated(t *testing.T) {
server, _ := newWebTestServer(t)
form := url.Values{"from": {"2026-08-01"}, "to": {"2026-08-04"}}
req := httptest.NewRequest(http.MethodPost, "/sync", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, req)
assert.Equal(t, http.StatusFound, response.Code)
location, err := url.Parse(response.Header().Get("Location"))
require.NoError(t, err)
assert.Equal(t, "/strava/login", location.Path)
assert.Equal(t, "/sync?from=2026-08-01&to=2026-08-04", location.Query().Get("return_to"))
}
func TestStravaLoginRedirectsToAuthorize(t *testing.T) {
server, _ := newWebTestServer(t)
req := httptest.NewRequest(http.MethodGet, "/strava/login?return_to=/sync", nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, req)
assert.Equal(t, http.StatusFound, response.Code)
location, err := url.Parse(response.Header().Get("Location"))
require.NoError(t, err)
assert.Equal(t, "www.strava.com", location.Host)
assert.Equal(t, "/oauth/authorize", location.Path)
assert.Equal(t, "activity:read_all", location.Query().Get("scope"))
assert.Equal(t, "http://localhost:8080/strava/callback", location.Query().Get("redirect_uri"))
assert.NotEmpty(t, location.Query().Get("state"))
}
func TestParseDateRangeMakesEndInclusive(t *testing.T) {
from, to, err := parseDateRange("2026-08-01", "2026-08-04")
require.NoError(t, err)
assert.Equal(t, "2026-08-01T00:00:00Z", from.Format("2006-01-02T15:04:05Z07:00"))
assert.Equal(t, "2026-08-05T00:00:00Z", to.Format("2006-01-02T15:04:05Z07:00"))
}
func TestFormatCotacolPer100Km(t *testing.T) {
assert.Equal(t, "20.0", formatCotacolPer100Km(2, 10_000))
assert.Equal(t, "-", formatCotacolPer100Km(2, 0))
}
func TestParseRideSort(t *testing.T) {
tests := []struct {
name string
query string
column string
descending bool
}{
{name: "default", query: "", column: rideSortStarted, descending: true},
{name: "ascending distance", query: "sort=distance&dir=asc", column: rideSortDistance, descending: false},
{name: "invalid values", query: "sort=unknown&dir=sideways", column: rideSortStarted, descending: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
query, err := url.ParseQuery(tt.query)
require.NoError(t, err)
got := parseRideSort(query)
assert.Equal(t, tt.column, got.Column)
assert.Equal(t, tt.descending, got.Descending)
})
}
}
func TestRideSortHeadersToggleDirection(t *testing.T) {
headers := rideSortHeaders(RideSort{Column: rideSortDistance, Descending: true})
require.Len(t, headers, 7)
assert.Equal(t, "/?dir=asc&sort=distance", headers[2].URL)
assert.Equal(t, "descending", headers[2].AriaSort)
assert.Contains(t, headers[2].Class, "active")
assert.Equal(t, "/?dir=asc&sort=name", headers[0].URL)
headers = rideSortHeaders(RideSort{Column: rideSortDistance, Descending: false})
assert.Equal(t, "/?dir=desc&sort=distance", headers[2].URL)
assert.Equal(t, "ascending", headers[2].AriaSort)
}
func TestSortRideViewsByCotacolKeepsMissingLast(t *testing.T) {
items := []RideView{
{cotacolScore: 4, cotacolReady: true},
{cotacolScore: 1, cotacolReady: true},
{cotacolReady: false},
}
sortRideViews(items, RideSort{Column: rideSortCotacol})
assert.Equal(t, 1.0, items[0].cotacolScore)
assert.Equal(t, 4.0, items[1].cotacolScore)
assert.False(t, items[2].cotacolReady)
sortRideViews(items, RideSort{Column: rideSortCotacol, Descending: true})
assert.Equal(t, 4.0, items[0].cotacolScore)
assert.Equal(t, 1.0, items[1].cotacolScore)
assert.False(t, items[2].cotacolReady)
}
func TestHandlerSortsRidesByDistance(t *testing.T) {
server, db := newWebTestServer(t)
require.NoError(t, rides.Save(db, rides.Ride{
ExternalID: "strava:550e8400-e29b-41d4-a716-446655440000",
GPXPath: "near.gpx",
Name: "Near Ride",
Type: "Ride",
StartDate: time.Date(2026, 8, 1, 7, 0, 0, 0, time.UTC),
DistanceM: 20_000,
}))
require.NoError(t, rides.Save(db, rides.Ride{
ExternalID: "strava:6ba7b810-9dad-41d1-80b4-00c04fd430c8",
GPXPath: "far.gpx",
Name: "Far Ride",
Type: "Ride",
StartDate: time.Date(2026, 8, 2, 7, 0, 0, 0, time.UTC),
DistanceM: 40_000,
}))
req := httptest.NewRequest(http.MethodGet, "/?sort=distance&dir=asc", nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, req)
body := response.Body.String()
assert.Equal(t, http.StatusOK, response.Code)
assert.Less(t, strings.Index(body, "Near Ride"), strings.Index(body, "Far Ride"))
assert.Contains(t, body, "dir=desc&sort=distance")
}
|