Files
TREK/server/tests/unit/db/repositories/TourWaypoints.repository.test.ts
T
Maurice 188953158a fix(tours): a city walk's walking time, stays and total read apart and alike everywhere, and a stay plans no route again (CW-02)
A stay, a name, an importance or a protection set in a stop's dialog no
longer plans a city walk's route again: the walk stays ready to save, its
time overview follows at once, and one undo takes the change back without
routing either. A stop moved or added plans the route again as before. A
walk saves no breaks, so a cafe never counts as a stay and as a break at
once, and a break left from another type neither counts nor holds its save
up.

The planner and the tour detail show a walk's walking time, its stays and
their total apart and in the same words: 45 minutes of walking, a museum of
90 minutes and a cafe of 30 minutes make 2 h 45 min. A stay not entered yet
makes the stays and the total at least what is known, stays nobody entered
read not known yet, and stays entered as 0 read 0 min. A total entered by
hand keeps its own label, and the computed total shows beside it. The head
of the tour detail, the day row on the desktop and the phone, the tour
summary and the library of other trips show that same total, with a ≥ where
a stay is missing. Their time on the move is called walking, and the summary
says a walk is planned with its stays rather than breaks.

The tour list and the library carry the minutes a walk's stops are entered
with and how many have no stay yet, counted from the walk's points, so the
way back of a loop is no stop and its start's stay counts once. A loop run
the other way keeps its start as the stop there, with its key, name and
stay.

The planner's waypoint list keeps the rows of a leg in a file of their own,
and the tour detail its name heading in a part of its own, so both stay
within their size limits.
2026-10-20 07:09:57 +00:00

227 lines
9.1 KiB
TypeScript

