Files
TREK/client/tests/unit/slices/assignmentsSlice.test.ts
T
Maurice 154c88b48a feat(roadtrip): leave a stop at its End and show the stay that makes
A visit's End could be edited and was shown in road trip mode, but the
schedule never read it: the drive always left a stop after its stay, so
a traveller who knew when they had to leave but not how long they would
stand there had no way to say so.

The End is now the time the drive leaves. RoadtripStop.leaveAt is
filled from the visit's End (or its place's) on the client by
useRoadtripRoutes and on the server by roadtrip-plan.service, so
calculate_roadtrip and get_roadtrip_context agree with the planner, and
computeSchedule takes it as the departure it already knew how to hold.
With Start and End both set the stay is the time between them. With
daily travel times on, the stay is the time until then, and an End
before the window start starts the day early the way a Start does.
Before the first fixed time the chain works backwards through an End as
it does through a pinned Start.

An End is read against the drive: the last passing of that clock time
counts as missed when the drive went past it on the road or earlier
the same day, anything else is the next one, so a drive across
midnight neither waits a whole day nor pretends to be on time. A drive
that gets there after the End leaves on arrival and files a
missedLeave finding, shown beside the late badge in the rail and on the
phone.

The rail's STAY badge, the inspector's Stay tile, the phone stop sheet
and the phone chain show the stay the End makes, with an "until 14:00"
hint. It is read off the schedule and never written to
duration_minutes: the stay belongs to the place, the End to one visit,
and a stored copy would go stale as soon as the arrival moved. For such
a stop the stay dialog, desktop and phone, shows that stay read only
and offers to remove the End, through the visit's time route with an
offline path (assignmentRepo.setTimes, setAssignmentTimes). While the
road trip is on, the End field in both place forms says the drive
leaves at that time.

The dead checkoutAt in the plan service is gone. New strings in all 23
locales.
2026-09-19 19:24:52 +02:00

