Files
TREK/server/tests/unit/db/repositories/TourPackingLinks.repository.test.ts
T
Maurice f833a05461 feat(tours): point a tour at its trip's packing list
A tour can now point at the packing list of its trip, as a whole or at one
of its categories (say "Hiking gear" for a hike), and shows it in its detail.
The new Packing list section reads the list live as the user may see it: the
backpack tile with the item count on its corner and the category's own
colour, how many are packed with a thin progress bar, the first items ticked
off or not with their quantity, how many more there are, and a button that
opens the trip's Lists tab. Who may change the tour links one, changes it or
lets go through a dialog that lists the whole list and every category with
its colour and item count, and writes only on its confirm.

It is a reference, never a copy: the items stay in Packing, are ticked off
and edited there, and each traveller sees in the tour only the items Packing
shows them, so an item kept to oneself never shows through a tour. A
category that is renamed, cleared or only holds items of others reads as
missing, an empty one as empty, a link that could not be read says so with a
retry, and while Lists are switched off the link stays, dimmed, and can
still be let go. Removing the link, or the tour, leaves the list and every
item as they are. Nothing suggests or changes an item by itself.

On the server the link is a row of its own in the tours domain
(tour_packing_links, migration 046110), one per tour and gone with it.
GET, PUT and DELETE /api/trips/:tripId/tours/:placeId/packing-link read it
with the trip and change it with place_edit; linking needs the Lists addon
and, for a category, an item of it the user can see. The writes answer the
tour with its link and tell the trip through tours:changed, so the offline
client queues them as writes of the tour and replays them like one. A copied
trip keeps its tours' links, a tour saved as new or taken over does not.
The MCP tools get_tour_packing, link_tour_packing and unlink_tour_packing run
over the same service and checks; the read also hands the tour's facts as
context for suggesting what to pack, which only becomes an item through the
packing tools. The empty category placeholder now lives in @trek/shared so
both sides leave it out alike. All 27 locales carry the new strings, and
the wiki describes the link and the tools.
2026-10-20 15:35:08 +02:00

106 lines
4.8 KiB
TypeScript

/**
* `TourPackingLinksRepository` (P2D-XD-01): the packing list, or the category
* of it, a tour points at. Seeded through the ORM factories over the migrated
* snapshot schema.
*/
import { PackingItems } from '../../../../src/db/entities/PackingItems.entity';
import { Places } from '../../../../src/db/entities/Places.entity';
import { TourPackingLinks } from '../../../../src/db/entities/TourPackingLinks.entity';
import { Users } from '../../../../src/db/entities/Users.entity';
import type { TourPackingLinksRepository } from '../../../../src/db/repositories/TourPackingLinks.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { makePackingItem } from '../../../helpers/factories/packing';
import { makePlace } from '../../../helpers/factories/places';
import { countRows, deleteRows, findRow, updateRows } from '../../../helpers/factories/rows';
import { linkTourPacking, makeTour } from '../../../helpers/factories/tours';
import { 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: TourPackingLinksRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
repo = t.repo(TourPackingLinks);
});
beforeEach(() => {
resetTestDb(testDb);
t.clear();
});
afterAll(async () => {
await t.close();
testDb.close();
});
/** A trip with a hike and a packing item, the user who links and a second tour that points at nothing. */
async function fixture() {
const { user } = await makeUser(t);
const trip = await makeTrip(t, user.id);
const hike = await makePlace(t, trip.id, { name: 'Ridge' });
await makeTour(t, hike.id);
const walk = await makePlace(t, trip.id, { name: 'Old town' });
await makeTour(t, walk.id, { tourTypeRef: 'walk' });
const item = await makePackingItem(t, trip.id, { name: 'Boots', category: 'Hiking gear' });
return { user, trip, hike, walk, item };
}
describe('TourPackingLinksRepository', () => {
it('TPLREPO-001: a tour without a link reads as undefined', async () => {
const { hike } = await fixture();
expect(await repo.findForTour(hike.id)).toBeUndefined();
});
it('TPLREPO-002: put points the tour at a category, or at the whole list with null', async () => {
const { user, hike, walk } = await fixture();
await repo.put(hike.id, 'Hiking gear', user.id);
await repo.put(walk.id, null, user.id);
expect(await repo.findForTour(hike.id)).toEqual({ category: 'Hiking gear', linked_at: expect.any(String) });
expect(await repo.findForTour(walk.id)).toEqual({ category: null, linked_at: expect.any(String) });
expect((await findRow(t, TourPackingLinks, { place: hike.id }))?.linked_by).toBe(user.id);
});
it('TPLREPO-003: a second put replaces what the tour pointed at, on the same row, with a new time', async () => {
const { user, hike } = await fixture();
const { user: other } = await makeUser(t);
await repo.put(hike.id, 'Hiking gear', user.id);
await updateRows(t, TourPackingLinks, { place: hike.id }, { linked_at: '2020-01-01 00:00:00' });
await repo.put(hike.id, null, other.id);
const row = await findRow(t, TourPackingLinks, { place: hike.id });
expect(row).toMatchObject({ category: null, linked_by: other.id });
expect(row?.linked_at).not.toBe('2020-01-01 00:00:00');
expect(await countRows(t, TourPackingLinks)).toBe(1);
});
it('TPLREPO-004: remove lets go of the link and says whether there was one, and leaves the packing items alone', async () => {
const { user, hike, walk } = await fixture();
await repo.put(hike.id, 'Hiking gear', user.id);
expect(await repo.remove(hike.id)).toBe(true);
expect(await repo.remove(hike.id)).toBe(false);
expect(await repo.remove(walk.id)).toBe(false);
expect(await repo.findForTour(hike.id)).toBeUndefined();
expect(await countRows(t, PackingItems)).toBe(1);
});
it('TPLREPO-005: the link goes with its tour, and turns its author unknown when that account goes', async () => {
const { user, hike, walk } = await fixture();
const { user: linker } = await makeUser(t);
await linkTourPacking(t, hike.id, 'Hiking gear', user.id);
await linkTourPacking(t, walk.id, null, linker.id);
await deleteRows(t, Users, { id: linker.id });
const kept = await findRow(t, TourPackingLinks, { place: walk.id });
expect(kept?.category).toBeNull();
expect(kept?.linked_by ?? null).toBeNull();
await deleteRows(t, Places, { id: hike.id });
expect(await repo.findForTour(hike.id)).toBeUndefined();
expect(await repo.findForTour(walk.id)).toEqual({ category: null, linked_at: expect.any(String) });
});
});