Files
TREK/server/tests/unit/db/repositories/PlaceChangeLog.repository.test.ts
T
Maurice 382ba79584 feat(places): visits read from trips, change history, linked tours and bookings, personal tags
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.
2026-11-29 21:16:50 +01:00

180 lines
8.7 KiB
TypeScript

/**
* PlaceChangeLogRepository and the table under it (PLC-14): the details it
* reads of trip places and saved places, the trip places a photo fill
* reaches, the changes it writes and drops, the page it reads with who made
* each change, the list a saved place's change was made in, the CHECK on
* its owner, and the rows that go with their place, list or account.
*/
import { CollectionPlaces } from '../../../../src/db/entities/CollectionPlaces.entity';
import { Collections } from '../../../../src/db/entities/Collections.entity';
import { PlaceChangeLog } from '../../../../src/db/entities/PlaceChangeLog.entity';
import { Places } from '../../../../src/db/entities/Places.entity';
import { Users } from '../../../../src/db/entities/Users.entity';
import type { PlaceChangeLogRepository } from '../../../../src/db/repositories/PlaceChangeLog.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { makeCollection, makeCollectionPlace } from '../../../helpers/factories/collections';
import { makeCategory, makePlace } from '../../../helpers/factories/places';
import { countRows, deleteRows, insertRow, updateRows } from '../../../helpers/factories/rows';
import { makeTour } from '../../../helpers/factories/tours';
import { makeTrip } from '../../../helpers/factories/trips';
import { makeUser } from '../../../helpers/factories/users';
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: PlaceChangeLogRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
repo = t.repo(PlaceChangeLog);
});
beforeEach(async () => {
t.clear();
await deleteRows(t, PlaceChangeLog);
});
afterAll(async () => {
await t.close();
testDb.close();
});
const change = (over: Partial<Parameters<PlaceChangeLogRepository['insertChanges']>[0][number]>) => ({
place_id: null,
collection_place_id: null,
field: 'name',
old_value: 'A',
new_value: 'B',
user_id: null,
automatic: 0,
...over,
});
async function seed() {
const { user } = await makeUser(t);
const category = await makeCategory(t);
const trip = await makeTrip(t, user.id);
const place = await makePlace(t, trip.id, {
name: 'Café',
address: 'Herrengasse 14',
lat: 48.2,
lng: 16.3,
category: category.id,
opening_hours: 'Mo-Fr 8-18',
google_place_id: 'g-cafe',
});
const list = await makeCollection(t, user.id);
const saved = await makeCollectionPlace(t, list.id, user.id, { name: 'Saved café', status: 'want', notes: 'Cake' });
return { user, category, trip, place, list, saved };
}
describe('PlaceChangeLogRepository', () => {
it('PCLREPO-001: reads the details of trip places and saved places with their category names', async () => {
const s = await seed();
expect(await repo.tripPlaceDetails([s.place.id, 999_999])).toEqual([
expect.objectContaining({
id: s.place.id,
name: 'Café',
address: 'Herrengasse 14',
lat: 48.2,
lng: 16.3,
category_name: s.category.name,
opening_hours: 'Mo-Fr 8-18',
}),
]);
expect(await repo.savedPlaceDetails([s.saved.id])).toEqual([
expect.objectContaining({ id: s.saved.id, name: 'Saved café', status: 'want', notes: 'Cake' }),
]);
expect(await repo.tripPlaceDetails([])).toEqual([]);
});
it('PCLREPO-002: finds the trip places of a Google id that have no picture yet', async () => {
const s = await seed();
const pictured = await makePlace(t, s.trip.id, { google_place_id: 'g-cafe', image_url: '/uploads/places/a.jpg' });
const blank = await makePlace(t, s.trip.id, { google_place_id: 'g-cafe', image_url: '' });
const ids = await repo.tripPlaceIdsWithoutImage('g-cafe');
expect([...ids].sort((a, b) => a - b)).toEqual([s.place.id, blank.id].sort((a, b) => a - b));
expect(ids).not.toContain(pictured.id);
});
it('PCLREPO-003: writes changes, pages them newest first with their editor, and drops a cleared field', async () => {
const s = await seed();
await repo.insertChanges([
change({ place_id: s.place.id, field: 'name', user_id: s.user.id }),
change({ place_id: s.place.id, field: 'notes', old_value: null, new_value: 'x', automatic: 1 }),
change({ collection_place_id: s.saved.id, field: 'status', old_value: 'idea', new_value: 'want' }),
]);
await repo.insertChanges([]);
const page = await repo.page([s.place.id], [s.saved.id], null, 10);
expect(page.map((row) => row.field)).toEqual(['status', 'notes', 'name']);
expect(page[2]).toEqual(expect.objectContaining({ user_id: s.user.id, username: s.user.username, automatic: 0 }));
expect(page[1]).toEqual(expect.objectContaining({ user_id: null, username: null, automatic: 1 }));
const after = page[1]?.id ?? 0;
expect((await repo.page([s.place.id], [s.saved.id], after, 10)).map((row) => row.field)).toEqual(['name']);
expect((await repo.page([s.place.id], [], null, 1)).map((row) => row.field)).toEqual(['notes']);
expect(await repo.page([], [], null, 10)).toEqual([]);
await repo.deleteCleared([
{ place_id: s.place.id, collection_place_id: null, fields: ['notes'] },
{ place_id: null, collection_place_id: s.saved.id, fields: ['status'] },
{ place_id: s.place.id, collection_place_id: null, fields: [] },
]);
await repo.deleteCleared([]);
expect((await repo.page([s.place.id], [s.saved.id], null, 10)).map((row) => row.field)).toEqual(['name']);
});
it('PCLREPO-004: the changes go with their place, and name nobody once the account is gone', async () => {
const s = await seed();
const { user: other } = await makeUser(t);
await repo.insertChanges([
change({ place_id: s.place.id, user_id: other.id }),
change({ collection_place_id: s.saved.id, user_id: other.id }),
]);
await deleteRows(t, Users, { id: other.id });
expect(await countRows(t, PlaceChangeLog, { collectionPlace: s.saved.id, user: null })).toBe(1);
await deleteRows(t, Places, { id: s.place.id });
expect(await countRows(t, PlaceChangeLog, { place: s.place.id })).toBe(0);
expect(await countRows(t, PlaceChangeLog)).toBe(1);
});
it('PCLREPO-005: a saved place moved to another list shows only the changes made there, the old ones again on its return', async () => {
const s = await seed();
await repo.insertChanges([change({ collection_place_id: s.saved.id, field: 'notes', user_id: s.user.id })]);
expect(await countRows(t, PlaceChangeLog, { collectionPlace: s.saved.id, collection: s.list.id })).toBe(1);
const other = await makeCollection(t, s.user.id);
await updateRows(t, CollectionPlaces, { id: s.saved.id }, { collection: other.id });
expect(await repo.page([], [s.saved.id], null, 10)).toEqual([]);
await repo.insertChanges([change({ collection_place_id: s.saved.id, field: 'name' })]);
expect((await repo.page([], [s.saved.id], null, 10)).map((row) => row.field)).toEqual(['name']);
await updateRows(t, CollectionPlaces, { id: s.saved.id }, { collection: s.list.id });
expect((await repo.page([], [s.saved.id], null, 10)).map((row) => row.field)).toEqual(['notes']);
// A deleted list takes the changes made in it; the place lives on in the other one.
await updateRows(t, CollectionPlaces, { id: s.saved.id }, { collection: other.id });
await deleteRows(t, Collections, { id: s.list.id });
expect(await countRows(t, PlaceChangeLog, { collectionPlace: s.saved.id })).toBe(1);
});
it('PCLREPO-006: the CHECK holds exactly one owner, and a list beside a saved place only', async () => {
const s = await seed();
const row = { field: 'name', old_value: 'A', new_value: 'B' };
await expect(insertRow(t, PlaceChangeLog, { ...row })).rejects.toThrow();
await expect(
insertRow(t, PlaceChangeLog, { ...row, place: s.place.id, collectionPlace: s.saved.id, collection: s.list.id }),
).rejects.toThrow();
await expect(insertRow(t, PlaceChangeLog, { ...row, collectionPlace: s.saved.id })).rejects.toThrow();
await expect(insertRow(t, PlaceChangeLog, { ...row, place: s.place.id, collection: s.list.id })).rejects.toThrow();
await insertRow(t, PlaceChangeLog, { ...row, place: s.place.id });
await insertRow(t, PlaceChangeLog, { ...row, collectionPlace: s.saved.id, collection: s.list.id });
expect(await countRows(t, PlaceChangeLog)).toBe(2);
});
it('PCLREPO-007: a tour has no details to follow, so no write records a change of it', async () => {
const s = await seed();
const tour = await makePlace(t, s.trip.id, { name: 'Ridge loop' });
await makeTour(t, tour.id);
expect((await repo.tripPlaceDetails([s.place.id, tour.id])).map((row) => row.id)).toEqual([s.place.id]);
});
});