mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
382ba79584
A visit is a place planned on a day of one of the reader's trips; nothing is kept or confirmed by hand. A place keeps a log of changes to its details, links the tours and bookings it is about, and carries tags private to the reader, which a copy into a trip takes along only when picked. Contracts, routes, MCP tools and migrations for both engines.
184 lines
9.6 KiB
TypeScript
184 lines
9.6 KiB
TypeScript
/**
|
|
* PlaceActivityLinksRepository and the table under it (PLC-18): the checks a
|
|
* link relies on, the links it writes and takes off, the bookings and tours
|
|
* it reads for trip places (linked, by their own place, stay or plan entry,
|
|
* by a station), the kept tours of saved places, and the shape the CHECK,
|
|
* the UNIQUE pairs and the cascades hold.
|
|
*/
|
|
import { CollectionPlaces } from '../../../../src/db/entities/CollectionPlaces.entity';
|
|
import { PlaceActivityLinks } from '../../../../src/db/entities/PlaceActivityLinks.entity';
|
|
import { Places } from '../../../../src/db/entities/Places.entity';
|
|
import { Reservations } from '../../../../src/db/entities/Reservations.entity';
|
|
import { Users } from '../../../../src/db/entities/Users.entity';
|
|
import type { PlaceActivityLinksRepository } from '../../../../src/db/repositories/PlaceActivityLinks.repository';
|
|
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
|
import { linkPlaceActivity } from '../../../helpers/factories/place-activities';
|
|
import { countRows, deleteRows, findRow, updateRows } from '../../../helpers/factories/rows';
|
|
import { makeUser } from '../../../helpers/factories/users';
|
|
import { seedPlaceActivities, type PlaceActivitiesFixture } from '../../../helpers/place-activities-fixture';
|
|
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: PlaceActivityLinksRepository;
|
|
let f: PlaceActivitiesFixture;
|
|
|
|
beforeAll(async () => {
|
|
t = await createTestOrm(testDb);
|
|
repo = t.repo(PlaceActivityLinks);
|
|
});
|
|
beforeEach(async () => {
|
|
resetTestDb(testDb);
|
|
t.clear();
|
|
f = await seedPlaceActivities(t);
|
|
});
|
|
afterAll(async () => {
|
|
await t.close();
|
|
testDb.close();
|
|
});
|
|
|
|
describe('PlaceActivityLinksRepository', () => {
|
|
it('PAL-REPO-001: finds the places and activities a link names only where they belong', async () => {
|
|
const { alps, rome, places, saved, bookings, kept, lists } = f;
|
|
expect(await repo.tripPlace(alps.id, places.cafe.id)).toEqual({ id: places.cafe.id, trip_id: alps.id });
|
|
expect(await repo.tripPlace(rome.id, places.cafe.id)).toBeUndefined();
|
|
expect(await repo.savedPlace(saved.savedCafe.id)).toEqual({ id: saved.savedCafe.id, collection_id: lists.food.id });
|
|
expect(await repo.savedPlace(999999)).toBeUndefined();
|
|
expect(await repo.bookingInTrip(alps.id, bookings.guided.id)).toBe(true);
|
|
expect(await repo.bookingInTrip(rome.id, bookings.guided.id)).toBe(false);
|
|
expect(await repo.tourInTrip(alps.id, places.ridge.id)).toBe(true);
|
|
expect(await repo.tourInTrip(alps.id, places.cafe.id)).toBe(false);
|
|
expect(await repo.tourInTrip(rome.id, places.ridge.id)).toBe(false);
|
|
expect(await repo.keptTourInList(lists.food.id, kept.keptRidge.id)).toBe(true);
|
|
expect(await repo.keptTourInList(lists.food.id, kept.keptRome.id)).toBe(false);
|
|
});
|
|
|
|
it('PAL-REPO-002: links once, unlinks once, and reads the linked bookings, tours and kept tours', async () => {
|
|
const { me, places, saved, bookings, kept } = f;
|
|
expect(await repo.link({ place_id: places.cafe.id, reservation_id: bookings.guided.id }, me.id)).toBe(true);
|
|
expect(await repo.link({ place_id: places.cafe.id, reservation_id: bookings.guided.id }, me.id)).toBe(false);
|
|
expect(await repo.link({ place_id: places.cafe.id, tour_place_id: places.ridge.id }, me.id)).toBe(true);
|
|
expect(
|
|
await repo.link({ collection_place_id: saved.savedCafe.id, collection_tour_id: kept.keptRidge.id }, me.id),
|
|
).toBe(true);
|
|
|
|
const [booking] = await repo.linkedBookings([places.cafe.id]);
|
|
expect(booking).toMatchObject({
|
|
source_id: places.cafe.id,
|
|
trip_id: f.alps.id,
|
|
trip_title: 'Alps',
|
|
reservation_id: bookings.guided.id,
|
|
title: 'Guided tour',
|
|
type: 'tour',
|
|
status: 'confirmed',
|
|
reservation_time: '2026-07-01T10:00',
|
|
date: '2026-07-01',
|
|
day_number: 1,
|
|
});
|
|
expect(await repo.linkedTours([places.cafe.id])).toEqual([
|
|
expect.objectContaining({
|
|
source_id: places.cafe.id,
|
|
tour_id: places.ridge.id,
|
|
title: 'Ridge walk',
|
|
tour_type: 'hike',
|
|
}),
|
|
]);
|
|
expect(await repo.linkedKeptTours([saved.savedCafe.id])).toEqual([
|
|
expect.objectContaining({
|
|
source_id: saved.savedCafe.id,
|
|
collection_id: f.lists.food.id,
|
|
collection_name: 'Food',
|
|
collection_tour_id: kept.keptRidge.id,
|
|
title: 'Kept ridge',
|
|
}),
|
|
]);
|
|
expect(await repo.linkedBookings([])).toEqual([]);
|
|
|
|
expect(await repo.unlinkTrip(places.cafe.id, { reservation_id: bookings.guided.id })).toBe(true);
|
|
expect(await repo.unlinkTrip(places.cafe.id, { reservation_id: bookings.guided.id })).toBe(false);
|
|
expect(await repo.unlinkTrip(places.cafe.id, { tour_place_id: places.ridge.id })).toBe(true);
|
|
expect(await repo.unlinkSaved(saved.savedCafe.id, kept.keptRidge.id)).toBe(true);
|
|
expect(await repo.unlinkSaved(saved.savedCafe.id, kept.keptRidge.id)).toBe(false);
|
|
expect(await countRows(t, PlaceActivityLinks, {})).toBe(0);
|
|
});
|
|
|
|
it('PAL-REPO-003: reads the bookings that belong to a place by its own place, stay or plan entry, and the station tours', async () => {
|
|
const { places, bookings } = f;
|
|
const own = await repo.implicitBookings([places.hotel.id, places.camp.id, places.cafe.id]);
|
|
expect(own.map((row) => [row.source_id, row.title, row.via]).sort()).toEqual(
|
|
[
|
|
[places.hotel.id, 'Hotel Sonne', 'place'],
|
|
[places.camp.id, 'Lake cabin', 'stay'],
|
|
[places.cafe.id, 'Dinner', 'plan'],
|
|
].sort(),
|
|
);
|
|
expect(own.find((row) => row.via === 'stay')?.reservation_id).toBe(bookings.cabin.id);
|
|
expect(await repo.implicitTours([places.museum.id])).toEqual([
|
|
expect.objectContaining({ source_id: places.museum.id, tour_id: places.ridge.id }),
|
|
]);
|
|
expect(await repo.implicitTours([places.cafe.id])).toEqual([]);
|
|
});
|
|
|
|
it('PAL-REPO-004: lists the candidates of a trip and a list', async () => {
|
|
const bookings = await repo.tripBookings(f.alps.id);
|
|
expect(bookings.map((row) => row.title).sort()).toEqual(['Dinner', 'Guided tour', 'Hotel Sonne', 'Lake cabin']);
|
|
expect(bookings.every((row) => row.source_id === 0)).toBe(true);
|
|
const tours = await repo.tripTours(f.alps.id);
|
|
expect(tours.map((row) => row.title)).toEqual(['Old town walk', 'Ridge walk']);
|
|
expect(tours.find((row) => row.title === 'Old town walk')?.tour_type).toBe('walk');
|
|
const kept = await repo.keptTours(f.lists.food.id);
|
|
expect(kept.map((row) => row.title)).toEqual(['Kept lake loop', 'Kept ridge']);
|
|
});
|
|
|
|
it('PAL-REPO-005: the CHECK refuses a mixed or empty link, the UNIQUE a second one, and an account that goes leaves its links', async () => {
|
|
const { me, places, saved, bookings } = f;
|
|
await expect(
|
|
linkPlaceActivity(t, {
|
|
place: places.cafe.id,
|
|
reservation: bookings.guided.id,
|
|
tourPlace: places.ridge.id,
|
|
} as never),
|
|
).rejects.toThrow();
|
|
await expect(
|
|
linkPlaceActivity(t, { place: places.cafe.id, collectionTour: f.kept.keptRidge.id } as never),
|
|
).rejects.toThrow();
|
|
await expect(linkPlaceActivity(t, { collectionPlace: saved.savedCafe.id } as never)).rejects.toThrow();
|
|
await linkPlaceActivity(t, { place: places.cafe.id, reservation: bookings.guided.id }, me.id);
|
|
await expect(linkPlaceActivity(t, { place: places.cafe.id, reservation: bookings.guided.id })).rejects.toThrow();
|
|
// Who linked it is kept as unknown when that account goes; the link stays.
|
|
const { user: helper } = await makeUser(t);
|
|
await linkPlaceActivity(t, { place: places.museum.id, reservation: bookings.guided.id }, helper.id);
|
|
await deleteRows(t, Users, { id: helper.id });
|
|
const kept = await findRow(t, PlaceActivityLinks, { place: places.museum.id, reservation: bookings.guided.id });
|
|
expect(kept?.linked_by).toBeNull();
|
|
expect(await countRows(t, PlaceActivityLinks, {})).toBe(2);
|
|
});
|
|
|
|
it('PAL-REPO-006: deleting the booking, the tour or the place takes only its links', async () => {
|
|
const { places, bookings } = f;
|
|
await linkPlaceActivity(t, { place: places.cafe.id, reservation: bookings.guided.id });
|
|
await linkPlaceActivity(t, { place: places.museum.id, reservation: bookings.guided.id });
|
|
await linkPlaceActivity(t, { place: places.cafe.id, tourPlace: places.ridge.id });
|
|
await deleteRows(t, Reservations, { id: bookings.guided.id });
|
|
expect(await countRows(t, PlaceActivityLinks, {})).toBe(1);
|
|
await deleteRows(t, Places, { id: places.ridge.id });
|
|
expect(await countRows(t, PlaceActivityLinks, {})).toBe(0);
|
|
expect(await findRow(t, Places, { id: places.cafe.id })).not.toBeNull();
|
|
});
|
|
|
|
it('PAL-REPO-007: a saved place moved to another list reads none of the tours of its old list, and has them back on its return', async () => {
|
|
const { me, saved, kept, lists } = f;
|
|
await repo.link({ collection_place_id: saved.savedCafe.id, collection_tour_id: kept.keptRidge.id }, me.id);
|
|
await updateRows(t, CollectionPlaces, { id: saved.savedCafe.id }, { collection: lists.sights.id });
|
|
expect(await repo.linkedKeptTours([saved.savedCafe.id])).toEqual([]);
|
|
// The link itself stays: it belongs to the old list, which may get the place back.
|
|
expect(await countRows(t, PlaceActivityLinks, { collectionPlace: saved.savedCafe.id })).toBe(1);
|
|
await updateRows(t, CollectionPlaces, { id: saved.savedCafe.id }, { collection: lists.food.id });
|
|
const back = await repo.linkedKeptTours([saved.savedCafe.id]);
|
|
expect(back.map((row) => [row.title, row.collection_id])).toEqual([['Kept ridge', lists.food.id]]);
|
|
});
|
|
});
|