/** * 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 * the order a reorder writes and the trip tags a copy takes. COLLECTIONS-HELPERS-001 to 014. */ 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, tagKeyOf, tripDedupSet, tripPlaceFields, tripTagIdsOf, } 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(); }); }); describe('tripTagIdsOf', () => { it('COLLECTIONS-HELPERS-014: reuses known tags, makes each missing name once, the first spelling wins', async () => { const known = new Map([[tagKeyOf(' Food '), 7]]); const made: Array<[string, string]> = []; let next = 100; const makeTag = (name: string, color: string) => { made.push([name, color]); next += 1; return Promise.resolve(next); }; const ids = await tripTagIdsOf( [ { name: 'food', color: '#111111' }, { name: ' Rooftop ', color: '#222222' }, { name: 'rooftop', color: '#333333' }, { name: 'Quiet', color: '#444444' }, ], known, makeTag, ); expect(ids).toEqual([7, 101, 101, 102]); expect(made).toEqual([ ['Rooftop', '#222222'], ['Quiet', '#444444'], ]); expect(known.get('rooftop')).toBe(101); expect(await tripTagIdsOf([], known, makeTag)).toEqual([]); }); });