Files
TREK/shared/src/trip/trip.schema.spec.ts
T
Maurice c8d4ed896e fix(trips): let a trip run past 365 days and refuse a longer range instead of clipping it
A trip longer than a year stopped at day 365: generateDays capped the day
rows at MAX_TRIP_DAYS while the trip kept its full end date, so the last
days had a date but no day to plan on, in the day list, the transport and
booking dialogs and the phone's date pickers (#2403).

The limit is MAX_TRIP_DAYS in @trek/shared now, 999 days, shared by the
server, both trip forms and the MCP tools, and a range past it is refused
rather than cut short: 400 on REST, an error result on MCP, bad params on
the plugin RPC, and a message in the forms before anything is sent. The
check runs before the trip row and the budget are touched, also catches a
start date moved past the stored end on its own, and holds a day_count
rebuild to the same bound. A trip that already carries a longer range can
still be renamed. The MCP-only 90-day ceiling on create_trip goes with it;
it clipped dated trips the same way.

Closes #2403
2026-09-20 12:34:34 +02:00

74 lines
2.6 KiB
TypeScript

import {
MAX_TRIP_DAYS,
tripSpanDays,
tripCreateRequestSchema,
tripUpdateRequestSchema,
tripAddMemberRequestSchema,
activeTripResponseSchema,
} from './trip.schema';
import { describe, it, expect } from 'vitest';
describe('tripCreateRequestSchema', () => {
it('requires a title; dates/currency/reminder optional', () => {
expect(tripCreateRequestSchema.safeParse({ title: 'Japan' }).success).toBe(true);
expect(
tripCreateRequestSchema.safeParse({
title: 'Japan',
start_date: '2026-07-01',
day_count: 7,
}).success,
).toBe(true);
expect(tripCreateRequestSchema.safeParse({}).success).toBe(false);
});
});
describe('MAX_TRIP_DAYS', () => {
it('lets a trip run past a year and keeps day numbers to three digits (#2403)', () => {
expect(MAX_TRIP_DAYS).toBeGreaterThan(366);
expect(MAX_TRIP_DAYS).toBeLessThan(1000);
});
});
describe('tripSpanDays', () => {
it('counts both ends, across a leap day and a year boundary', () => {
expect(tripSpanDays('2026-07-01', '2026-07-01')).toBe(1);
expect(tripSpanDays('2026-07-01', '2026-07-07')).toBe(7);
expect(tripSpanDays('2024-02-28', '2024-03-01')).toBe(3);
expect(tripSpanDays('2025-01-26', '2026-01-28')).toBe(368);
});
it('goes below one when the end precedes the start', () => {
expect(tripSpanDays('2026-07-07', '2026-07-01')).toBe(-5);
});
});
describe('tripUpdateRequestSchema', () => {
it('is fully partial and accepts is_archived + cover_image', () => {
expect(tripUpdateRequestSchema.safeParse({}).success).toBe(true);
expect(tripUpdateRequestSchema.safeParse({ is_archived: 1, cover_image: null }).success).toBe(true);
});
});
describe('tripAddMemberRequestSchema', () => {
it('requires an identifier', () => {
expect(tripAddMemberRequestSchema.safeParse({ identifier: 'bob@x.y' }).success).toBe(true);
expect(tripAddMemberRequestSchema.safeParse({}).success).toBe(false);
});
});
describe('activeTripResponseSchema', () => {
it('accepts a trip with or without dates, and an explicit null', () => {
expect(activeTripResponseSchema.safeParse({ trip: { id: 1, title: 'Japan' } }).success).toBe(true);
expect(
activeTripResponseSchema.safeParse({ trip: { id: 1, title: 'Japan', start_date: null, end_date: null } }).success,
).toBe(true);
expect(activeTripResponseSchema.safeParse({ trip: null }).success).toBe(true);
});
it('rejects a missing trip key — "no trip" is null, not absent', () => {
expect(activeTripResponseSchema.safeParse({}).success).toBe(false);
expect(activeTripResponseSchema.safeParse({ trip: { title: 'Japan' } }).success).toBe(false);
});
});