fix(roadtrip): keep the shaping a traveller did, through every path that moves a stop

A via records which stop it follows by POSITION, so anything that renumbers a
day's stops moves the ground under it. The road-trip rail corrects for that;
almost nothing else did.

reanchor() never renumbered `sequence`. It is allocated per leg and starts at 0
in each, and re-anchoring MERGES two legs whenever the stop between them goes
away — so leg A's 0,1 ended up beside leg B's 0,1, and everything that draws the
route orders by sequence. The drive ran A0, B0, A1, B1: forward, back, and
forward again. Persisted, surviving a reload, and unrepairable except by deleting
the vias. The old anchor is read before the updates and used as the primary sort
key, so the vias of the earlier leg stay ahead of the ones that merged into it.

Reordering the only two stops of a day deleted every via on it. A reorder is
modelled as remove-then-insert, and the removal half refuses to treat a two-stop
day as still having a drive — right when a stop really goes away, catastrophic
when nothing was removed. Two stops is exactly the shape "follow this track"
produces, up to nine vias on that one leg, gone to a single drag with no prompt
and no undo.

Removing a stop, reordering within a day, and assigning a place mid-day never
re-anchored at all, though the helpers exist and three other paths use them. The
detour then sat on a leg the traveller never chose, or on a leg that no longer
existed and vanished — until a later stop brought it back bending a road nobody
asked for. Reordering hands a whole new ordering rather than one move, so the
anchors there follow the stop they were pinned behind instead of arithmetic on
the index, which is correct for any permutation.

Duplicating a trip dropped all of it: `stop_type` was missing from the place
insert, and neither via nor day-track table was copied. The duplicate looked
complete and quietly drove somewhere else.

A failed read of the via list emptied it. That read runs after every write and
feeds the routing key, so a 502, a dropped connection or a tab going offline
re-routed the whole trip along the roads the traveller had steered away from,
with nothing said and nothing retried — indistinguishable from having deleted
them. Only a 404 or 403 empties now; anything else keeps the last known list and
says it may be stale.
This commit is contained in:
Maurice
2026-09-07 14:00:22 +02:00
committed by Maurice
parent c1f7587704
commit 1e1dd9b814
18 changed files with 579 additions and 28 deletions
@@ -754,6 +754,52 @@ describe('folded trip CRUD', () => {
expect((testDb.prepare('SELECT title FROM trips WHERE id = ?').get(secondCopy) as any).title).toBe('Origin');
});
it('TRIP-SVC-061: copy carries the road-trip shaping, not just the places', () => {
// A via is the road the traveller chose over the one the router prefers, and
// a day track is the line a day was fitted to. Leaving them behind gave back
// a trip that looks complete and quietly drives somewhere else — noticed
// only once somebody edits the copy, with nothing left to recover from.
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Norway', start_date: '2025-06-01', end_date: '2025-06-02' });
const days = getDays(trip.id);
const stop = createPlace(testDb, trip.id, { name: 'Geiranger' });
const track = createPlace(testDb, trip.id, { name: 'Scenic route' });
testDb.prepare("UPDATE places SET stop_type = 'fuel' WHERE id = ?").run(stop.id);
testDb.prepare("UPDATE places SET route_geometry = '[[1,2],[3,4]]' WHERE id = ?").run(track.id);
createDayAssignment(testDb, days[0].id, stop.id);
testDb.prepare(
'INSERT INTO roadtrip_vias (day_id, after_order_index, sequence, lat, lng) VALUES (?, 0, 0, 62.1, 7.2), (?, 0, 1, 62.2, 7.3)',
).run(days[0].id, days[0].id);
testDb.prepare('INSERT INTO roadtrip_day_tracks (day_id, place_id, stray_km) VALUES (?, ?, 1.5)')
.run(days[0].id, track.id);
const newTripId = svc.copy(trip.id, user.id, 'Clone');
const newDays = getDays(newTripId);
// The kind of stop each place is survives the copy.
const copiedStop = testDb.prepare("SELECT stop_type FROM places WHERE trip_id = ? AND name = 'Geiranger'")
.get(newTripId) as { stop_type: string | null };
expect(copiedStop.stop_type).toBe('fuel');
const vias = testDb.prepare('SELECT after_order_index, sequence, lat, lng FROM roadtrip_vias WHERE day_id = ? ORDER BY sequence')
.all(newDays[0].id) as { after_order_index: number; sequence: number; lat: number; lng: number }[];
expect(vias).toEqual([
{ after_order_index: 0, sequence: 0, lat: 62.1, lng: 7.2 },
{ after_order_index: 0, sequence: 1, lat: 62.2, lng: 7.3 },
]);
// The track points at the COPY's place, never back at the original.
const copiedTrack = testDb.prepare('SELECT place_id, stray_km FROM roadtrip_day_tracks WHERE day_id = ?')
.get(newDays[0].id) as { place_id: number; stray_km: number };
const copiedTrackPlace = testDb.prepare("SELECT id FROM places WHERE trip_id = ? AND name = 'Scenic route'")
.get(newTripId) as { id: number };
expect(copiedTrack.place_id).toBe(copiedTrackPlace.id);
expect(copiedTrack.stray_km).toBe(1.5);
// And the original keeps exactly what it had.
expect(testDb.prepare('SELECT COUNT(*) c FROM roadtrip_vias WHERE day_id = ?').get(days[0].id)).toEqual({ c: 2 });
});
it('TRIP-SVC-060: copying a trip keeps a staged booking staged', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Origin', start_date: '2025-06-01', end_date: '2025-06-02' });