Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

26.6 KB
Raw Blame
import { withStationaryJoins } from './nightBookends';
import { spillChains } from './nightSpill';
import type { QuietDay, RoadtripDay, RoadtripStop } from './planning-types';
import type { RouteSegment } from './planning-types';

import { describe, it, expect } from 'vitest';

type PlanDay = Pick<RoadtripDay, 'dayId' | 'dayNumber' | 'date' | 'title' | 'stops'>;

function stop(over: Partial<RoadtripStop> & { assignmentId: number; name: string }): RoadtripStop {
  return {
    ownerDayId: 1,
    ownerIndex: 0,
    placeId: over.assignmentId * 10,
    lat: 53.5,
    lng: 9.9,
    time: null,
    dwellMinutes: null,
    legMode: null,
    incomingLegMode: null,
    stopType: null,
    ...over,
  };
}

/** A day of the plan, with every stop's stored position filled in for it. */
function day(dayId: number, dayNumber: number, stops: RoadtripStop[]): PlanDay {
  return {
    dayId,
    dayNumber,
    date: null,
    title: null,
    stops: stops.map((s, i) => ({ ...s, ownerDayId: dayId, ownerIndex: i })),
  };
}

const seg = (minutes: number, km = 100): RouteSegment =>
  ({
    distance: km * 1000,
    duration: minutes * 60,
    distanceText: `${km} km`,
    durationText: `${minutes} min`,
    mode: 'driving',
  }) as RouteSegment;

/** Every leg the same length, which is all most of these cases need. */
const everyLeg =
  (minutes: number, km = 100) =>
  () => ({
    seg: seg(minutes, km),
    line: [
      [0, 0],
      [1, 1],
    ] as [number, number][],
  });

/** No leg routed at all, which is what an unrouted or offline trip looks like. */
const noLegs = () => undefined;

