/** * The pure half of the place history (PLC-14): how a stored value reads, what * a snapshot of a trip place and a saved place holds, which fields a write * changed or cleared, and the rows that follows. */ import { changeRowsOf, diffDetails, historyValue, positionValue, savedPlaceSnapshot, targetKey, tripPlaceSnapshot, } from '../../../../src/nest/place-changes/place-changes.helpers'; import { AUTOMATIC_CHANGE, savedPlaceTarget, tripPlaceTarget, } from '../../../../src/nest/place-changes/place-changes.types'; import { describe, expect, it } from 'vitest'; const details = { id: 3, name: 'Café', address: ' ', lat: 48.2, lng: 16.3, category_name: 'Food', description: null, notes: 'Book', website: '', phone: '+43', image_url: '/uploads/places/a.jpg', }; describe('place change helpers', () => { it('PCHG-H-001: a blank or missing value reads as none, a number as its text', () => { expect(historyValue(null)).toBeNull(); expect(historyValue(undefined)).toBeNull(); expect(historyValue(' ')).toBeNull(); expect(historyValue(' Café ')).toBe(' Café '); expect(historyValue(0)).toBe('0'); }); it('PCHG-H-002: the position is both coordinates or none', () => { expect(positionValue(48.2, 16.3)).toBe('48.2,16.3'); expect(positionValue(0, 0)).toBe('0,0'); expect(positionValue(null, 16.3)).toBeNull(); expect(positionValue(48.2, null)).toBeNull(); }); it('PCHG-H-003: a trip place keeps opening hours, a saved place its status', () => { expect(tripPlaceSnapshot({ ...details, opening_hours: 'Mo-Fr 8-18' })).toEqual({ name: 'Café', address: null, position: '48.2,16.3', category: 'Food', description: null, notes: 'Book', website: null, phone: '+43', image: '/uploads/places/a.jpg', opening_hours: 'Mo-Fr 8-18', }); const saved = savedPlaceSnapshot({ ...details, status: 'want' }); expect(saved.status).toBe('want'); expect('opening_hours' in saved).toBe(false); }); it('PCHG-H-004: a write changes the fields it set to another value and clears the ones it emptied', () => { const diff = diffDetails( { name: 'Café', notes: 'Book', phone: null, website: 'https://a' }, { name: 'Café Wien', notes: null, phone: '+43', website: 'https://a' }, ); expect(diff).toEqual({ changed: [ { field: 'name', old_value: 'Café', new_value: 'Café Wien' }, { field: 'phone', old_value: null, new_value: '+43' }, ], cleared: ['notes'], }); }); it('PCHG-H-005: the rows name the place, the editor and whether TREK filled the value in', () => { const diff = { changed: [{ field: 'name' as const, old_value: 'A', new_value: 'B' }], cleared: ['notes' as const] }; expect(changeRowsOf(tripPlaceTarget(3), diff, 7)).toEqual({ writes: [ { place_id: 3, collection_place_id: null, field: 'name', old_value: 'A', new_value: 'B', user_id: 7, automatic: 0, }, ], clear: { place_id: 3, collection_place_id: null, fields: ['notes'] }, }); expect(changeRowsOf(savedPlaceTarget(5), diff, AUTOMATIC_CHANGE).writes[0]).toEqual( expect.objectContaining({ place_id: null, collection_place_id: 5, user_id: null, automatic: 1 }), ); expect(changeRowsOf(savedPlaceTarget(5), diff, null).writes[0]).toEqual( expect.objectContaining({ user_id: null, automatic: 0 }), ); expect(targetKey(tripPlaceTarget(3))).not.toBe(targetKey(savedPlaceTarget(3))); }); });