summaryrefslogtreecommitdiff
path: root/web/frontend/ride-detail-logic.test.ts
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-08-16 14:23:32 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-08-16 14:23:32 +0200
commit53ab332e6d2afe9818914e0026310e4cadcc4202 (patch)
tree198d708fa05d4247bdcc0cf2900fdf551d61b7d6 /web/frontend/ride-detail-logic.test.ts
parent2d18d177f48285eb123e7da8680fc00e7a6fe19c (diff)
refactor(web): migrate browser code to TypeScript
Diffstat (limited to 'web/frontend/ride-detail-logic.test.ts')
-rw-r--r--web/frontend/ride-detail-logic.test.ts46
1 files changed, 46 insertions, 0 deletions
diff --git a/web/frontend/ride-detail-logic.test.ts b/web/frontend/ride-detail-logic.test.ts
new file mode 100644
index 0000000..024f018
--- /dev/null
+++ b/web/frontend/ride-detail-logic.test.ts
@@ -0,0 +1,46 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+import {
+ categoryForScore,
+ climbMetrics,
+ cotacolForClimb,
+ elevationAtDistance,
+ formatDistance,
+ formatElevation,
+ nearestPointIndex,
+} from "./ride-detail-logic.ts";
+
+const points = [
+ { distanceKm: 0, elevationM: 100 },
+ { distanceKm: 0.12, elevationM: 106 },
+ { distanceKm: 0.2, elevationM: 114 },
+ { distanceKm: 0.25, elevationM: 120 },
+];
+
+test("finds the nearest profile point by distance", () => {
+ assert.equal(nearestPointIndex(points, 0.01), 0);
+ assert.equal(nearestPointIndex(points, 0.15), 1);
+ assert.equal(nearestPointIndex(points, 0.24), 3);
+});
+
+test("interpolates elevation between route points", () => {
+ assert.equal(elevationAtDistance(points, 0, 100), 105);
+ assert.equal(elevationAtDistance(points, 2, 250), 120);
+});
+
+test("calculates climb metrics with the Cotacol score", () => {
+ const metrics = climbMetrics(points, { startIndex: 0, endIndex: 3 });
+ assert.ok(metrics);
+
+ assert.equal(metrics.distanceKm, 0.25);
+ assert.equal(metrics.elevationGain, 20);
+ assert.equal(metrics.slope, 8);
+ assert.equal(metrics.cotacol, cotacolForClimb(points, 0, 3));
+ assert.equal(metrics.category, categoryForScore(metrics.score));
+});
+
+test("formats profile labels consistently", () => {
+ assert.equal(formatDistance(2.4), "2.4 km");
+ assert.equal(formatDistance(12.4), "12 km");
+ assert.equal(formatElevation(123.6), "124 m");
+});