Files
TREK/client/src/utils/mergedOrder.test.ts
T
Maurice 24bc132476 fix(planner): keep an untimed stop between the bookings it was dropped between
The desktop day list hides the stop a lodging booking puts on the day, but
the places are stored across the full day. Bookings and notes were positioned
by counting only the visible places, so every hidden row ahead of a stop
pushed it one slot behind the booking it was dropped above, and it fell
below the evening flight on every drag or arrow move.

Position bookings and notes in the same full-day count the places are
stored in, and share that count with the page's reorder.

Closes #3100
2026-11-16 10:29:47 +01:00

146 lines
6.9 KiB
TypeScript

// FE-UTIL-MERGEDORDER-001 to -009: the reorder writes the desktop day plan and the
// phone's plan timeline both derive from a day's new timeline order.
import { describe, expect, it } from 'vitest';
import { getMergedItems } from './dayMerge';
import {
keepDayPositions,
metadataWithLegPositions,
moveRow,
planMergedOrder,
withHiddenRows,
withTransportPositions,
} from './mergedOrder';
const place = (id: number) => ({ type: 'place', data: { id } });
const note = (id: number) => ({ type: 'note', data: { id } });
const transport = (id: number, leg?: number) => ({
type: 'transport',
data: { id, __leg: leg == null ? undefined : { index: leg } },
});
describe('planMergedOrder', () => {
it('FE-UTIL-MERGEDORDER-001: places get sequential slots and what sits between two places shares the gap', () => {
const plan = planMergedOrder([place(1), note(7), transport(9), place(2), place(3)]);
expect(plan.assignmentIds).toEqual([1, 2, 3]);
expect(plan.noteUpdates).toEqual([{ id: 7, sort_order: 1 / 3 }]);
expect(plan.transportUpdates).toEqual([{ id: 9, day_plan_position: 2 / 3 }]);
expect(plan.legPosUpdates).toEqual({});
});
it('FE-UTIL-MERGEDORDER-002: rows before the first place sit below zero', () => {
const plan = planMergedOrder([note(7), place(1)]);
expect(plan.noteUpdates).toEqual([{ id: 7, sort_order: -0.5 }]);
expect(plan.assignmentIds).toEqual([1]);
});
it('FE-UTIL-MERGEDORDER-003: the legs of one booking are positioned leg by leg', () => {
const plan = planMergedOrder([place(1), transport(5, 0), place(2), transport(5, 1)]);
expect(plan.legPosUpdates).toEqual({ 5: { 0: 0.5, 1: 1.5 } });
expect(plan.transportUpdates).toEqual([]);
});
it('FE-UTIL-MERGEDORDER-004: a row of any other type takes a slot but writes nothing', () => {
const plan = planMergedOrder([place(1), { type: 'other', data: { id: 3 } }, note(4), place(2)]);
expect(plan.noteUpdates).toEqual([{ id: 4, sort_order: 2 / 3 }]);
expect(plan.transportUpdates).toEqual([]);
});
});
describe('withTransportPositions', () => {
it('FE-UTIL-MERGEDORDER-005: writes the moved bookings for that day and leaves the rest as they were', () => {
const untouched = { id: 2, day_plan_position: 4, day_positions: { 3: 4 } };
const moved = { id: 1, day_plan_position: 0, day_positions: { 2: 9 } };
const fresh = { id: 3 };
const result = withTransportPositions(
[moved, untouched, fresh],
[
{ id: 1, day_plan_position: 1.5 },
{ id: 3, day_plan_position: 0.25 },
],
7
);
expect(result[0]).toEqual({ id: 1, day_plan_position: 1.5, day_positions: { 2: 9, 7: 1.5 } });
expect(result[1]).toBe(untouched);
expect(result[2]).toEqual({ id: 3, day_plan_position: 0.25, day_positions: { 7: 0.25 } });
});
});
describe('metadataWithLegPositions', () => {
it('FE-UTIL-MERGEDORDER-006: writes each moved leg position for the day, from a string or an object', () => {
const legs = [{ from: 'FRA', day_positions: { 1: 0.2 } }, { from: 'DXB' }];
const fromString = metadataWithLegPositions(JSON.stringify({ airline: 'LH', legs }), { 1: 2.5 }, 4);
expect(fromString).toEqual({
airline: 'LH',
legs: [
{ from: 'FRA', day_positions: { 1: 0.2 } },
{ from: 'DXB', day_positions: { 4: 2.5 } },
],
});
const fromObject = metadataWithLegPositions({ legs }, { 0: 1.5 }, 4);
expect(fromObject).toEqual({ legs: [{ from: 'FRA', day_positions: { 1: 0.2, 4: 1.5 } }, { from: 'DXB' }] });
expect((fromObject?.legs as unknown[])[1]).toBe(legs[1]);
});
it('FE-UTIL-MERGEDORDER-007: metadata without legs, or that cannot be read, has nothing to move', () => {
expect(metadataWithLegPositions(JSON.stringify({ airline: 'LH' }), { 0: 1 }, 4)).toBeNull();
expect(metadataWithLegPositions('{not json', { 0: 1 }, 4)).toBeNull();
expect(metadataWithLegPositions(null, { 0: 1 }, 4)).toBeNull();
expect(metadataWithLegPositions('', { 0: 1 }, 4)).toBeNull();
});
});
describe('moveRow', () => {
it('FE-UTIL-MERGEDORDER-008: puts the row in front of the target, behind it with after, at the end without one', () => {
const rows = ['a', 'b', 'c', 'd'];
expect(moveRow(rows, 3, (r) => r === 'b', false)).toEqual({ order: ['a', 'd', 'b', 'c'], index: 1 });
expect(moveRow(rows, 0, (r) => r === 'c', true)).toEqual({ order: ['b', 'c', 'a', 'd'], index: 2 });
expect(moveRow(rows, 1, () => false, false)).toEqual({ order: ['a', 'c', 'd', 'b'], index: 3 });
expect(rows).toEqual(['a', 'b', 'c', 'd']);
});
});
describe('keepDayPositions', () => {
it('FE-UTIL-MERGEDORDER-009: keeps the day slots of the replaced copy only when the new one has none', () => {
const prev: { id: number; day_positions?: Record<string, number> | null } = { id: 1, day_positions: { 4: 1.5 } };
expect(keepDayPositions({ id: 1 }, prev)).toEqual({ id: 1, day_positions: { 4: 1.5 } });
expect(keepDayPositions({ id: 1, day_positions: null }, prev)).toEqual({ id: 1, day_positions: null });
expect(keepDayPositions({ id: 1 }, undefined)).toEqual({ id: 1 });
});
});
describe('planMergedOrder over a day with hidden rows (#3100)', () => {
it('FE-UTIL-MERGEDORDER-010: hidden rows keep their slots and the shown rows fill the rest in their new order', () => {
expect(withHiddenRows([10, 1, 2, 11], [2, 1])).toEqual([10, 2, 1, 11]);
expect(withHiddenRows([1, 2], [2, 1])).toEqual([2, 1]);
});
it('FE-UTIL-MERGEDORDER-011: bookings and notes are positioned in the full day the places are stored in', () => {
const plan = planMergedOrder([transport(8), place(1), note(7), transport(9), place(2)], [10, 1, 2]);
expect(plan.assignmentIds).toEqual([1, 2]);
expect(plan.transportUpdates).toEqual([
{ id: 8, day_plan_position: 0.5 },
{ id: 9, day_plan_position: 1 + 2 / 3 },
]);
expect(plan.noteUpdates).toEqual([{ id: 7, sort_order: 1 + 1 / 3 }]);
});
it('FE-UTIL-MERGEDORDER-012: an untimed stop dropped between two timed bookings stays there behind a hidden hotel row', () => {
const dayId = 5;
const tour = { id: 8, day_id: dayId, reservation_time: '2026-11-25T12:00' };
const flight = { id: 9, day_id: dayId, reservation_time: '2026-11-25T19:15' };
const newOrder = [transport(8), place(1), transport(9), place(2)];
const plan = planMergedOrder(newOrder, [10, 2, 1]);
const stored = withHiddenRows([10, 2, 1], plan.assignmentIds);
const dayAssignments = stored
.filter((id) => id !== 10)
.map((id) => ({ id, order_index: stored.indexOf(id), place: { place_time: null } }));
const positioned = [tour, flight].map((r) => {
const update = plan.transportUpdates.find((u) => u.id === r.id);
return { ...r, day_positions: { [dayId]: update?.day_plan_position } };
});
const drawn = getMergedItems({ dayAssignments, dayNotes: [], dayTransports: positioned, dayId });
expect(drawn.map((it) => `${it.type}:${it.data.id}`)).toEqual(['transport:8', 'place:1', 'transport:9', 'place:2']);
});
});