Files
TREK/client/src/store/stayStops.test.ts
T
Maurice 5e0245046d feat(planner): show the stop a booked night writes without a reload
The server announces the day stop over the socket like any other assignment,
but the socket deliberately skips the session that sent the request, so the one
person who could not see their new stop was the one who had just booked the
night. That looks exactly like the bug it fixes.

The stay writes hand their answer to one applier, which folds the stop into the
store through the same path the socket uses, so the IndexedDB write-through
happens either way and the dedupe by assignment id comes for free. Three
surfaces call it: the day-detail picker, its edit branch and the phone sheet.
The road-trip flow does not, because it writes its own assignment before the
stay and there is never a mirrored stop to apply.
2026-09-13 01:13:33 +02:00

42 lines
1.9 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest'
import { useTripStore } from './tripStore'
import { applyStayStops } from './stayStops'
import type { Assignment } from '../types'
vi.mock('../db/offlineDb', () => ({
offlineDb: { days: { get: vi.fn(), put: vi.fn() }, places: { put: vi.fn(), delete: vi.fn() } },
upsertAccommodations: vi.fn(),
}))
const stop = (id: number, dayId: number): Assignment =>
({ id, day_id: dayId, place_id: 7, order_index: 0, notes: null, place: { id: 7, name: 'Hotel Adlon' } }) as unknown as Assignment
beforeEach(() => {
useTripStore.setState({ assignments: {} })
})
describe('applyStayStops', () => {
it('FE-STAY-STOPS-001 puts the stop a booking reported into the day it belongs to', () => {
// The socket skips the session that sent the request, so the answer is the only
// way the person who just booked the night learns about their own stop.
applyStayStops({ assignment: stop(77, 3) })
expect(useTripStore.getState().assignments['3']).toEqual([stop(77, 3)])
})
it('FE-STAY-STOPS-002 takes a stop back off the old day when the booking moved', () => {
useTripStore.setState({ assignments: { '3': [stop(77, 3)] } })
applyStayStops({ assignment: stop(78, 4), removedAssignments: [{ id: 77, dayId: 3 }] })
expect(useTripStore.getState().assignments['3']).toEqual([])
expect(useTripStore.getState().assignments['4']).toEqual([stop(78, 4)])
})
it('FE-STAY-STOPS-003 does nothing for a write that left the day plan alone', () => {
useTripStore.setState({ assignments: { '3': [stop(77, 3)] } })
// Booking a night at a place that was already planned for that day, and every
// older server that answers without the field at all.
applyStayStops({ assignment: null, removedAssignments: [] })
applyStayStops(undefined)
expect(useTripStore.getState().assignments['3']).toEqual([stop(77, 3)])
})
})