Files
TREK/server/tests/unit/nest/collections.helpers.test.ts
T
Maurice 82165855c7 feat(collections): own order, KML/KMZ import, copy options, position search, place names on the trip map
- A list keeps its own order: drag a place by its grip (desktop and phone) or
  move it with the arrow keys. Reordering is off while a search or filter hides
  places and the list says why. New PUT /collections/:id/places/reorder and the
  MCP tool reorder_collection_places; new places go to the end. Client and
  server now order ties the same way (sort_order, oldest first, id).
- Copying saved places into a trip no longer takes their notes and tags along
  unasked. The picker offers both; a tag taken along becomes a trip tag (a trip
  tag of the same name is reused), never someone's personal tag.
- The source trip and place a saved place names are only kept when the user can
  open that trip and the place belongs to it.
- A KML or KMZ map can be read into a list: the dialog lists every placemark to
  keep or leave out and says which entries were skipped and why
  (POST /collections/map/read writes nothing; the import does).
- A saved place without coordinates says so and can be looked up on the map
  from its edit form, on the phone too, where a missing position could not be
  added at all. A typed address without a pick says it will not be on the map.
- The trip map can show the place names beside the pins (desktop and phone,
  both renderers). Names that would cover another pin or a control give way,
  the selected place and the planned stops first.
2026-11-29 14:57:00 +01:00

242 lines
8.1 KiB
TypeScript

