fix(tours): long rides save, the horse route store stays bounded

A ride sends its full line with heights and every horse route section's
own line besides, about fifty bytes a point, so a ride along a few
thousand points of bridleway hit the API's 100kb body ceiling and the
save failed with a 413. Creating and editing a tour are now measured
against TOUR_WRITE_MAX_BYTES (2 MB), like the book and list file
routes; every other route, the tour copy among them, keeps 100kb.

The horse routes kept in memory are now bounded by section points as
well as by count (64 answers, 500k points), so panning a dense map can
no longer grow the server by hundreds of megabytes.

A search that found no connection can be asked again on the view whose
routes are shown, and a ride with a recorded time shows the horseshoe
beside its riding time instead of footprints.
This commit is contained in:
Maurice
2026-10-20 12:59:07 +02:00
parent 0331d9950f
commit 78366a9024
10 changed files with 182 additions and 25 deletions
@@ -1,7 +1,9 @@
import { DomainError } from '../../../src/nest/common/domain-error';
import { OverpassElementsClient } from '../../../src/nest/overpass/overpass-elements.service';
import {
HORSE_ROUTES_KEPT_MAX,
HORSE_ROUTES_KEPT_MS,
HORSE_ROUTES_KEPT_POINTS_MAX,
HORSE_ROUTES_UNAVAILABLE_MESSAGE,
TourHorseRoutesService,
} from '../../../src/nest/tours/tour-horse-routes.service';
@@ -10,7 +12,7 @@ import { HORSE_ROUTES_ATTRIBUTION, HORSE_ROUTES_UNAVAILABLE_CODE, horseRoutesRes
import { beforeEach, describe, expect, it, vi } from 'vitest';
// HORSE-SVC-001 to HORSE-SVC-005
// HORSE-SVC-001 to HORSE-SVC-006
const query = vi.fn<(q: string, caller: string) => Promise<unknown[]>>();
let service: TourHorseRoutesService;
@@ -87,14 +89,42 @@ describe('TourHorseRoutesService (HOR-01)', () => {
expect((await service.list(box, NOW)).routes).toHaveLength(1);
});
it('HORSE-SVC-005: keeps at most two hundred answers, letting the one kept longest go', async () => {
it('HORSE-SVC-005: keeps a bounded number of answers, letting the one kept longest go', async () => {
query.mockResolvedValue([]);
const nth = (n: number) => ({ south: 40 + n * 0.1, west: 5, north: 40.05 + n * 0.1, east: 5.05 });
for (let n = 0; n <= 200; n += 1) await service.list(nth(n), NOW);
expect(query).toHaveBeenCalledTimes(201);
await service.list(nth(200), NOW);
expect(query).toHaveBeenCalledTimes(201);
for (let n = 0; n <= HORSE_ROUTES_KEPT_MAX; n += 1) await service.list(nth(n), NOW);
expect(query).toHaveBeenCalledTimes(HORSE_ROUTES_KEPT_MAX + 1);
await service.list(nth(HORSE_ROUTES_KEPT_MAX), NOW);
await service.list(nth(1), NOW);
expect(query).toHaveBeenCalledTimes(HORSE_ROUTES_KEPT_MAX + 1);
await service.list(nth(0), NOW);
expect(query).toHaveBeenCalledTimes(202);
expect(query).toHaveBeenCalledTimes(HORSE_ROUTES_KEPT_MAX + 2);
});
it('HORSE-SVC-006: keeps no more section points than its budget, letting the one kept longest go', async () => {
// Ways of ten thousand points, together just over half the budget: two such answers never fit at once.
const perWay = 10_000;
const wayCount = Math.ceil((HORSE_ROUTES_KEPT_POINTS_MAX / 2 + 1) / perWay);
const ids = Array.from({ length: wayCount }, (_, w) => w + 1);
const dense = [
{ type: 'relation', id: 7, tags: { route: 'horse' }, members: ids.map((ref) => ({ type: 'way', ref, role: '' })) },
...ids.map((id) => ({
type: 'way',
id,
tags: { horse: 'designated' },
nodes: Array.from({ length: perWay }, (_, i) => id * perWay + i + 1),
geometry: Array.from({ length: perWay }, (_, i) => ({ lat: 47.15 + i * 1e-5, lon: 11 + id * 0.001 })),
})),
];
query.mockResolvedValue(dense);
const other = { south: 48.12, west: 11.21, north: 48.18, east: 11.29 };
await service.list(box, NOW);
await service.list(other, NOW);
expect(query).toHaveBeenCalledTimes(2);
// The second answer pushed the first out, and stays itself.
await service.list(other, NOW);
expect(query).toHaveBeenCalledTimes(2);
await service.list(box, NOW);
expect(query).toHaveBeenCalledTimes(3);
});
});