Files
TREK/server/tests/unit/db/repositories/TourStationVisits.repository.test.ts
T
Maurice 1969ca57d9 feat(tours): prepare_tour_change prepares a change to a saved city walk for review
An assistant can now prepare a change to a saved city walk and show the
traveller what it would do before anything is saved. Preparing writes
nothing: no tour, station, visit or day plan changes, and turning the
proposal down keeps the plan as it was.

- The changes apply in order and name the saved stations by their keys:
  add a station after another (a spontaneous stop is an add after the
  station the traveller is at), remove, replace and move. Stops put after
  the same station keep the order they were given in.
- What the travellers did stays as it is: a station visited or skipped on
  any day of the walk is never removed, replaced or moved, nothing goes
  before the last station already done, a protected station keeps its
  place and a leg drawn by hand stays as drawn. A proposal breaking a rule
  is refused whole, naming every change and why. Other tour types, and a
  walk that is a recorded track, are refused too: so far only a city walk
  takes prepared changes, since the others plan their way with routing of
  their own the server cannot run.
- The answer sets the walk after the change against the saved one: each
  station with what changed it and where it stood, what was removed, the
  way on foot before and after planned by the same router (the stored
  distance and walking time given apart, never mixed in), the stays as far
  as known, every leg with the new ones marked, the gaps that could not be
  worked out, a warning for a must station removed or replaced, and each
  new station as create_place takes it, to keep it for later as a place of
  the trip instead.
- The proposal names the revision it was prepared on, so a save of it is
  refused once the tour has moved on, and says whether the traveller may
  change the trip's places at all.

The way on foot of a new walk, its creation and a prepared change now come
from one CityWalkFootWayService instead of a copy in the briefing service.
2026-10-20 15:14:00 +02:00

175 lines
8.9 KiB
TypeScript