describe('spillChains', () => {
  it('FE-NIGHTSPILL-001: leaves an ordinary day exactly where it is', () => {
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '09:00', dwellMinutes: 60 }),
        stop({ assignmentId: 2, name: 'Berlin' }),
      ]),
    ];

    const chains = spillChains(plan, [], everyLeg(180));

    expect(chains).toHaveLength(1);
    expect(chains[0]!.stops.map((s) => s.name)).toEqual(['Hamburg', 'Berlin']);
    expect(chains[0]!.spills).toEqual([]);
  });

  it('FE-NIGHTSPILL-002: hands the stops reached after midnight to the next day', () => {
    // 21:00 + 1 h 30 stay = 22:30, then a three hour drive arrives at 01:30 tomorrow.
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '21:00', dwellMinutes: 90 }),
        stop({ assignmentId: 2, name: 'Neuruppin' }),
      ]),
      day(2, 2, [
        stop({ assignmentId: 3, name: 'Wittenberg', time: '10:00' }),
        stop({ assignmentId: 4, name: 'Leipzig' }),
      ]),
    ];

    const chains = spillChains(plan, [], everyLeg(180));

    expect(chains[0]!.stops.map((s) => s.name)).toEqual(['Hamburg']);
    expect(chains[1]!.stops.map((s) => s.name)).toEqual(['Neuruppin', 'Wittenberg', 'Leipzig']);
  });

  it('FE-NIGHTSPILL-003: the spill carries the night drive and the time it set off', () => {
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '21:00', dwellMinutes: 90 }),
        stop({ assignmentId: 2, name: 'Neuruppin' }),
      ]),
      day(2, 2, [
        stop({ assignmentId: 3, name: 'Wittenberg', time: '10:00' }),
        stop({ assignmentId: 4, name: 'Leipzig' }),
      ]),
    ];

    const chains = spillChains(plan, [], everyLeg(180, 240));

    expect(chains[1]!.spills).toHaveLength(1);
    const spill = chains[1]!.spills[0]!;
    expect(spill).toMatchObject({ at: 0, count: 1, fromDayNumber: 1, departure: '22:30' });
    // The leg is the drive that crossed midnight, not the one after it: without it the
    // card would gain a stop and none of the distance that reaches it.
    expect(spill.leg?.distance).toBe(240000);
    expect(spill.line.length).toBeGreaterThan(1);
  });

  it('FE-NIGHTSPILL-004: a stop that moved keeps the day the server stores it on', () => {
    const plan = [
      day(7, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '21:00', dwellMinutes: 90 }),
        stop({ assignmentId: 2, name: 'Neuruppin' }),
      ]),
      day(8, 2, [stop({ assignmentId: 3, name: 'Wittenberg', time: '10:00' })]),
    ];

    const chains = spillChains(plan, [], everyLeg(180));

    // Drawn under day 2, stored on day 1 — which is what every callback still names.
    const moved = chains[1]!.stops.find((s) => s.name === 'Neuruppin')!;
    expect(moved?.ownerDayId).toBe(7);
    expect(moved?.ownerIndex).toBe(1);
  });

  it('FE-NIGHTSPILL-005: the last day of a trip keeps its own crossing, having nowhere to send it', () => {
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '21:00', dwellMinutes: 90 }),
        stop({ assignmentId: 2, name: 'Neuruppin' }),
      ]),
    ];

    const chains = spillChains(plan, [], everyLeg(180));

    expect(chains).toHaveLength(1);
    expect(chains[0]!.stops.map((s) => s.name)).toEqual(['Hamburg', 'Neuruppin']);
    expect(chains[0]!.spills).toEqual([]);
    // And it still says so: the arrival is a day past the one the card is under.
    expect(chains[0]!.schedule.entries[1]!.dayOffset).toBe(1);
  });

  it('FE-NIGHTSPILL-006: a pinned time resolved to yesterday does not tear the day apart', () => {
    // The case that broke it: 07:00 then 23:00. `computeSchedule` resolves the second pin
    // to the occurrence nearest the drive reaching it, so nine hours late reads as nearer
    // than fifteen hours of waiting and 23:00 lands on the PREVIOUS day. The whole chain
    // is then lifted to stay positive, which leaves the FIRST stop wearing a day offset
    // and the one after it none. Placing stops on those offsets moved the early stop to
    // tomorrow and left the late one behind, visiting them out of order.
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Cobbelsdorf', time: '07:00', dwellMinutes: 45 }),
        stop({ assignmentId: 2, name: 'Wittenberg', time: '23:00', dwellMinutes: 180 }),
        stop({ assignmentId: 3, name: 'Leipzig' }),
      ]),
      day(2, 2, [stop({ assignmentId: 4, name: 'Dresden', time: '12:00' })]),
    ];

    const chains = spillChains(plan, [], everyLeg(50));

    // Cobbelsdorf and Wittenberg both stay: nothing between them crosses midnight.
    expect(chains[0]!.stops.map((s) => s.name)).toEqual(['Cobbelsdorf', 'Wittenberg']);
    // Leipzig is reached at 02:50, after the clock went back past 23:00, so it moves.
    expect(chains[1]!.stops.map((s) => s.name)).toEqual(['Leipzig', 'Dresden']);
  });

  it('FE-NIGHTSPILL-007: two nights land on two different days', () => {
    // 20:00, then two twelve-hour drives: 08:00 the next day, 20:00 that evening, and
    // 08:00 the day after — two backwards jumps of the clock, two days on.
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'A', time: '20:00' }),
        stop({ assignmentId: 2, name: 'B' }),
        stop({ assignmentId: 3, name: 'C' }),
        stop({ assignmentId: 4, name: 'D' }),
      ]),
      day(2, 2, []),
      day(3, 3, []),
    ];

    const chains = spillChains(plan, [], everyLeg(12 * 60));

    expect(chains[0]!.stops.map((s) => s.name)).toEqual(['A']);
    expect(chains[1]!.stops.map((s) => s.name)).toEqual(['B', 'C']);
    expect(chains[2]!.stops.map((s) => s.name)).toEqual(['D']);
  });

  it('FE-NIGHTSPILL-008: a day whose legs never routed moves nothing', () => {
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '21:00' }),
        stop({ assignmentId: 2, name: 'Neuruppin' }),
      ]),
      day(2, 2, [stop({ assignmentId: 3, name: 'Wittenberg', time: '10:00' })]),
    ];

    const chains = spillChains(plan, [], noLegs);

    // No drive, no arrival, no crossing — and a guess would be worse than nothing.
    expect(chains[0]!.stops.map((s) => s.name)).toEqual(['Hamburg', 'Neuruppin']);
    expect(chains[1]!.spills).toEqual([]);
  });

  it('FE-NIGHTSPILL-009: a day that carries no drive of its own still receives one', () => {
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '21:00', dwellMinutes: 90 }),
        stop({ assignmentId: 2, name: 'Neuruppin' }),
      ]),
    ];
    const quiet: QuietDay[] = [{ dayId: 2, dayNumber: 2, date: null, title: null, stops: [] }];

    const chains = spillChains(plan, quiet, everyLeg(180));

    expect(chains).toHaveLength(2);
    expect(chains[1]!.dayId).toBe(2);
    expect(chains[1]!.stops.map((s) => s.name)).toEqual(['Neuruppin']);
  });

  it('FE-NIGHTSPILL-010: the crossing is not marked twice on a stop that moved', () => {
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '21:00', dwellMinutes: 90 }),
        stop({ assignmentId: 2, name: 'Neuruppin' }),
      ]),
      day(2, 2, [stop({ assignmentId: 3, name: 'Wittenberg', time: '10:00' })]),
    ];

    const chains = spillChains(plan, [], everyLeg(180));

    // The block around it is what says the night happened; a band as well would say it
    // twice. And it is not "the next day" once it is drawn under the next day's date.
    expect(chains[1]!.schedule.warnings.some((w) => w.code === 'overnight')).toBe(false);
    expect(chains[1]!.schedule.entries[0]!.dayOffset).toBe(0);
  });

  it('FE-NIGHTSPILL-011: a finding about a stop travels with it, renumbered', () => {
    // Wittenberg is pinned two hours before the drive can get there, which is a "late"
    // finding against index 1 of day 1. It moves to day 2 as index 0.
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '21:00', dwellMinutes: 90 }),
        stop({ assignmentId: 2, name: 'Neuruppin', time: '00:30' }),
      ]),
      day(2, 2, [stop({ assignmentId: 3, name: 'Wittenberg', time: '10:00' })]),
    ];

    const chains = spillChains(plan, [], everyLeg(180));

    const late = chains[1]!.schedule.warnings.filter((w) => w.code === 'late');
    expect(late).toHaveLength(1);
    expect(late[0]!.index).toBe(0);
  });

  it('FE-NIGHTSPILL-012: two stops crossing together are one block, not two', () => {
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '21:00', dwellMinutes: 90 }),
        stop({ assignmentId: 2, name: 'Neuruppin' }),
        stop({ assignmentId: 3, name: 'Potsdam' }),
      ]),
      day(2, 2, [stop({ assignmentId: 4, name: 'Wittenberg', time: '10:00' })]),
    ];

    const chains = spillChains(plan, [], everyLeg(180));

    expect(chains[1]!.stops.map((s) => s.name)).toEqual(['Neuruppin', 'Potsdam', 'Wittenberg']);
    expect(chains[1]!.spills).toHaveLength(1);
    expect(chains[1]!.spills[0]!).toMatchObject({ at: 0, count: 2, fromDayNumber: 1 });
  });

  it('FE-NIGHTSPILL-018: a stop left at a set time holds the drive until then', () => {
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '09:00', dwellMinutes: 0 }),
        stop({ assignmentId: 2, name: 'Lueneburg', dwellMinutes: 30, leaveAt: '14:00' }),
        stop({ assignmentId: 3, name: 'Celle' }),
      ]),
    ];

    const chains = spillChains(plan, [], everyLeg(60));

    expect(chains[0]!.schedule.entries[1]!).toMatchObject({ arrival: '10:00', departure: '14:00' });
    expect(chains[0]!.schedule.entries[2]!.arrival).toBe('15:00');
    expect(chains[0]!.schedule.warnings).toEqual([]);
  });

  it('FE-NIGHTSPILL-019: a missed leave time keeps its minutes when the stop moves', () => {
    // Hamburg is left at 22:30 and the road takes three hours, so Neuruppin is reached at
    // half past one: an hour after the half past midnight it was meant to be left at.
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '21:00', dwellMinutes: 90 }),
        stop({ assignmentId: 2, name: 'Neuruppin', leaveAt: '00:30' }),
      ]),
      day(2, 2, [stop({ assignmentId: 3, name: 'Wittenberg', time: '10:00' })]),
    ];

    const chains = spillChains(plan, [], everyLeg(180));

    expect(chains[1]!.stops[0]!.name).toBe('Neuruppin');
    expect(chains[1]!.schedule.warnings).toContainEqual({ index: 0, code: 'missedLeave', minutes: 60 });
  });

  it('FE-NIGHTSPILL-020: a leave time the night drive went past is missed, not held until the next evening', () => {
    // Hamburg at eight, four and a half hours to Neuruppin: in at half past midnight, an
    // hour after the half past eleven it was meant to be left at. Read on the day of the
    // arrival it held the drive there for twenty-three hours and Potsdam fell a day behind.
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '20:00', dwellMinutes: 0 }),
        stop({ assignmentId: 2, name: 'Neuruppin', leaveAt: '23:30' }),
        stop({ assignmentId: 3, name: 'Potsdam' }),
      ]),
      day(2, 2, [stop({ assignmentId: 4, name: 'Wittenberg', time: '10:00' })]),
      day(3, 3, [stop({ assignmentId: 5, name: 'Leipzig', time: '10:00' })]),
    ];

    const chains = spillChains(plan, [], everyLeg(270));
    const dayTwo = chains.find((c) => c.dayNumber === 2)!;

    expect(dayTwo.stops.map((s) => s.name)).toEqual(['Neuruppin', 'Potsdam', 'Wittenberg']);
    expect(dayTwo.schedule.entries[0]!).toMatchObject({ arrival: '00:30', departure: '00:30' });
    expect(dayTwo.schedule.entries[1]!.arrival).toBe('05:00');
    expect(dayTwo.schedule.warnings).toContainEqual({ index: 0, code: 'missedLeave', minutes: 60 });
    expect(chains.find((c) => c.dayNumber === 3)!.stops.map((s) => s.name)).toEqual(['Leipzig']);
  });

  it('FE-NIGHTSPILL-021: a first stop left in the morning after a long stay keeps its day together', () => {
    // A hotel stayed at for twelve hours and left at eight. Worked back from its stay it
    // was reached the evening before, and the rest of the day moved onto the next card.
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hotel', dwellMinutes: 720, leaveAt: '08:00' }),
        stop({ assignmentId: 2, name: 'Celle' }),
        stop({ assignmentId: 3, name: 'Hannover' }),
      ]),
      day(2, 2, [stop({ assignmentId: 4, name: 'Hildesheim' }), stop({ assignmentId: 5, name: 'Goslar' })]),
    ];

    const chains = spillChains(plan, [], everyLeg(60));

    expect(chains[0]!.stops.map((s) => s.name)).toEqual(['Hotel', 'Celle', 'Hannover']);
    expect(chains[0]!.schedule.entries.map((e) => e.arrival)).toEqual(['00:00', '09:00', '10:00']);
    expect(chains[0]!.schedule.warnings).toEqual([]);
    // Nothing reaches into day two either: the day ended at ten in the morning.
    expect(chains[1]!.stops.map((s) => s.name)).toEqual(['Hildesheim', 'Goslar']);
    expect(chains[1]!.schedule.entries.map((e) => e.arrival)).toEqual([null, null]);
    expect(chains[1]!.spills).toEqual([]);
  });

  // A pinned stop the drive reaches late reads EARLIER on the clock than the stop before
  // it, because the schedule keeps the traveller's hour and flags the lateness. That is
  // not a night: the schedule itself files the stop on day 0. Counted as one, the stop
  // and every stop behind it left for the top of the next day's card, which reads as the
  // road trip shuffling the day.
  it('FE-NIGHTSPILL-022: a pinned stop reached late behind a stop without an hour stays on its day', () => {
    // 09:00 + 1 h, a three hour drive to Celle (13:00), three more to a 12:00 in Hannover.
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '09:00', dwellMinutes: 60 }),
        stop({ assignmentId: 2, name: 'Celle' }),
        stop({ assignmentId: 3, name: 'Hannover', time: '12:00' }),
      ]),
      day(2, 2, [stop({ assignmentId: 4, name: 'Goslar' })]),
    ];

    const chains = spillChains(plan, [], everyLeg(180));

    expect(chains[0]!.stops.map((s) => s.name)).toEqual(['Hamburg', 'Celle', 'Hannover']);
    expect(chains[0]!.schedule.warnings).toContainEqual({ index: 2, code: 'late', minutes: 240 });
    expect(chains[1]!.stops.map((s) => s.name)).toEqual(['Goslar']);
    expect(chains[1]!.spills).toEqual([]);
  });

  it('FE-NIGHTSPILL-023: a day stored against its hours keeps every stop on its own card', () => {
    // The order a stop dragged onto the day header used to be stored in: at the end,
    // behind stops with later hours. 10:00 after 14:00 is a stop reached late, not tomorrow.
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Lübeck', time: '14:00', dwellMinutes: 60 }),
        stop({ assignmentId: 2, name: 'Wismar', time: '10:00' }),
        stop({ assignmentId: 3, name: 'Rostock', time: '16:00' }),
      ]),
      day(2, 2, [stop({ assignmentId: 4, name: 'Stralsund' })]),
    ];

    const chains = spillChains(plan, [], everyLeg(30));

    expect(chains[0]!.stops.map((s) => s.name)).toEqual(['Lübeck', 'Wismar', 'Rostock']);
    expect(chains[0]!.schedule.warnings).toContainEqual({ index: 1, code: 'late', minutes: 330 });
    expect(chains[1]!.stops.map((s) => s.name)).toEqual(['Stralsund']);
    expect(chains[1]!.spills).toEqual([]);
  });

  it('FE-NIGHTSPILL-024: the stop behind one reached late is timed on from its pinned hour', () => {
    // The schedule carries on from Wismar's 10:00, so Kühlungsborn comes out at 10:30:
    // earlier than Lübeck's 14:00, later than the hour the drive just went on from.
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Lübeck', time: '14:00', dwellMinutes: 60 }),
        stop({ assignmentId: 2, name: 'Wismar', time: '10:00' }),
        stop({ assignmentId: 3, name: 'Kühlungsborn' }),
      ]),
      day(2, 2, [stop({ assignmentId: 4, name: 'Stralsund' })]),
    ];

    const chains = spillChains(plan, [], everyLeg(30));

    expect(chains[0]!.stops.map((s) => s.name)).toEqual(['Lübeck', 'Wismar', 'Kühlungsborn']);
    expect(chains[0]!.schedule.entries[2]!.arrival).toBe('10:30');
    expect(chains[1]!.stops.map((s) => s.name)).toEqual(['Stralsund']);
    expect(chains[1]!.spills).toEqual([]);
  });

  it('FE-NIGHTSPILL-025: a stop reached late after midnight still takes the stops behind it along', () => {
    // 20:00 + 1 h, four hours to a 00:30 in Berlin: late, and tomorrow. The leg out of
    // Berlin never routed, so Potsdam has no clock of its own and goes where Berlin goes.
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '20:00', dwellMinutes: 60 }),
        stop({ assignmentId: 2, name: 'Berlin', time: '00:30' }),
        stop({ assignmentId: 3, name: 'Potsdam' }),
      ]),
      day(2, 2, [stop({ assignmentId: 4, name: 'Dresden', time: '12:00' })]),
    ];
    const legFor = (from: RoadtripStop) => (from.name === 'Berlin' ? undefined : everyLeg(240)());

    const chains = spillChains(plan, [], legFor);

    expect(chains[0]!.stops.map((s) => s.name)).toEqual(['Hamburg']);
    expect(chains[1]!.stops.map((s) => s.name)).toEqual(['Berlin', 'Potsdam', 'Dresden']);
    expect(chains[1]!.schedule.warnings).toContainEqual({ index: 0, code: 'late', minutes: 30 });
  });

  it('FE-NIGHTSPILL-026: the clock going back past a late hour is still a night', () => {
    // FE-NIGHTSPILL-006 with a night at Wittenberg: 23:00, reached late, eight hours on
    // and fifty minutes to Leipzig at 07:50. That is later than Cobbelsdorf's 07:00 but
    // earlier than the 23:00 it was timed on from, so it is tomorrow morning.
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Cobbelsdorf', time: '07:00', dwellMinutes: 45 }),
        stop({ assignmentId: 2, name: 'Wittenberg', time: '23:00', dwellMinutes: 480 }),
        stop({ assignmentId: 3, name: 'Leipzig' }),
      ]),
      day(2, 2, [stop({ assignmentId: 4, name: 'Dresden', time: '12:00' })]),
    ];

    const chains = spillChains(plan, [], everyLeg(50));

    expect(chains[0]!.stops.map((s) => s.name)).toEqual(['Cobbelsdorf', 'Wittenberg']);
    expect(chains[1]!.stops.map((s) => s.name)).toEqual(['Leipzig', 'Dresden']);
    expect(chains[1]!.schedule.entries[0]!.arrival).toBe('07:50');
  });

  it('FE-NIGHTSPILL-027: a stop without a clock follows a night drive out of a late hour', () => {
    // 06:00 + 12 h, three hours to a 20:00 in Kassel: late. An hour there, ten hours
    // through the night to Munich at 07:00, and Garmisch behind it never routed.
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '06:00', dwellMinutes: 720 }),
        stop({ assignmentId: 2, name: 'Kassel', time: '20:00', dwellMinutes: 60 }),
        stop({ assignmentId: 3, name: 'München' }),
        stop({ assignmentId: 4, name: 'Garmisch' }),
      ]),
      day(2, 2, [stop({ assignmentId: 5, name: 'Innsbruck' })]),
    ];
    const legMinutes: Record<string, number> = { Hamburg: 180, Kassel: 600 };
    const legFor = (from: RoadtripStop) => (legMinutes[from.name] ? everyLeg(legMinutes[from.name]!)() : undefined);

    const chains = spillChains(plan, [], legFor);

    expect(chains[0]!.stops.map((s) => s.name)).toEqual(['Hamburg', 'Kassel']);
    expect(chains[0]!.schedule.warnings).toContainEqual({ index: 1, code: 'late', minutes: 60 });
    expect(chains[1]!.stops.map((s) => s.name)).toEqual(['München', 'Garmisch', 'Innsbruck']);
  });

  describe('a stay that runs past midnight carries the clock into the next day', () => {
    // Standing somewhere for twenty-four hours is not over when the date changes. The
    // day after used to begin at nothing, so a trip whose second day had no pinned time
    // showed no times at all, right after the first day had said exactly when it ends.
    it('FE-NIGHTSPILL-013: the next day starts where the stay ends', () => {
      const plan = [
        day(1, 1, [
          stop({ assignmentId: 1, name: 'Hamburg', time: '07:56' }),
          stop({ assignmentId: 2, name: 'Mercure', time: '08:00', dwellMinutes: 24 * 60 }),
        ]),
        day(2, 2, [stop({ assignmentId: 3, name: 'Panorama' }), stop({ assignmentId: 4, name: 'Bergedorf' })]),
      ];

      const chains = spillChains(plan, [], everyLeg(11));
      const dayTwo = chains.find((c) => c.dayNumber === 2)!;

      // 08:00 is when the stay ENDS; getting to the first stop still takes the eleven
      // minutes the road takes.
      expect(dayTwo.schedule.entries[0]!.arrival).toBe('08:11');
      expect(dayTwo.schedule.entries[1]!.arrival).toBe('08:22');
    });

    it('FE-NIGHTSPILL-014: an ordinary evening hands on nothing', () => {
      // The night between two days is not a wait somebody is serving out. Only a stay
      // that genuinely runs past midnight reaches into the morning.
      const plan = [
        day(1, 1, [stop({ assignmentId: 1, name: 'Hamburg', time: '18:00', dwellMinutes: 60 })]),
        day(2, 2, [stop({ assignmentId: 2, name: 'Panorama' })]),
      ];

      const chains = spillChains(plan, [], everyLeg(11));

      expect(chains.find((c) => c.dayNumber === 2)!.schedule.entries[0]!.arrival).toBeNull();
    });

    it('FE-NIGHTSPILL-015: a stop pinned before the stay ends keeps its clock and is flagged', () => {
      const plan = [
        day(1, 1, [stop({ assignmentId: 1, name: 'Mercure', time: '08:00', dwellMinutes: 24 * 60 })]),
        day(2, 2, [stop({ assignmentId: 2, name: 'Panorama', time: '07:00' })]),
      ];

      const chains = spillChains(plan, [], everyLeg(11));
      const dayTwo = chains.find((c) => c.dayNumber === 2)!;

      // Never moved: the hour is the traveller's.
      expect(dayTwo.schedule.entries[0]!.arrival).toBe('07:00');
      // 71 minutes short, not 60: the stay ends at eight and the road takes eleven.
      expect(dayTwo.schedule.warnings).toContainEqual({ index: 0, code: 'late', minutes: 71 });
    });

    it('FE-NIGHTSPILL-017: the stop after the stay moves to the day it is reached on', () => {
      // Both stops sit on day one, but the second is reached a full day after the first.
      // The clock reads later, not earlier, so the backwards-jump rule never sees it; the
      // schedule's own day count does.
      const plan = [
        day(1, 1, [
          stop({ assignmentId: 1, name: 'Hamburg', time: '07:56' }),
          stop({ assignmentId: 2, name: 'Mercure', time: '08:00', dwellMinutes: 24 * 60 }),
          stop({ assignmentId: 3, name: 'Panorama' }),
        ]),
        day(2, 2, []),
      ];

      const chains = spillChains(plan, [], everyLeg(10));

      expect(chains.find((c) => c.dayNumber === 1)!.stops.map((s) => s.name)).toEqual(['Hamburg', 'Mercure']);
      const dayTwo = chains.find((c) => c.dayNumber === 2)!;
      expect(dayTwo.stops.map((s) => s.name)).toEqual(['Panorama']);
      expect(dayTwo.schedule.entries[0]!.arrival).toBe('08:10');
      // Marked as having set off yesterday, so the card can say where it came from.
      expect(dayTwo.spills[0]).toMatchObject({ at: 0, count: 1, fromDayNumber: 1 });
    });

    it('FE-NIGHTSPILL-016: a stay of two whole days walks the clock forward day by day', () => {
      const plan = [
        day(1, 1, [stop({ assignmentId: 1, name: 'Mercure', time: '08:00', dwellMinutes: 48 * 60 })]),
        day(2, 2, [stop({ assignmentId: 2, name: 'Nothing planned' })]),
        day(3, 3, [stop({ assignmentId: 3, name: 'Berlin' })]),
      ];

      const chains = spillChains(plan, [], everyLeg(11));

      // Day two is still inside the stay, so it cannot begin at all.
      expect(chains.find((c) => c.dayNumber === 2)!.schedule.entries[0]!.arrival).toBeNull();
      expect(chains.find((c) => c.dayNumber === 3)!.schedule.entries[0]!.arrival).toBe('08:11');
    });
  });
});

