aboutsummaryrefslogtreecommitdiff
path: root/climb/detection_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'climb/detection_test.go')
-rw-r--r--climb/detection_test.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/climb/detection_test.go b/climb/detection_test.go
new file mode 100644
index 0000000..3c6a33b
--- /dev/null
+++ b/climb/detection_test.go
@@ -0,0 +1,84 @@
+package climb_test
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/martinlehoux/biking_home/climb"
+ "github.com/martinlehoux/biking_home/core"
+ "github.com/martinlehoux/biking_home/internal/routetest"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// describe renders a detected climb as the ride-style
+// "start-end: length at slope (pts - cat)" string, so positions are asserted.
+func describe(route climb.Route, detected climb.DetectedClimb) string {
+ seg := detected.Climb
+ startKm := route.Distances[detected.Start] / 1000
+ endKm := route.Distances[detected.Start+len(seg.Distances)-1] / 1000
+ lengthKm := (seg.Distances[len(seg.Distances)-1] - seg.Distances[0]) / 1000
+ slopePercent := climb.Slope(seg.Distances, seg.Elevations) * 100
+ pts := int(seg.DifficultyScore())
+ return fmt.Sprintf("%.1fkm-%.1fkm: %.1fkm at %.1f%% (%d pts - %s)", startKm, endKm, lengthKm, slopePercent, pts, climb.CategoryOf(seg.DifficultyScore()))
+}
+
+func TestClimbThenFalseFlat(t *testing.T) {
+ route := routetest.MustRoute("2km at 7% | 10km at 1%")
+ detected := climb.Detect(route, nil, climb.ClimbSlopeMinimum)
+
+ assert.Len(t, detected, 1)
+ assert.Equal(t, "0.0km-2.0km: 2.0km at 7.0% (98 pts - Cat 3)", describe(route, detected[0]))
+}
+
+func TestSmallClimbWithDescentInsideFalseFlat(t *testing.T) {
+ route := routetest.MustRoute("10km at 1% | 2km at 7% | 1km at -7% | 10km at 1%")
+ detected := climb.Detect(route, nil, climb.ClimbSlopeMinimum)
+
+ assert.Len(t, detected, 1)
+ assert.Equal(t, "10.0km-12.0km: 2.0km at 7.0% (98 pts - Cat 3)", describe(route, detected[0]))
+}
+
+func TestDetectClimbsSkipsMiddleClaim(t *testing.T) {
+ // A climb, a false flat giving a saddle, then a climb again. Claiming the
+ // saddle's descent base should leave both climbs detected separately.
+ route := routetest.MustRoute("2km at 7% | 1km at -7% | 2km at 7%")
+ detected := climb.Detect(route, []core.Interval{core.NewInterval(20, 20)}, climb.ClimbSlopeMinimum)
+
+ // No free region falls under the min distance except the two climbs.
+ assert.Len(t, detected, 2)
+ assert.Equal(t, "0.0km-2.0km: 2.0km at 7.0% (98 pts - Cat 3)", describe(route, detected[0]))
+ assert.Equal(t, "3.0km-5.0km: 2.0km at 7.0% (98 pts - Cat 3)", describe(route, detected[1]))
+}
+
+func TestDetectClimbsOverlappingClaimDropsClimb(t *testing.T) {
+ // Claiming the ascent region entirely swallows the climb.
+ route := routetest.MustRoute("2km at 7% | 20km at 0%")
+ detected := climb.Detect(route, []core.Interval{core.NewInterval(0, 40)}, climb.ClimbSlopeMinimum)
+ assert.Empty(t, detected)
+}
+
+func TestDetectClimbsIgnoresOutOfRangeClaim(t *testing.T) {
+ route := routetest.MustRoute("2km at 7% | 20km at 0%")
+ detected := climb.Detect(route, []core.Interval{core.NewInterval(-5, -1)}, climb.ClimbSlopeMinimum)
+ require.Len(t, detected, 1)
+ assert.Equal(t, "0.0km-2.0km: 2.0km at 7.0% (98 pts - Cat 3)", describe(route, detected[0]))
+}
+
+func TestDetectClimbsWithUnsortedClaimsDropsOverlappingClimb(t *testing.T) {
+ // Reproduces ride 6 Chemin des Lauriers bug: Detect must not depend on
+ // caller-provided order. Two official claims out of ID order (Bastides
+ // 2140-2627 before Lauriers 1215-1588) caused freeRegions to skip the
+ // earlier claim and leave the overlapping Cat 4 1352-1601 visible.
+ // Minimal reproduction: two climbs 0-2km and 3-5km separated by a descent.
+ route := routetest.MustRoute("2km at 7% | 1km at -7% | 2km at 7%")
+ // Both climbs claimed, but in reverse order (mimics ORDER BY id returning 30-50 before 0-20).
+ // Sorted input correctly yields 0 detected climbs.
+ sorted := []core.Interval{core.NewInterval(0, 20), core.NewInterval(30, 50)}
+ assert.Empty(t, climb.Detect(route, sorted, climb.ClimbSlopeMinimum))
+
+ unsorted := []core.Interval{core.NewInterval(30, 50), core.NewInterval(0, 20)}
+ detected := climb.Detect(route, unsorted, climb.ClimbSlopeMinimum)
+ // Before fix this fails: freeRegions skips {0,20} because 20 < 30 and returns 1 climb.
+ assert.Empty(t, detected, "unsorted claimed ranges must still suppress overlapping climbs — expected 0, got %v", detected)
+}