M1.2 Insertion-index algorithm + unit tests #11

Closed
opened 2026-07-27 11:04:00 +00:00 by jmz · 3 comments
Owner

Given the polyline anchors and a clicked point, compute the segment that minimizes added detour and the index to splice at. Pure function + unit tests, independent of the map UI.

Given the polyline anchors and a clicked point, compute the segment that minimizes added detour and the index to splice at. Pure function + unit tests, independent of the map UI.
Author
Owner

Code complete (validation gated on CI toolchain).

Wrote web/src/lib/util/route_insertion.ts → chooseInsertionIndex(anchors, point).

Design: score prepend / every interior gap / append by route-length increase (ends = raw leg to the endpoint; mid = detour d(prev,p)+d(p,next)−d(prev,next)); minimum wins, ties → append. This makes the endpoint guard automatic — no distance threshold — and matches the agreed min-detour behavior. Reuses haversineDistance from gpx/utils.ts.

Tests: route_insertion.test.ts — 8 vitest cases (empty, single anchor, append-beyond-end, prepend-before-start, insert gap-1, insert gap-2 = the reported bug, off-line mid-insert, symmetric tie→append). Logic validated 8/8 via a standalone node runner. Formal vitest run is gated on the dev toolchain — tracked under M0.2 (#7). Leaving open until it runs green in CI.

**Code complete** (validation gated on CI toolchain). Wrote `web/src/lib/util/route_insertion.ts` → `chooseInsertionIndex(anchors, point)`. **Design:** score prepend / every interior gap / append by *route-length increase* (ends = raw leg to the endpoint; mid = detour `d(prev,p)+d(p,next)−d(prev,next)`); minimum wins, ties → append. This makes the endpoint guard automatic — no distance threshold — and matches the agreed min-detour behavior. Reuses `haversineDistance` from `gpx/utils.ts`. **Tests:** `route_insertion.test.ts` — 8 vitest cases (empty, single anchor, append-beyond-end, prepend-before-start, insert gap-1, insert gap-2 = the reported bug, off-line mid-insert, symmetric tie→append). Logic **validated 8/8** via a standalone node runner. Formal `vitest` run is gated on the dev toolchain — tracked under M0.2 (#7). Leaving open until it runs green in CI.
Author
Owner

Landed in the fork. Commit cfeb45dc on feat/smart-insertion (web/src/lib/util/route_insertion.ts + route_insertion.test.ts). PR compare: https://git.mirus-tech.com/oss/wanderer/compare/main...feat/smart-insertion . Running vitest against the full clone now to replace the standalone-validation caveat.

**Landed in the fork.** Commit `cfeb45dc` on `feat/smart-insertion` (`web/src/lib/util/route_insertion.ts` + `route_insertion.test.ts`). PR compare: https://git.mirus-tech.com/oss/wanderer/compare/main...feat/smart-insertion . Running `vitest` against the full clone now to replace the standalone-validation caveat.
Author
Owner

✅ vitest green — 2 test files, 11 tests passed (my 8 route_insertion cases + the 3 existing trail tests) run from the full fork clone, resolving the real $lib haversine import. Standalone-validation caveat lifted. Algorithm + unit tests complete; closing. Wiring tracked in #12.

✅ **vitest green** — `2 test files, 11 tests passed` (my 8 `route_insertion` cases + the 3 existing `trail` tests) run from the full fork clone, resolving the real `$lib` haversine import. Standalone-validation caveat lifted. Algorithm + unit tests complete; closing. Wiring tracked in #12.
jmz closed this issue 2026-07-27 12:33:38 +00:00
Sign in to join this conversation.
No labels
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
public/wanderer#11
No description provided.