describe('a booked night reached after midnight', () => {
  const bookend = (phase: 'morning' | 'evening') => ({
    phase,
    accommodationId: 5,
    reservationId: null,
    checkingOut: false,
    checkingIn: false,
    checkOut: null,
  });

  it('FE-NIGHTSPILL-028: the drive back to the hotel goes on the next card, and the morning waits for it', () => {
    // 20:00 + 1 h and four hours back to the hotel: in at 01:00. The night at the hotel
    // is one spot, so the morning sets out no earlier than that.
    const plan = [
      day(1, 1, [
        stop({ assignmentId: 1, name: 'Hamburg', time: '20:00', dwellMinutes: 60 }),
        stop({
          assignmentId: -6_000_000_003,
          name: 'Back to the hotel',
          lat: 52,
          lng: 13,
          bookend: bookend('evening'),
        }),
      ]),
      day(2, 2, [
        stop({ assignmentId: -6_000_000_004, name: 'From the hotel', lat: 52, lng: 13, bookend: bookend('morning') }),
        stop({ assignmentId: 3, name: 'Dresden' }),
      ]),
    ];

    const chains = spillChains(
      plan,
      [],
      withStationaryJoins(() => ({ ...everyLeg(240)(), vias: [] })),
    );

    expect(chains[0]!.stops.map((s) => s.name)).toEqual(['Hamburg']);
    expect(chains[1]!.stops.map((s) => s.name)).toEqual(['Back to the hotel', 'From the hotel', 'Dresden']);
    expect(chains[1]!.spills).toEqual([expect.objectContaining({ at: 0, count: 1, fromDayNumber: 1 })]);
    expect(chains[1]!.schedule.entries.map((e) => e.arrival)).toEqual(['01:00', '01:00', '05:00']);
  });
});