/**
* `TourStationVisitsRepository` (CW-03): how far a city walk got on its days,
* per day assignment and station key. Seeded through the ORM factories over
* the migrated snapshot schema.
*/
import { DayAssignments } from '../../../../src/db/entities/DayAssignments.entity';
import { TourStationVisits } from '../../../../src/db/entities/TourStationVisits.entity';
import type { TourStationVisitsRepository } from '../../../../src/db/repositories/TourStationVisits.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { makeDayAssignment } from '../../../helpers/factories/itinerary';
import { makePlace } from '../../../helpers/factories/places';
import { deleteRows, findRows } from '../../../helpers/factories/rows';
import { makeTour, markStationVisit } from '../../../helpers/factories/tours';
import { makeDay, makeTrip } from '../../../helpers/factories/trips';
import { makeUser } from '../../../helpers/factories/users';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const testDb = createSnapshotTestDb();
let t: TestOrm;
let repo: TourStationVisitsRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
repo = t.repo(TourStationVisits);
});
beforeEach(() => {
resetTestDb(testDb);
t.clear();
});
afterAll(async () => {
await t.close();
testDb.close();
});
/** Trip A with a walk on days 1 and 2 and a plain place on day 1; trip B with a walk of its own on its day. */
async function fixture() {
const { user } = await makeUser(t);
const trip = await makeTrip(t, user.id);
const day1 = await makeDay(t, trip.id, { day_number: 1 });
const day2 = await makeDay(t, trip.id, { day_number: 2 });
const walk = await makePlace(t, trip.id, { name: 'Old town' });
await makeTour(t, walk.id, { tourTypeRef: 'walk' });
const cafe = await makePlace(t, trip.id, { name: 'Cafe' });
const onDay1 = await makeDayAssignment(t, day1.id, walk.id);
const onDay2 = await makeDayAssignment(t, day2.id, walk.id);
await makeDayAssignment(t, day1.id, cafe.id);
const other = await makeTrip(t, user.id);
const otherDay = await makeDay(t, other.id, { day_number: 1 });
const otherWalk = await makePlace(t, other.id, { name: 'Harbour' });
await makeTour(t, otherWalk.id, { tourTypeRef: 'walk' });
const foreign = await makeDayAssignment(t, otherDay.id, otherWalk.id);
return { trip, day1, day2, walk, cafe, onDay1, onDay2, other, otherDay, otherWalk, foreign };
}
describe('TourStationVisitsRepository', () => {
it('TSVREPO-001: assignmentInTrip names the place and day of an assignment of the trip, nothing across trips', async () => {
const { trip, day1, walk, onDay1, foreign } = await fixture();
expect(await repo.assignmentInTrip(onDay1.id, trip.id)).toEqual({ place_id: walk.id, day_id: day1.id });
expect(await repo.assignmentInTrip(foreign.id, trip.id)).toBeUndefined();
expect(await repo.assignmentInTrip(999_999, trip.id)).toBeUndefined();
});
it('TSVREPO-002: assignmentOnDay finds the walk on that day of that trip, and the first of two on one day', async () => {
const { trip, day1, day2, walk, cafe, onDay1, onDay2, other, otherDay } = await fixture();
expect(await repo.assignmentOnDay(trip.id, walk.id, day1.id)).toBe(onDay1.id);
expect(await repo.assignmentOnDay(trip.id, walk.id, day2.id)).toBe(onDay2.id);
expect(await repo.assignmentOnDay(other.id, walk.id, day1.id)).toBeUndefined();
expect(await repo.assignmentOnDay(trip.id, walk.id, otherDay.id)).toBeUndefined();
expect(await repo.assignmentOnDay(trip.id, cafe.id, day2.id)).toBeUndefined();
// A second assignment on day 2 placed first in the day keeps the first one found by id.
await makeDayAssignment(t, day2.id, walk.id, { order_index: -1 });
expect(await repo.assignmentOnDay(trip.id, walk.id, day2.id)).toBe(onDay2.id);
});
it('TSVREPO-003: set marks a station, and a second set replaces its status on the same row', async () => {
const { onDay1 } = await fixture();
await repo.set(onDay1.id, 'stop-b', 'visited');
await repo.set(onDay1.id, 'stop-a', 'skipped');
expect(await repo.listForAssignment(onDay1.id)).toEqual([
{ assignment_id: onDay1.id, station_key: 'stop-a', status: 'skipped' },
{ assignment_id: onDay1.id, station_key: 'stop-b', status: 'visited' },
]);
await repo.set(onDay1.id, 'stop-a', 'visited');
const rows = await findRows(t, TourStationVisits, { assignment: onDay1.id }, { station_key: 'asc' });
expect(rows.map((row) => [row.station_key, row.status])).toEqual([
['stop-a', 'visited'],
['stop-b', 'visited'],
]);
expect(rows[0]?.updated_at).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
});
it('TSVREPO-004: clear takes back only that station on that day', async () => {
const { onDay1, onDay2 } = await fixture();
await markStationVisit(t, onDay1.id, 'stop-a', 'visited');
await markStationVisit(t, onDay1.id, 'stop-b', 'skipped');
await markStationVisit(t, onDay2.id, 'stop-a', 'visited');
await repo.clear(onDay1.id, 'stop-a');
expect(await repo.listForAssignment(onDay1.id)).toEqual([
{ assignment_id: onDay1.id, station_key: 'stop-b', status: 'skipped' },
]);
expect(await repo.listForAssignment(onDay2.id)).toEqual([
{ assignment_id: onDay2.id, station_key: 'stop-a', status: 'visited' },
]);
});
it('TSVREPO-005: listForTrip lists every visit of the trip by assignment and key, none of another trip', async () => {
const { trip, onDay1, onDay2, foreign } = await fixture();
await markStationVisit(t, onDay2.id, 'stop-a', 'skipped');
await markStationVisit(t, onDay1.id, 'stop-b', 'visited');
await markStationVisit(t, onDay1.id, 'stop-a', 'visited');
await markStationVisit(t, foreign.id, 'stop-a', 'visited');
expect(await repo.listForTrip(trip.id)).toEqual([
{ assignment_id: onDay1.id, station_key: 'stop-a', status: 'visited' },
{ assignment_id: onDay1.id, station_key: 'stop-b', status: 'visited' },
{ assignment_id: onDay2.id, station_key: 'stop-a', status: 'skipped' },
]);
});
it('TSVREPO-008: listForPlace lists the visits of one walk on all its days with the day, none of another walk or trip', async () => {
const { trip, day1, day2, walk, onDay1, onDay2, other, otherWalk, foreign } = await fixture();
await markStationVisit(t, onDay2.id, 'stop-a', 'skipped');
await markStationVisit(t, onDay1.id, 'stop-b', 'visited');
await markStationVisit(t, onDay1.id, 'stop-a', 'visited');
await markStationVisit(t, foreign.id, 'stop-a', 'visited');
expect(await repo.listForPlace(trip.id, walk.id)).toEqual([
{ day_id: day1.id, assignment_id: onDay1.id, station_key: 'stop-a', status: 'visited' },
{ day_id: day1.id, assignment_id: onDay1.id, station_key: 'stop-b', status: 'visited' },
{ day_id: day2.id, assignment_id: onDay2.id, station_key: 'stop-a', status: 'skipped' },
]);
expect(await repo.listForPlace(other.id, walk.id)).toEqual([]);
expect(await repo.listForPlace(trip.id, otherWalk.id)).toEqual([]);
});
it('TSVREPO-006:dropStationsNotIn keeps the named stations on every day, drops the rest, leaves other walks alone and names the days', async () => {
const { walk, onDay1, onDay2, foreign } = await fixture();
await markStationVisit(t, onDay1.id, 'stop-a', 'visited');
await markStationVisit(t, onDay1.id, 'stop-b', 'visited');
await markStationVisit(t, onDay2.id, 'stop-a', 'skipped');
await markStationVisit(t, onDay2.id, 'stop-b', 'visited');
await markStationVisit(t, foreign.id, 'stop-b', 'visited');
expect(await repo.dropStationsNotIn(walk.id, ['stop-a'])).toEqual([onDay1.id, onDay2.id].sort((a, b) => a - b));
expect((await repo.listForAssignment(onDay1.id)).map((v) => v.station_key)).toEqual(['stop-a']);
expect((await repo.listForAssignment(onDay2.id)).map((v) => v.station_key)).toEqual(['stop-a']);
expect(await repo.dropStationsNotIn(walk.id, ['stop-a'])).toEqual([]);
await markStationVisit(t, onDay2.id, 'stop-c', 'skipped');
expect(await repo.dropStationsNotIn(walk.id, [])).toEqual([onDay1.id, onDay2.id].sort((a, b) => a - b));
expect(await repo.listForAssignment(onDay1.id)).toEqual([]);
expect(await repo.listForAssignment(onDay2.id)).toEqual([]);
expect(await repo.listForAssignment(foreign.id)).toEqual([
{ assignment_id: foreign.id, station_key: 'stop-b', status: 'visited' },
]);
});
it('TSVREPO-007: a removed day assignment takes its visits with it', async () => {
const { onDay1, onDay2 } = await fixture();
await markStationVisit(t, onDay1.id, 'stop-a', 'visited');
await markStationVisit(t, onDay2.id, 'stop-a', 'visited');
await deleteRows(t, DayAssignments, { id: onDay1.id });
expect(await repo.listForAssignment(onDay1.id)).toEqual([]);
expect(await repo.listForAssignment(onDay2.id)).toHaveLength(1);
});
});