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.

12.7 KB
Raw Blame
import {
  buildWalletPassRequest,
  clean,
  displayTime,
  kindOf,
  serialFor,
  withOffset,
  type WalletPassSource,
  type WalletWords,
} from '../../../../src/nest/wallet-pass/wallet-pass.builder';

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

const WORDS: WalletWords = {
  locale: 'en-GB',
  departure: 'Departure',
  arrival: 'Arrival',
  pickup: 'Pickup',
  dropOff: 'Drop-off',
  checkIn: 'Check-in',
  checkOut: 'Check-out',
  time: 'Time',
  seat: 'Seat',
  platform: 'Platform',
  flightNumber: 'Flight No.',
  trainNumber: 'Train No.',
  airline: 'Airline',
  confirmation: 'Booking Code',
  location: 'Location / Address',
};

function source(
  over: Partial<WalletPassSource['reservation']> = {},
  rest: Partial<WalletPassSource> = {},
): WalletPassSource {
  return {
    reservation: {
      id: 7,
      trip_id: 42,
      title: 'Hotel Granvia Kyoto',
      type: 'hotel',
      reservation_time: '2027-04-02T15:00',
      reservation_end_time: '2027-04-06T11:00',
      confirmation_number: 'HG-48213',
      location: 'Karasuma-dori, Kyoto',
      created_at: '2026-11-01 10:00:00',
      metadata: { check_in_time: '15:00', check_out_time: '11:00' },
      ...over,
    },
    endpoints: [],
    tripTitle: 'Japan 2027',
    place: { lat: 34.985, lng: 135.758, name: 'Hotel Granvia Kyoto' },
    ...rest,
  };
}

