aboutsummaryrefslogtreecommitdiff
path: root/rideanalysis/attempts_test.go
blob: 6fab8e15f3db1d395c527047c5e08b050855c8da (plain) (blame)
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

package rideanalysis_test

import (
	"testing"
	"time"

	"github.com/martinlehoux/biking_home/internal/ridetest"
	"github.com/martinlehoux/biking_home/official_climb"
	"github.com/martinlehoux/biking_home/ride"
	"github.com/martinlehoux/biking_home/rideanalysis"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

var attemptRideStart = time.Date(2026, 9, 1, 8, 0, 0, 0, time.UTC)

func attemptRideForClimb(t *testing.T) (ride.Ride, official_climb.OfficialClimb) {
	t.Helper()
	parsed := ridetest.MustRide("1km at 14.7% | 1km at -6.7%", ridetest.StartCoord(43.61, 5.42), ridetest.StartElevation(300), ridetest.Sampling(1000), ridetest.StartTime(attemptRideStart))
	return parsed, official_climb.OfficialClimb{Name: "Col de Test", StartCoord: parsed.Coords[0], EndCoord: parsed.Coords[1], Sample: parsed.Sample(0, 1)}
}

func TestAttemptsSnapshotsOfficialClimbs(t *testing.T) {
	parsed, climb := attemptRideForClimb(t)
	analysis := rideanalysis.Analyze(parsed, nil, []official_climb.OfficialClimb{{ID: 42, Name: climb.Name, StartCoord: climb.StartCoord, EndCoord: climb.EndCoord, Sample: climb.Sample}}, official_climb.DefaultMatchPolicy())

	attempts := rideanalysis.Attempts(7, analysis, parsed)
	require.Len(t, attempts, 1)
	assert.Equal(t, int64(7), attempts[0].RideID)
	assert.Equal(t, int64(42), attempts[0].ClimbID)
	assert.Equal(t, 0, attempts[0].Occurrence)
	assert.InDelta(t, 1000, attempts[0].DistanceM, 1)
	assert.InDelta(t, 147, attempts[0].ElevationGainM, 1)
	assert.InDelta(t, 14.7, attempts[0].SlopePercent, 0.1)
	assert.Positive(t, attempts[0].Cotacol)
	assert.Equal(t, "v1", attempts[0].CotacolAlgoVersion)
	assert.Equal(t, attemptRideStart, attempts[0].StartedAt.UTC())
	assert.False(t, attempts[0].EndedAt.IsZero(), "attempt end must come from ride timestamps")
}

func TestAttemptsSkipsUnmatchedClimbs(t *testing.T) {
	parsed, _ := attemptRideForClimb(t)
	analysis := rideanalysis.Analyze(parsed, nil, nil, official_climb.DefaultMatchPolicy())

	assert.Empty(t, rideanalysis.Attempts(7, analysis, parsed))
}