fix(trips): count trip dates in calendar days, the same on REST and MCP

Filling in a missing end date and keeping a trip's length when its start
moved both shifted a local-time Date, so a trip crossing a DST change
lost or gained a day. Both now use addIsoDays. The one-date-makes-a-week
default moves from the REST controller into TripsService.create, so the
MCP create_trip tool no longer stores a half-dated trip, and the trip and
its days are written in one transaction.
This commit is contained in:
Maurice
2026-10-07 16:11:41 +02:00
parent bfa512589c
commit e0aedbb2c6
6 changed files with 65 additions and 29 deletions
@@ -161,10 +161,10 @@ describe('TripsController (parity with the legacy /api/trips route)', async () =
expect(tripCreateRequestSchema.safeParse({ title: '' }).success).toBe(false);
});
it('infers end_date from start_date (+6 days) and creates', async () => {
it('passes a single start date on as it is; the service fills in the week', async () => {
const create = vi.fn().mockReturnValue({ trip: { id: 9 }, tripId: 9, reminderDays: 0 });
await tc(svc({ create } as Partial<TripsService>)).create(user, { title: 'T', start_date: '2026-07-01' }, req);
expect(create).toHaveBeenCalledWith(1, expect.objectContaining({ start_date: '2026-07-01', end_date: '2026-07-07' }));
expect(create).toHaveBeenCalledWith(1, expect.objectContaining({ start_date: '2026-07-01', end_date: null }));
});
it('400 when end_date precedes start_date', async () => {
@@ -173,10 +173,10 @@ describe('TripsController (parity with the legacy /api/trips route)', async () =
});
});
it('infers start_date from end_date (-6 days) and parses day_count', async () => {
it('passes a single end date on as it is and parses day_count', async () => {
const create = vi.fn().mockReturnValue({ trip: { id: 9 }, tripId: 9, reminderDays: 0 });
await tc(svc({ create } as Partial<TripsService>)).create(user, prePipeCreateBody({ title: 'T', end_date: '2026-07-07', day_count: '40' }), req);
expect(create).toHaveBeenCalledWith(1, expect.objectContaining({ start_date: '2026-07-01', end_date: '2026-07-07', day_count: 40 }));
expect(create).toHaveBeenCalledWith(1, expect.objectContaining({ start_date: null, end_date: '2026-07-07', day_count: 40 }));
});
it('clamps a non-numeric day_count to the default of 7', async () => {
@@ -1116,6 +1116,24 @@ describe('folded trip CRUD', () => {
expect(getDays(tripId)).toHaveLength(3);
});
it('TRIP-SVC-090: one given date makes a week, counted in calendar days across a DST change', async () => {
const { user } = createUser(testDb);
const fromStart = await svc.create(user.id, { title: 'Spring', start_date: '2026-03-25' });
expect(fromStart.trip).toMatchObject({ start_date: '2026-03-25', end_date: '2026-03-31' });
expect(getDays(fromStart.tripId)).toHaveLength(7);
const fromEnd = await svc.create(user.id, { title: 'Autumn', end_date: '2026-10-30' });
expect(fromEnd.trip).toMatchObject({ start_date: '2026-10-24', end_date: '2026-10-30' });
});
it('TRIP-SVC-091: the trip and its days commit together', async () => {
const { user } = createUser(testDb);
const before = (testDb.prepare('SELECT COUNT(*) AS c FROM trips').get() as { c: number }).c;
const generate = vi.spyOn(svc, 'generateDays').mockRejectedValueOnce(new Error('disk I/O error'));
await expect(svc.create(user.id, { title: 'Half', start_date: '2026-05-01', end_date: '2026-05-03' })).rejects.toThrow('disk I/O error');
generate.mockRestore();
expect((testDb.prepare('SELECT COUNT(*) AS c FROM trips').get() as { c: number }).c).toBe(before);
});
it('TRIP-SVC-080: create without a currency takes the display currency, admin default included, else EUR', async () => {
const { user } = createUser(testDb);
const setUser = (value: string) =>