/**
* `TourWaypointsRepository`: TW1 (`listForPlace`), TW2 (`listForTrip`), TW2b (`listForPlaces`), TW3
* (`insertForPlace`) and TW4 (`deleteForPlace`), with a leg that follows a
* via ferrata's own line.
*/
import { TourWaypoints } from '../../../../src/db/entities/TourWaypoints.entity';
import type {
TourWaypointRow,
TourWaypointsRepository,
} from '../../../../src/db/repositories/TourWaypoints.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { createPlace, createTrip, createUser } from '../../../helpers/factories';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { createTour } from '../../../helpers/tours-repos';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const testDb = createSnapshotTestDb();
let t: TestOrm;
let repo: TourWaypointsRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
repo = t.repo(TourWaypoints);
});
beforeEach(() => {
resetTestDb(testDb);
t.clear();
});
afterAll(async () => {
await t.close();
testDb.close();
});
const ROUTE: TourWaypointRow[] = [
{ lat: 47.1, lng: 11.1, role: 'end', sequence: 2 },
{ lat: 47, lng: 11, role: 'start', sequence: 0 },
{ lat: 47.05, lng: 11.05, role: 'via', sequence: 1 },
];
function tourInTrip() {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id);
createTour(testDb, place.id);
return { trip, place };
}
describe('TourWaypointsRepository', () => {
it('TWREPO-001: insertForPlace writes every point and listForPlace reads them back in route order', async () => {
const { place } = tourInTrip();
await repo.insertForPlace(place.id, ROUTE);
expect(await repo.listForPlace(place.id)).toEqual([...ROUTE].sort((a, b) => a.sequence - b.sequence));
});
it('TWREPO-002: insertForPlace with no points writes nothing', async () => {
const { place } = tourInTrip();
await repo.insertForPlace(place.id, []);
expect(await repo.listForPlace(place.id)).toEqual([]);
});
it('TWREPO-003: deleteForPlace clears one tour and leaves another alone', async () => {
const first = tourInTrip();
const second = tourInTrip();
await repo.insertForPlace(first.place.id, ROUTE);
await repo.insertForPlace(second.place.id, ROUTE);
await repo.deleteForPlace(first.place.id);
expect(await repo.listForPlace(first.place.id)).toEqual([]);
expect(await repo.listForPlace(second.place.id)).toHaveLength(3);
});
it('TWREPO-004: listForTrip returns every waypoint of the trip with its place, ordered by place and sequence', async () => {
const { trip, place } = tourInTrip();
const other = tourInTrip();
const second = createPlace(testDb, trip.id);
createTour(testDb, second.id);
await repo.insertForPlace(second.id, ROUTE.slice(0, 2));
await repo.insertForPlace(place.id, ROUTE);
await repo.insertForPlace(other.place.id, ROUTE);
const rows = await repo.listForTrip(trip.id);
expect(rows.map((r) => [r.place_id, r.sequence])).toEqual([
[place.id, 0],
[place.id, 1],
[place.id, 2],
[second.id, 0],
[second.id, 2],
]);
expect(rows[0]).toEqual({ place_id: place.id, lat: 47, lng: 11, role: 'start', sequence: 0 });
});
it('TWREPO-005: a fixed leg is stored with its waypoint and read back whole; routed legs carry none', async () => {
const { trip, place } = tourInTrip();
const fixed_leg = {
kind: 'ferrata' as const,
ref: 'way/23971675',
name: 'Alpspitz Ferrata',
geometry: [
[47.4343, 11.0501],
[47.432, 11.0485],
[47.4295, 11.0478],
] as [number, number][],
};
await repo.insertForPlace(place.id, [
{ lat: 47.4387, lng: 11.0523, role: 'start', sequence: 0 },
{ lat: 47.4343, lng: 11.0501, role: 'via', sequence: 1, fixed_leg },
{ lat: 47.4295, lng: 11.0478, role: 'end', sequence: 2 },
]);
const points = await repo.listForPlace(place.id);
expect(points[1]).toEqual({ lat: 47.4343, lng: 11.0501, role: 'via', sequence: 1, fixed_leg });
expect(points[0]).not.toHaveProperty('fixed_leg');
expect(points[2]).not.toHaveProperty('fixed_leg');
expect((await repo.listForTrip(trip.id))[1]).toEqual({ place_id: place.id, ...points[1] });
});
it('TWREPO-006: a stored fixed leg that no longer decodes reads as a routed leg', async () => {
const { place } = tourInTrip();
await repo.insertForPlace(place.id, [
{ lat: 47, lng: 11, role: 'start', sequence: 0 },
{ lat: 47.1, lng: 11.1, role: 'end', sequence: 1 },
]);
await t.em.nativeUpdate(TourWaypoints, { place: place.id, sequence: 0 }, { fixed_leg: '{"kind":"boat"}' });
t.clear();
expect((await repo.listForPlace(place.id))[0]).toEqual({ lat: 47, lng: 11, role: 'start', sequence: 0 });
});
it('TWREPO-007: a shaping point is stored as such and read back; a stop carries no flag (P2A-03)', async () => {
const { place } = tourInTrip();
await repo.insertForPlace(place.id, [
{ lat: 47, lng: 11, role: 'start', sequence: 0 },
{ lat: 47.05, lng: 11.05, role: 'via', sequence: 1, shaping: true },
{ lat: 47.1, lng: 11.1, role: 'end', sequence: 2 },
]);
const points = await repo.listForPlace(place.id);
expect(points[1]).toEqual({ lat: 47.05, lng: 11.05, role: 'via', sequence: 1, shaping: true });
expect(points[0]).not.toHaveProperty('shaping');
});
it('TWREPO-008: a point that stands for a place keeps it, and the place lists as linkable only in its own trip (PR1-10)', async () => {
const { trip, place } = tourInTrip();
const hut = createPlace(testDb, trip.id, { name: 'Hut' });
const otherTour = createPlace(testDb, trip.id, { name: 'Other tour' });
createTour(testDb, otherTour.id);
const { user } = createUser(testDb);
const elsewhere = createPlace(testDb, createTrip(testDb, user.id).id, { name: 'Elsewhere' });
await repo.insertForPlace(place.id, [
{ lat: 47, lng: 11, role: 'start', sequence: 0, linked_place_id: hut.id },
{ lat: 47.1, lng: 11.1, role: 'end', sequence: 1 },
]);
expect((await repo.listForPlace(place.id)).map((point) => point.linked_place_id ?? null)).toEqual([hut.id, null]);
expect((await repo.listForTrip(trip.id))[0]).toMatchObject({ linked_place_id: hut.id });
const linkable = await repo.linkablePlaceIds(trip.id, [hut.id, otherTour.id, elsewhere.id, 999_999, hut.id]);
expect([...linkable]).toEqual([hut.id]);
expect(await repo.linkablePlaceIds(trip.id, [])).toEqual(new Set());
expect(await repo.placeNames(trip.id, [hut.id, elsewhere.id])).toEqual(new Map([[hut.id, 'Hut']]));
expect(await repo.placeNames(trip.id, [])).toEqual(new Map());
});
it("TWREPO-009: a city walk's station keeps its key, name, stay, importance, protection and hours; a plain point carries none (CW-01 to CW-04)", async () => {
const { trip, place } = tourInTrip();
const station: TourWaypointRow = {
lat: 48.1,
lng: 11.5,
role: 'start',
sequence: 0,
key: 'stop-cathedral',
name: 'Cathedral',
stay_minutes: 30,
importance: 'must',
protected: true,
opening_hours: 'Mo-Su 09:00-18:00',
};
const passedThrough: TourWaypointRow = {
lat: 48.11,
lng: 11.51,
role: 'via',
sequence: 1,
key: 'stop-bridge',
stay_minutes: 0,
importance: 'optional',
};
const plain: TourWaypointRow = { lat: 48.12, lng: 11.52, role: 'end', sequence: 2 };
await repo.insertForPlace(place.id, [station, passedThrough, plain]);
const points = await repo.listForPlace(place.id);
expect(points).toEqual([station, passedThrough, plain]);
for (const field of ['key', 'name', 'stay_minutes', 'importance', 'protected', 'opening_hours']) {
expect(points[2]).not.toHaveProperty(field);
}
expect(points[1]).not.toHaveProperty('protected');
expect(points[1]).not.toHaveProperty('name');
expect(await repo.listForTrip(trip.id)).toEqual(points.map((point) => ({ place_id: place.id, ...point })));
});
it('TWREPO-010: listForPlaces reads the waypoints of the tours asked for, by place and sequence, and none for none (CW-02)', async () => {
const { trip, place } = tourInTrip();
const second = createPlace(testDb, trip.id);
createTour(testDb, second.id);
const unasked = createPlace(testDb, trip.id);
createTour(testDb, unasked.id);
await repo.insertForPlace(second.id, [{ lat: 47.2, lng: 11.2, role: 'start', sequence: 0, stay_minutes: 30 }]);
await repo.insertForPlace(place.id, ROUTE);
await repo.insertForPlace(unasked.id, ROUTE);
const rows = await repo.listForPlaces([second.id, place.id, second.id]);
expect(rows.map((row) => [row.place_id, row.sequence])).toEqual([
[place.id, 0],
[place.id, 1],
[place.id, 2],
[second.id, 0],
]);
expect(rows[3]).toEqual({
place_id: second.id,
lat: 47.2,
lng: 11.2,
role: 'start',
sequence: 0,
stay_minutes: 30,
});
expect(await repo.listForPlaces([])).toEqual([]);
});
});