mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +00:00
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:
@@ -32,8 +32,9 @@ import { FakeRealtimeService } from '../helpers/fake-realtime';
|
||||
import { createTestMikroOrmModule, createTestOrm, type TestOrm } from '../helpers/test-orm';
|
||||
import { TestUnitOfWorkModule } from '../helpers/test-uow';
|
||||
import { sessionCookie } from './harness';
|
||||
import type { NestExpressApplication } from '@nestjs/platform-express';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { FERRATA_BBOX_TOO_LARGE_MESSAGE, HORSE_ROUTES_ATTRIBUTION } from '@trek/shared';
|
||||
import { FERRATA_BBOX_TOO_LARGE_MESSAGE, HORSE_ROUTES_ATTRIBUTION, TOUR_WRITE_MAX_BYTES } from '@trek/shared';
|
||||
|
||||
import cookieParser from 'cookie-parser';
|
||||
import type { Server } from 'http';
|
||||
@@ -157,7 +158,9 @@ describe('Horse tours e2e (real guards + temp SQLite)', () => {
|
||||
.overrideProvider(OverpassElementsClient)
|
||||
.useValue({ query: overpassQuery })
|
||||
.compile();
|
||||
const nest = moduleRef.createNestApplication();
|
||||
const nest = moduleRef.createNestApplication<NestExpressApplication>();
|
||||
// The ceiling bootstrap.ts gives a tour create or edit, in place of the 100kb default.
|
||||
nest.useBodyParser('json', { limit: TOUR_WRITE_MAX_BYTES });
|
||||
nest.use(cookieParser());
|
||||
nest.useGlobalPipes(new ZodValidationPipe());
|
||||
nest.useGlobalFilters(new TrekExceptionFilter());
|
||||
@@ -266,6 +269,27 @@ describe('Horse tours e2e (real guards + temp SQLite)', () => {
|
||||
expect(await countRows(orm, Tours)).toBe(0);
|
||||
});
|
||||
|
||||
it('saves and opens again a ride along a long section, a body far past 100kb', async () => {
|
||||
// A few thousand points at OSM's seven decimals, as a section of bridleway comes.
|
||||
const line = Array.from({ length: 4000 }, (_, i) => [
|
||||
Number((47 + i * 0.00001).toFixed(7)),
|
||||
Number((11 + i * 0.0000013).toFixed(7)),
|
||||
]);
|
||||
const end = line[line.length - 1];
|
||||
const long = rideBody({
|
||||
route_geometry: line.map(([lat, lng], i) => [lat, lng, 600 + (i % 40)]),
|
||||
waypoints: [
|
||||
{ lat: 47, lng: 11, role: 'start', sequence: 0, fixed_leg: { ...SECTION, geometry: line } },
|
||||
{ lat: end[0], lng: end[1], role: 'end', sequence: 1 },
|
||||
],
|
||||
});
|
||||
expect(JSON.stringify(long).length).toBeGreaterThan(150 * 1024);
|
||||
const placeId = await create(long);
|
||||
const res = await request(server).get(`/api/trips/5/tours/${placeId}`).set('Cookie', sessionCookie(OWNER));
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.waypoints[0].fixed_leg.geometry).toEqual(line);
|
||||
});
|
||||
|
||||
it('a member who may only read cannot save one', async () => {
|
||||
expect((await send('post', '', rideBody(), VIEWER)).status).toBe(403);
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Miscellaneous integration tests.
|
||||
* Covers MISC-001, 002, 004, 007, 008, 013, 015.
|
||||
* Covers MISC-001, 002, 004, 007, 008, 009 to 014.
|
||||
*/
|
||||
import { buildApp } from '../../src/bootstrap';
|
||||
import { db as testDb } from '../../src/db/database';
|
||||
@@ -8,6 +8,7 @@ import { authCookie } from '../helpers/auth';
|
||||
import { createUser } from '../helpers/factories';
|
||||
import { resetTestDb, resetRateLimits } from '../helpers/test-db';
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
import { TOUR_WRITE_MAX_BYTES } from '@trek/shared';
|
||||
|
||||
import type { Application } from 'express';
|
||||
import request from 'supertest';
|
||||
@@ -158,4 +159,35 @@ describe('Request body ceiling', () => {
|
||||
.send({ name: 'x'.repeat(200 * 1024) });
|
||||
expect(elsewhere.status).toBe(413);
|
||||
});
|
||||
|
||||
// A tour is sent whole on create and edit, its line and every fixed leg's
|
||||
// line besides: a ride along a horse route passes 100kb (HOR-01). Only those
|
||||
// two routes take the larger ceiling, and only up to TOUR_WRITE_MAX_BYTES.
|
||||
it('MISC-013: a tour create or edit over 100kb reaches the router', async () => {
|
||||
const created = await request(app)
|
||||
.post('/api/trips/5/tours')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send({ name: 'x'.repeat(300 * 1024) });
|
||||
expect(created.status).not.toBe(413);
|
||||
|
||||
const edited = await request(app)
|
||||
.put('/api/trips/5/tours/9')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send({ name: 'x'.repeat(300 * 1024) });
|
||||
expect(edited.status).not.toBe(413);
|
||||
});
|
||||
|
||||
it('MISC-014: a tour past its own ceiling is refused, and the other tour routes keep 100kb', async () => {
|
||||
const huge = await request(app)
|
||||
.post('/api/trips/5/tours')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send({ name: 'x'.repeat(TOUR_WRITE_MAX_BYTES + 1) });
|
||||
expect(huge.status).toBe(413);
|
||||
|
||||
const copy = await request(app)
|
||||
.post('/api/trips/5/tours/9/copy')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send({ name: 'x'.repeat(200 * 1024) });
|
||||
expect(copy.status).toBe(413);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user