feat(tours): add non-motorized day tour planning

This commit is contained in:
elmocito
2026-10-04 12:59:55 +02:00
parent c93cf2bce9
commit 9e475f447a
202 changed files with 16129 additions and 383 deletions
@@ -6,7 +6,7 @@ import { useSaveToCollectionStore } from '../../../../src/store/saveToCollection
import type { Day, Place } from '../../../../src/types'
import { buildPlanner, buildShell } from '../../../helpers/mobileTrip'
import { resetAllStores, seedStore } from '../../../helpers/store'
import { fireEvent, render, screen } from '../../../helpers/render'
import { act, fireEvent, render, screen, waitFor } from '../../../helpers/render'
// FE-MOB-BRACT-001 to FE-MOB-BRACT-013
// This sheet reads its labels from the real TranslationProvider, so the
@@ -107,14 +107,57 @@ describe('MBrowseActionsSheet', () => {
expect(screen.getByRole('button', { name: /Tokyo Bay/ })).toBeInTheDocument()
})
it('FE-MOB-BRACT-010: the quick-add payload opens with the day list already expanded and dated', () => {
it('FE-MOB-BRACT-010: the quick-add payload opens with the day list already expanded and dated', async () => {
const { planner, shell } = setup({}, { sheet: { id: 'bract', payload: { placeId: 77, dayPicker: true } } })
expect(screen.getByRole('button', { name: /Add to a day\?/ })).toHaveAttribute('aria-expanded', 'true')
expect(screen.getByText('Thu, Apr 2')).toBeInTheDocument()
fireEvent.click(screen.getByRole('button', { name: /Tokyo Bay/ }))
expect(shell.closeSheet).toHaveBeenCalledTimes(1)
expect(planner.handleAssignToDay).toHaveBeenCalledWith(77, 2)
await waitFor(() => expect(shell.closeSheet).toHaveBeenCalledTimes(1))
})
it('waits for the assignment handler before closing the mobile sheet', async () => {
let resolveAssignment!: () => void
const assignment = new Promise<void>(resolve => { resolveAssignment = resolve })
const { planner, shell } = setup({ handleAssignToDay: vi.fn(() => assignment.then(() => true)) }, {
sheet: { id: 'bract', payload: { placeId: 77, dayPicker: true } },
})
fireEvent.click(screen.getByRole('button', { name: /Tokyo Bay/ }))
expect(shell.closeSheet).not.toHaveBeenCalled()
expect(planner.handleAssignToDay).toHaveBeenCalledWith(77, 2)
await act(async () => { resolveAssignment(); await assignment })
await waitFor(() => expect(shell.closeSheet).toHaveBeenCalledOnce())
})
it('keeps the mobile sheet open when the assignment handler reports failure', async () => {
const { planner, shell } = setup({ handleAssignToDay: vi.fn().mockResolvedValue(false) }, {
sheet: { id: 'bract', payload: { placeId: 77, dayPicker: true } },
})
fireEvent.click(screen.getByRole('button', { name: /Tokyo Bay/ }))
await waitFor(() => expect(planner.handleAssignToDay).toHaveBeenCalledWith(77, 2))
expect(shell.closeSheet).not.toHaveBeenCalled()
})
it('offers only view and day attachment for a dormant Tour in the generic Places pool', () => {
enableCollections()
const tourPlace = { ...PLACE, tour_place_id: PLACE.id }
const { planner } = setup({
places: [tourPlace],
can: vi.fn(() => true),
isTourPlace: vi.fn((placeId: number) => placeId === PLACE.id),
})
expect(screen.getByRole('button', { name: /View details/ })).toBeInTheDocument()
expect(screen.queryByRole('button', { name: /^Edit/ })).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: /^Delete/ })).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: /Save to Collection/ })).not.toBeInTheDocument()
expect(screen.getByRole('button', { name: /Add to a day/ })).toBeInTheDocument()
expect(planner.handleDeletePlace).not.toHaveBeenCalled()
})
it('FE-MOB-BRACT-011: numbers an untitled, undated day by its position when day_number is missing or zero', () => {
@@ -279,6 +279,16 @@ describe('MPlaceEditSheet', () => {
expect(planner.setShowPlaceForm).toHaveBeenCalledWith(false)
})
it('requires explicit permanent confirmation when a dormant Tour is deleted from the Place editor', async () => {
const tour = { ...EDITED, tour_place_id: EDITED.id } as Place
const { planner } = setup({ editingPlace: tour, isTourPlace: vi.fn(() => true) })
fireEvent.click(screen.getByRole('button', { name: 'common.delete' }))
expect(planner.toast.warning).toHaveBeenCalledWith('tours.delete.confirmBody')
fireEvent.click(screen.getByRole('button', { name: 'tours.delete.confirmAction' }))
await waitFor(() => expect(planner.confirmDeletePlace).toHaveBeenCalledTimes(1))
})
it('FE-MOB-PLEDIT-022: cancelling an armed delete un-stages the id', () => {
const { planner } = setup({ editingPlace: EDITED })
fireEvent.click(screen.getByRole('button', { name: 'common.delete' }))
@@ -104,6 +104,7 @@ function makePlanner(overrides: Record<string, unknown> = {}) {
addFile: vi.fn().mockResolvedValue(undefined),
ratePlace: vi.fn().mockResolvedValue(undefined),
},
isTourPlace: vi.fn<TripPlanner['isTourPlace']>(() => false),
can: vi.fn(() => true),
reservations: [] as unknown[],
TRANSPORT_TYPES: new Set(['flight', 'train', 'bus', 'car', 'taxi', 'bicycle', 'cruise', 'ferry', 'transit', 'transport_other']),
@@ -35,6 +35,7 @@ function place(over: Partial<Place>): Place {
const LOUVRE = place({ id: 1, name: 'Louvre', address: 'Rue de Rivoli', category_id: 1 })
const EIFFEL = place({ id: 2, name: 'Eiffel Tower', description: 'Iron lady' })
const SEINE = place({ id: 3, name: 'Seine Track', category_id: 2, route_geometry: 'abc', route_color: '#ff0000' })
const TOUR = place({ id: 4, name: 'Ridge walk', tour_place_id: 4 })
const PLACES = [LOUVRE, EIFFEL, SEINE]
@@ -290,6 +291,23 @@ describe('MPlacesBrowser', () => {
expect(screen.queryByText(/places.selectionCount/)).not.toBeInTheDocument()
})
it('routes a Tour row to read-only detail instead of mobile Place bulk management', () => {
const planner = makePlanner({
places: [LOUVRE, TOUR],
isTourPlace: vi.fn((placeId: number) => placeId === TOUR.id),
})
renderBrowser(planner)
fireEvent.click(screen.getByRole('button', { name: 'common.select' }))
fireEvent.click(row('Louvre'))
fireEvent.click(row('Ridge walk'))
expect(planner.handlePlaceClick).toHaveBeenCalledWith(TOUR.id)
expect(screen.getByText('places.selectionCount:1')).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'places.deleteSelected' })).toBeEnabled()
expect(screen.getByRole('button', { name: 'places.changeCategory' })).toBeEnabled()
expect(screen.getByRole('button', { name: 'common.deselectAll' })).toBeInTheDocument()
})
it('FE-MOB-PBROW-020b: a failed bulk delete keeps the selection for a retry', async () => {
const planner = makePlanner()
;(planner.confirmDeletePlaces as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error('nope'))
@@ -358,6 +358,33 @@ describe('MPlanTimeline', () => {
})
})
describe('Tours read-only timeline integration', () => {
const tour = { ...MUSEUM, id: 91, place_id: 901,
place: { ...MUSEUM.place, id: 901, name: 'Assigned Tour' }, tour_place_id: 901 } as Assignment
const item: MergedItem = { type: 'place', sortKey: 0, data: tour }
const row: PlanRow = { key: 'pl-91', kind: 'place', item, assignment: tour, linkedReservations: [], invalidTour: true }
it('shows one localized invalid-endpoint warning and selects the Tour through the shared Place path', () => {
const { planner } = renderTimeline({ rows: [row], merged: [item], upNext: null }, { toursEnabled: true })
expect(screen.getAllByText('tours.dayRoute.endpointUnknown')).toHaveLength(1)
fireEvent.click(screen.getByText('Assigned Tour'))
expect(planner.handlePlaceClick).toHaveBeenCalledWith(901, 91)
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
expect(screen.queryByText('tour-planner')).not.toBeInTheDocument()
})
it('does not show the warning for valid status or with Tours disabled', () => {
const validRow: PlanRow = { ...row, invalidTour: false }
const first = renderTimeline({ rows: [validRow], merged: [item], upNext: null }, { toursEnabled: true })
expect(screen.queryByText('tours.dayRoute.endpointUnknown')).not.toBeInTheDocument()
first.unmount()
renderTimeline({ rows: [row], merged: [item], upNext: null }, { toursEnabled: false })
expect(screen.queryByText('tours.dayRoute.endpointUnknown')).not.toBeInTheDocument()
})
})
describe('per-segment travel mode', () => {
/** The timeline only offers the mode menu in edit mode, where many buttons exist. */
const renderEditing = () => renderTimeline({}, {}, { mode: 'edit' })
@@ -3,6 +3,7 @@ import type { ReactNode } from 'react'
import { BedDouble, MapPin } from 'lucide-react'
import type { BookingExpenseRequest } from '../../../../src/components/Planner/BookingCostsSection.types'
import type { ExpensePrefill } from '../../../../src/components/Budget/CostsPanel'
import type { TourListItem } from '@trek/shared'
import type { MTripShellApi, TripPlanner } from '../../../../src/mobile/screens/trip/MTripShell'
import type { BudgetItem, Reservation, Trip } from '../../../../src/types'
import { useAuthStore } from '../../../../src/store/authStore'
@@ -187,7 +188,7 @@ const JOURNEY = {
} as unknown as Reservation
function renderHost(plannerOverrides: Partial<TripPlanner> = {}, shellOverrides: Partial<MTripShellApi> = {}) {
const planner = buildPlanner(plannerOverrides)
const planner = buildPlanner({ isTourPlace: vi.fn(() => false), ...plannerOverrides })
const shell = buildShell(shellOverrides)
render(<MTripSheets planner={planner} shell={shell} />)
return { planner, shell }
@@ -418,6 +419,44 @@ describe('MTripSheets', () => {
expect(screen.getByTestId('stub-cost')).toHaveAttribute('data-me', '-1')
})
it.each([[false, false], [true, false], [false, true], [true, true]])(
'keeps the mobile Tour host View/Attach-only with place_edit=%s day_edit=%s', (placeEdit, dayEdit) => {
const mobilePlace = {
id: 42, trip_id: 1, name: 'Mobile ridge', lat: 48, lng: 11,
route_geometry: '[[48,11,500],[48.01,11.01,510]]', route_color: '#ff0000',
} as never
const mobileTour = {
place_id: 42, name: 'Mobile ridge', tour_type: 'hike', distance: 1,
elevation_gain: 10, elevation_loss: 0, duration: null, difficulty: null,
wanderer_ref: null, match_confidence: 1, tour_group_id: null,
max_hiking_difficulty: 2, planned: false, caution: false,
} as TourListItem
const readOnlyFile = {
id: 19, trip_id: 1, place_id: 42, filename: 'ridge.gpx', original_name: 'ridge.gpx',
file_size: 32, mime_type: 'application/gpx+xml', url: '/api/files/19', created_at: '2026-01-01',
}
const can = vi.fn((permission: string) => permission === 'place_edit' ? placeEdit : permission === 'day_edit' && dayEdit)
renderHost({
selectedPlace: mobilePlace,
selectedTour: mobileTour,
selectedDayId: 7,
days: [{ id: 7, trip_id: 1, day_number: 1, title: 'Day one' } as never],
files: [readOnlyFile as never],
can: can as TripPlanner['can'],
canUploadFiles: true,
})
expect(document.querySelectorAll('[aria-modal="true"]')).toHaveLength(1)
expect(screen.queryByRole('button', { name: 'Edit' })).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Delete' })).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Upload' })).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Edit track colour' })).not.toBeInTheDocument()
fireEvent.click(screen.getByRole('button', { name: '1 files' }))
expect(screen.getByText('ridge.gpx')).toBeInTheDocument()
expect(Boolean(screen.queryByRole('button', { name: /Add to day/i }))).toBe(dayEdit)
},
)
it('FE-MOB-SHOST-025: the delete-place confirm runs the planner confirmation and disarms', () => {
const { planner } = renderHost({ deletePlaceId: 101 })
expect(screen.getByTestId('stub-confirm')).toHaveAttribute('data-title', 'common.delete')
@@ -428,6 +467,17 @@ describe('MTripSheets', () => {
expect(planner.setDeletePlaceId).toHaveBeenCalledWith(null)
})
it('shows permanent Tour deletion language in the mobile confirmation', () => {
const { planner } = renderHost({ deletePlaceId: 101, isTourPlace: vi.fn(() => true) })
const confirm = screen.getByTestId('stub-confirm')
expect(confirm).toHaveAttribute('data-confirm', 'tours.delete.confirmAction')
expect(confirm).toHaveTextContent('tours.delete.confirmBody')
expect(confirm).not.toHaveTextContent('Undo')
fireEvent.click(screen.getByText('confirm delete'))
expect(planner.confirmDeletePlace).toHaveBeenCalledTimes(1)
})
it('FE-MOB-SHOST-029: a night booked at the place is said before the yes, and only then', () => {
// The server takes the night down with the place, and the booking and the
// expense with the night. The planner builds the sentence; the sheet has to
@@ -9,7 +9,7 @@ import { getDisplayTimeForDay, getMergedItems, getTransportForDay, type MergedIt
import { buildDayRouteRuns, hotelBookendOf, type DayRoutePoint } from '../../../../src/components/Map/dayRoutePlan'
import { buildAssignment, buildDayNote, buildPlace, buildReservation } from '../../../helpers/factories'
import type {
Accommodation, Assignment, Day, DayNote, Reservation, RouteSegment, TranslationFn,
Accommodation, Assignment, Day, DayNote, Place, Reservation, RouteSegment, TranslationFn,
} from '../../../../src/types'
// FE-MOB-PTLM-001 to FE-MOB-PTLM-060
@@ -56,6 +56,23 @@ const placeItem = (a: Assignment): MergedItem => ({ type: 'place', sortKey: a.or
const noteItem = (n: DayNote): MergedItem => ({ type: 'note', sortKey: n.sort_order ?? 0, data: n })
const transportItem = (r: TransportEntry): MergedItem => ({ type: 'transport', sortKey: 0, data: r })
function tourAssignment(id: number, order: number, geometry: [number, number][], currentGeometry = JSON.stringify(geometry)): Assignment {
const [lat, lng] = geometry[0]
const tourPlace = buildPlace({ id: id + 100, name: `Tour ${id}`, lat, lng, route_geometry: currentGeometry })
return buildAssignment({ id, day_id: 2, order_index: order, place_id: tourPlace.id, place: tourPlace,
tour_place_id: tourPlace.id, tour_route_geometry: JSON.stringify(geometry) })
}
function tourRows(assignments: Assignment[], routeSegments: RouteSegment[], toursEnabled = true) {
return buildPlanRows({
merged: assignments.map(placeItem), reservations: [], routeSegments, dayId: 2, toursEnabled,
places: assignments.flatMap(assignment => assignment.place ? [assignment.place as Place] : []),
})
}
const connectors = (rows: ReturnType<typeof buildPlanRows>) =>
rows.flatMap(row => row.kind === 'conn' ? [row.seg] : [])
function accommodation(overrides: Partial<Accommodation>): Accommodation {
return {
id: 1,
@@ -269,6 +286,100 @@ describe('planTimelineModel — buildPlanRows', () => {
// Only the first Museum → Park hop finds the single matching leg.
expect(rows.filter(r => r.kind === 'conn')).toHaveLength(1)
})
it('uses only Tour boundary connectors and leaves saved geometry unrouted', () => {
const before = assignment(31, 0, place(301, 'Before', 48.1, 11.1))
const tour = tourAssignment(32, 1, [[48.103, 11.103], [48.11, 11.11], [48.12, 11.12]])
const after = assignment(33, 2, place(303, 'After', 48.13, 11.13))
const incoming = seg([48.1, 11.1], [48.103, 11.103])
const outgoing = seg([48.12, 11.12], [48.13, 11.13])
const automaticTourRoute = seg([48.103, 11.103], [48.12, 11.12])
const rows = tourRows([before, tour, after], [incoming, outgoing, automaticTourRoute])
expect(connectors(rows)).toEqual([incoming, outgoing])
expect(rows.map(row => row.kind)).toEqual(['place', 'conn', 'place', 'conn', 'place'])
expect(rows.filter(row => row.kind === 'conn').map(row => row.assignmentId)).toEqual([before.id, tour.id])
})
it('uses the current Place projection when the saved assignment geometry has not refreshed', () => {
const before = assignment(35, 0, place(351, 'Before', 48.1, 11.1))
const tour = tourAssignment(36, 1, [[48.103, 11.103], [48.12, 11.12]], JSON.stringify([[48.105, 11.105], [48.125, 11.125]]))
const after = assignment(37, 2, place(353, 'After', 48.13, 11.13))
const currentIncoming = seg([48.1, 11.1], [48.105, 11.105])
const currentOutgoing = seg([48.125, 11.125], [48.13, 11.13])
const staleIncoming = seg([48.1, 11.1], [48.103, 11.103])
expect(connectors(tourRows([before, tour, after], [currentIncoming, currentOutgoing, staleIncoming])))
.toEqual([currentIncoming, currentOutgoing])
})
it('connects first, last and consecutive Tours only at valid boundaries', () => {
const before = assignment(41, 0, place(401, 'Before', 48.1, 11.1))
const first = tourAssignment(42, 1, [[48.11, 11.11], [48.12, 11.12]])
const second = tourAssignment(43, 2, [[48.13, 11.13], [48.14, 11.14]])
const after = assignment(44, 3, place(404, 'After', 48.15, 11.15))
const segments = [
seg([48.1, 11.1], [48.11, 11.11]),
seg([48.12, 11.12], [48.13, 11.13]),
seg([48.14, 11.14], [48.15, 11.15]),
]
const firstToAfter = seg([48.12, 11.12], [48.15, 11.15])
expect(connectors(tourRows([first, after], [firstToAfter]))).toEqual([firstToAfter])
expect(connectors(tourRows([before, first], [segments[0]]))).toEqual([segments[0]])
const rows = tourRows([before, first, second, after], segments)
expect(connectors(rows)).toEqual(segments)
expect(rows.filter(row => row.kind === 'conn').map(row => row.assignmentId)).toEqual([before.id, first.id, second.id])
})
it('connects a loop Tour at its shared boundary without routing through its geometry', () => {
const before = assignment(51, 0, place(501, 'Before', 48.1, 11.1))
const loop = tourAssignment(52, 1, [[48.11, 11.11], [48.12, 11.12], [48.11, 11.11]])
const after = assignment(53, 2, place(503, 'After', 48.13, 11.13))
const incoming = seg([48.1, 11.1], [48.11, 11.11])
const outgoing = seg([48.11, 11.11], [48.13, 11.13])
const inside = seg([48.11, 11.11], [48.11, 11.11])
expect(connectors(tourRows([before, loop, after], [incoming, outgoing, inside]))).toEqual([incoming, outgoing])
})
it('keeps excluded Tours visible and bridges surrounding Places as the central route builder does', () => {
const before = assignment(61, 0, place(601, 'Before', 48.1, 11.1))
const tour = { ...tourAssignment(62, 1, [[48.11, 11.11], [48.12, 11.12]]), route_excluded: true }
const after = assignment(63, 2, place(603, 'After', 48.13, 11.13))
const bridge = seg([48.1, 11.1], [48.13, 11.13])
const rows = tourRows([before, tour, after], [bridge])
expect(rows.map(row => row.kind)).toEqual(['place', 'conn', 'place', 'place'])
expect(connectors(rows)).toEqual([bridge])
expect(rows.some(row => row.kind === 'place' && row.assignment.id === tour.id)).toBe(true)
})
it('exposes invalid-Tour warning state and never invents an outgoing endpoint', () => {
const before = assignment(71, 0, place(701, 'Before', 48.1, 11.1))
const tour = tourAssignment(72, 1, [[48.11, 11.11], [48.12, 11.12]], 'not valid geometry')
const after = assignment(73, 2, place(703, 'After', 48.13, 11.13))
const incoming = seg([48.1, 11.1], [48.11, 11.11])
const falseOutgoing = seg([48.11, 11.11], [48.13, 11.13])
const rows = tourRows([before, tour, after], [incoming, falseOutgoing])
expect(connectors(rows)).toEqual([incoming])
expect(rows.find(row => row.kind === 'place' && row.assignment.id === tour.id)).toMatchObject({ invalidTour: true })
const noPlaceAnchor = { ...tour, place: buildPlace({ id: tour.place!.id, name: 'Tour without anchor', lat: null, lng: null, route_geometry: 'not valid geometry' }) }
const noAnchorRows = tourRows([before, noPlaceAnchor, after], [seg([48.1, 11.1], [48.13, 11.13])])
expect(connectors(noAnchorRows)).toEqual([])
expect(noAnchorRows.find(row => row.kind === 'place' && row.assignment.id === tour.id)).toMatchObject({ invalidTour: true })
})
it('keeps Tours-off routing on the legacy single Place anchor', () => {
const before = assignment(81, 0, place(801, 'Before', 48.1, 11.1))
const tour = tourAssignment(82, 1, [[48.11, 11.11], [48.12, 11.12]])
const after = assignment(83, 2, place(803, 'After', 48.13, 11.13))
const rows = tourRows([before, tour, after], [
seg([48.1, 11.1], [48.11, 11.11]),
seg([48.11, 11.11], [48.13, 11.13]),
], false)
expect(connectors(rows).map(item => [item.from, item.to])).toEqual([
[[48.1, 11.1], [48.11, 11.11]],
[[48.11, 11.11], [48.13, 11.13]],
])
expect(rows.find(row => row.kind === 'place' && row.assignment.id === tour.id)).toMatchObject({ invalidTour: false })
})
})
describe('planTimelineModel — hotel chips and legs', () => {