fix(tours): checklist entries within their contract, whole locks and a leaner reading

A lock flight chained into one group of more than 30 parts broke the
checklist's contract and made every decision about it fail; an entry now
names its nearest part and the first others, a portage lists at most 30
obstacles and a checklist at most 100 own entries. Reading along a line
takes in the parts of a lock the corridor cut off, so its data basis no
longer depends on how the route runs. A line or an area stands at the
middle of its extent, so adding a node leaves it in place, and an earlier
decision is judged by where the obstacle was, not by the route point of its
passage. The cache keeps its readings without their lines, for one data
version at a time, and the data version is read without the region
boundaries.
This commit is contained in:
Maurice
2026-10-17 02:07:22 +00:00
parent d46b6fb646
commit 2a4b71b3d9
12 changed files with 308 additions and 79 deletions
@@ -182,4 +182,30 @@ describe('PaddleFeaturesRepository', () => {
expect(stats.count).toBe(2);
expect(stats.max_id).toEqual(expect.any(Number));
});
it('PADFEATREPO-004: reads only the parts of the given locks inside a box', async () => {
await add('oberpfalz');
await features.replaceRegion('oberpfalz', [
{ ...feature('node/1', 49.5, 12.5), group_key: 'lock:node/1' },
{ ...feature('node/2', 49.51, 12.5), group_key: 'lock:node/1' },
{ ...feature('node/3', 49.52, 12.5), group_key: 'lock:node/3' },
{ ...feature('node/4', 49.9, 12.5), group_key: 'lock:node/1' },
]);
const parts = await features.findGroupParts(
['lock:node/1'],
{ south: 49.4, west: 12.4, north: 49.6, east: 12.6 },
10,
);
expect(parts.map((row) => row.osm_ref)).toEqual(['node/1', 'node/2']);
});
it('PADREGREPO-010: names when each region was last read, without its boundary', async () => {
await add('oberpfalz');
await add('bayern');
await regions.updateRegion('oberpfalz', { status: 'ready', imported_at: '2026-10-02 04:20:00' });
expect(await regions.readTimes()).toEqual([
{ id: 'bayern', imported_at: null },
{ id: 'oberpfalz', imported_at: '2026-10-02 04:20:00' },
]);
});
});
@@ -278,3 +278,27 @@ describe('PaddleObstaclesService.dataVersion', () => {
expect(await service.dataVersion()).toBe(empty);
});
});
describe('PaddleObstaclesService.readAlong: whole locks', () => {
it('PADOBS-015: a lock the corridor cuts through comes with its parts beyond it, other locks and objects do not', async () => {
await region('oberpfalz');
const part = (ref: string, lat: number, lng: number, group: string | null) => ({
...weir('oberpfalz', ref, lat, lng),
kind: 'lock_gate',
group_key: group,
});
await insertRows(orm(), PaddleFeatures, [
part('node/1', 49.2, 12.005, 'lock:node/1'),
// 1.1 km north of the line, out of the corridor but part of the same lock.
part('node/2', 49.21, 12.005, 'lock:node/1'),
// Another lock out there, and a lone weir.
part('node/3', 49.21, 12.006, 'lock:node/3'),
weir('oberpfalz', 'node/4', 49.21, 12.007),
]);
const along = await service.readAlong([
[49.2, 12.0],
[49.2, 12.01],
]);
expect(along.features.map((feature) => feature.ref).sort()).toEqual(['node/1', 'node/2']);
});
});
@@ -1,39 +1,59 @@
/**
* PaddleCheckCache: the readings of the last lines, the least recently used
* one dropped once it holds more than it keeps.
* PaddleCheckCache: the readings of the last lines for one version of the
* obstacle data, kept without their lines, the least recently used one
* dropped once it holds more than it keeps, and all of them dropped with a
* new version.
*/
import { PaddleCheckCache, type PaddleCheckEvaluation } from '../../../src/nest/tours/tour-paddle-check.cache';
import type { PaddleCheckLine } from '@trek/shared';
import { describe, expect, it } from 'vitest';
const line = (totalM: number): PaddleCheckLine => ({ points: [], alongM: [], totalM, portages: [], locks: [] });
function evaluation(totalM: number): PaddleCheckEvaluation {
return {
line: { points: [], alongM: [], totalM, portages: [], locks: [] },
line: line(totalM),
candidates: [],
own: [],
dataKeys: new Set(),
dataKeys: new Set([`node/${totalM}`]),
complete: true,
data: { data: 'covered', data_as_of: null, read_at: null },
};
}
describe('PaddleCheckCache', () => {
it('PCC-001: answers what it was given and nothing it was not', () => {
it('PCC-001: answers what it was given with the line it is asked for, and nothing it was not', () => {
const cache = new PaddleCheckCache();
cache.atVersion('v1');
cache.set('a', evaluation(1));
expect(cache.get('a')?.line.totalM).toBe(1);
expect(cache.get('b')).toBeUndefined();
const asked = line(7);
const hit = cache.get('a', asked);
expect(hit?.line).toBe(asked);
expect(hit?.dataKeys.has('node/1')).toBe(true);
expect(cache.get('b', asked)).toBeUndefined();
cache.set('a', evaluation(2));
expect(cache.get('a')?.line.totalM).toBe(2);
expect(cache.get('a', asked)?.dataKeys.has('node/2')).toBe(true);
});
it('PCC-002: past 256 readings drops the one used longest ago, a reading just read is kept', () => {
it('PCC-002: past 64 readings drops the one used longest ago, a reading just read is kept', () => {
const cache = new PaddleCheckCache();
for (let i = 0; i < 256; i++) cache.set(`line-${i}`, evaluation(i));
expect(cache.get('line-0')).toBeDefined();
cache.set('line-256', evaluation(256));
expect(cache.get('line-0')).toBeDefined();
expect(cache.get('line-1')).toBeUndefined();
expect(cache.get('line-256')).toBeDefined();
cache.atVersion('v1');
for (let i = 0; i < 64; i++) cache.set(`line-${i}`, evaluation(i));
expect(cache.get('line-0', line(0))).toBeDefined();
cache.set('line-64', evaluation(64));
expect(cache.get('line-0', line(0))).toBeDefined();
expect(cache.get('line-1', line(1))).toBeUndefined();
expect(cache.get('line-64', line(64))).toBeDefined();
});
it('PCC-003: a new version of the data drops every reading, the same version keeps them', () => {
const cache = new PaddleCheckCache();
cache.atVersion('v1');
cache.set('a', evaluation(1));
cache.atVersion('v1');
expect(cache.get('a', line(1))).toBeDefined();
cache.atVersion('v2');
expect(cache.get('a', line(1))).toBeUndefined();
});
});