|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +import { describe, it, expect, vi } from 'vitest'; |
| 3 | +import { ItineraryStore } from './itinerary-store'; |
| 4 | + |
| 5 | +describe('ItineraryStore', () => { |
| 6 | + it('add appends a stop', () => { |
| 7 | + const s = new ItineraryStore(); |
| 8 | + s.add(2, 'Sainte-Chapelle', 'morning'); |
| 9 | + expect(s.stops().some((x) => x.place === 'Sainte-Chapelle')).toBe(true); |
| 10 | + }); |
| 11 | + |
| 12 | + it('move matches place case-insensitively and returns the stop', () => { |
| 13 | + const s = new ItineraryStore(); |
| 14 | + s.add(1, 'Louvre'); |
| 15 | + const moved = s.move('louvre', 2); |
| 16 | + expect(moved?.day).toBe(2); |
| 17 | + expect(s.move('atlantis', 1)).toBeUndefined(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('clearDay removes only stops for that day', () => { |
| 21 | + const s = new ItineraryStore(); |
| 22 | + s.add(1, 'Louvre'); |
| 23 | + s.add(1, 'Eiffel Tower'); |
| 24 | + s.add(2, "Musée d'Orsay"); |
| 25 | + const removed = s.clearDay(1); |
| 26 | + expect(removed).toBe(2); |
| 27 | + expect(s.stops().every((x) => x.day !== 1)).toBe(true); |
| 28 | + // day 2 stop still exists |
| 29 | + expect(s.stops().some((x) => x.day === 2)).toBe(true); |
| 30 | + }); |
| 31 | + |
| 32 | + it('remove deletes the stop with the given id', () => { |
| 33 | + const s = new ItineraryStore(); |
| 34 | + s.add(1, 'Louvre'); |
| 35 | + s.add(1, 'Eiffel Tower'); |
| 36 | + const first = s.stops()[0]; |
| 37 | + s.remove(first.id); |
| 38 | + expect(s.stops().find((x) => x.id === first.id)).toBeUndefined(); |
| 39 | + expect(s.stops().length).toBe(1); |
| 40 | + }); |
| 41 | + |
| 42 | + it('reset clears all stops', () => { |
| 43 | + const s = new ItineraryStore(); |
| 44 | + s.add(3, 'Versailles'); |
| 45 | + s.reset(); |
| 46 | + expect(s.stops().length).toBe(0); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +describe('ItineraryStore — server-state model', () => { |
| 51 | + it('starts empty (no seed)', () => { |
| 52 | + const store = new ItineraryStore(); |
| 53 | + expect(store.stops()).toEqual([]); |
| 54 | + expect(store.days()).toEqual([]); |
| 55 | + }); |
| 56 | + |
| 57 | + it('hydrates from a server itinerary snapshot', () => { |
| 58 | + const store = new ItineraryStore(); |
| 59 | + store.hydrate([{ id: 'x', day: 1, place: 'Louvre' }]); |
| 60 | + expect(store.stops().map((s) => s.place)).toEqual(['Louvre']); |
| 61 | + }); |
| 62 | + |
| 63 | + it('does not touch localStorage on update', () => { |
| 64 | + const spy = vi.spyOn(Storage.prototype, 'setItem'); |
| 65 | + const store = new ItineraryStore(); |
| 66 | + store.add(1, 'Eiffel Tower'); |
| 67 | + expect(spy).not.toHaveBeenCalled(); |
| 68 | + spy.mockRestore(); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('reorder', () => { |
| 73 | + it('moves a stop to a new index within the same day', () => { |
| 74 | + const s = new ItineraryStore(); |
| 75 | + s.add(1, 'Louvre'); |
| 76 | + s.add(1, 'Eiffel Tower'); |
| 77 | + const eiffel = s.stops().find((x) => x.place === 'Eiffel Tower')!; |
| 78 | + s.reorder(eiffel.id, 1, 0); |
| 79 | + const day1 = s.days().find((g) => g.day === 1)!; |
| 80 | + expect(day1.stops.map((x) => x.place)).toEqual(['Eiffel Tower', 'Louvre']); |
| 81 | + }); |
| 82 | + |
| 83 | + it('moves a stop across days at a specific index', () => { |
| 84 | + const s = new ItineraryStore(); |
| 85 | + s.add(1, 'Louvre'); |
| 86 | + s.add(1, 'Eiffel Tower'); |
| 87 | + s.add(2, "Musée d'Orsay"); |
| 88 | + const orsay = s.stops().find((x) => x.place === "Musée d'Orsay")!; |
| 89 | + s.reorder(orsay.id, 1, 0); |
| 90 | + const day1 = s.days().find((g) => g.day === 1)!; |
| 91 | + expect(day1.stops[0].place).toBe("Musée d'Orsay"); |
| 92 | + expect(day1.stops[0].day).toBe(1); |
| 93 | + }); |
| 94 | + |
| 95 | + it('reorder by unknown id is a no-op', () => { |
| 96 | + const s = new ItineraryStore(); |
| 97 | + s.add(1, 'Louvre'); |
| 98 | + const before = s.stops(); |
| 99 | + s.reorder('does-not-exist', 1, 0); |
| 100 | + expect(s.stops()).toEqual(before); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe('recentlyChangedId', () => { |
| 105 | + it('is null initially', () => { |
| 106 | + const s = new ItineraryStore(); |
| 107 | + expect(s.recentlyChangedId()).toBeNull(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('is set after an agent-source add', () => { |
| 111 | + const s = new ItineraryStore(); |
| 112 | + const added = s.add(3, 'Sacré-Cœur'); |
| 113 | + expect(s.recentlyChangedId()).toBe(added.id); |
| 114 | + }); |
| 115 | + |
| 116 | + it('is NOT set after a user-source add', () => { |
| 117 | + const s = new ItineraryStore(); |
| 118 | + s.add(3, 'Sacré-Cœur', undefined, { source: 'user' }); |
| 119 | + expect(s.recentlyChangedId()).toBeNull(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('clears 1600ms after the change', async () => { |
| 123 | + vi.useFakeTimers(); |
| 124 | + const s = new ItineraryStore(); |
| 125 | + s.add(3, 'Sacré-Cœur'); |
| 126 | + expect(s.recentlyChangedId()).not.toBeNull(); |
| 127 | + vi.advanceTimersByTime(1600); |
| 128 | + expect(s.recentlyChangedId()).toBeNull(); |
| 129 | + vi.useRealTimers(); |
| 130 | + }); |
| 131 | +}); |
| 132 | + |
| 133 | +describe('focus', () => { |
| 134 | + it('focus sets and clears focusedStopId', () => { |
| 135 | + const s = new ItineraryStore(); |
| 136 | + s.add(1, 'Louvre'); |
| 137 | + const id = s.stops()[0].id; |
| 138 | + expect(s.focusedStopId()).toBeNull(); |
| 139 | + s.focus(id); |
| 140 | + expect(s.focusedStopId()).toBe(id); |
| 141 | + s.focus(null); |
| 142 | + expect(s.focusedStopId()).toBeNull(); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +describe('coordinates', () => { |
| 147 | + it('add accepts optional lat/lng via opts.coords', () => { |
| 148 | + const s = new ItineraryStore(); |
| 149 | + const added = s.add(2, 'Sacré-Cœur', undefined, { |
| 150 | + coords: { lat: 48.8867, lng: 2.3431 }, |
| 151 | + }); |
| 152 | + expect(added.lat).toBeCloseTo(48.8867, 3); |
| 153 | + expect(added.lng).toBeCloseTo(2.3431, 3); |
| 154 | + }); |
| 155 | + |
| 156 | + it('add without coords leaves lat/lng undefined', () => { |
| 157 | + const s = new ItineraryStore(); |
| 158 | + const added = s.add(2, 'Somewhere'); |
| 159 | + expect(added.lat).toBeUndefined(); |
| 160 | + expect(added.lng).toBeUndefined(); |
| 161 | + }); |
| 162 | +}); |
0 commit comments