Files
TREK/client/tests/unit/slices/assignmentsSlice.test.ts
T
Maurice ed18c37903 fix: the findings of the final 4.3.0 audit
Every file the release touches was read once more, and what survived an
independent check against the code is fixed here, with a test for each.

Data and credentials
- A document sync connection edited to a new address no longer carries the
  stored token along: a blank form under another origin is refused, the same
  rule the probe route already had. Dawarich and AirTrail testConnection send
  the stored key only to the host it was stored for.
- The key rotation script now covers document_connections.secrets and
  trip_document_links.webhook_secret, which it had skipped.
- Turning a booked night into a pause in the road trip popup asks first and
  says that the booking and its expense go with it. Deleting a place says the
  same when a night is booked there, on the desktop and on the phone.
- A moved version marker over unchanged bytes no longer replaces the TREK file
  and its attachments; a real revision carries the attachments over.

Planner
- A drop in the day plan names the row it lands ahead of in the full day, so a
  hidden hotel stop no longer shifts it one slot early; the store sorts before
  it splices. Via re-anchoring, the booked-night guard and the sidebar's day
  membership read the stored day, the index space the server uses.
- A hotel stop placed mid-day re-pins the road trip vias of that day, and the
  booking session receives the new stop order too. The Public API and the iCal
  feed list a booked hotel once. A plugin can no longer write a stay against
  another trip's day.

Road trip and map
- The route overview paces its requests to the public routers and retries a
  rate limit, and it says which legs it could not route instead of quietly
  reporting a shorter trip. A click on a via handle no longer drops a second
  via; handles hide when zooming out. Read-only members cannot edit the driving
  limits. The phone shows a way back when no day is picked and prints the night
  drive departure in the chosen clock format.

Integrations and search
- Google transit keeps conventional trains when the train chip is on.
  TREK_PLACES_ENABLED=false stops the details and enrichment calls too, and
  accepts the whole boolean family. A caller-supplied description no longer
  lands in the instance-wide enrichment cache. Nominatim searches carry a
  timeout. An OSM place id is checked for shape before it reaches Overpass.
- Dawarich puts a stay on the trip whose dates hold it, the Atlas hand-off
  knows what the Atlas already shows, and a host change with a new key clears
  the old instance's state.
- Document sync: a webhook address pasted by hand works on the token alone,
  deleting a connection unregisters its subscriptions, Nextcloud under a
  sub-path connects, Synology answers a name collision before uploading and
  verifies a move into the bin, Paperless titles are capped at 128 characters,
  a DNS failure reads as unreachable rather than as a policy verdict, videos
  from the store are accepted like uploads, and remote_missing has a way out.
  Conflict buttons only for the owner, a refused choice is reported, a refused
  credential gets a Reconnect button, disconnecting asks first on the desktop
  as it does on the phone, and the folder picker no longer spins forever.

Release
- The server holds the release notice back from a bundle that cannot draw it,
  so the previous shell served from the service worker cache after an update
  no longer shows bare keys and no longer lets a click use the notice up.
- Import into an existing list gets the same body limit as a new list. Chat
  images say when the limit is reached and the new controls are translated.
  The transit backend switch reports a failed save. The Helm chart carries the
  Amap credentials in its Secret; the shipped templates document the Places
  API and Amap variables and the Overpass default. The wiki follows.
2026-09-20 13:54:31 +02:00

486 lines
20 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-023: the position counts in the day as sorted, not as the store happens to hold it', async () => {
// A row that came in over the socket sits at the end of the stored list whatever
// its order_index says, so the day the planner shows and the list the splice
// ran on disagreed and the new row landed behind the wrong neighbour.
const place = buildPlace({ id: 10, trip_id: 1 });
const later = buildAssignment({ id: 2, day_id: 1, order_index: 1 });
const first = buildAssignment({ id: 1, day_id: 1, order_index: 0 });
seedStore(useTripStore, { places: [place], assignments: { '1': [later, first] } });
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-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);
});
});
});