457 lines
18 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { http, HttpResponse } from 'msw';
import { useTripStore } from '../../../src/store/tripStore';
import { resetAllStores, seedStore } from '../../helpers/store';
import { buildPlace, buildAssignment } from '../../helpers/factories';
import { server } from '../../helpers/msw/server';
vi.mock('../../../src/api/websocket', () => ({
connect: vi.fn(),
disconnect: vi.fn(),
getSocketId: vi.fn(() => null),
joinTrip: vi.fn(),
leaveTrip: vi.fn(),
addListener: vi.fn(),
removeListener: vi.fn(),
setRefetchCallback: vi.fn(),
setPreReconnectHook: vi.fn(),
}));
beforeEach(() => {
resetAllStores();
});
describe('assignmentsSlice', () => {
describe('assignPlaceToDay', () => {
it('FE-ASSIGN-001: assignPlaceToDay adds optimistic temp ID (negative) immediately', async () => {
const place = buildPlace({ id: 10, trip_id: 1 });
seedStore(useTripStore, {
places: [place],
assignments: { '1': [] },
});
// Don't await — check state mid-flight
let tempAdded = false;
server.use(
http.post('/api/trips/1/days/1/assignments', async () => {
const state = useTripStore.getState();
const dayAssignments = state.assignments['1'];
if (dayAssignments.some(a => a.id < 0)) {
tempAdded = true;
}
const result = buildAssignment({ day_id: 1, place_id: 10, place });
return HttpResponse.json({ assignment: result });
}),
);
await useTripStore.getState().assignPlaceToDay(1, 1, 10);
expect(tempAdded).toBe(true);
});
it('FE-ASSIGN-002: after API success, temp ID is replaced with real assignment', async () => {
const place = buildPlace({ id: 10, trip_id: 1 });
seedStore(useTripStore, {
places: [place],
assignments: { '1': [] },
});
const realAssignment = buildAssignment({ id: 999, day_id: 1, place_id: 10, place });
server.use(
http.post('/api/trips/1/days/1/assignments', () =>
HttpResponse.json({ assignment: realAssignment })
),
);
await useTripStore.getState().assignPlaceToDay(1, 1, 10);
const dayAssignments = useTripStore.getState().assignments['1'];
expect(dayAssignments).toHaveLength(1);
expect(dayAssignments[0].id).toBe(999);
expect(dayAssignments.every(a => a.id > 0)).toBe(true);
});
it('FE-ASSIGN-003: on API failure, temp assignment is removed (rollback)', async () => {
const place = buildPlace({ id: 10, trip_id: 1 });
seedStore(useTripStore, {
places: [place],
assignments: { '1': [] },
});
server.use(
http.post('/api/trips/1/days/1/assignments', () =>
HttpResponse.json({ message: 'Error' }, { status: 500 })
),
);
await expect(useTripStore.getState().assignPlaceToDay(1, 1, 10)).rejects.toThrow();
const dayAssignments = useTripStore.getState().assignments['1'];
expect(dayAssignments).toHaveLength(0);
});
it('FE-ASSIGN-001b: returns undefined if place not found in store', async () => {
seedStore(useTripStore, {
places: [], // no places seeded
assignments: { '1': [] },
});
const result = await useTripStore.getState().assignPlaceToDay(1, 1, 999);
expect(result).toBeUndefined();
});
it('FE-ASSIGN-008: a day with no assignments entry yet is seeded from scratch', async () => {
const place = buildPlace({ id: 10, trip_id: 1 });
seedStore(useTripStore, { places: [place], assignments: {} });
server.use(
http.post('/api/trips/1/days/1/assignments', () =>
HttpResponse.json({ assignment: buildAssignment({ id: 999, day_id: 1, place_id: 10, place }) })
),
);
await useTripStore.getState().assignPlaceToDay(1, 1, 10);
expect(useTripStore.getState().assignments['1'].map(a => a.id)).toEqual([999]);
});
it('FE-ASSIGN-009: a response without a nested place falls back to the store place', async () => {
const place = buildPlace({ id: 10, trip_id: 1, name: 'Louvre' });
seedStore(useTripStore, { places: [place], assignments: { '1': [] } });
const bare = { ...buildAssignment({ id: 999, day_id: 1, place_id: 10 }), place: undefined };
server.use(
http.post('/api/trips/1/days/1/assignments', () => HttpResponse.json({ assignment: bare })),
);
await useTripStore.getState().assignPlaceToDay(1, 1, 10);
expect(useTripStore.getState().assignments['1'][0].place?.name).toBe('Louvre');
});
it('FE-ASSIGN-010: inserting at a position keeps the other rows and reorders server-side', async () => {
const place = buildPlace({ id: 10, trip_id: 1 });
const existing = buildAssignment({ id: 1, day_id: 1, order_index: 0 });
const other = buildAssignment({ id: 2, day_id: 1, order_index: 1 });
seedStore(useTripStore, { places: [place], assignments: { '1': [existing, other] } });
let reorderBody: number[] | undefined;
server.use(
http.post('/api/trips/1/days/1/assignments', () =>
HttpResponse.json({ assignment: buildAssignment({ id: 999, day_id: 1, place_id: 10, place, order_index: 7 }) })
),
http.put('/api/trips/1/days/1/assignments/reorder', async ({ request }) => {
const body = await request.json() as { orderedIds: number[] };
reorderBody = body.orderedIds;
return HttpResponse.json({ success: true });
}),
);
await useTripStore.getState().assignPlaceToDay(1, 1, 10, 1);
const items = useTripStore.getState().assignments['1'];
expect(items.map(a => a.id)).toEqual([1, 999, 2]);
expect(items.map(a => a.order_index)).toEqual([0, 1, 2]);
expect(reorderBody).toEqual([1, 999, 2]);
});
it('FE-ASSIGN-011: a failing follow-up reorder keeps the optimistic order', async () => {
const place = buildPlace({ id: 10, trip_id: 1 });
seedStore(useTripStore, {
places: [place],
assignments: { '1': [buildAssignment({ id: 1, day_id: 1, order_index: 0 })] },
});
server.use(
http.post('/api/trips/1/days/1/assignments', () =>
HttpResponse.json({ assignment: buildAssignment({ id: 999, day_id: 1, place_id: 10, place }) })
),
http.put('/api/trips/1/days/1/assignments/reorder', () =>
HttpResponse.json({ message: 'Error' }, { status: 500 })
),
);
// The reorder failure is swallowed — the create itself already succeeded.
await expect(useTripStore.getState().assignPlaceToDay(1, 1, 10, 0)).resolves.toBeDefined();
expect(useTripStore.getState().assignments['1'].map(a => a.id)).toEqual([999, 1]);
});
it('FE-ASSIGN-012: no reorder call when the day holds no server-side ids', async () => {
const place = buildPlace({ id: 10, trip_id: 1 });
seedStore(useTripStore, { places: [place], assignments: { '1': [] } });
let reorderCalls = 0;
server.use(
// An unsynced (still negative) id echoed back by the offline replay path.
http.post('/api/trips/1/days/1/assignments', () =>
HttpResponse.json({ assignment: buildAssignment({ id: -42, day_id: 1, place_id: 10, place }) })
),
http.put('/api/trips/1/days/1/assignments/reorder', () => {
reorderCalls += 1;
return HttpResponse.json({ success: true });
}),
);
await useTripStore.getState().assignPlaceToDay(1, 1, 10, 0);
expect(reorderCalls).toBe(0);
expect(useTripStore.getState().assignments['1'].map(a => a.id)).toEqual([-42]);
});
});
describe('removeAssignment', () => {
it('FE-ASSIGN-004: removeAssignment is optimistically removed, re-added on failure', async () => {
const place = buildPlace({ id: 10, trip_id: 1 });
const assignment = buildAssignment({ id: 100, day_id: 1, place });
seedStore(useTripStore, {
assignments: { '1': [assignment] },
});
server.use(
http.delete('/api/trips/1/days/1/assignments/100', () =>
HttpResponse.json({ message: 'Error' }, { status: 500 })
),
);
await expect(useTripStore.getState().removeAssignment(1, 1, 100)).rejects.toThrow();
// Should be rolled back
const dayAssignments = useTripStore.getState().assignments['1'];
expect(dayAssignments).toHaveLength(1);
expect(dayAssignments[0].id).toBe(100);
});
it('FE-ASSIGN-004b: removeAssignment success removes from store', async () => {
const place = buildPlace({ id: 10, trip_id: 1 });
const assignment = buildAssignment({ id: 100, day_id: 1, place });
seedStore(useTripStore, {
assignments: { '1': [assignment] },
});
await useTripStore.getState().removeAssignment(1, 1, 100);
expect(useTripStore.getState().assignments['1']).toHaveLength(0);
});
});
describe('reorderAssignments', () => {
it('FE-ASSIGN-005: reorderAssignments updates order_index of assignments', async () => {
const place1 = buildPlace({ id: 10 });
const place2 = buildPlace({ id: 20 });
const a1 = buildAssignment({ id: 1, day_id: 5, order_index: 0, place: place1 });
const a2 = buildAssignment({ id: 2, day_id: 5, order_index: 1, place: place2 });
seedStore(useTripStore, {
assignments: { '5': [a1, a2] },
});
await useTripStore.getState().reorderAssignments(1, 5, [2, 1]);
const dayAssignments = useTripStore.getState().assignments['5'];
const reorderedA2 = dayAssignments.find(a => a.id === 2);
const reorderedA1 = dayAssignments.find(a => a.id === 1);
expect(reorderedA2?.order_index).toBe(0);
expect(reorderedA1?.order_index).toBe(1);
});
it('FE-ASSIGN-005b: reorderAssignments rolls back on failure', async () => {
const place1 = buildPlace({ id: 10 });
const place2 = buildPlace({ id: 20 });
const a1 = buildAssignment({ id: 1, day_id: 5, order_index: 0, place: place1 });
const a2 = buildAssignment({ id: 2, day_id: 5, order_index: 1, place: place2 });
seedStore(useTripStore, {
assignments: { '5': [a1, a2] },
});
server.use(
http.put('/api/trips/1/days/5/assignments/reorder', () =>
HttpResponse.json({ message: 'Error' }, { status: 500 })
),
);
await expect(useTripStore.getState().reorderAssignments(1, 5, [2, 1])).rejects.toThrow();
const dayAssignments = useTripStore.getState().assignments['5'];
expect(dayAssignments.find(a => a.id === 1)?.order_index).toBe(0);
expect(dayAssignments.find(a => a.id === 2)?.order_index).toBe(1);
});
it('FE-ASSIGN-013: ids that are no longer on the day are dropped from the new order', async () => {
const a1 = buildAssignment({ id: 1, day_id: 5, order_index: 0 });
seedStore(useTripStore, { assignments: { '5': [a1] } });
await useTripStore.getState().reorderAssignments(1, 5, [999, 1]);
const dayAssignments = useTripStore.getState().assignments['5'];
expect(dayAssignments.map(a => a.id)).toEqual([1]);
expect(dayAssignments[0].order_index).toBe(1);
});
it('FE-ASSIGN-014: reordering a day with no assignments entry yields an empty list', async () => {
seedStore(useTripStore, { assignments: {} });
await useTripStore.getState().reorderAssignments(1, 5, [1, 2]);
expect(useTripStore.getState().assignments['5']).toEqual([]);
});
});
describe('moveAssignment', () => {
it('FE-ASSIGN-006: moveAssignment removes from source day and adds to target day', async () => {
const place = buildPlace({ id: 10 });
const assignment = buildAssignment({ id: 50, day_id: 1, order_index: 0, place });
seedStore(useTripStore, {
assignments: {
'1': [assignment],
'2': [],
},
});
await useTripStore.getState().moveAssignment(1, 50, 1, 2);
expect(useTripStore.getState().assignments['1']).toHaveLength(0);
expect(useTripStore.getState().assignments['2']).toHaveLength(1);
expect(useTripStore.getState().assignments['2'][0].id).toBe(50);
});
it('FE-ASSIGN-007: moveAssignment rolls back on failure', async () => {
const place = buildPlace({ id: 10 });
const assignment = buildAssignment({ id: 50, day_id: 1, order_index: 0, place });
seedStore(useTripStore, {
assignments: {
'1': [assignment],
'2': [],
},
});
server.use(
http.put('/api/trips/1/assignments/50/move', () =>
HttpResponse.json({ message: 'Error' }, { status: 500 })
),
);
await expect(useTripStore.getState().moveAssignment(1, 50, 1, 2)).rejects.toThrow();
// Rolled back: assignment back in day 1
expect(useTripStore.getState().assignments['1']).toHaveLength(1);
expect(useTripStore.getState().assignments['1'][0].id).toBe(50);
expect(useTripStore.getState().assignments['2']).toHaveLength(0);
});
it('FE-ASSIGN-015: an unknown assignment id is a no-op', async () => {
seedStore(useTripStore, { assignments: { '1': [buildAssignment({ id: 50, day_id: 1 })] } });
await useTripStore.getState().moveAssignment(1, 999, 1, 2);
expect(useTripStore.getState().assignments['1']).toHaveLength(1);
expect(useTripStore.getState().assignments['2']).toBeUndefined();
});
it('FE-ASSIGN-016: a source day with no assignments entry is a no-op', async () => {
seedStore(useTripStore, { assignments: {} });
await useTripStore.getState().moveAssignment(1, 50, 1, 2);
expect(useTripStore.getState().assignments).toEqual({});
});
it('FE-ASSIGN-017: dropping at an explicit index renumbers the target day and pushes the new order', async () => {
const moved = buildAssignment({ id: 50, day_id: 1, order_index: 0 });
const t1 = buildAssignment({ id: 60, day_id: 2, order_index: 1 });
const t2 = buildAssignment({ id: 61, day_id: 2, order_index: 0 });
seedStore(useTripStore, { assignments: { '1': [moved], '2': [t1, t2] } });
let reorderBody: number[] | undefined;
server.use(
http.put('/api/trips/1/days/2/assignments/reorder', async ({ request }) => {
const body = await request.json() as { orderedIds: number[] };
reorderBody = body.orderedIds;
return HttpResponse.json({ success: true });
}),
);
await useTripStore.getState().moveAssignment(1, 50, 1, 2, 1);
const target = useTripStore.getState().assignments['2'];
// Target day is sorted by order_index first (61, 60), then the drop lands at index 1.
expect(target.map(a => a.id)).toEqual([61, 50, 60]);
expect(target.map(a => a.order_index)).toEqual([0, 1, 2]);
expect(target[1].day_id).toBe(2);
expect(reorderBody).toEqual([61, 50, 60]);
});
it('FE-ASSIGN-018: moving onto a day with no assignments entry skips the reorder call', async () => {
const moved = buildAssignment({ id: 50, day_id: 1, order_index: 0 });
seedStore(useTripStore, { assignments: { '1': [moved] } });
let reorderCalls = 0;
server.use(
http.put('/api/trips/1/days/2/assignments/reorder', () => {
reorderCalls += 1;
return HttpResponse.json({ success: true });
}),
);
await useTripStore.getState().moveAssignment(1, 50, 1, 2);
expect(useTripStore.getState().assignments['2'].map(a => a.id)).toEqual([50]);
expect(reorderCalls).toBe(0);
});
});
describe('setAssignmentTimes', () => {
// The pool place still carries an End of its own, which the visit shows once its
// own override is gone, exactly as a fresh read of the day would.
const pool = () => buildPlace({ id: 10, trip_id: 1, place_time: null, end_time: '18:00' });
const visit = () => buildAssignment({
id: 5, day_id: 1, place_id: 10, assignment_time: '09:00', assignment_end_time: '14:00',
place: { ...pool(), place_time: '09:00', end_time: '14:00' },
});
it('FE-ASSIGN-020: clears the End at once, with the place End showing through, and keeps the Start', async () => {
seedStore(useTripStore, { places: [pool()], assignments: { '1': [visit()] } });
let seen: unknown;
server.use(
http.put('/api/trips/1/assignments/5/time', async ({ request }) => {
seen = await request.json();
const mid = useTripStore.getState().assignments['1'][0];
expect(mid).toMatchObject({ assignment_end_time: null, place: { end_time: '18:00', place_time: '09:00' } });
return HttpResponse.json({ assignment: { ...visit(), assignment_end_time: null, place: { ...pool(), place_time: '09:00' } } });
}),
);
await useTripStore.getState().setAssignmentTimes(1, 1, 5, { place_time: '09:00', end_time: null });
expect(seen).toEqual({ place_time: '09:00', end_time: null });
expect(useTripStore.getState().assignments['1'][0]).toMatchObject({
assignment_time: '09:00', assignment_end_time: null, place: { end_time: '18:00' },
});
});
it('FE-ASSIGN-021: a refused write puts the visit back as it was', async () => {
seedStore(useTripStore, { places: [pool()], assignments: { '1': [visit()] } });
server.use(http.put('/api/trips/1/assignments/5/time', () => HttpResponse.json({ error: 'no' }, { status: 403 })));
await expect(useTripStore.getState().setAssignmentTimes(1, 1, 5, { place_time: '09:00', end_time: null })).rejects.toBeTruthy();
expect(useTripStore.getState().assignments['1'][0]).toMatchObject({ assignment_end_time: '14:00', place: { end_time: '14:00' } });
});
it('FE-ASSIGN-022: a visit it cannot find, or one not yet saved, is left alone', async () => {
const unsaved = { ...visit(), id: -3 };
seedStore(useTripStore, { places: [pool()], assignments: { '1': [unsaved] } });
await useTripStore.getState().setAssignmentTimes(1, 1, 5, { place_time: null, end_time: null });
await useTripStore.getState().setAssignmentTimes(1, 1, -3, { place_time: null, end_time: null });
expect(useTripStore.getState().assignments['1'][0]).toEqual(unsaved);
});
});
describe('setAssignments', () => {
it('FE-ASSIGN-019: replaces the whole assignments map', () => {
seedStore(useTripStore, { assignments: { '1': [buildAssignment({ id: 1, day_id: 1 })] } });
const next = { '9': [buildAssignment({ id: 90, day_id: 9 })] };
useTripStore.getState().setAssignments(next);
expect(useTripStore.getState().assignments).toEqual(next);
});
});
});