diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-08-27 11:21:17 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-08-27 11:21:17 +0200 |
| commit | 825cf388b55a6c8441e02149749bb6e2968477c0 (patch) | |
| tree | e3394cef97aa43df9775a1465d3c99f386ce3a9b /climb/detection_test.go | |
| parent | 366e7b0d671395c2ad32f85ee7706a592db69d15 (diff) | |
Extract climb detection into a dedicated climb package
Move climb detection, slope, and Cotacol scoring out of ride into a new
climb package that operates on a geometry-only Route (distances, elevations,
coords). Ride embeds climb.Route and keeps its timing and telemetry columns.
- climb.Detect returns []DetectedClimb (Climb + ride-absolute Start).
- Climb owns its sliced geometry; indices are local, with Start reported at
the detection boundary for absolute position.
- Detected bounds use core.Interval; freeRegions claim suppression fixed.
- ride no longer builds slices: ClimbFromDist/ClimbFromIndexes/climbFrom and
ride.Climb/Interval/Range are removed; rideanalysis, mountain_pass MatchClimb,
and web views are rewired to climb types.
- New internal/routetest builds climb.Route from the ride DSL; ridetest reuses
it and adds timestamps and telemetry.
- Tests moved to climb (synthetic detection and difficulty) and kept in ride
for GPX-example integration; ride.comparison detection coverage retained.
- mountain_pass.nearestCrossing now reuses geo.Nearest.
Diffstat (limited to 'climb/detection_test.go')
| -rw-r--r-- | climb/detection_test.go | 84 |
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) +} |