describe('wallet pass builder', () => {
  it('WALLET-B-001: maps every booking type to a pass kind', () => {
    expect(kindOf('flight')).toBe('flight');
    expect(kindOf('cruise')).toBe('boat');
    expect(kindOf('ferry')).toBe('boat');
    expect(kindOf('transit')).toBe('bus');
    expect(kindOf('taxi')).toBe('car');
    expect(kindOf('hotel')).toBe('hotel');
    expect(kindOf('tour')).toBe('tour');
    expect(kindOf('transport_other')).toBe('other');
    expect(kindOf(null)).toBe('other');
  });

  it('WALLET-B-002: cleans texts of links and shortens them', () => {
    expect(clean('Pay at https://evil.example/now please', 60)).toBe('Pay at please');
    expect(clean('see www.example.com', 60)).toBe('see');
    expect(clean('  a   b  ', 10)).toBe('a b');
    expect(clean('x'.repeat(30), 10)).toBe(`${'x'.repeat(9)}…`);
    expect(clean('https://only.link', 10)).toBeUndefined();
    expect(clean(null, 10)).toBeUndefined();
  });

  it('WALLET-B-003: gives a wall-clock time the offset its zone has then', () => {
    expect(withOffset('2027-04-02', '15:00', 'Asia/Tokyo')).toBe('2027-04-02T15:00:00+09:00');
    expect(withOffset('2027-07-01', '08:30', 'Europe/Berlin')).toBe('2027-07-01T08:30:00+02:00');
    expect(withOffset('2027-01-15', '08:30', 'Europe/Berlin')).toBe('2027-01-15T08:30:00+01:00');
    expect(withOffset('2027-01-15', '08:30', 'America/St_Johns')).toBe('2027-01-15T08:30:00-03:30');
    expect(withOffset('2027-01-15', null, null)).toBe('2027-01-15T00:00:00+00:00');
    expect(withOffset('2027-01-15', '10:00', 'Not/AZone')).toBe('2027-01-15T10:00:00+00:00');
  });

  it('WALLET-B-004: shows a time as the wall clock says it, in the user language', () => {
    expect(displayTime('en-GB', '2027-04-02', '15:00')).toMatch(/Fri.*2 Apr.*15:00/);
    expect(displayTime('de-DE', '2027-04-02', null)).toMatch(/Fr.*2\. Apr/);
    expect(displayTime('no-such-LOCALE-x', '2027-04-02', null)).toMatch(/Apr/);
  });

  it('WALLET-B-005: a hotel becomes a pass with check-in, check-out, place and QR code', () => {
    const request = buildWalletPassRequest(source(), WORDS);
    expect(request).toMatchObject({
      kind: 'hotel',
      title: 'Hotel Granvia Kyoto',
      trip: 'Japan 2027',
      start: '2027-04-02T15:00:00+09:00',
      end: '2027-04-06T11:00:00+09:00',
      location: { latitude: 34.985, longitude: 135.758, name: 'Hotel Granvia Kyoto' },
      confirmation: { label: 'Booking Code', value: 'HG-48213' },
      locale: 'en-GB',
    });
    expect(request?.details?.map((d) => d.label)).toEqual(['Check-in', 'Check-out']);
    expect(request?.back).toEqual([{ label: 'Location / Address', value: 'Karasuma-dori, Kyoto' }]);
    expect(request?.serial).toBe(serialFor(source().reservation));
    expect(request?.serial).toMatch(/^trek-7-[0-9a-f]{16}$/);
  });

  it('WALLET-B-006: a flight becomes a boarding pass from and to, in the departure zone', () => {
    const request = buildWalletPassRequest(
      source(
        {
          title: 'LH 716',
          type: 'flight',
          reservation_time: '2027-04-01T13:25',
          reservation_end_time: null,
          metadata: { airline: 'Lufthansa', flight_number: 'LH716', seat: '34K' },
        },
        {
          place: null,
          endpoints: [
            {
              role: 'from',
              name: 'Frankfurt',
              code: 'FRA',
              lat: 50.03,
              lng: 8.57,
              timezone: 'Europe/Berlin',
              local_date: '2027-04-01',
              local_time: '13:25',
            },
            {
              role: 'to',
              name: 'Tokyo Haneda',
              code: 'HND',
              lat: 35.55,
              lng: 139.78,
              timezone: 'Asia/Tokyo',
              local_date: '2027-04-02',
              local_time: '08:50',
            },
          ],
        },
      ),
      WORDS,
    );
    expect(request).toMatchObject({
      kind: 'flight',
      start: '2027-04-01T13:25:00+02:00',
      end: '2027-04-02T08:50:00+09:00',
      from: { code: 'FRA', name: 'Frankfurt' },
      to: { code: 'HND', name: 'Tokyo Haneda' },
      location: { latitude: 50.03, longitude: 8.57, name: 'Frankfurt' },
    });
    expect(request?.details?.map((d) => d.label)).toEqual(['Departure', 'Flight No.', 'Seat', 'Arrival']);
    expect(request?.details?.[1]?.value).toBe('Lufthansa LH716');
  });

  const ENDS = [
    {
      role: 'from',
      name: 'Milan',
      code: 'MXP',
      lat: 45.63,
      lng: 8.72,
      timezone: 'Europe/Rome',
      local_date: '2027-04-02',
      local_time: '11:00',
    },
    {
      role: 'to',
      name: 'New York',
      code: 'JFK',
      lat: 40.64,
      lng: -73.78,
      timezone: 'America/New_York',
      local_date: '2027-04-02',
      local_time: '20:40',
    },
  ] as WalletPassSource['endpoints'];

  it('WALLET-B-011: a flight with a stop shows the seat of each leg, also one booked only on the second', () => {
    const seat = (metadata: Record<string, unknown>) =>
      buildWalletPassRequest(
        source({ type: 'flight', reservation_time: '2027-04-02T11:00', metadata }, { endpoints: ENDS }),
        WORDS,
      )?.details?.find((d) => d.label === 'Seat')?.value;
    const legs = [
      { from: 'MXP', to: 'IST' },
      { from: 'IST', to: 'JFK', seat: '14C' },
    ];
    expect(seat({ legs })).toBe('14C');
    expect(seat({ seat: '3A', legs: [{ from: 'MXP', to: 'IST', seat: '3A' }, legs[1]] })).toBe('3A / 14C');
    // An older booking keeps its flat seat on the first leg.
    expect(
      seat({
        seat: '3A',
        legs: [
          { from: 'MXP', to: 'IST' },
          { from: 'IST', to: 'JFK' },
        ],
      }),
    ).toBe('3A');
    expect(
      seat({
        legs: [
          { from: 'MXP', to: 'IST' },
          { from: 'IST', to: 'JFK' },
        ],
      }),
    ).toBeUndefined();
  });

  it('WALLET-B-007: a journey without both ends is a plain booking; a train shows its number and platform', () => {
    const half = buildWalletPassRequest(source({ type: 'train', metadata: {} }, { place: null, endpoints: [] }), WORDS);
    expect(half?.kind).toBe('other');
    expect(half?.from).toBeUndefined();

    const train = buildWalletPassRequest(
      source(
        { type: 'train', metadata: { train_number: 'ICE 597', platform: '7', seat: '61' } },
        {
          place: null,
          endpoints: [
            {
              role: 'from',
              name: 'Berlin Hbf',
              code: null,
              lat: 52.52,
              lng: 13.37,
              timezone: null,
              local_date: null,
              local_time: null,
            },
            {
              role: 'to',
              name: 'München Hbf',
              code: null,
              lat: 48.14,
              lng: 11.56,
              timezone: null,
              local_date: null,
              local_time: null,
            },
          ],
        },
      ),
      WORDS,
    );
    expect(train?.kind).toBe('train');
    // No zone on the endpoint: looked up from its coordinates.
    expect(train?.start).toBe('2027-04-02T15:00:00+02:00');
    // At most four lines under the title; the arrival is the one that gives way.
    expect(train?.details?.map((d) => d.label)).toEqual(['Departure', 'Train No.', 'Seat', 'Platform']);
    expect(train?.from).toEqual({ name: 'Berlin Hbf' });
  });

  it('WALLET-B-008: a car shows pickup and drop-off, a restaurant its time', () => {
    const car = buildWalletPassRequest(source({ type: 'car', title: 'Sixt' }), WORDS);
    expect(car?.details?.map((d) => d.label)).toEqual(['Pickup', 'Drop-off']);
    const dinner = buildWalletPassRequest(
      source({ type: 'restaurant', title: 'Gion Karyo', reservation_end_time: null }),
      WORDS,
    );
    expect(dinner?.kind).toBe('restaurant');
    expect(dinner?.details).toEqual([{ label: 'Time', value: expect.stringMatching(/15:00/) }]);
    expect(dinner?.end).toBeUndefined();
  });

  it('WALLET-B-009: a confirmation that is no code goes on the back; no date means no pass', () => {
    const odd = buildWalletPassRequest(source({ confirmation_number: 'Ref: 12#34 (see mail)' }), WORDS);
    expect(odd?.confirmation).toBeUndefined();
    expect(odd?.back).toContainEqual({ label: 'Booking Code', value: 'Ref: 12#34 (see mail)' });
    expect(buildWalletPassRequest(source({ reservation_time: null }), WORDS)).toBeNull();
    expect(buildWalletPassRequest(source({ reservation_time: 'soon' }), WORDS)).toBeNull();
  });

  it('WALLET-B-010: a booking without place or trip still makes a pass, its title cleaned', () => {
    const request = buildWalletPassRequest(
      source(
        { title: 'Museum https://tickets.example', type: 'event', location: null, confirmation_number: null },
        { place: null, tripTitle: null },
      ),
      WORDS,
    );
    expect(request).toMatchObject({ kind: 'event', title: 'Museum' });
    expect(request?.trip).toBeUndefined();
    expect(request?.location).toBeUndefined();
    expect(request?.back).toBeUndefined();
    expect(request?.start).toBe('2027-04-02T15:00:00+00:00');
  });

  // A hotel whose dates live on its stay, and a booking linked to a day, carry no
  // date of their own. The app shows the stay's and the day's dates, so the pass
  // used to refuse a booking the user could see dated ("give the booking a date").
  it('WALLET-B-011: a hotel without its own time takes the dates and times of its stay', () => {
    const request = buildWalletPassRequest(
      source(
        { reservation_time: null, reservation_end_time: null, metadata: {} },
        { linked: { startDate: '2027-01-08', endDate: '2027-01-11', checkIn: '15:00', checkOut: '10:00:00' } },
      ),
      WORDS,
    );
    expect(request).toMatchObject({
      kind: 'hotel',
      start: '2027-01-08T15:00:00+09:00',
      end: '2027-01-11T10:00:00+09:00',
    });
    expect(request?.details).toEqual([
      { label: 'Check-in', value: displayTime('en-GB', '2027-01-08', '15:00') },
      { label: 'Check-out', value: displayTime('en-GB', '2027-01-11', '10:00') },
    ]);
  });

  it('WALLET-B-012: the booking own check-in time comes before the stay one', () => {
    const request = buildWalletPassRequest(
      source(
        { reservation_time: null, reservation_end_time: null, metadata: { check_in_time: '16:30' } },
        { linked: { startDate: '2027-01-08', endDate: null, checkIn: '15:00', checkOut: null } },
      ),
      WORDS,
    );
    expect(request?.start).toBe('2027-01-08T16:30:00+09:00');
    expect(request?.end).toBeUndefined();
  });

  it('WALLET-B-013: a day-linked booking with only a clock is dated by its day', () => {
    const request = buildWalletPassRequest(
      source(
        {
          type: 'restaurant',
          title: 'Gion Karyo',
          reservation_time: '19:30',
          reservation_end_time: null,
          metadata: {},
        },
        { linked: { startDate: '2027-04-03', endDate: null } },
      ),
      WORDS,
    );
    expect(request).toMatchObject({ kind: 'restaurant', start: '2027-04-03T19:30:00+09:00' });
  });

  it('WALLET-B-014: its own date still wins over the linked day, and nothing at all still means no pass', () => {
    const own = buildWalletPassRequest(
      source({}, { linked: { startDate: '2027-01-08', endDate: '2027-01-11' } }),
      WORDS,
    );
    expect(own?.start).toBe('2027-04-02T15:00:00+09:00');
    expect(
      buildWalletPassRequest(source({ reservation_time: null }, { linked: { startDate: null, endDate: null } }), WORDS),
    ).toBeNull();
  });
});