package web
import (
"bytes"
"database/sql"
"fmt"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/jftuga/geodist"
"github.com/martinlehoux/biking_home/config"
"github.com/martinlehoux/biking_home/mountain_pass"
"github.com/martinlehoux/biking_home/ride"
"github.com/martinlehoux/biking_home/rides"
"github.com/martinlehoux/biking_home/strava"
_ "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,
cotacol_score real,
cotacol_algo_version text,
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)
_, err = db.Exec(`
create table mountain_passes (
id integer primary key,
external_id text unique not null,
name text not null,
country_code text not null,
department_code text not null,
elevation integer not null,
latitude real,
longitude real
)
`)
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 testGPXPath(t *testing.T, name string) string {
t.Helper()
path := filepath.Join(t.TempDir(), name)
require.NoError(t, os.WriteFile(path, []byte(`100200`), 0o600))
return path
}
type fakeSyncClient struct {
activities []strava.Activity
results map[int64]fakeSyncResult
}
type fakeSyncResult struct {
activity strava.Activity
data []byte
err error
}
func (c fakeSyncClient) List(time.Time, time.Time) ([]strava.Activity, error) {
return c.activities, nil
}
func (c fakeSyncClient) Get(id int64) (strava.Activity, []byte, error) {
result, ok := c.results[id]
if !ok {
return strava.Activity{}, nil, fmt.Errorf("missing fake activity %d", id)
}
return result.activity, result.data, result.err
}
const emptyTrackGPX = ``
const validTrackGPX = `100200`
func TestHandlerRendersRidesPage(t *testing.T) {
server, db := newWebTestServer(t)
require.NoError(t, rides.Save(db, rides.Ride{
ExternalID: "strava:1234",
GPXPath: testGPXPath(t, "long.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: testGPXPath(t, "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")
assert.Contains(t, response.Body.String(), `href="/rides/1"`)
}
func TestHandlerRendersRideDetailWithEmbeddedRoute(t *testing.T) {
server, db := newWebTestServer(t)
require.NoError(t, rides.Save(db, rides.Ride{
ExternalID: "strava:550e8400-e29b-41d4-a716-446655440000",
GPXPath: testGPXPath(t, "detail.gpx"),
Name: "Detailed Ride",
Type: "Ride",
StartDate: time.Date(2026, 8, 1, 7, 0, 0, 0, time.UTC),
DistanceM: 20_000,
}))
item, found, err := rides.GetByExternalID(db, "strava:550e8400-e29b-41d4-a716-446655440000")
require.NoError(t, err)
require.True(t, found)
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/rides/%d", item.ID), nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, req)
body := response.Body.String()
assert.Equal(t, http.StatusOK, response.Code)
assert.Contains(t, body, "Detailed Ride")
assert.Contains(t, body, `id="ride-route"`)
assert.Contains(t, body, `id="ride-profile"`)
assert.Contains(t, body, `id="ride-profile-chart"`)
assert.Contains(t, body, `id="ride-map"`)
assert.Contains(t, body, ``)
assert.Contains(t, body, ``)
assert.NotContains(t, body, "pointermove")
staticRequest := httptest.NewRequest(http.MethodGet, "/static/ride-detail.js", nil)
staticResponse := httptest.NewRecorder()
server.Handler().ServeHTTP(staticResponse, staticRequest)
script := staticResponse.Body.String()
assert.Equal(t, http.StatusOK, staticResponse.Code)
assert.Equal(t, "text/javascript; charset=utf-8", staticResponse.Header().Get("Content-Type"))
assert.Contains(t, script, "pointermove")
assert.Contains(t, script, "circleMarker")
assert.Contains(t, script, "crossing.passElevationM")
assert.Contains(t, script, "const labelY = plot.top - 6")
assert.Contains(t, body, "leaflet@1.9.4/dist/leaflet.js")
assert.Contains(t, script, "tile.openstreetmap.org/{z}/{x}/{y}.png")
assert.Contains(t, body, `"type":"FeatureCollection"`)
assert.Contains(t, body, `"type":"LineString"`)
assert.Contains(t, body, `"coordinates":[[5,43]`)
}
func TestBuildRideProfileIncludesClimbsAndCrossings(t *testing.T) {
parsed := ride.FromColumns(
[]float64{0, 1000, 2000},
[]float64{300, 447, 380},
[]geodist.Coord{{Lat: 43.61, Lon: 5.42}, {Lat: 43.62, Lon: 5.43}, {Lat: 43.63, Lon: 5.44}},
make([]time.Time, 3),
)
passes := []mountain_pass.MountainPass{{
Name: "Pas de Magnan",
Elevation: 440,
Coord: &geodist.Coord{Lat: 43.62, Lon: 5.43},
}}
profile := buildRideProfile(parsed, passes)
require.Len(t, profile.Points, 3)
assert.Equal(t, 1.0, profile.Points[1].DistanceKm)
assert.Equal(t, 447.0, profile.Points[1].ElevationM)
require.Len(t, profile.Climbs, 1)
assert.Equal(t, "Pas de Magnan", profile.Climbs[0].Name)
assert.Equal(t, 1.0, profile.Climbs[0].TopKm)
require.Len(t, profile.Crossings, 1)
assert.Equal(t, "Pas de Magnan", profile.Crossings[0].Name)
assert.Equal(t, 1.0, profile.Crossings[0].DistanceKm)
}
func TestHandlerRendersRideDetailWithoutRoute(t *testing.T) {
server, db := newWebTestServer(t)
item := rides.Ride{
ExternalID: "strava:14701658670",
Name: "Indoor Ride",
Type: "VirtualRide",
StartDate: time.Date(2026, 8, 1, 7, 0, 0, 0, time.UTC),
DistanceM: 20_000,
}
require.NoError(t, rides.Save(db, item))
stored, found, err := rides.GetByExternalID(db, item.ExternalID)
require.NoError(t, err)
require.True(t, found)
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/rides/%d", stored.ID), nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, req)
body := response.Body.String()
assert.Equal(t, http.StatusOK, response.Code)
assert.Contains(t, body, "Indoor Ride")
assert.NotContains(t, body, "The recorded route is unavailable.")
assert.NotContains(t, body, `id="ride-map"`)
}
func TestHandlerReturnsNotFoundForUnknownRide(t *testing.T) {
server, _ := newWebTestServer(t)
req := httptest.NewRequest(http.MethodGet, "/rides/999999", nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, req)
assert.Equal(t, http.StatusNotFound, response.Code)
}
func TestHandlerShowsErrorForUnavailableRideRoute(t *testing.T) {
server, db := newWebTestServer(t)
_, err := db.Exec(`
INSERT INTO rides (external_id, gpx_path, name, type, start_date, distance_m, moving_time_s, elapsed_time_s, total_elevation_gain_m, average_speed_mps)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, "strava:6ba7b810-9dad-41d1-80b4-00c04fd430c8", "missing.gpx", "Unavailable Route", "Ride", "2026-08-01T07:00:00Z", 20_000, 0, 0, 0, 0)
require.NoError(t, err)
item, found, err := rides.GetByExternalID(db, "strava:6ba7b810-9dad-41d1-80b4-00c04fd430c8")
require.NoError(t, err)
require.True(t, found)
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/rides/%d", item.ID), nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, req)
body := response.Body.String()
assert.Equal(t, http.StatusOK, response.Code)
assert.Contains(t, body, "Unavailable Route")
assert.Contains(t, body, "The recorded route is unavailable.")
assert.NotContains(t, body, `id="ride-route"`)
}
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 TestSyncPageIncludesProgressUIWhenAuthorized(t *testing.T) {
server, _ := newWebTestServer(t)
appConfig, err := config.Load(server.configPath)
require.NoError(t, err)
appConfig.Strava.AccessToken = "access"
appConfig.Strava.RefreshToken = "refresh"
appConfig.Strava.ExpiresAt = time.Now().Add(time.Hour).Unix()
require.NoError(t, config.Save(server.configPath, appConfig))
req := httptest.NewRequest(http.MethodGet, "/sync", nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, req)
body := response.Body.String()
assert.Equal(t, http.StatusOK, response.Code)
assert.Contains(t, body, `id="sync-form"`)
assert.Contains(t, body, `id="sync-progress"`)
assert.Contains(t, body, `