Files
TREK/server/tests/unit/db/repositories/TourWaypoints.repository.test.ts
T
Maurice c956fe92ba Merge dev into feat/tours and port Tours onto MikroORM
dev now runs on MikroORM, so the positional migrations, seeds.ts and
DatabaseService the Tours server side was built on are gone. Behaviour
is unchanged; persistence moves onto dev's architecture:

- one MikroORM migration for tour_types, tours and tour_waypoints,
  idempotent so instances that ran the old positional steps keep
  their data; the hike tour type is seeded inside it, the addon row
  in the AddonSeeder
- entities and repositories for the three tables, tours domain on
  repositories and UnitOfWork
- tour facet joins in places, assignments and days re-applied on the
  repository code

Fixes found while porting:

- GPX import checks addon, trip access and permission in the handler
  instead of guards, so a refused upload gets a 403/404 instead of a
  connection reset
- copying a trip copies its tours and waypoints
- a tour cannot be moved onto a day that already has it
- updateTour checks existence inside its transaction

Adds tours e2e tests and rewrites the tours unit tests onto the
migrated test database.
2026-10-06 22:43:09 +02:00

79 lines
3.2 KiB
TypeScript

/**
* `TourWaypointsRepository`: TW1 (`listForPlace`), TW2 (`listForTrip`), TW3
* (`insertForPlace`) and TW4 (`deleteForPlace`).
*/
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { createPlace, createTrip, createUser } from '../../../helpers/factories';
import { createTour } from '../../../helpers/tours-repos';
import { TourWaypoints } from '../../../../src/db/entities/TourWaypoints.entity';
import type { TourWaypointRow, TourWaypointsRepository } from '../../../../src/db/repositories/TourWaypoints.repository';
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 });
});
});