/**
* The pure helpers of the collections domain: the link helpers, which read and
* write the JSON TEXT column a list or a saved place keeps its links in, the
* listing fields an update writes, the cursor of a page of places, and a saved
* place as the list file carries it, the grouping of the batched hydration
* reads, the trip dedup set a copy checks, the place fields a trip save takes
* and the order a reorder writes. COLLECTIONS-HELPERS-001 to 013.
*/
import type { CollectionPlaceRow } from '../../../src/db/repositories/Collections.repository';
import type { PlaceRow } from '../../../src/db/repositories/Places.repository';
import {
decodePlacesCursor,
encodePlacesCursor,
filePlaceOf,
groupByPid,
listingWriteOf,
parseLinks,
placeOrderOf,
serializeLinks,
tripDedupSet,
tripPlaceFields,
} from '../../../src/nest/collections/collections.helpers';
import { describe, expect, it } from 'vitest';
const links = [{ url: 'https://menu.example/', label: 'Menu' }];
describe('parseLinks', () => {
it('COLLECTIONS-HELPERS-001: reads a stored array of links back', () => {
expect(parseLinks(JSON.stringify(links))).toEqual(links);
});
it('COLLECTIONS-HELPERS-002: answers undefined for a missing, empty, unreadable or non-array value', () => {
expect(parseLinks(null)).toBeUndefined();
expect(parseLinks('')).toBeUndefined();
expect(parseLinks('{not json')).toBeUndefined();
expect(parseLinks(JSON.stringify(links[0]))).toBeUndefined();
});
});
describe('serializeLinks', () => {
it('COLLECTIONS-HELPERS-003: writes links as JSON and no links as null', () => {
expect(serializeLinks(links)).toBe(JSON.stringify(links));
expect(serializeLinks([])).toBeNull();
expect(serializeLinks(undefined)).toBeNull();
});
});
describe('listingWriteOf', () => {
it('COLLECTIONS-HELPERS-004: writes each provider id, contact and price field the update names, null clearing it', () => {
expect(
listingWriteOf({
google_place_id: 'ChIJseptime',
google_ftid: null,
osm_id: 'node/42',
website: 'https://septime-charonne.fr',
phone: null,
price: 0,
currency: 'EUR',
}),
).toEqual({
google_place_id: 'ChIJseptime',
google_ftid: null,
osm_id: 'node/42',
website: 'https://septime-charonne.fr',
phone: null,
price: 0,
currency: 'EUR',
});
});
it('COLLECTIONS-HELPERS-005: leaves out what the update does not name, so the stored value stays', () => {
expect(listingWriteOf({ name: 'Septime', notes: 'Book ahead' })).toEqual({});
expect(listingWriteOf({ website: undefined, phone: '+33 1' })).toEqual({ phone: '+33 1' });
});
});
describe('encodePlacesCursor / decodePlacesCursor', () => {
it('COLLECTIONS-HELPERS-006: a cursor gives back the position it was made from', () => {
const position = { sortOrder: 3, createdAt: '2026-05-01 10:00:00', id: 42 };
const cursor = encodePlacesCursor(position);
expect(cursor).toMatch(/^[A-Za-z0-9_-]+$/);
expect(decodePlacesCursor(cursor)).toEqual(position);
expect(decodePlacesCursor(encodePlacesCursor({ sortOrder: -1, createdAt: '', id: 1 }))).toEqual({
sortOrder: -1,
createdAt: '',
id: 1,
});
});
it('COLLECTIONS-HELPERS-007: anything else reads as no position', () => {
const encode = (value: unknown) => Buffer.from(JSON.stringify(value)).toString('base64url');
expect(decodePlacesCursor('not-a-cursor')).toBeNull();
expect(decodePlacesCursor('')).toBeNull();
expect(decodePlacesCursor(encode({ id: 1 }))).toBeNull();
expect(decodePlacesCursor(encode([0, '2026-05-01 10:00:00']))).toBeNull();
expect(decodePlacesCursor(encode([0.5, '2026-05-01 10:00:00', 1]))).toBeNull();
expect(decodePlacesCursor(encode([0, '2026-05-01 10:00:00', 0]))).toBeNull();
});
});
describe('filePlaceOf', () => {
const row: CollectionPlaceRow = {
id: 9,
collection_id: 4,
owner_id: 1,
saved_by: 1,
name: 'Septime',
description: 'Natural wine',
lat: 48.853,
lng: 2.381,
address: '80 Rue de Charonne',
category_id: 3,
price: 45,
currency: 'EUR',
notes: 'Book ahead',
image_url: 'https://images.example/septime.jpg',
google_place_id: 'ChIJseptime',
google_ftid: null,
osm_id: 'node/42',
website: 'https://septime-charonne.fr',
phone: '+33 1 43 67 38 29',
status: 'revisit',
source_trip_id: 7,
source_place_id: 70,
links: JSON.stringify(links),
sort_order: 0,
created_at: '2026-05-01 10:00:00',
updated_at: null,
category_name: 'Restaurant',
category_color: '#ff0000',
category_icon: 'utensils',
};
it('COLLECTIONS-HELPERS-008: a saved place as the list file carries it: no ids, the category by name, the labels given', () => {
expect(filePlaceOf(row, ['Dinner'])).toEqual({
name: 'Septime',
description: 'Natural wine',
lat: 48.853,
lng: 2.381,
address: '80 Rue de Charonne',
notes: 'Book ahead',
price: 45,
currency: 'EUR',
website: 'https://septime-charonne.fr',
phone: '+33 1 43 67 38 29',
image_url: 'https://images.example/septime.jpg',
google_place_id: 'ChIJseptime',
google_ftid: null,
osm_id: 'node/42',
status: 'revisit',
links,
category: 'Restaurant',
labels: ['Dinner'],
});
});
it('COLLECTIONS-HELPERS-009: an upload of this server stays behind, it resolves nowhere else', () => {
expect(filePlaceOf({ ...row, image_url: '/uploads/places/septime.jpg' }, []).image_url).toBeNull();
expect(filePlaceOf({ ...row, image_url: null, links: null }, [])).toMatchObject({
image_url: null,
links: undefined,
});
});
});
describe('groupByPid', () => {
it('COLLECTIONS-HELPERS-010: groups the rows by their place, each group in the order the rows came', () => {
const rows = [
{ pid: 2, v: 'a' },
{ pid: 1, v: 'b' },
{ pid: 2, v: 'c' },
];
expect(groupByPid(rows, (r) => r.v)).toEqual({ 1: ['b'], 2: ['a', 'c'] });
expect(groupByPid([], (r: { pid: number }) => r)).toEqual({});
});
});
describe('tripDedupSet', () => {
it('COLLECTIONS-HELPERS-011: names by their trimmed lower case, coordinates only for a nameless place, every provider id', () => {
const set = tripDedupSet([
{ name: ' Pantheon ', lat: 41.9, lng: 12.47, google_place_id: 'g1', google_ftid: null, osm_id: null },
{ name: '', lat: 41.8, lng: 12.4, google_place_id: null, google_ftid: 'f1', osm_id: 'node/2' },
]);
expect([...set.names]).toEqual(['pantheon']);
expect(set.coords).toEqual([{ lat: 41.8, lng: 12.4 }]);
expect([...set.externalIds].sort()).toEqual(['f1', 'g1', 'node/2']);
});
});
describe('tripPlaceFields', () => {
it('COLLECTIONS-HELPERS-012: takes what a list keeps of a trip place and nothing of its itinerary', () => {
const place = {
id: 5,
trip_id: 1,
name: 'Louvre',
description: 'Museum',
lat: 48.86,
lng: 2.34,
address: 'Rue de Rivoli',
category_id: 3,
price: 22,
currency: 'EUR',
notes: 'Go early',
image_url: null,
google_place_id: 'g',
google_ftid: null,
osm_id: null,
website: 'https://louvre.fr',
phone: null,
place_time: '09:00',
reservation_status: 'confirmed',
} as unknown as PlaceRow;
expect(tripPlaceFields(place)).toEqual({
name: 'Louvre',
description: 'Museum',
lat: 48.86,
lng: 2.34,
address: 'Rue de Rivoli',
category_id: 3,
price: 22,
currency: 'EUR',
notes: 'Go early',
image_url: null,
google_place_id: 'g',
google_ftid: null,
osm_id: null,
website: 'https://louvre.fr',
phone: null,
});
});
});
describe('placeOrderOf (PLC-03)', () => {
it('COLLECTIONS-HELPERS-013: the named ids first, the rest after them as they were; null for a foreign or doubled id', () => {
expect(placeOrderOf([1, 2, 3, 4], [3, 1])).toEqual([3, 1, 2, 4]);
expect(placeOrderOf([1, 2], [2, 1])).toEqual([2, 1]);
expect(placeOrderOf([1, 2], [2, 9])).toBeNull();
expect(placeOrderOf([1, 2], [2, 2])).toBeNull();
});
});