mirror of
https://github.com/bulwarkmail/native.git
synced 2026-11-02 16:07:12 +00:00
feat: read schema.org booking, parcel and order markup from verified mail
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
// Mirrors webmail lib/structured-mail/__tests__/booking-markup.test.ts at ba6d5e448; keep the two in step.
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { readStructuredMail, readStructuredMailDetailed } from '..';
|
||||
|
||||
const CTX = { mailDate: new Date('2026-11-20T08:00:00Z') };
|
||||
const page = (...blocks: unknown[]) => `<html><head>${blocks.map((b) => `<script type="application/ld+json">${JSON.stringify(b)}</script>`).join('')}</head><body><p>Hello</p></body></html>`;
|
||||
|
||||
function flight(departureTime: string, status = 'http://schema.org/ReservationConfirmed') {
|
||||
return {
|
||||
'@context': 'http://schema.org', '@type': 'FlightReservation', reservationNumber: 'PK7Q2Z', reservationStatus: status,
|
||||
underName: { '@type': 'Person', name: 'Jana Beispiel' },
|
||||
reservationFor: {
|
||||
'@type': 'Flight', flightNumber: '1072', airline: { '@type': 'Airline', name: 'Swiss', iataCode: 'LX' },
|
||||
departureAirport: { '@type': 'Airport', iataCode: 'ZRH' }, departureTime,
|
||||
arrivalAirport: { '@type': 'Airport', iataCode: 'BER' }, arrivalTime: '2026-12-03T09:05:00+01:00',
|
||||
},
|
||||
};
|
||||
}
|
||||
const ORDER = { '@context': 'http://schema.org', '@type': 'Order', orderNumber: 'SO-77', merchant: 'Shop' };
|
||||
|
||||
describe('readStructuredMailDetailed', () => {
|
||||
it('reports booking markup for a valid booking, with the same cards as readStructuredMail', () => {
|
||||
const html = page(flight('2026-12-03T07:40:00+01:00'));
|
||||
const detailed = readStructuredMailDetailed(html, CTX);
|
||||
expect(detailed.hadBookingMarkup).toBe(true);
|
||||
expect(detailed.cards).toEqual(readStructuredMail(html, CTX));
|
||||
expect(detailed.cards).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('reports booking markup for a cancelled booking', () => {
|
||||
const detailed = readStructuredMailDetailed(page(flight('2026-12-03T07:40:00+01:00', 'http://schema.org/ReservationCancelled')), CTX);
|
||||
expect(detailed).toMatchObject({ hadBookingMarkup: true, cards: [{ status: 'cancelled' }] });
|
||||
});
|
||||
|
||||
it('reports booking markup that validation or merging rejected', () => {
|
||||
const conflicting = readStructuredMailDetailed(page(flight('2026-12-03T07:40:00+01:00'), flight('2026-12-03T08:10:00+01:00')), CTX);
|
||||
expect(conflicting).toEqual({ cards: [], hadBookingMarkup: true });
|
||||
const incomplete = readStructuredMailDetailed(page(flight('2026-12-03')), CTX);
|
||||
expect(incomplete).toEqual({ cards: [], hadBookingMarkup: true });
|
||||
});
|
||||
|
||||
it('reports booking markup for reservation types it has no card for', () => {
|
||||
const boat = { '@context': 'https://schema.org', '@type': 'BoatReservation', reservationNumber: 'B-1' };
|
||||
expect(readStructuredMailDetailed(page(boat), CTX)).toEqual({ cards: [], hadBookingMarkup: true });
|
||||
});
|
||||
|
||||
it('reports none for shipments only, foreign contexts and plain mail', () => {
|
||||
expect(readStructuredMailDetailed(page(ORDER), CTX)).toMatchObject({ hadBookingMarkup: false, cards: [{ kind: 'shipment' }] });
|
||||
expect(readStructuredMailDetailed(page({ ...flight('2026-12-03T07:40:00+01:00'), '@context': 'http://example.org' }), CTX).hadBookingMarkup).toBe(false);
|
||||
expect(readStructuredMailDetailed('<html><body>Your Reservation is attached.</body></html>', CTX)).toEqual({ cards: [], hadBookingMarkup: false });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,556 @@
|
||||
// Mirrors webmail lib/structured-mail/__tests__/calendar.test.ts at 87d9f8923; keep the two in step.
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { CalendarEvent } from '../../../api/types';
|
||||
import {
|
||||
bookingEndsAt,
|
||||
bookingEventPatch,
|
||||
bookingEventSlots,
|
||||
bookingEventUid,
|
||||
bookingToEvents,
|
||||
eventNeedsUpdate,
|
||||
fnv1a64,
|
||||
organizationalDomain,
|
||||
} from '../calendar';
|
||||
import { normalizeCalendarEventLike, sanitizeOutgoingCalendarEventData } from '../event-normalization';
|
||||
import { loadAirportZones, readStructuredMail, resolveCards, type Booking, type StructuredCard, type TransportBooking } from '..';
|
||||
|
||||
const ctx = 'http://schema.org';
|
||||
|
||||
/** Cards as the reader gets them: parsed, validated, merged and zone-resolved. */
|
||||
async function cards(items: unknown[], mailDate = '2026-10-30T10:00:00Z'): Promise<StructuredCard[]> {
|
||||
const html = `<html><head>${items.map((item) => `<script type="application/ld+json">${JSON.stringify(item)}</script>`).join('')}</head><body></body></html>`;
|
||||
return resolveCards(readStructuredMail(html, { mailDate: new Date(mailDate) }), await loadAirportZones());
|
||||
}
|
||||
|
||||
async function booking(item: unknown, mailDate?: string): Promise<Booking> {
|
||||
const found = await cards([item], mailDate);
|
||||
expect(found).toHaveLength(1);
|
||||
return found[0] as Booking;
|
||||
}
|
||||
|
||||
interface FlightSpec {
|
||||
ref?: string;
|
||||
airline?: { name?: string; iataCode?: string };
|
||||
number?: string;
|
||||
dep: [name: string, code: string, time: string];
|
||||
arr: [name: string, code: string, time: string];
|
||||
status?: string;
|
||||
passenger?: string;
|
||||
seat?: string;
|
||||
}
|
||||
|
||||
function flight(spec: FlightSpec) {
|
||||
return {
|
||||
'@context': ctx, '@type': 'FlightReservation',
|
||||
...(spec.ref !== undefined ? { reservationNumber: spec.ref } : { reservationNumber: 'KT4M9Z' }),
|
||||
underName: { '@type': 'Person', name: spec.passenger ?? 'Erika Mustermann' },
|
||||
...(spec.seat ? { airplaneSeat: spec.seat } : {}),
|
||||
...(spec.status ? { reservationStatus: `http://schema.org/${spec.status}` } : {}),
|
||||
checkinUrl: 'https://checkin.example-air.test/KT4M9Z',
|
||||
reservationFor: {
|
||||
'@type': 'Flight', flightNumber: spec.number ?? '110', airline: spec.airline ?? { name: 'United', iataCode: 'UA' },
|
||||
departureAirport: { '@type': 'Airport', name: spec.dep[0], iataCode: spec.dep[1] }, departureTime: spec.dep[2],
|
||||
arrivalAirport: { '@type': 'Airport', name: spec.arr[0], iataCode: spec.arr[1] }, arrivalTime: spec.arr[2],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const SENDER = { senderDomain: 'united.com' };
|
||||
const SFO_JFK = flight({
|
||||
dep: ['San Francisco International Airport', 'SFO', '2026-12-04T20:15:00-08:00'],
|
||||
arr: ['John F. Kennedy International Airport', 'JFK', '2026-12-05T06:30:00-05:00'],
|
||||
seat: '9A',
|
||||
});
|
||||
|
||||
describe('organizationalDomain', () => {
|
||||
it('keeps the last two labels, three under a listed two-label public suffix', () => {
|
||||
expect(organizationalDomain('booking.flixbus.com')).toBe('flixbus.com');
|
||||
expect(organizationalDomain('flixbus.com')).toBe('flixbus.com');
|
||||
expect(organizationalDomain('mail.Lufthansa.COM.')).toBe('lufthansa.com');
|
||||
expect(organizationalDomain('news.easyjet.co.uk')).toBe('easyjet.co.uk');
|
||||
expect(organizationalDomain('shop.example.co.uk')).toBe('example.co.uk');
|
||||
expect(organizationalDomain('mail.qantas.com.au')).toBe('qantas.com.au');
|
||||
expect(organizationalDomain('info.bahn.de')).toBe('bahn.de');
|
||||
});
|
||||
|
||||
it('treats short brand names under country domains as brands, not suffixes', () => {
|
||||
expect(organizationalDomain('mail.klm.nl')).toBe('klm.nl');
|
||||
expect(organizationalDomain('info.klm.nl')).toBe('klm.nl');
|
||||
expect(organizationalDomain('news.dhl.de')).toBe('dhl.de');
|
||||
// Not listed, so both collapse; the reference still has to match (see the list's comment).
|
||||
expect(organizationalDomain('x.gouv.fr')).toBe(organizationalDomain('y.gouv.fr'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('fnv1a64', () => {
|
||||
it('matches the published FNV-1a 64-bit test vectors', () => {
|
||||
expect(fnv1a64('')).toBe('cbf29ce484222325');
|
||||
expect(fnv1a64('a')).toBe('af63dc4c8601ec8c');
|
||||
expect(fnv1a64('foobar')).toBe('85944171f73967e8');
|
||||
});
|
||||
});
|
||||
|
||||
describe('bookingToEvents: flights', () => {
|
||||
it('maps SFO 20:15 −08:00 → JFK 06:30 −05:00 to a Los Angeles start and an exact duration', async () => {
|
||||
const card = await booking(SFO_JFK);
|
||||
const [event] = bookingToEvents(card, SENDER);
|
||||
expect(event).toMatchObject({
|
||||
'@type': 'Event',
|
||||
title: 'UA 110 SFO → JFK',
|
||||
start: '2026-12-04T20:15:00',
|
||||
timeZone: 'America/Los_Angeles',
|
||||
duration: 'PT7H15M',
|
||||
showWithoutTime: false,
|
||||
locations: {
|
||||
dep: { '@type': 'Location', name: 'San Francisco International Airport (SFO)', timeZone: 'America/Los_Angeles', relativeTo: 'start' },
|
||||
arr: { '@type': 'Location', name: 'John F. Kennedy International Airport (JFK)', timeZone: 'America/New_York', relativeTo: 'end' },
|
||||
},
|
||||
});
|
||||
expect(event.uid).toMatch(/^bulwark-sm-flight-[0-9a-f]{16}@structured\.bulwark$/);
|
||||
expect(event.description).toBe([
|
||||
'Booking reference: KT4M9Z',
|
||||
'Passengers: Erika Mustermann (9A)',
|
||||
'Check in: https://checkin.example-air.test/KT4M9Z',
|
||||
'Added from a mail from united.com',
|
||||
].join('\n'));
|
||||
expect(event).not.toHaveProperty('participants');
|
||||
expect(event).not.toHaveProperty('calendarIds');
|
||||
});
|
||||
|
||||
it.each([
|
||||
// Westbound over the date line: the local arrival date is a day later.
|
||||
['HNL → NRT', ['Honolulu', 'HNL', '2026-12-04T11:00:00-10:00'], ['Tokyo Narita', 'NRT', '2026-12-05T15:30:00+09:00'], '2026-12-04T11:00:00', 'Pacific/Honolulu', 'Asia/Tokyo', 'PT9H30M'],
|
||||
['LAX → SYD', ['Los Angeles', 'LAX', '2026-12-04T22:30:00-08:00'], ['Sydney', 'SYD', '2026-12-06T08:45:00+11:00'], '2026-12-04T22:30:00', 'America/Los_Angeles', 'Australia/Sydney', 'PT15H15M'],
|
||||
// Eastbound: the local arrival time is earlier than the departure.
|
||||
['NRT → HNL', ['Tokyo Narita', 'NRT', '2026-12-04T21:00:00+09:00'], ['Honolulu', 'HNL', '2026-12-04T09:15:00-10:00'], '2026-12-04T21:00:00', 'Asia/Tokyo', 'Pacific/Honolulu', 'PT7H15M'],
|
||||
['SYD → LAX', ['Sydney', 'SYD', '2026-12-04T09:50:00+11:00'], ['Los Angeles', 'LAX', '2026-12-04T06:20:00-08:00'], '2026-12-04T09:50:00', 'Australia/Sydney', 'America/Los_Angeles', 'PT15H30M'],
|
||||
] as const)('crosses the date line %s', async (_name, dep, arr, start, depZone, arrZone, duration) => {
|
||||
const card = await booking(flight({ dep: [...dep], arr: [...arr] }));
|
||||
const [event] = bookingToEvents(card, SENDER);
|
||||
expect(event).toMatchObject({
|
||||
start, timeZone: depZone, duration,
|
||||
locations: { dep: { timeZone: depZone, relativeTo: 'start' }, arr: { timeZone: arrZone, relativeTo: 'end' } },
|
||||
});
|
||||
});
|
||||
|
||||
it('counts the real hours of a flight that lands after a DST change', async () => {
|
||||
// New York moves to EDT at 02:00 on 14 March 2027; 23:00 GMT → 03:30 EDT is 8 h 30.
|
||||
const card = await booking(flight({
|
||||
airline: { name: 'British Airways', iataCode: 'BA' }, number: '117',
|
||||
dep: ['London Heathrow', 'LHR', '2027-03-13T23:00:00+00:00'],
|
||||
arr: ['New York JFK', 'JFK', '2027-03-14T03:30:00-04:00'],
|
||||
}), '2027-03-01T10:00:00Z');
|
||||
const [event] = bookingToEvents(card, { senderDomain: 'ba.com' });
|
||||
expect(event).toMatchObject({ start: '2027-03-13T23:00:00', timeZone: 'Europe/London', duration: 'PT8H30M' });
|
||||
});
|
||||
|
||||
it('adds no event for a cancelled leg but still names its uid for removal', async () => {
|
||||
const outbound = flight({ dep: ['Frankfurt', 'FRA', '2026-12-01T10:15:00+01:00'], arr: ['New York JFK', 'JFK', '2026-12-01T13:05:00-05:00'], airline: { name: 'Lufthansa', iataCode: 'LH' }, number: '400', ref: 'QX7K2M' });
|
||||
const back = { ...flight({ dep: ['New York JFK', 'JFK', '2026-12-08T17:30:00-05:00'], arr: ['Frankfurt', 'FRA', '2026-12-09T07:20:00+01:00'], airline: { name: 'Lufthansa', iataCode: 'LH' }, number: '401', ref: 'QX7K2M', status: 'ReservationCancelled' }) };
|
||||
const [card] = (await cards([outbound, back])) as TransportBooking[];
|
||||
expect(card.legs).toHaveLength(2);
|
||||
const events = bookingToEvents(card, { senderDomain: 'lufthansa.com' });
|
||||
expect(events.map((e) => e.title)).toEqual(['LH 400 FRA → JFK']);
|
||||
const slots = bookingEventSlots(card, { senderDomain: 'lufthansa.com' });
|
||||
expect(slots.map((s) => s.cancelled)).toEqual([false, true]);
|
||||
expect(slots[1].uid).toBe(bookingEventUid(card, 'lufthansa.com', card.legs[1]));
|
||||
});
|
||||
|
||||
it('gives a pass leg without arrival a zero-length event', async () => {
|
||||
const pass: TransportBooking = {
|
||||
key: 'flight|ref:pq1', kind: 'flight', reference: 'PQ1RST', passengers: ['Erika Mustermann'], links: {}, source: 'pkpass',
|
||||
legs: [{
|
||||
carrierCode: 'LH', number: '400', seats: [{ passenger: 'Erika Mustermann', seat: '31C' }],
|
||||
departure: { place: { code: 'FRA', name: 'Frankfurt' }, at: { date: '2026-12-01', time: '10:15:00', offsetMinutes: 60, utc: '2026-12-01T09:15:00Z' } },
|
||||
}],
|
||||
};
|
||||
const [resolved] = resolveCards([pass], await loadAirportZones()) as TransportBooking[];
|
||||
const [event] = bookingToEvents(resolved, { senderDomain: 'lufthansa.com' });
|
||||
expect(event).toMatchObject({ start: '2026-12-01T10:15:00', timeZone: 'Europe/Berlin', duration: 'PT0S', title: 'LH 400 FRA' });
|
||||
expect(event.locations).not.toHaveProperty('arr');
|
||||
});
|
||||
});
|
||||
|
||||
describe('bookingToEvents: fixed offsets', () => {
|
||||
const bus = (dep: string, arr: string) => ({
|
||||
'@context': ctx, '@type': 'BusReservation', reservationNumber: 'BX20471', underName: 'Erika Mustermann',
|
||||
reservationFor: {
|
||||
'@type': 'BusTrip', busNumber: 'N12', busCompany: { name: 'Coastline Coaches' },
|
||||
departureBusStop: { '@type': 'BusStation', name: 'Harbour Terminal' }, departureTime: dep,
|
||||
arrivalBusStop: { '@type': 'BusStation', name: 'Airport Stop 3' }, arrivalTime: arr,
|
||||
},
|
||||
});
|
||||
|
||||
it('uses Etc/GMT+8 for a −08:00 time without a known zone, keeping the wall clock', async () => {
|
||||
const card = await booking(bus('2026-12-04T20:15:00-08:00', '2026-12-04T22:45:00-08:00'));
|
||||
const [event] = bookingToEvents(card, { senderDomain: 'coastline.test' });
|
||||
expect(event).toMatchObject({
|
||||
title: 'N12 Harbour Terminal → Airport Stop 3',
|
||||
start: '2026-12-04T20:15:00', timeZone: 'Etc/GMT+8', duration: 'PT2H30M',
|
||||
locations: { dep: { timeZone: 'Etc/GMT+8', relativeTo: 'start' }, arr: { timeZone: 'Etc/GMT+8', relativeTo: 'end' } },
|
||||
});
|
||||
});
|
||||
|
||||
it('falls back to the UTC instant in Etc/UTC for a half-hour offset', async () => {
|
||||
const card = await booking(bus('2026-12-04T10:00:00+05:30', '2026-12-04T13:15:00+05:30'));
|
||||
const [event] = bookingToEvents(card, { senderDomain: 'coastline.test' });
|
||||
expect(event).toMatchObject({ start: '2026-12-04T04:30:00', timeZone: 'Etc/UTC', duration: 'PT3H15M' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('bookingToEvents: trains, stays, cars, events, tables', () => {
|
||||
it('names stations with their addresses and counts the extra hour of a DST change', async () => {
|
||||
// Berlin falls back from CEST to CET at 03:00 on 25 October 2026: 22:00 → 08:00 is 11 hours.
|
||||
const card = await booking({
|
||||
'@context': ctx, '@type': 'TrainReservation', reservationNumber: 'ZUG4711', underName: 'Erika Mustermann',
|
||||
reservationFor: {
|
||||
'@type': 'TrainTrip', trainNumber: 'NJ 40421', provider: { name: 'Nachtzug' },
|
||||
departureStation: { '@type': 'TrainStation', name: 'München Hbf', address: { streetAddress: 'Bayerstraße 10A', postalCode: '80335', addressLocality: 'München', addressCountry: 'DE' } },
|
||||
departureTime: '2026-10-24T22:00:00+02:00',
|
||||
arrivalStation: { '@type': 'TrainStation', name: 'Hamburg Hbf', address: { streetAddress: 'Hachmannplatz 16', postalCode: '20099', addressLocality: 'Hamburg', addressCountry: 'DE' } },
|
||||
arrivalTime: '2026-10-25T08:00:00+01:00',
|
||||
},
|
||||
}, '2026-10-20T10:00:00Z');
|
||||
const [event] = bookingToEvents(card, { senderDomain: 'nachtzug.test' });
|
||||
expect(event).toMatchObject({
|
||||
title: 'NJ 40421 München Hbf → Hamburg Hbf',
|
||||
start: '2026-10-24T22:00:00', timeZone: 'Europe/Berlin', duration: 'PT11H',
|
||||
locations: {
|
||||
dep: { name: 'München Hbf, Bayerstraße 10A, 80335 München', timeZone: 'Europe/Berlin', relativeTo: 'start' },
|
||||
arr: { name: 'Hamburg Hbf, Hachmannplatz 16, 20099 Hamburg', timeZone: 'Europe/Berlin', relativeTo: 'end' },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
const hotel = (checkin: string, checkout: string) => ({
|
||||
'@context': ctx, '@type': 'LodgingReservation', reservationNumber: 'HB-88213', underName: 'Erika Mustermann',
|
||||
reservationFor: {
|
||||
'@type': 'Hotel', name: 'Hotel Adlon Kempinski', telephone: '+49 30 22610',
|
||||
address: { streetAddress: 'Unter den Linden 77', postalCode: '10117', addressLocality: 'Berlin', addressCountry: 'DE' },
|
||||
},
|
||||
checkinDate: checkin, checkoutDate: checkout,
|
||||
});
|
||||
|
||||
it('makes a stay with dates only an all-day event that includes the check-out day', async () => {
|
||||
const card = await booking(hotel('2026-12-04', '2026-12-06'));
|
||||
const [event] = bookingToEvents(card, { senderDomain: 'kempinski.com' });
|
||||
expect(event).toMatchObject({
|
||||
title: 'Hotel Adlon Kempinski', start: '2026-12-04T00:00:00', showWithoutTime: true, timeZone: null,
|
||||
duration: 'P3D', freeBusyStatus: 'free',
|
||||
locations: { hotel: { name: 'Hotel Adlon Kempinski, Unter den Linden 77, 10117 Berlin' } },
|
||||
});
|
||||
expect(event.locations?.hotel).not.toHaveProperty('timeZone');
|
||||
expect(event.description).toContain('Phone: +49 30 22610');
|
||||
});
|
||||
|
||||
it('makes a stay with times a timed, free event in the hotel zone', async () => {
|
||||
const card = await booking(hotel('2026-12-04T15:00:00+01:00', '2026-12-06T11:00:00+01:00'));
|
||||
const [event] = bookingToEvents(card, { senderDomain: 'kempinski.com' });
|
||||
expect(event).toMatchObject({
|
||||
start: '2026-12-04T15:00:00', timeZone: 'Europe/Berlin', duration: 'PT44H', showWithoutTime: false, freeBusyStatus: 'free',
|
||||
locations: { hotel: { timeZone: 'Europe/Berlin' } },
|
||||
});
|
||||
});
|
||||
|
||||
it('maps a rental car from pickup to drop-off, marked free', async () => {
|
||||
const card = await booking({
|
||||
'@context': ctx, '@type': 'RentalCarReservation', reservationNumber: 'RC-55120', underName: 'Erika Mustermann',
|
||||
reservationFor: { '@type': 'RentalCar', name: 'Golf', brand: { name: 'VW' }, rentalCompany: { name: 'Sixt' } },
|
||||
pickupLocation: { '@type': 'Place', name: 'Sixt Berlin Hauptbahnhof', address: { addressLocality: 'Berlin', addressCountry: 'DE' } },
|
||||
pickupTime: '2026-12-04T09:00:00+01:00',
|
||||
dropoffLocation: { '@type': 'Place', name: 'Sixt Berlin Hauptbahnhof', address: { addressLocality: 'Berlin', addressCountry: 'DE' } },
|
||||
dropoffTime: '2026-12-07T18:00:00+01:00',
|
||||
});
|
||||
const [event] = bookingToEvents(card, { senderDomain: 'sixt.de' });
|
||||
expect(event).toMatchObject({
|
||||
title: 'Rental car: Sixt', start: '2026-12-04T09:00:00', timeZone: 'Europe/Berlin', duration: 'PT81H', freeBusyStatus: 'free',
|
||||
locations: { pickup: { relativeTo: 'start', timeZone: 'Europe/Berlin' }, dropoff: { relativeTo: 'end', timeZone: 'Europe/Berlin' } },
|
||||
});
|
||||
expect(event.description).toContain('Car: VW Golf');
|
||||
});
|
||||
|
||||
const concert = (start: string, end?: string, address: Record<string, string> = { addressLocality: 'Berlin', addressCountry: 'DE' }) => ({
|
||||
'@context': ctx, '@type': 'EventReservation', reservationNumber: 'EV-30017', underName: 'Erika Mustermann',
|
||||
reservedTicket: { '@type': 'Ticket', ticketNumber: 'T-9001' },
|
||||
reservationFor: {
|
||||
'@type': 'Event', name: 'Grafikdesign: Illustrator-Workshop', startDate: start, ...(end ? { endDate: end } : {}),
|
||||
location: { '@type': 'Place', name: 'Werkstatt Mitte', address },
|
||||
},
|
||||
});
|
||||
|
||||
it('gives an event without an end an explicit zero duration', async () => {
|
||||
const card = await booking(concert('2026-07-18T14:00:00+02:00'), '2026-06-01T10:00:00Z');
|
||||
const [event] = bookingToEvents(card, { senderDomain: 'workshops.test' });
|
||||
expect(event).toMatchObject({ start: '2026-07-18T14:00:00', timeZone: 'Europe/Berlin', duration: 'PT0S', locations: { venue: { name: 'Werkstatt Mitte, Berlin' } } });
|
||||
expect(event.description).toContain('Tickets: T-9001');
|
||||
});
|
||||
|
||||
it('runs an event to its end, to the end of the day for a date-only end, and keeps floating times floating', async () => {
|
||||
const timed = await booking(concert('2026-07-18T14:00:00+02:00', '2026-07-18T19:00:00+02:00'), '2026-06-01T10:00:00Z');
|
||||
expect(bookingToEvents(timed, { senderDomain: 'workshops.test' })[0]).toMatchObject({ duration: 'PT5H', timeZone: 'Europe/Berlin' });
|
||||
const toDayEnd = await booking(concert('2026-07-18T14:00:00+02:00', '2026-07-18'), '2026-06-01T10:00:00Z');
|
||||
expect(bookingToEvents(toDayEnd, { senderDomain: 'workshops.test' })[0]).toMatchObject({ duration: 'PT9H59M59S' });
|
||||
const floating = await booking(concert('2026-07-18T14:00:00', '2026-07-18T16:30:00', { addressLocality: 'Somewhere' }), '2026-06-01T10:00:00Z');
|
||||
expect(bookingToEvents(floating, { senderDomain: 'workshops.test' })[0]).toMatchObject({ start: '2026-07-18T14:00:00', timeZone: null, duration: 'PT2H30M' });
|
||||
});
|
||||
|
||||
it('makes a date-only event all-day over its days', async () => {
|
||||
const card = await booking(concert('2026-07-18', '2026-07-19'), '2026-06-01T10:00:00Z');
|
||||
expect(bookingToEvents(card, { senderDomain: 'workshops.test' })[0]).toMatchObject({
|
||||
start: '2026-07-18T00:00:00', showWithoutTime: true, timeZone: null, duration: 'P2D',
|
||||
});
|
||||
});
|
||||
|
||||
it('books a table as a zero-length event with the party size', async () => {
|
||||
const card = await booking({
|
||||
'@context': ctx, '@type': 'FoodEstablishmentReservation', reservationNumber: 'TR-2207', underName: 'Erika Mustermann', partySize: 2,
|
||||
reservationFor: { '@type': 'Restaurant', name: 'Restaurant Tim Raue', address: { streetAddress: 'Rudi-Dutschke-Straße 26', postalCode: '10969', addressLocality: 'Berlin', addressCountry: 'DE' } },
|
||||
startTime: '2026-12-05T19:30:00+01:00',
|
||||
});
|
||||
const [event] = bookingToEvents(card, { senderDomain: 'tim-raue.de' });
|
||||
expect(event).toMatchObject({
|
||||
title: 'Restaurant Tim Raue', start: '2026-12-05T19:30:00', timeZone: 'Europe/Berlin', duration: 'PT0S',
|
||||
locations: { restaurant: { name: 'Restaurant Tim Raue, Rudi-Dutschke-Straße 26, 10969 Berlin', timeZone: 'Europe/Berlin' } },
|
||||
});
|
||||
expect(event.description).toBe('Booking reference: TR-2207\nParty size: 2\nAdded from a mail from tim-raue.de');
|
||||
});
|
||||
|
||||
it('uses the labels it is given and names the mail subject', async () => {
|
||||
const card = await booking(SFO_JFK);
|
||||
const [event] = bookingToEvents(card, {
|
||||
senderDomain: 'mail.united.com',
|
||||
mailSubjectOrProvider: 'Your trip to New York',
|
||||
labels: { reference: (r) => `Buchungscode: ${r}` },
|
||||
});
|
||||
expect(event.description).toContain('Buchungscode: KT4M9Z');
|
||||
expect(event.description).toContain('Added from the mail "Your trip to New York" from united.com');
|
||||
});
|
||||
});
|
||||
|
||||
describe('bookingEventUid', () => {
|
||||
const lh = (time: [string, string], extra: Partial<FlightSpec> = {}) => flight({
|
||||
airline: { name: 'Lufthansa', iataCode: 'LH' }, number: '400', ref: 'QX7K2M',
|
||||
dep: ['Frankfurt', 'FRA', time[0]], arr: ['New York JFK', 'JFK', time[1]], ...extra,
|
||||
});
|
||||
|
||||
it('stays the same when the schedule changes, and the event then needs an update', async () => {
|
||||
const before = await booking(lh(['2026-12-01T10:15:00+01:00', '2026-12-01T13:05:00-05:00']));
|
||||
const after = await booking(lh(['2026-12-01T12:40:00+01:00', '2026-12-01T15:35:00-05:00']));
|
||||
const [old] = bookingToEvents(before, { senderDomain: 'lufthansa.com' });
|
||||
const [now] = bookingToEvents(after, { senderDomain: 'lufthansa.com' });
|
||||
expect(now.uid).toBe(old.uid);
|
||||
expect(eventNeedsUpdate(old as CalendarEvent, now)).toBe(true);
|
||||
});
|
||||
|
||||
it('differs across sender domains and is shared by the subdomains of one', async () => {
|
||||
const card = await booking(lh(['2026-12-01T10:15:00+01:00', '2026-12-01T13:05:00-05:00']));
|
||||
expect(bookingEventUid(card, 'lufthansa.com')).not.toBe(bookingEventUid(card, 'lufthansa-deals.com'));
|
||||
expect(bookingEventUid(card, 'booking.flixbus.com')).toBe(bookingEventUid(card, 'flixbus.com'));
|
||||
expect(bookingEventUid(card, 'noreply@mail.lufthansa.com')).toBe(bookingEventUid(card, 'lufthansa.com'));
|
||||
});
|
||||
|
||||
it('keys on the IATA code, and on the airline name only when there is no code', async () => {
|
||||
const times: [string, string] = ['2026-12-01T10:15:00+01:00', '2026-12-01T13:05:00-05:00'];
|
||||
const short = await booking(lh(times));
|
||||
const long = await booking(lh(times, { airline: { name: 'Deutsche Lufthansa AG', iataCode: 'LH' } }));
|
||||
expect(bookingEventUid(long, 'lufthansa.com')).toBe(bookingEventUid(short, 'lufthansa.com'));
|
||||
const nameOnly = await booking(lh(times, { airline: { name: 'Lufthansa' } }));
|
||||
const otherName = await booking(lh(times, { airline: { name: 'Lufthansa Group' } }));
|
||||
expect((nameOnly as TransportBooking).legs[0].carrierCode).toBeUndefined();
|
||||
expect(bookingEventUid(nameOnly, 'lufthansa.com')).not.toBe(bookingEventUid(otherName, 'lufthansa.com'));
|
||||
expect(bookingEventUid(nameOnly, 'lufthansa.com')).not.toBe(bookingEventUid(short, 'lufthansa.com'));
|
||||
});
|
||||
|
||||
it('ignores leading zeros of the flight number, so a boarding pass finds the markup event', async () => {
|
||||
const times: [string, string] = ['2026-12-01T10:15:00+01:00', '2026-12-01T13:05:00-05:00'];
|
||||
const padded = (await booking(lh(times, { number: '0072' }))) as TransportBooking;
|
||||
const plain = (await booking(lh(times, { number: '72' }))) as TransportBooking;
|
||||
expect(padded.legs[0].number).toBe('0072');
|
||||
expect(bookingEventUid(padded, 'lufthansa.com')).toBe(bookingEventUid(plain, 'lufthansa.com'));
|
||||
const other = await booking(lh(times, { number: '720' }));
|
||||
expect(bookingEventUid(other, 'lufthansa.com')).not.toBe(bookingEventUid(plain, 'lufthansa.com'));
|
||||
});
|
||||
|
||||
it('adds the departure date only when there is no reference', async () => {
|
||||
const noRef = (time: [string, string]) => ({ ...lh(time), reservationNumber: undefined });
|
||||
const day1 = await booking(noRef(['2026-12-01T10:15:00+01:00', '2026-12-01T13:05:00-05:00']));
|
||||
const day1Later = await booking(noRef(['2026-12-01T14:15:00+01:00', '2026-12-01T17:05:00-05:00']));
|
||||
const day2 = await booking(noRef(['2026-12-02T10:15:00+01:00', '2026-12-02T13:05:00-05:00']));
|
||||
expect(bookingEventUid(day1Later, 'lufthansa.com')).toBe(bookingEventUid(day1, 'lufthansa.com'));
|
||||
expect(bookingEventUid(day2, 'lufthansa.com')).not.toBe(bookingEventUid(day1, 'lufthansa.com'));
|
||||
});
|
||||
|
||||
it('gives a leg the same uid whatever other legs the mail lists, and offers no event for legs that would share one', async () => {
|
||||
const day1 = lh(['2026-12-01T10:15:00+01:00', '2026-12-01T13:05:00-05:00']);
|
||||
const day8 = lh(['2026-12-08T10:15:00+01:00', '2026-12-08T13:05:00-05:00']);
|
||||
const back = flight({ airline: { name: 'Lufthansa', iataCode: 'LH' }, number: '401', ref: 'QX7K2M', dep: ['New York JFK', 'JFK', '2026-12-08T17:30:00-05:00'], arr: ['Frankfurt', 'FRA', '2026-12-09T07:20:00+01:00'] });
|
||||
const [both] = (await cards([day1, day8, back])) as TransportBooking[];
|
||||
const [one] = (await cards([day1, back])) as TransportBooking[];
|
||||
expect(both.legs).toHaveLength(3);
|
||||
expect(bookingEventUid(both, 'lufthansa.com', both.legs[0])).toBe(bookingEventUid(one, 'lufthansa.com', one.legs[0]));
|
||||
expect(bookingEventUid(both, 'lufthansa.com', both.legs[2])).toBe(bookingEventUid(one, 'lufthansa.com', one.legs[1]));
|
||||
// LH 400 FRA twice under one reference: one uid can't hold both, so neither is offered (nor removed); LH 401 still is.
|
||||
const slots = bookingEventSlots(both, { senderDomain: 'lufthansa.com' });
|
||||
expect(slots.map((slot) => [slot.event !== null, slot.cancelled])).toEqual([[false, false], [false, false], [true, false]]);
|
||||
expect(bookingToEvents(both, { senderDomain: 'lufthansa.com' }).map((e) => e.title)).toEqual(['LH 401 JFK → FRA']);
|
||||
});
|
||||
|
||||
it('ignores spaces in train numbers', async () => {
|
||||
const train = (number: string) => ({
|
||||
'@context': ctx, '@type': 'TrainReservation', reservationNumber: 'ZUG577',
|
||||
reservationFor: {
|
||||
'@type': 'TrainTrip', trainNumber: number, provider: { name: 'DB' },
|
||||
departureStation: { name: 'Hannover Hbf', address: { addressCountry: 'DE' } }, departureTime: '2026-12-01T10:00:00+01:00',
|
||||
arrivalStation: { name: 'Berlin Hbf', address: { addressCountry: 'DE' } }, arrivalTime: '2026-12-01T11:40:00+01:00',
|
||||
},
|
||||
});
|
||||
const spaced = await booking(train('ICE 577'));
|
||||
expect(bookingEventUid(spaced, 'bahn.de')).toBe(bookingEventUid(await booking(train('ICE577')), 'bahn.de'));
|
||||
expect(bookingEventUid(spaced, 'bahn.de')).not.toBe(bookingEventUid(await booking(train('ICE 578')), 'bahn.de'));
|
||||
});
|
||||
|
||||
it('keeps the uid of a rescheduled car, event or table that has a reference, and keys on the date without one', async () => {
|
||||
const table = (ref: string | undefined, start: string) => ({
|
||||
'@context': ctx, '@type': 'FoodEstablishmentReservation', ...(ref ? { reservationNumber: ref } : {}), partySize: 2,
|
||||
reservationFor: { '@type': 'Restaurant', name: 'Restaurant Tim Raue', address: { addressCountry: 'DE' } }, startTime: start,
|
||||
});
|
||||
const car = (ref: string | undefined, pickup: string, dropoff: string) => ({
|
||||
'@context': ctx, '@type': 'RentalCarReservation', ...(ref ? { reservationNumber: ref } : {}),
|
||||
reservationFor: { '@type': 'RentalCar', name: 'Golf', rentalCompany: { name: 'Sixt' } },
|
||||
pickupLocation: { name: 'Sixt Berlin', address: { addressCountry: 'DE' } }, pickupTime: pickup,
|
||||
dropoffLocation: { name: 'Sixt Berlin', address: { addressCountry: 'DE' } }, dropoffTime: dropoff,
|
||||
});
|
||||
const show = (ref: string | undefined, name: string, start: string) => ({
|
||||
'@context': ctx, '@type': 'EventReservation', ...(ref ? { reservationNumber: ref } : {}),
|
||||
reservationFor: { '@type': 'Event', name, startDate: start, location: { name: 'Werkstatt', address: { addressCountry: 'DE' } } },
|
||||
});
|
||||
const uid = async (item: unknown) => bookingEventUid(await booking(item), 'example-tickets.de');
|
||||
expect(await uid(table('TR-1', '2026-12-05T19:30:00+01:00'))).toBe(await uid(table('TR-1', '2026-12-06T20:00:00+01:00')));
|
||||
expect(await uid(table(undefined, '2026-12-05T19:30:00+01:00'))).not.toBe(await uid(table(undefined, '2026-12-06T19:30:00+01:00')));
|
||||
expect(await uid(car('RC-1', '2026-12-04T09:00:00+01:00', '2026-12-07T18:00:00+01:00'))).toBe(await uid(car('RC-1', '2026-12-05T09:00:00+01:00', '2026-12-08T18:00:00+01:00')));
|
||||
expect(await uid(car(undefined, '2026-12-04T09:00:00+01:00', '2026-12-07T18:00:00+01:00'))).not.toBe(await uid(car(undefined, '2026-12-05T09:00:00+01:00', '2026-12-08T18:00:00+01:00')));
|
||||
expect(await uid(show('EV-1', 'Workshop', '2026-12-05T14:00:00+01:00'))).toBe(await uid(show('EV-1', 'Workshop', '2026-12-12T14:00:00+01:00')));
|
||||
// One order, two events: the name keeps them apart.
|
||||
expect(await uid(show('EV-1', 'Workshop', '2026-12-05T14:00:00+01:00'))).not.toBe(await uid(show('EV-1', 'Concert', '2026-12-05T14:00:00+01:00')));
|
||||
expect(await uid(show(undefined, 'Workshop', '2026-12-05T14:00:00+01:00'))).not.toBe(await uid(show(undefined, 'Workshop', '2026-12-12T14:00:00+01:00')));
|
||||
});
|
||||
|
||||
it('keys stays on reference and hotel, adding the check-in date only without reference', async () => {
|
||||
const stay = (ref: string | undefined, checkin: string) => ({
|
||||
'@context': ctx, '@type': 'LodgingReservation', ...(ref ? { reservationNumber: ref } : {}),
|
||||
reservationFor: { '@type': 'Hotel', name: 'Hotel Adlon Kempinski' }, checkinDate: checkin, checkoutDate: '2026-12-09',
|
||||
});
|
||||
const a = await booking(stay('HB-1', '2026-12-04'));
|
||||
const moved = await booking(stay('HB-1', '2026-12-05'));
|
||||
expect(bookingEventUid(moved, 'kempinski.com')).toBe(bookingEventUid(a, 'kempinski.com'));
|
||||
const b = await booking(stay(undefined, '2026-12-04'));
|
||||
const c = await booking(stay(undefined, '2026-12-05'));
|
||||
expect(bookingEventUid(b, 'kempinski.com')).not.toBe(bookingEventUid(c, 'kempinski.com'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('eventNeedsUpdate', () => {
|
||||
/** What CalendarEvent/get returns for an event created from `wanted`. */
|
||||
function serverShaped(wanted: Partial<CalendarEvent>): CalendarEvent {
|
||||
const locations = Object.fromEntries(Object.entries(wanted.locations ?? {}).map(([id, loc]) => [id, {
|
||||
...loc, description: null, locationTypes: null, coordinates: null, links: null,
|
||||
timeZone: loc.timeZone ?? null, relativeTo: loc.relativeTo ?? null, iCalendar: { name: 'vlocation' },
|
||||
}]));
|
||||
return {
|
||||
id: 'b', calendarIds: { a: true }, isDraft: false, isOrigin: true, utcStart: '2026-12-05T04:15:00Z', utcEnd: '2026-12-05T11:30:00Z',
|
||||
sequence: 0, created: '2026-10-30T10:00:00Z', updated: '2026-10-30T10:00:00Z', status: 'confirmed', freeBusyStatus: 'busy',
|
||||
privacy: 'public', color: null, keywords: null, categories: null, locale: null, replyTo: null, organizerCalendarAddress: null,
|
||||
participants: null, recurrenceRules: null, recurrenceOverrides: null, alerts: null, useDefaultAlerts: false, virtualLocations: null, links: null,
|
||||
...wanted,
|
||||
locations,
|
||||
} as unknown as CalendarEvent;
|
||||
}
|
||||
|
||||
it('sees no change in the event the server gives back for the same booking', async () => {
|
||||
const [wanted] = bookingToEvents(await booking(SFO_JFK), SENDER);
|
||||
expect(eventNeedsUpdate(serverShaped(wanted), wanted)).toBe(false);
|
||||
});
|
||||
|
||||
it('sees a changed start, duration, zone or location', async () => {
|
||||
const [wanted] = bookingToEvents(await booking(SFO_JFK), SENDER);
|
||||
const stored = serverShaped(wanted);
|
||||
expect(eventNeedsUpdate({ ...stored, start: '2026-12-04T21:15:00' }, wanted)).toBe(true);
|
||||
expect(eventNeedsUpdate({ ...stored, duration: 'PT7H' }, wanted)).toBe(true);
|
||||
expect(eventNeedsUpdate({ ...stored, timeZone: 'America/Denver' }, wanted)).toBe(true);
|
||||
expect(eventNeedsUpdate({ ...stored, locations: { ...stored.locations, arr: { ...stored.locations!.arr, timeZone: 'America/Chicago' } } }, wanted)).toBe(true);
|
||||
expect(eventNeedsUpdate({ ...stored, locations: { dep: stored.locations!.dep } }, wanted)).toBe(true);
|
||||
});
|
||||
|
||||
it('does not compare titles or descriptions: the user may rename the event or write notes', async () => {
|
||||
const [wanted] = bookingToEvents(await booking(SFO_JFK), SENDER);
|
||||
expect(eventNeedsUpdate({ ...serverShaped(wanted), title: 'Flight to New York', description: 'my notes' }, wanted)).toBe(false);
|
||||
});
|
||||
|
||||
it('ignores the zone of two all-day events', async () => {
|
||||
const stay = await booking({
|
||||
'@context': ctx, '@type': 'LodgingReservation', reservationNumber: 'HB-3', reservationFor: { '@type': 'Hotel', name: 'Hotel' },
|
||||
checkinDate: '2026-12-04', checkoutDate: '2026-12-06',
|
||||
});
|
||||
const [wanted] = bookingToEvents(stay, { senderDomain: 'hotel.test' });
|
||||
expect(eventNeedsUpdate({ ...serverShaped(wanted), timeZone: 'Europe/Berlin' }, wanted)).toBe(false);
|
||||
expect(eventNeedsUpdate({ ...serverShaped(wanted), duration: 'P2D' }, wanted)).toBe(true);
|
||||
});
|
||||
|
||||
it('sees no change in a midnight-to-midnight span the store saved as all-day', async () => {
|
||||
const card = await booking({
|
||||
'@context': ctx, '@type': 'RentalCarReservation', reservationNumber: 'RC-0000',
|
||||
reservationFor: { '@type': 'RentalCar', name: 'Golf', rentalCompany: { name: 'Sixt' } },
|
||||
pickupLocation: { name: 'Sixt Berlin', address: { addressCountry: 'DE' } }, pickupTime: '2026-12-04T00:00:00+01:00',
|
||||
dropoffLocation: { name: 'Sixt Berlin', address: { addressCountry: 'DE' } }, dropoffTime: '2026-12-07T00:00:00+01:00',
|
||||
});
|
||||
const [wanted] = bookingToEvents(card, { senderDomain: 'sixt.de' });
|
||||
expect(wanted).toMatchObject({ start: '2026-12-04T00:00:00', duration: 'PT72H', timeZone: 'Europe/Berlin' });
|
||||
// What the store's createEvent sends, and what its lookup turns the server's answer into.
|
||||
const sent = sanitizeOutgoingCalendarEventData({ ...wanted });
|
||||
expect(sent).toMatchObject({ showWithoutTime: true, duration: 'P3D', timeZone: null });
|
||||
const readBack = normalizeCalendarEventLike(serverShaped(sent));
|
||||
expect(eventNeedsUpdate(readBack, wanted)).toBe(false);
|
||||
expect(bookingEventPatch(readBack, wanted, 'sixt.de')).toEqual({});
|
||||
});
|
||||
|
||||
it('patches only what moved, and keeps the user\'s title, notes and own locations', async () => {
|
||||
const before = await booking(SFO_JFK);
|
||||
const after = await booking({ ...SFO_JFK, reservationFor: { ...SFO_JFK.reservationFor, departureTime: '2026-12-04T21:00:00-08:00', arrivalTime: '2026-12-05T07:15:00-05:00' } });
|
||||
const [old] = bookingToEvents(before, SENDER);
|
||||
const [now] = bookingToEvents(after, SENDER);
|
||||
const mine = { '@type': 'Location', name: 'Gate B12' } as CalendarEvent['locations'] extends Record<string, infer L> | null ? L : never;
|
||||
const edited = { ...serverShaped(old), title: 'Flight to New York', description: `${old.description}\nmy notes`, locations: { ...serverShaped(old).locations, mine } };
|
||||
const patch = bookingEventPatch(edited, now, 'united.com');
|
||||
expect(patch).toEqual({ start: '2026-12-04T21:00:00', duration: 'PT7H15M', timeZone: 'America/Los_Angeles', showWithoutTime: false });
|
||||
expect(patch).not.toHaveProperty('title');
|
||||
expect(patch).not.toHaveProperty('description');
|
||||
expect(patch).not.toHaveProperty('locations');
|
||||
|
||||
const diverted = await booking({ ...SFO_JFK, reservationFor: { ...SFO_JFK.reservationFor, arrivalAirport: { '@type': 'Airport', name: 'Newark Liberty International Airport', iataCode: 'EWR' } } });
|
||||
const [toEwr] = bookingToEvents(diverted, SENDER);
|
||||
const locationPatch = bookingEventPatch(edited, toEwr, 'united.com');
|
||||
expect(locationPatch.locations).toMatchObject({ mine: { name: 'Gate B12' }, arr: { name: 'Newark Liberty International Airport (EWR)' } });
|
||||
expect(locationPatch).not.toHaveProperty('title');
|
||||
expect(locationPatch).not.toHaveProperty('start');
|
||||
});
|
||||
|
||||
it('updates title and description while the description is still the generated one', async () => {
|
||||
const [old] = bookingToEvents(await booking(SFO_JFK), SENDER);
|
||||
const diverted = await booking({ ...SFO_JFK, reservationFor: { ...SFO_JFK.reservationFor, arrivalAirport: { '@type': 'Airport', name: 'Newark Liberty International Airport', iataCode: 'EWR' } } });
|
||||
const [toEwr] = bookingToEvents(diverted, { ...SENDER, mailSubjectOrProvider: 'Change to your flight' });
|
||||
const patch = bookingEventPatch(serverShaped(old), toEwr, 'united.com');
|
||||
expect(patch).toMatchObject({ title: 'UA 110 SFO → EWR', description: toEwr.description });
|
||||
expect(bookingEventPatch(serverShaped(old), toEwr)).not.toHaveProperty('title');
|
||||
});
|
||||
});
|
||||
|
||||
describe('bookingEndsAt', () => {
|
||||
it('ends a trip at its last arrival and a stay at the end of the check-out day', async () => {
|
||||
expect(bookingEndsAt(await booking(SFO_JFK))).toBe(Date.parse('2026-12-05T11:30:00Z'));
|
||||
const stay = await booking({
|
||||
'@context': ctx, '@type': 'LodgingReservation', reservationNumber: 'HB-2', reservationFor: { '@type': 'Hotel', name: 'Hotel' },
|
||||
checkinDate: '2026-12-04', checkoutDate: '2026-12-06',
|
||||
});
|
||||
expect(bookingEndsAt(stay)).toBe(new Date(2026, 11, 7).getTime());
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
// extract.ts on the app's parser (html-parser.ts), which stands in for the
|
||||
// webmail's DOMParser: what the ported extract tests don't cover.
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { extractStructuredData } from '../extract';
|
||||
|
||||
describe('extractStructuredData on the app parser', () => {
|
||||
it('reads microdata with entities and nested items from the default parser', () => {
|
||||
const html = `<div itemscope itemtype="http://schema.org/ParcelDelivery">
|
||||
<span itemprop="carrier">Deutsche Post & DHL</span>
|
||||
<div itemprop="partOfOrder" itemscope itemtype="http://schema.org/Order"><meta itemprop="orderNumber" content="A-1"></div>
|
||||
</div>`;
|
||||
expect(extractStructuredData(html)).toEqual([{
|
||||
source: 'microdata',
|
||||
item: {
|
||||
'@context': 'https://schema.org', '@type': 'ParcelDelivery', carrier: 'Deutsche Post & DHL',
|
||||
partOfOrder: { '@type': 'Order', orderNumber: 'A-1' },
|
||||
},
|
||||
}]);
|
||||
});
|
||||
|
||||
it('skips JSON-LD after the Outlook forward separator', () => {
|
||||
const ld = '<script type="application/ld+json">{"@context":"http://schema.org","@type":"Order","orderNumber":"1"}</script>';
|
||||
expect(extractStructuredData(`<div>${ld}</div>`)).toHaveLength(1);
|
||||
expect(extractStructuredData(`<p>FYI</p><div id="divRplyFwdMsg">From: x</div><div>${ld}</div>`)).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,103 @@
|
||||
// Mirrors webmail lib/structured-mail/__tests__/extract.test.ts at 76b0c2fba; keep the two in step.
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { extractStructuredData, mayContainStructuredData } from '../extract';
|
||||
|
||||
const page = (head: string, body = '') => `<!DOCTYPE html><html><head>${head}</head><body>${body}</body></html>`;
|
||||
const ld = (value: unknown, type = 'application/ld+json') => `<script type="${type}">${JSON.stringify(value)}</script>`;
|
||||
const order = { '@context': 'http://schema.org', '@type': 'Order', orderNumber: '42' };
|
||||
|
||||
describe('extractStructuredData: JSON-LD', () => {
|
||||
it('reads blocks in head and body, type compared loosely', () => {
|
||||
const html = page(ld(order), ld({ ...order, orderNumber: '43' }, ' Application/LD+JSON '));
|
||||
expect(extractStructuredData(html).map((f) => f.item)).toEqual([order, { ...order, orderNumber: '43' }]);
|
||||
});
|
||||
|
||||
it('reads a block nested inside a microdata tree', () => {
|
||||
const html = page('', `<div itemscope itemtype="http://schema.org/EmailMessage">${ld(order)}</div>`);
|
||||
expect(extractStructuredData(html).filter((f) => f.source === 'json-ld').map((f) => f.item)).toEqual([order]);
|
||||
});
|
||||
|
||||
it('skips mail without any type it reads, without parsing', () => {
|
||||
const html = page(ld({ '@context': 'http://schema.org', '@type': 'PromotionCard' }));
|
||||
expect(mayContainStructuredData(html)).toBe(false);
|
||||
expect(extractStructuredData(html)).toEqual([]);
|
||||
});
|
||||
|
||||
it('ignores markup inside quoted or forwarded parts', () => {
|
||||
const quoted = `<blockquote>${ld(order)}</blockquote><div class="gmail_quote">${ld(order)}</div>`;
|
||||
const outlook = `<p>FYI</p><div id="divRplyFwdMsg">From: someone</div><div>${ld(order)}</div>`;
|
||||
expect(extractStructuredData(page('', quoted))).toEqual([]);
|
||||
expect(extractStructuredData(page('', outlook))).toEqual([]);
|
||||
});
|
||||
|
||||
it('caps the number of blocks it reads', () => {
|
||||
const html = page(Array.from({ length: 60 }, () => ld(order)).join(''));
|
||||
expect(extractStructuredData(html)).toHaveLength(50);
|
||||
});
|
||||
|
||||
it('runs no script and loads nothing while parsing', () => {
|
||||
const html = page('<script>globalThis.__structuredMailRan = true</script>', `<img src="https://tracker.invalid/x.gif">${ld(order)}`);
|
||||
extractStructuredData(html);
|
||||
expect((globalThis as unknown as Record<string, unknown>).__structuredMailRan).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractStructuredData: microdata', () => {
|
||||
it('reads values from content, href, src, datetime and text', () => {
|
||||
const html = page('', `
|
||||
<div itemscope itemtype="http://schema.org/ParcelDelivery">
|
||||
<meta itemprop="trackingNumber" content=" 123 ">
|
||||
<a itemprop="trackingUrl" href="https://track.example.com/123">track</a>
|
||||
<img itemprop="image" src="https://img.example.com/a.png">
|
||||
<time itemprop="expectedArrivalUntil" datetime="2026-11-02">2 Nov</time>
|
||||
<span itemprop="carrier"> DHL
|
||||
Express </span>
|
||||
</div>`);
|
||||
expect(extractStructuredData(html)).toEqual([{
|
||||
source: 'microdata',
|
||||
item: {
|
||||
'@context': 'https://schema.org', '@type': 'ParcelDelivery', trackingNumber: '123',
|
||||
trackingUrl: 'https://track.example.com/123', image: 'https://img.example.com/a.png',
|
||||
expectedArrivalUntil: '2026-11-02', carrier: 'DHL Express',
|
||||
},
|
||||
}]);
|
||||
});
|
||||
|
||||
it('nests items, splits itemprop lists and turns repeats into arrays', () => {
|
||||
const html = page('', `
|
||||
<div itemscope itemtype="https://schema.org/Order">
|
||||
<meta itemprop="orderNumber" content="7">
|
||||
<div itemprop="merchant seller" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="Shop"></div>
|
||||
<meta itemprop="orderStatus" content="">
|
||||
<link itemprop="orderStatus" href="http://schema.org/OrderProcessing">
|
||||
<div itemprop="acceptedOffer" itemscope itemtype="http://schema.org/Offer">
|
||||
<div itemprop="itemOffered" itemscope itemtype="http://schema.org/Product"><meta itemprop="name" content="One"></div>
|
||||
</div>
|
||||
<div itemprop="acceptedOffer" itemscope itemtype="http://schema.org/Offer">
|
||||
<div itemprop="itemOffered" itemscope itemtype="http://schema.org/Product"><meta itemprop="name" content="Two"></div>
|
||||
</div>
|
||||
</div>`);
|
||||
const [{ item }] = extractStructuredData(html);
|
||||
const shop = { '@type': 'Organization', name: 'Shop' };
|
||||
expect(item).toEqual({
|
||||
'@context': 'https://schema.org', '@type': 'Order', orderNumber: '7', merchant: shop, seller: shop,
|
||||
orderStatus: 'http://schema.org/OrderProcessing',
|
||||
acceptedOffer: [
|
||||
{ '@type': 'Offer', itemOffered: { '@type': 'Product', name: 'One' } },
|
||||
{ '@type': 'Offer', itemOffered: { '@type': 'Product', name: 'Two' } },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('ignores items not typed on schema.org', () => {
|
||||
const html = page('', `
|
||||
<div itemscope itemtype="http://example.com/Order"><meta itemprop="orderNumber" content="1"></div>
|
||||
<div itemscope><meta itemprop="orderNumber" content="2"></div>`);
|
||||
expect(extractStructuredData(html)).toEqual([]);
|
||||
});
|
||||
|
||||
it('accepts several itemtypes and keeps the schema.org ones', () => {
|
||||
const html = page('', '<div itemscope itemtype="http://example.com/X http://schema.org/Order"><meta itemprop="orderNumber" content="1"></div>');
|
||||
expect(extractStructuredData(html)[0].item).toMatchObject({ '@type': 'Order', orderNumber: '1' });
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,79 @@
|
||||
[
|
||||
{
|
||||
"id": "flixbus-booking",
|
||||
"sender": "booking.flixbus.com",
|
||||
"source": "real",
|
||||
"mailDate": "2024-05-28T15:18:21Z",
|
||||
"html": "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><script type=\"application/ld+json\">[{\"@context\":\"http://schema.org\",\"@type\":\"BusReservation\",\"reservationNumber\":\"3165000001\",\"underName\":{\"@type\":\"Person\",\"name\":\"Erika Mustermann\",\"email\":\"kunde@example.org\"},\"reservationStatus\":\"http://schema.org/ReservationConfirmed\",\"reservationFor\":{\"@type\":\"BusTrip\",\"busNumber\":\"FLX30\",\"busName\":\"Berlin Ostbahnhof\",\"busCompany\":{\"@type\":\"Organization\",\"name\":\"Flix\"},\"departureBusStop\":{\"@type\":\"BusStop\",\"name\":\"Hannover Hauptbahnhof (FlixTrain)\",\"address\":{\"@type\":\"PostalAddress\",\"streetAddress\":\"Ernst-August-Platz 1\",\"addressLocality\":\"Hannover\",\"postalCode\":\"30159\",\"addressCountry\":\"DE\"}},\"departureTime\":\"2024-06-04T11:44:00+0200\",\"arrivalBusStop\":{\"@type\":\"BusStop\",\"name\":\"Berlin Ostbahnhof (FlixTrain)\",\"address\":{\"@type\":\"PostalAddress\",\"streetAddress\":\"Koppenstr. 3\",\"addressLocality\":\"Berlin\",\"postalCode\":\"10243\",\"addressCountry\":\"DE\"}},\"arrivalTime\":\"2024-06-04T13:44:00+0200\"},\"reservedTicket\":{\"@type\":\"Ticket\",\"ticketNumber\":\"3165000001\",\"ticketToken\":\"https://shop.global.flixbus.com/pdfqr/3165000001/00000000000000000000000000000000000000000000000000?mfb_source=email\",\"underName\":{\"@type\":\"Person\",\"name\":\"Erika Mustermann\"},\"ticketedSeat\":{\"@type\":\"Seat\",\"seatingType\":\"Coach\",\"seatNumber\":\"6D\"}}},{\"@context\":\"http://schema.org\",\"@type\":\"BusReservation\",\"reservationNumber\":\"3165000001\",\"underName\":{\"@type\":\"Person\",\"name\":\"Max Mustermann\",\"email\":\"kunde@example.org\"},\"reservationStatus\":\"http://schema.org/ReservationConfirmed\",\"reservationFor\":{\"@type\":\"BusTrip\",\"busNumber\":\"FLX30\",\"busName\":\"Berlin Ostbahnhof\",\"busCompany\":{\"@type\":\"Organization\",\"name\":\"Flix\"},\"departureBusStop\":{\"@type\":\"BusStop\",\"name\":\"Hannover Hauptbahnhof (FlixTrain)\",\"address\":{\"@type\":\"PostalAddress\",\"streetAddress\":\"Ernst-August-Platz 1\",\"addressLocality\":\"Hannover\",\"postalCode\":\"30159\",\"addressCountry\":\"DE\"}},\"departureTime\":\"2024-06-04T11:44:00+0200\",\"arrivalBusStop\":{\"@type\":\"BusStop\",\"name\":\"Berlin Ostbahnhof (FlixTrain)\",\"address\":{\"@type\":\"PostalAddress\",\"streetAddress\":\"Koppenstr. 3\",\"addressLocality\":\"Berlin\",\"postalCode\":\"10243\",\"addressCountry\":\"DE\"}},\"arrivalTime\":\"2024-06-04T13:44:00+0200\"},\"reservedTicket\":{\"@type\":\"Ticket\",\"ticketNumber\":\"3165000001\",\"ticketToken\":\"https://shop.global.flixbus.com/pdfqr/3165000001/00000000000000000000000000000000000000000000000000?mfb_source=email\",\"underName\":{\"@type\":\"Person\",\"name\":\"Max Mustermann\"},\"ticketedSeat\":{\"@type\":\"Seat\",\"seatingType\":\"Coach\",\"seatNumber\":\"6C\"}}}]</script></head><body></body></html>"
|
||||
},
|
||||
{
|
||||
"id": "flixbus-invoice",
|
||||
"sender": "flixbus.com",
|
||||
"source": "real",
|
||||
"mailDate": "2024-05-28T15:18:20Z",
|
||||
"html": "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><script type=\"application/ld+json\">[{\"@context\":\"http:\\/\\/schema.org\",\"@type\":\"BusReservation\",\"reservationNumber\":3165000001,\"underName\":{\"@type\":\"Person\",\"name\":\"Max Mustermann\",\"email\":\"kunde@example.org\"},\"bookingAgent\":{\"@type\":\"Organization\",\"name\":\"Flix SE\",\"url\":\"http:\\/\\/shop.flixbus.de\"},\"bookingTime\":\"2024-05-28T17:18:16+0200\",\"modifiedTime\":\"2024-05-28T17:18:17+0200\",\"reservationStatus\":\"http:\\/\\/schema.org\\/ReservationConfirmed\",\"reservationFor\":{\"@type\":\"BusTrip\",\"busNumber\":\"FLX30\",\"busName\":\"Berlin Ostbahnhof\",\"busCompany\":{\"@type\":\"Organization\",\"name\":\"Flix SE\"},\"departureBusStop\":{\"@type\":\"BusStop\",\"name\":\"Hannover Hauptbahnhof (FlixTrain)\",\"address\":{\"@type\":\"PostalAddress\",\"streetAddress\":\"Ernst-August-Platz 1\",\"addressLocality\":\"Hannover\",\"addressRegion\":\"\",\"postalCode\":\"30159\",\"addressCountry\":\"DE\"},\"directions\":\"Wagennummerierung beginnt an der Zugspitze. Wagen 100 befindet sich in der Zugmitte.\"},\"departureTime\":\"2024-06-04T11:44:00+0200\",\"arrivalBusStop\":{\"@type\":\"BusStop\",\"name\":\"Berlin Ostbahnhof (FlixTrain)\",\"address\":{\"@type\":\"PostalAddress\",\"streetAddress\":\"Koppenstr. 3\",\"addressLocality\":\"Berlin\",\"addressRegion\":\"\",\"postalCode\":\"10243\",\"addressCountry\":\"DE\"},\"directions\":\"\"},\"arrivalTime\":\"2024-06-04T13:44:00+0200\"},\"reservedTicket\":{\"@type\":\"Ticket\",\"ticketNumber\":3165000001,\"ticketToken\":\"https:\\/\\/api.flixbus.com\\/qrcode\\/de\\/3165000001\\/00000000000000000000000000000000000000000000000000\",\"price\":\"14.9700\",\"priceCurrency\":\"EUR\",\"underName\":{\"@type\":\"Person\",\"name\":\"Max Mustermann\"}}},{\"@context\":\"http:\\/\\/schema.org\",\"@type\":\"BusReservation\",\"reservationNumber\":3165000001,\"underName\":{\"@type\":\"Person\",\"name\":\"Erika Mustermann\",\"email\":\"kunde@example.org\"},\"bookingAgent\":{\"@type\":\"Organization\",\"name\":\"Flix SE\",\"url\":\"http:\\/\\/shop.flixbus.de\"},\"bookingTime\":\"2024-05-28T17:18:16+0200\",\"modifiedTime\":\"2024-05-28T17:18:17+0200\",\"reservationStatus\":\"http:\\/\\/schema.org\\/ReservationConfirmed\",\"reservationFor\":{\"@type\":\"BusTrip\",\"busNumber\":\"FLX30\",\"busName\":\"Berlin Ostbahnhof\",\"busCompany\":{\"@type\":\"Organization\",\"name\":\"Flix SE\"},\"departureBusStop\":{\"@type\":\"BusStop\",\"name\":\"Hannover Hauptbahnhof (FlixTrain)\",\"address\":{\"@type\":\"PostalAddress\",\"streetAddress\":\"Ernst-August-Platz 1\",\"addressLocality\":\"Hannover\",\"addressRegion\":\"\",\"postalCode\":\"30159\",\"addressCountry\":\"DE\"},\"directions\":\"Wagennummerierung beginnt an der Zugspitze. Wagen 100 befindet sich in der Zugmitte.\"},\"departureTime\":\"2024-06-04T11:44:00+0200\",\"arrivalBusStop\":{\"@type\":\"BusStop\",\"name\":\"Berlin Ostbahnhof (FlixTrain)\",\"address\":{\"@type\":\"PostalAddress\",\"streetAddress\":\"Koppenstr. 3\",\"addressLocality\":\"Berlin\",\"addressRegion\":\"\",\"postalCode\":\"10243\",\"addressCountry\":\"DE\"},\"directions\":\"\"},\"arrivalTime\":\"2024-06-04T13:44:00+0200\"},\"reservedTicket\":{\"@type\":\"Ticket\",\"ticketNumber\":3165000001,\"ticketToken\":\"https:\\/\\/api.flixbus.com\\/qrcode\\/de\\/3165000001\\/00000000000000000000000000000000000000000000000000\",\"price\":\"14.9700\",\"priceCurrency\":\"EUR\",\"underName\":{\"@type\":\"Person\",\"name\":\"Erika Mustermann\"}}}]</script></head><body></body></html>"
|
||||
},
|
||||
{
|
||||
"id": "kaufland-order-1",
|
||||
"sender": "kaufland-marktplatz.de",
|
||||
"source": "real",
|
||||
"mailDate": "2024-10-07T20:20:57Z",
|
||||
"html": "<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body><span itemscope itemtype=\"http://schema.org/Order\"> <span itemprop=\"merchant\" itemscope itemtype=\"http://schema.org/Organization\"> <meta itemprop=\"name\" content=\"Kaufland.de\"> <link itemprop=\"url\" href=\"https://www.kaufland.de\"> </span> <span itemprop=\"potentialAction\" itemscope itemtype=\"http://schema.org/ViewAction\"> <link itemprop=\"target\" href=\"https://www.kaufland.de/account/orders?utm_medium=email&utm_content=purchase_confirmation_gmu_de-de&utm_source=real&utm_campaign=customer#100000000000001\"> <meta itemprop=\"name\" content=\"Bestellung ansehen\"> <meta itemprop=\"url\" content=\"https://www.kaufland.de/account/orders?utm_medium=email&utm_content=purchase_confirmation_gmu_de-de&utm_source=real&utm_campaign=customer#100000000000001\"> </span> <meta itemprop=\"orderNumber\" content=\"M100AA1\"> <meta itemprop=\"priceCurrency\" content=\"EUR\"> <meta itemprop=\"price\" content=\"399,00 €\"> <span itemprop=\"acceptedOffer\" itemscope itemtype=\"http://schema.org/Offer\"> <span itemprop=\"itemOffered\" itemscope itemtype=\"http://schema.org/Product\"> <meta itemprop=\"name\" content=\"Creality K1 3D-Drucker\"> <meta itemprop=\"image\" content=\"https://media.cdn.kaufland.de/product-images/100x100/4af2c802b184554243cd708e9d83a180.jpg\"> <meta itemprop=\"sku\" content=\"464541313\"> <meta itemprop=\"url\" content=\"https://www.kaufland.de/product/464541313/?utm_medium=email&utm_content=purchase_confirmation_gmu_de-de&utm_source=real&utm_campaign=customer\"> </span> <meta itemprop=\"price\" content=\"399,00 €\"> <meta itemprop=\"priceCurrency\" content=\"EUR\"> <span itemprop=\"eligibleQuantity\" itemscope itemtype=\"http://schema.org/QuantitativeValue\"> <meta itemprop=\"value\" content=\"1\"> </span> </span> <meta itemprop=\"orderStatus\" content=\"\"> <meta itemprop=\"url\" content=\"https://www.kaufland.de/account/orders?utm_medium=email&utm_content=purchase_confirmation_gmu_de-de&utm_source=real&utm_campaign=customer#100000000000001\"> <meta itemprop=\"orderStatus\" content=\"http://schema.org/OrderProcessing\"> <span itemprop=\"priceSpecification\" itemscope itemtype=\"http://schema.org/PriceSpecification\"> <meta itemprop=\"price\" content=\"0,00 €\"> <meta itemprop=\"priceCurrency\" content=\"EUR\"> </span> </span></body></html>"
|
||||
},
|
||||
{
|
||||
"id": "kaufland-parcel-1",
|
||||
"sender": "kaufland-marktplatz.de",
|
||||
"source": "real",
|
||||
"mailDate": "2024-10-08T13:50:25Z",
|
||||
"html": "<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body><span itemscope=\"\" itemtype=\"http://schema.org/ParcelDelivery\"> <span itemprop=\"deliveryAddress\" itemscope=\"\" itemtype=\"http://schema.org/PostalAddress\"> <meta itemprop=\"name\" content=\"Erika Mustermann\"> <meta itemprop=\"streetAddress\" content=\"Musterweg 1\"> <meta itemprop=\"addressLocality\" content=\"Musterstadt\"> <meta itemprop=\"addressRegion\" content=\"Musterstadt\"> <meta itemprop=\"addressCountry\" content=\"DE\"> <meta itemprop=\"postalCode\" content=\"12345\"> </span> <meta itemprop=\"expectedArrivalUntil\" content=\"2024-10-13CEST03:10:23\"> <span itemprop=\"itemShipped\" itemscope=\"\" itemtype=\"http://schema.org/Product\"> <meta itemprop=\"name\" content=\"Creality K1 3D-Drucker\"> <link itemprop=\"url\" href=\"https://www.kaufland.de/product/464541313/?utm_medium=email&utm_content=purchase_sent_confirmation&utm_source=real&utm_campaign=customer\"> <link itemprop=\"image\" href=\"https://media.cdn.kaufland.de/product-images/100x100/4af2c802b184554243cd708e9d83a180.jpg\"> <meta itemprop=\"sku\" content=\"464541313\"> </span> <span itemprop=\"partOfOrder\" itemscope=\"\" itemtype=\"http://schema.org/Order\"> <meta itemprop=\"orderNumber\" content=\"M100AA1\"> <span itemprop=\"merchant\" itemscope=\"\" itemtype=\"http://schema.org/Organization\"> <meta itemprop=\"name\" content=\"Kaufland.de\"> <link itemprop=\"sameAs\" href=\"https://www.kaufland.de\"> </span> <link itemprop=\"orderStatus\" href=\"http://schema.org/OrderInTransit\"> </span> </span></body></html>"
|
||||
},
|
||||
{
|
||||
"id": "kaufland-order-2",
|
||||
"sender": "kaufland-marktplatz.de",
|
||||
"source": "real",
|
||||
"mailDate": "2024-11-11T14:33:42Z",
|
||||
"html": "<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body><span itemscope itemtype=\"http://schema.org/Order\"> <span itemprop=\"merchant\" itemscope itemtype=\"http://schema.org/Organization\"> <meta itemprop=\"name\" content=\"Kaufland.de\"> <link itemprop=\"url\" href=\"https://www.kaufland.de\"> </span> <span itemprop=\"potentialAction\" itemscope itemtype=\"http://schema.org/ViewAction\"> <link itemprop=\"target\" href=\"https://www.kaufland.de/account/orders?utm_medium=email&utm_content=purchase_confirmation_gmu_de-de&utm_source=real&utm_campaign=customer#100000000000002\"> <meta itemprop=\"name\" content=\"Bestellung ansehen\"> <meta itemprop=\"url\" content=\"https://www.kaufland.de/account/orders?utm_medium=email&utm_content=purchase_confirmation_gmu_de-de&utm_source=real&utm_campaign=customer#100000000000002\"> </span> <meta itemprop=\"orderNumber\" content=\"M200BB2\"> <meta itemprop=\"priceCurrency\" content=\"EUR\"> <meta itemprop=\"price\" content=\"29,99 €\"> <span itemprop=\"acceptedOffer\" itemscope itemtype=\"http://schema.org/Offer\"> <span itemprop=\"itemOffered\" itemscope itemtype=\"http://schema.org/Product\"> <meta itemprop=\"name\" content=\"Atomstack Maker AC1 Kamera 5 Megapixel 400x400mm Fotografiebereich für Atomstack NEJE Tool Graviermaschine\"> <meta itemprop=\"image\" content=\"https://media.cdn.kaufland.de/product-images/100x100/29e4eac9c493c96fbe5d122f305ea21b.jpg\"> <meta itemprop=\"sku\" content=\"460132763\"> <meta itemprop=\"url\" content=\"https://www.kaufland.de/product/460132763/?utm_medium=email&utm_content=purchase_confirmation_gmu_de-de&utm_source=real&utm_campaign=customer\"> </span> <meta itemprop=\"price\" content=\"29,99 €\"> <meta itemprop=\"priceCurrency\" content=\"EUR\"> <span itemprop=\"eligibleQuantity\" itemscope itemtype=\"http://schema.org/QuantitativeValue\"> <meta itemprop=\"value\" content=\"1\"> </span> </span> <meta itemprop=\"orderStatus\" content=\"\"> <meta itemprop=\"url\" content=\"https://www.kaufland.de/account/orders?utm_medium=email&utm_content=purchase_confirmation_gmu_de-de&utm_source=real&utm_campaign=customer#100000000000002\"> <meta itemprop=\"orderStatus\" content=\"http://schema.org/OrderProcessing\"> <span itemprop=\"priceSpecification\" itemscope itemtype=\"http://schema.org/PriceSpecification\"> <meta itemprop=\"price\" content=\"0,00 €\"> <meta itemprop=\"priceCurrency\" content=\"EUR\"> </span> </span></body></html>"
|
||||
},
|
||||
{
|
||||
"id": "kaufland-parcel-2",
|
||||
"sender": "kaufland-marktplatz.de",
|
||||
"source": "real",
|
||||
"mailDate": "2024-11-12T10:20:17Z",
|
||||
"html": "<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body><span itemscope=\"\" itemtype=\"http://schema.org/ParcelDelivery\"> <span itemprop=\"deliveryAddress\" itemscope=\"\" itemtype=\"http://schema.org/PostalAddress\"> <meta itemprop=\"name\" content=\"Erika Mustermann\"> <meta itemprop=\"streetAddress\" content=\"Musterweg 1\"> <meta itemprop=\"addressLocality\" content=\"Musterstadt\"> <meta itemprop=\"addressRegion\" content=\"Musterstadt\"> <meta itemprop=\"addressCountry\" content=\"DE\"> <meta itemprop=\"postalCode\" content=\"12345\"> </span> <meta itemprop=\"expectedArrivalUntil\" content=\"2024-11-17CET11:11:17\"> <span itemprop=\"itemShipped\" itemscope=\"\" itemtype=\"http://schema.org/Product\"> <meta itemprop=\"name\" content=\"Atomstack Maker AC1 Kamera 5 Megapixel 400x400mm Fotografiebereich für Atomstack NEJE Tool Graviermaschine\"> <link itemprop=\"url\" href=\"https://www.kaufland.de/product/460132763/?utm_medium=email&utm_content=purchase_sent_confirmation&utm_source=real&utm_campaign=customer\"> <link itemprop=\"image\" href=\"https://media.cdn.kaufland.de/product-images/100x100/29e4eac9c493c96fbe5d122f305ea21b.jpg\"> <meta itemprop=\"sku\" content=\"460132763\"> </span> <span itemprop=\"partOfOrder\" itemscope=\"\" itemtype=\"http://schema.org/Order\"> <meta itemprop=\"orderNumber\" content=\"M200BB2\"> <span itemprop=\"merchant\" itemscope=\"\" itemtype=\"http://schema.org/Organization\"> <meta itemprop=\"name\" content=\"Kaufland.de\"> <link itemprop=\"sameAs\" href=\"https://www.kaufland.de\"> </span> <link itemprop=\"orderStatus\" href=\"http://schema.org/OrderInTransit\"> </span> </span></body></html>"
|
||||
},
|
||||
{
|
||||
"id": "ahoi-event",
|
||||
"sender": "ahoi.cloud",
|
||||
"source": "real",
|
||||
"mailDate": "2021-05-28T13:30:10Z",
|
||||
"html": "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><script type=\"application/ld+json\">[{\"@context\":\"https:\\/\\/schema.org\",\"@type\":\"EventReservation\",\"underName\":{\"@type\":\"Person\",\"name\":\"Erika Mustermann\"},\"reservationNumber\":\"AB12CD\",\"reservationStatus\":\"http:\\/\\/schema.org\\/Confirmed\",\"modifiedTime\":\"2021-05-28T15:30:04+02:00\",\"modifyReservationUrl\":\"http:\\/\\/default\\/buchung\\/AB12CD\\/Mustermann\",\"ticketDownloadUrl\":\"http:\\/\\/default\\/ticket\\/00000000000000000000000000000000.pdf\",\"ticketToken\":\"http:\\/\\/default\\/ticket\\/00000000000000000000000000000000.pdf\",\"ticketNumber\":\"AB12CD-1\",\"reservationFor\":{\"@type\":\"Event\",\"name\":\"Grafikdesign: Illustrator-Workshop\",\"startDate\":\"2021-07-18T14:00:00+02:00\",\"endDate\":\"2021-07-18T19:00:00+02:00\"}}]</script></head><body></body></html>"
|
||||
},
|
||||
{
|
||||
"id": "k4g-order",
|
||||
"sender": "k4g.com",
|
||||
"source": "real",
|
||||
"mailDate": "2021-12-04T22:05:29Z",
|
||||
"html": "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><script type=\"application/ld+json\">{\"@context\":\"http:\\/\\/schema.org\",\"@type\":\"Order\",\"merchant\":{\"@type\":\"Organization\",\"name\":\"K4G\"},\"orderNumber\":\"A1B2C3D4\",\"priceCurrency\":\"EUR\",\"price\":25.99,\"acceptedOffer\":[{\"@type\":\"Offer\",\"itemOffered\":{\"@type\":\"Product\",\"name\":\"Farming Simulator 22 Steam CD Key\",\"sku\":\"892E8BE9\",\"url\":\"https:\\/\\/k4g.com\\/product\\/farming-simulator-22-steam-global-cd-key-6C745D16\",\"image\":\"https:\\/\\/cdn.k4g.com\\/files\\/cover\\/3b1c373f87c917c637c21b681a9b5464.jpeg\"},\"price\":25.99,\"priceCurrency\":\"EUR\",\"eligibleQuantity\":{\"@type\":\"QuantitativeValue\",\"value\":1},\"seller\":{\"@type\":\"Organization\",\"name\":\"Fenyx_Entertainment\"}}],\"url\":\"https:\\/\\/k4g.com\\/order?id=A1B2C3D4&token=0000000000000000000000000000000000000000000000000000000000000000\",\"priceSpecification\":{\"@type\":\"PriceSpecification\",\"price\":25.99,\"priceCurrency\":\"EUR\",\"valueAddedTaxIncluded\":true},\"orderStatus\":\"OrderProcessing\"}</script></head><body></body></html>"
|
||||
},
|
||||
{
|
||||
"id": "uber-order-1",
|
||||
"sender": "uber.com",
|
||||
"source": "real",
|
||||
"mailDate": "2025-01-22T22:39:53Z",
|
||||
"html": "<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body><div itemscope itemtype=\"http://schema.org/Order\" style=\"display:none!important\"><div itemprop=\"merchant\" itemscope itemtype=\"http://schema.org/Organization\"><meta itemprop=\"name\" content=\"UberEATS\"></div></div></body></html>"
|
||||
},
|
||||
{
|
||||
"id": "uber-order-2",
|
||||
"sender": "uber.com",
|
||||
"source": "real",
|
||||
"mailDate": "2025-02-06T00:08:10Z",
|
||||
"html": "<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body><div itemscope itemtype=\"http://schema.org/Order\" style=\"display:none!important\"><div itemprop=\"merchant\" itemscope itemtype=\"http://schema.org/Organization\"><meta itemprop=\"name\" content=\"UberEATS\"></div></div></body></html>"
|
||||
},
|
||||
{
|
||||
"id": "zave-newsletter",
|
||||
"sender": "p.zave.it",
|
||||
"source": "real",
|
||||
"mailDate": "2024-11-12T00:27:23Z",
|
||||
"html": "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><script type=\"application/ld+json\">\r\n{\r\n \"@context\": \"http://schema.org\",\r\n \"@type\": \"FlightReservation\",\r\n \"reservationNumber\": \"RXJ34P\",\r\n \"reservationStatus\": \"http://schema.org/Confirmed\",\r\n \"underName\": {\r\n \"@type\": \"Person\",\r\n \"name\": \"Eva Green\"\r\n },\r\n \"reservationFor\": {\r\n \"@type\": \"Flight\",\r\n \"flightNumber\": \"110\",\r\n \"airline\": {\r\n \"@type\": \"Airline\",\r\n \"name\": \"United\",\r\n \"iataCode\": \"UA\"\r\n },\r\n \"departureAirport\": {\r\n \"@type\": \"Airport\",\r\n \"name\": \"San Francisco Airport\",\r\n \"iataCode\": \"SFO\"\r\n },\r\n \"departureTime\": \"2027-03-04T20:15:00-08:00\",\r\n \"arrivalAirport\": {\r\n \"@type\": \"Airport\",\r\n \"name\": \"John F. Kennedy International Airport\",\r\n \"iataCode\": \"JFK\"\r\n },\r\n \"arrivalTime\": \"2027-03-05T06:30:00-05:00\"\r\n }\r\n}\r\n</script></head><body></body></html>"
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,336 @@
|
||||
// Mirrors webmail lib/structured-mail/__tests__/fixtures/synthetic-mails.ts at fa324170a; keep the two in step.
|
||||
/**
|
||||
* Self-written fixtures, modelled on Google's markup reference and on the
|
||||
* kinds of broken input KItinerary's tests cover (written from scratch, not
|
||||
* copied). Booking references and names are made up. `expected` lives in
|
||||
* structured-mail-fixtures.test.ts.
|
||||
*/
|
||||
|
||||
export interface MailFixture {
|
||||
id: string;
|
||||
sender: string;
|
||||
source: 'real' | 'synthetic';
|
||||
mailDate: string;
|
||||
html: string;
|
||||
}
|
||||
|
||||
const MAIL_DATE = '2026-10-30T09:00:00Z';
|
||||
const page = (head: string, body = '') => `<!DOCTYPE html><html><head><meta charset="utf-8">${head}</head><body><p>Thank you for your booking.</p>${body}</body></html>`;
|
||||
const script = (text: string) => `<script type="application/ld+json">${text}</script>`;
|
||||
const ld = (value: unknown) => script(JSON.stringify(value));
|
||||
const C = 'http://schema.org';
|
||||
|
||||
const person = (name: string) => ({ '@type': 'Person', name });
|
||||
const airport = (name: string, iataCode: string) => ({ '@type': 'Airport', name, iataCode });
|
||||
|
||||
function flightRes(o: {
|
||||
ref: string; name: string; seat?: string; airline?: [string, string]; number: string;
|
||||
from: [string, string]; dep: string; to: [string, string]; arr: string; status?: string; checkin?: string;
|
||||
}) {
|
||||
return {
|
||||
'@context': C, '@type': 'FlightReservation', reservationNumber: o.ref,
|
||||
reservationStatus: `http://schema.org/${o.status ?? 'ReservationConfirmed'}`,
|
||||
underName: person(o.name),
|
||||
...(o.seat ? { airplaneSeat: o.seat } : {}),
|
||||
...(o.checkin ? { checkinUrl: o.checkin } : {}),
|
||||
reservationFor: {
|
||||
'@type': 'Flight', flightNumber: o.number,
|
||||
airline: { '@type': 'Airline', name: (o.airline ?? ['Lufthansa', 'LH'])[0], iataCode: (o.airline ?? ['Lufthansa', 'LH'])[1] },
|
||||
departureAirport: airport(...o.from), departureTime: o.dep,
|
||||
arrivalAirport: airport(...o.to), arrivalTime: o.arr,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const fra: [string, string] = ['Frankfurt Airport', 'FRA'];
|
||||
const jfk: [string, string] = ['John F. Kennedy International Airport', 'JFK'];
|
||||
const out1 = { number: '400', from: fra, dep: '2026-12-01T10:15:00+01:00', to: jfk, arr: '2026-12-01T13:05:00-05:00' };
|
||||
const back1 = { number: '401', from: jfk, dep: '2026-12-08T17:30:00-05:00', to: fra, arr: '2026-12-09T07:20:00+01:00' };
|
||||
|
||||
const eurowingsLegs = [
|
||||
flightRes({ ref: 'EW7Q2K', name: 'Erika Mustermann', airline: ['Eurowings', 'EW'], number: 'EW582', from: ['Köln/Bonn', 'CGN'], dep: '2026-11-20T06:40:00+01:00', to: ['Palma de Mallorca', 'PMI'], arr: '2026-11-20T09:05:00+01:00', seat: '3A' }),
|
||||
flightRes({ ref: 'EW7Q2K', name: 'Erika Mustermann', airline: ['Eurowings', 'EW'], number: 'EW583', from: ['Palma de Mallorca', 'PMI'], dep: '2026-11-27T10:00:00+01:00', to: ['Köln/Bonn', 'CGN'], arr: '2026-11-27T12:25:00+01:00', seat: '14F' }),
|
||||
];
|
||||
|
||||
const voloteaPax = ['Erika Mustermann', 'Max Mustermann'].map((name, i) => flightRes({
|
||||
ref: 'V0L8TA', name, airline: ['Volotea', 'V7'], number: '1520', seat: `1${i}C`,
|
||||
from: ['Venezia', 'VCE'], dep: '2026-12-12T14:10:00+01:00', to: ['Nantes', 'NTE'], arr: '2026-12-12T16:15:00+01:00',
|
||||
}));
|
||||
|
||||
const hotel = {
|
||||
'@context': C, '@type': 'LodgingReservation', reservationNumber: 'HB-58213',
|
||||
reservationStatus: 'http://schema.org/Confirmed', underName: person('Erika Mustermann'),
|
||||
reservationFor: {
|
||||
'@type': 'Hotel', name: 'Hotel Am Markt', telephone: '+49 30 1234567',
|
||||
address: { '@type': 'PostalAddress', streetAddress: 'Marktplatz 1', addressLocality: 'Berlin', postalCode: '10178', addressCountry: 'DE' },
|
||||
},
|
||||
checkinDate: '2026-12-04T15:00:00+01:00', checkoutDate: '2026-12-06T11:00:00+01:00',
|
||||
modifyReservationUrl: 'https://www.hotel-booking.example.com/manage/HB-58213',
|
||||
};
|
||||
|
||||
const microdataFlight = (o: { ref: string; name: string; number: string; code: string; airline: string; from: [string, string]; dep: string; to: [string, string]; arr?: string }) => `
|
||||
<div itemscope itemtype="http://schema.org/FlightReservation">
|
||||
<meta itemprop="reservationNumber" content="${o.ref}">
|
||||
<link itemprop="reservationStatus" href="http://schema.org/Confirmed">
|
||||
<div itemprop="underName" itemscope itemtype="http://schema.org/Person"><meta itemprop="name" content="${o.name}"></div>
|
||||
<div itemprop="reservationFor" itemscope itemtype="http://schema.org/Flight">
|
||||
<meta itemprop="flightNumber" content="${o.number}">
|
||||
<div itemprop="airline" itemscope itemtype="http://schema.org/Airline"><meta itemprop="name" content="${o.airline}"><meta itemprop="iataCode" content="${o.code}"></div>
|
||||
<div itemprop="departureAirport" itemscope itemtype="http://schema.org/Airport"><meta itemprop="name" content="${o.from[0]}"><meta itemprop="iataCode" content="${o.from[1]}"></div>
|
||||
<meta itemprop="departureTime" content="${o.dep}">
|
||||
<div itemprop="arrivalAirport" itemscope itemtype="http://schema.org/Airport"><meta itemprop="name" content="${o.to[0]}"><meta itemprop="iataCode" content="${o.to[1]}"></div>
|
||||
${o.arr ? `<meta itemprop="arrivalTime" content="${o.arr}">` : ''}
|
||||
</div>
|
||||
</div>`;
|
||||
|
||||
const parcel = {
|
||||
'@context': C, '@type': 'ParcelDelivery',
|
||||
carrier: { '@type': 'Organization', name: 'DHL' }, trackingNumber: '00340434161234567890',
|
||||
trackingUrl: 'https://www.dhl.de/de/privatkunden/pakete-empfangen/verfolgen.html?piececode=00340434161234567890',
|
||||
expectedArrivalUntil: '2026-11-03T18:00:00+01:00',
|
||||
itemShipped: [{ '@type': 'Product', name: 'Wanderschuhe', image: 'https://img.shop.example.com/p/1.jpg' }, { '@type': 'Product', name: 'Socken' }],
|
||||
partOfOrder: { '@type': 'Order', orderNumber: 'SO-449120', merchant: { '@type': 'Organization', name: 'Outdoor Shop' }, orderStatus: 'http://schema.org/OrderInTransit' },
|
||||
};
|
||||
|
||||
const orderObj = {
|
||||
'@context': C, '@type': 'Order', merchant: { '@type': 'Organization', name: 'Outdoor Shop' }, orderNumber: 'SO-449120',
|
||||
orderStatus: 'http://schema.org/OrderProcessing', priceCurrency: 'EUR', price: '129.90',
|
||||
acceptedOffer: [
|
||||
{ '@type': 'Offer', itemOffered: { '@type': 'Product', name: 'Wanderschuhe' }, price: '119.90', priceCurrency: 'EUR', eligibleQuantity: { '@type': 'QuantitativeValue', value: '1' } },
|
||||
{ '@type': 'Offer', itemOffered: { '@type': 'Product', name: 'Socken' }, price: '5.00', priceCurrency: 'EUR', eligibleQuantity: { '@type': 'QuantitativeValue', value: '2' } },
|
||||
],
|
||||
url: 'https://www.outdoor-shop.example.com/account/orders/SO-449120',
|
||||
};
|
||||
|
||||
export const syntheticMails: MailFixture[] = [
|
||||
// Two passengers, two legs: four objects, one booking.
|
||||
{
|
||||
id: 'flight-two-passengers-two-legs', sender: 'airline-a.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld([
|
||||
flightRes({ ref: 'QX7K2M', name: 'Erika Mustermann', seat: '12A', ...out1, checkin: 'https://www.airline-a.example.com/checkin?pnr=QX7K2M' }),
|
||||
flightRes({ ref: 'QX7K2M', name: 'Max Mustermann', seat: '12B', ...out1, checkin: 'https://www.airline-a.example.com/checkin?pnr=QX7K2M' }),
|
||||
flightRes({ ref: 'QX7K2M', name: 'Erika Mustermann', seat: '31K', ...back1 }),
|
||||
flightRes({ ref: 'QX7K2M', name: 'Max Mustermann', seat: '31J', ...back1 }),
|
||||
])),
|
||||
},
|
||||
// Eurowings-style: objects in a top-level array without commas between them.
|
||||
{
|
||||
id: 'eurowings-missing-commas', sender: 'airline-b.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(script(`[${eurowingsLegs.map((l) => JSON.stringify(l, null, 1)).join('\n')}]`)),
|
||||
},
|
||||
// Volotea-style: several objects without the array brackets.
|
||||
{
|
||||
id: 'volotea-missing-brackets', sender: 'airline-c.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(script(voloteaPax.map((p) => JSON.stringify(p)).join(',\n'))),
|
||||
},
|
||||
// Eventbrite-style trailing commas.
|
||||
{
|
||||
id: 'eventbrite-trailing-commas', sender: 'tickets.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(script(`{
|
||||
"@context": "http://schema.org", "@type": "EventReservation", "reservationNumber": "EB-1029384756",
|
||||
"underName": {"@type": "Person", "name": "Erika Mustermann",},
|
||||
"reservationFor": {"@type": "Event", "name": "Jazz im Park", "startDate": "2026-11-14T19:00:00+01:00", "endDate": "2026-11-14T22:30:00+01:00",
|
||||
"location": {"@type": "Place", "name": "Stadtpark", "address": {"@type": "PostalAddress", "streetAddress": "Parkweg 2", "addressLocality": "Hamburg", "addressCountry": "DE",},},},
|
||||
"ticketDownloadUrl": "https://tickets.example.com/t/EB-1029384756",
|
||||
}`)),
|
||||
},
|
||||
// Airbnb-style XML-entity-encoded JSON.
|
||||
{
|
||||
id: 'airbnb-entities', sender: 'stays.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(script(JSON.stringify({
|
||||
'@context': C, '@type': 'LodgingReservation', reservationNumber: 'HMXK29Q4ZP',
|
||||
reservationFor: { '@type': 'LodgingBusiness', name: 'Altbauwohnung am Kanal', address: { '@type': 'PostalAddress', addressLocality: 'Amsterdam', addressCountry: 'NL' } },
|
||||
checkinDate: '2027-01-15', checkoutDate: '2027-01-19', underName: person('Erika Mustermann'),
|
||||
}).replace(/"/g, '"'))),
|
||||
},
|
||||
// @graph wrapper around a hotel reservation and its organisation.
|
||||
{
|
||||
id: 'graph-hotel', sender: 'hotel-booking.example.com', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld({ '@context': C, '@graph': [{ ...hotel, '@context': undefined }, { '@type': 'Organization', name: 'Hotel Booking' }] })),
|
||||
},
|
||||
// Several @types; train seat with coach.
|
||||
{
|
||||
id: 'train-multiple-types', sender: 'rail.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld({
|
||||
'@context': C, '@type': ['TrainReservation', 'Thing'], reservationNumber: 'RX4471', underName: 'Erika Mustermann',
|
||||
reservationStatus: 'ReservationConfirmed',
|
||||
reservationFor: {
|
||||
'@type': 'TrainTrip', trainNumber: 'ICE 577', provider: { '@type': 'Organization', name: 'Rail Co' },
|
||||
departureStation: { '@type': 'TrainStation', name: 'Hamburg Hbf', address: { '@type': 'PostalAddress', addressLocality: 'Hamburg', addressCountry: 'DE' } },
|
||||
departureTime: '2026-11-18T08:01:00+01:00',
|
||||
arrivalStation: { '@type': 'TrainStation', name: 'München Hbf', address: { '@type': 'PostalAddress', addressLocality: 'München', addressCountry: 'DE' } },
|
||||
arrivalTime: '2026-11-18T13:58:00+01:00',
|
||||
},
|
||||
reservedTicket: { '@type': 'Ticket', ticketedSeat: { '@type': 'Seat', seatSection: '12', seatNumber: '45' } },
|
||||
})),
|
||||
},
|
||||
// reservationFor as an array: one reservation per event.
|
||||
{
|
||||
id: 'event-reservationfor-array', sender: 'tickets.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld({
|
||||
'@context': C, '@type': 'EventReservation', reservationNumber: 'KT-7781', underName: person('Erika Mustermann'),
|
||||
reservationFor: [
|
||||
{ '@type': 'TheaterEvent', name: 'Faust I', startDate: '2026-11-21T19:30:00+01:00', location: { '@type': 'Place', name: 'Schauspielhaus', address: { addressLocality: 'Bochum', addressCountry: 'DE' } } },
|
||||
{ '@type': 'TheaterEvent', name: 'Faust II', startDate: '2026-11-22T18:00:00+01:00', location: { '@type': 'Place', name: 'Schauspielhaus', address: { addressLocality: 'Bochum', addressCountry: 'DE' } } },
|
||||
],
|
||||
})),
|
||||
},
|
||||
// Legacy property names: reservationId, checkinTime/checkoutTime, Url; string address.
|
||||
{
|
||||
id: 'hotel-legacy-names', sender: 'hotel-chain.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld({
|
||||
'@context': 'https://schema.org/', '@type': 'LodgingReservation', reservationId: '84123376',
|
||||
reservationFor: { '@type': 'Resort', name: 'Seehotel Weiß', address: 'Seestraße 3, 83727 Schliersee, Deutschland' },
|
||||
checkinTime: '2026-12-27', checkoutTime: '2027-01-02', Url: 'https://hotel-chain.example.com/r/84123376',
|
||||
})),
|
||||
},
|
||||
// Restaurant subtype; string address.
|
||||
{
|
||||
id: 'restaurant-cafe', sender: 'tables.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld({
|
||||
'@context': C, '@type': 'FoodEstablishmentReservation', reservationNumber: 'T-2231', reservationStatus: 'http://schema.org/Confirmed',
|
||||
underName: person('Erika Mustermann'), partySize: 4, startTime: '2026-11-07T19:30:00+01:00',
|
||||
reservationFor: { '@type': 'CafeOrCoffeeShop', name: 'Café Kranzler', address: 'Kurfürstendamm 18, 10719 Berlin' },
|
||||
})),
|
||||
},
|
||||
// Event with a date-only end (end of that day) and a country object.
|
||||
{
|
||||
id: 'event-date-only-end', sender: 'festival.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld({
|
||||
'@context': C, '@type': 'EventReservation', reservationNumber: 'FS26-0042', underName: 'Erika Mustermann',
|
||||
reservationFor: { '@type': 'Festival', name: 'Winterfest', startDate: '2026-12-11', endDate: '2026-12-13',
|
||||
location: { '@type': 'Place', name: 'Messegelände', address: { '@type': 'PostalAddress', addressLocality: 'Wien', addressCountry: { '@type': 'Country', name: 'Austria' } } } },
|
||||
})),
|
||||
},
|
||||
// Lufthansa-style microdata with the arrival time missing: nothing.
|
||||
{
|
||||
id: 'microdata-flight-missing-arrival', sender: 'airline-d.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page('', microdataFlight({ ref: 'MD9X1Z', name: 'Erika Mustermann', number: '1234', code: 'LH', airline: 'Lufthansa', from: ['München', 'MUC'], dep: '2026-11-10T07:00:00+01:00', to: ['Hamburg', 'HAM'] })),
|
||||
},
|
||||
// Austrian-style microdata, two legs as two items.
|
||||
{
|
||||
id: 'microdata-two-legs', sender: 'airline-e.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page('',
|
||||
microdataFlight({ ref: 'OS4K7P', name: 'Erika Mustermann', number: 'OS122', code: 'OS', airline: 'Austrian', from: ['Wien', 'VIE'], dep: '2026-11-25T06:50:00+01:00', to: ['Frankfurt', 'FRA'], arr: '2026-11-25T08:15:00+01:00' })
|
||||
+ microdataFlight({ ref: 'OS4K7P', name: 'Erika Mustermann', number: 'OS125', code: 'OS', airline: 'Austrian', from: ['Frankfurt', 'FRA'], dep: '2026-11-27T19:40:00+01:00', to: ['Wien', 'VIE'], arr: '2026-11-27T21:00:00+01:00' })),
|
||||
},
|
||||
// Iberia-style microdata: itemprop lists and values from text and datetime.
|
||||
{
|
||||
id: 'microdata-itemprop-lists', sender: 'airline-f.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page('', `
|
||||
<table itemscope itemtype="http://schema.org/FlightReservation"><tr>
|
||||
<td>Booking <span itemprop="reservationNumber">IB55XQ</span></td>
|
||||
<td itemprop="underName" itemscope itemtype="http://schema.org/Person"><span itemprop="name">Erika Mustermann</span></td>
|
||||
<td itemprop="reservationFor" itemscope itemtype="http://schema.org/Flight">
|
||||
<span itemprop="airline provider" itemscope itemtype="http://schema.org/Airline"><span itemprop="name">Iberia</span> (<span itemprop="iataCode">IB</span>)</span>
|
||||
<span itemprop="flightNumber">IB 3107</span>
|
||||
<span itemprop="departureAirport" itemscope itemtype="http://schema.org/Airport"><span itemprop="name">Madrid</span> <span itemprop="iataCode">MAD</span></span>
|
||||
<time itemprop="departureTime" datetime="2026-11-16T09:30:00+01:00">16 Nov 09:30</time>
|
||||
<span itemprop="arrivalAirport" itemscope itemtype="http://schema.org/Airport"><span itemprop="name">Barcelona</span> <span itemprop="iataCode">BCN</span></span>
|
||||
<time itemprop="arrivalTime" datetime="2026-11-16T10:50:00+01:00">10:50</time>
|
||||
</td></tr></table>`),
|
||||
},
|
||||
// Dates-only stay at a Resort, https context.
|
||||
{
|
||||
id: 'hotel-dates-only', sender: 'stays.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld({
|
||||
'@context': 'https://schema.org', '@type': 'LodgingReservation', reservationNumber: 'RS-1188',
|
||||
reservationFor: { '@type': 'Resort', name: 'Strandresort Usedom', address: { '@type': 'PostalAddress', addressLocality: 'Heringsdorf', addressCountry: 'DE' } },
|
||||
checkinDate: '2027-06-01', checkoutDate: '2027-06-08',
|
||||
})),
|
||||
},
|
||||
// Rental car.
|
||||
{
|
||||
id: 'rental-car', sender: 'cars.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld({
|
||||
'@context': C, '@type': 'RentalCarReservation', reservationNumber: 'RC-99123', underName: person('Erika Mustermann'),
|
||||
reservationFor: { '@type': 'Car', name: 'Golf', brand: { '@type': 'Brand', name: 'VW' }, rentalCompany: { '@type': 'Organization', name: 'Car Rental Co' } },
|
||||
pickupLocation: { '@type': 'Place', name: 'Flughafen BER', address: { '@type': 'PostalAddress', addressLocality: 'Schönefeld', addressCountry: 'DE' } },
|
||||
pickupTime: '2026-12-04T09:00:00+01:00',
|
||||
dropoffLocation: { '@type': 'Place', name: 'Flughafen BER', address: { '@type': 'PostalAddress', addressLocality: 'Schönefeld', addressCountry: 'DE' } },
|
||||
dropoffTime: '2026-12-07T18:00:00+01:00',
|
||||
})),
|
||||
},
|
||||
// A parcel on its own.
|
||||
{
|
||||
id: 'parcel', sender: 'outdoor-shop.example.com', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld(parcel)),
|
||||
},
|
||||
// Order and parcel for the same order number in one mail: one card.
|
||||
{
|
||||
id: 'order-and-parcel', sender: 'outdoor-shop.example.com', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld([orderObj, parcel])),
|
||||
},
|
||||
// Cancelled flight.
|
||||
{
|
||||
id: 'flight-cancelled', sender: 'airline-a.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld(flightRes({ ref: 'QX7K2M', name: 'Erika Mustermann', ...out1, status: 'ReservationCancelled' }))),
|
||||
},
|
||||
// Eastbound over the date line: lands earlier on the wall clock.
|
||||
{
|
||||
id: 'flight-date-line-eastbound', sender: 'airline-g.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld(flightRes({ ref: 'QF8H2L', name: 'Erika Mustermann', airline: ['Qantas', 'QF'], number: '11', from: ['Sydney', 'SYD'], dep: '2026-12-04T09:50:00+11:00', to: ['Los Angeles', 'LAX'], arr: '2026-12-04T06:20:00-08:00' }))),
|
||||
},
|
||||
|
||||
// ---- doubtful: must give nothing
|
||||
{
|
||||
id: 'doubtful-arrival-before-departure', sender: 'airline-a.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld(flightRes({ ref: 'BAD001', name: 'Erika Mustermann', ...out1, arr: '2026-12-01T03:05:00-05:00' }))),
|
||||
},
|
||||
{
|
||||
id: 'doubtful-25-hour-flight', sender: 'airline-a.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld(flightRes({ ref: 'BAD002', name: 'Erika Mustermann', ...out1, arr: '2026-12-02T15:15:00-05:00' }))),
|
||||
},
|
||||
{
|
||||
id: 'doubtful-years-away', sender: 'stays.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld({ ...hotel, reservationNumber: 'BAD003', checkinDate: '2029-05-01', checkoutDate: '2029-05-03' })),
|
||||
},
|
||||
{
|
||||
id: 'doubtful-checkout-before-checkin', sender: 'stays.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld({ ...hotel, reservationNumber: 'BAD004', checkinDate: '2026-12-06', checkoutDate: '2026-12-04' })),
|
||||
},
|
||||
{
|
||||
id: 'doubtful-rental-missing-times', sender: 'cars.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld({ '@context': C, '@type': 'RentalCarReservation', reservationNumber: 'BAD005', reservationFor: { '@type': 'Car', name: 'Golf' }, pickupTime: '2026-12-04', dropoffTime: '2026-12-07' })),
|
||||
},
|
||||
{
|
||||
id: 'doubtful-bare-date-departure', sender: 'airline-a.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld(flightRes({ ref: 'BAD006', name: 'Erika Mustermann', ...out1, dep: '2026-12-01' }))),
|
||||
},
|
||||
{
|
||||
id: 'doubtful-google-example', sender: 'newsletter.example', source: 'synthetic', mailDate: '2026-12-20T09:00:00Z',
|
||||
html: page(ld(flightRes({ ref: 'RXJ34P', name: 'Eva Green', airline: ['United', 'UA'], number: '110', seat: '9A', from: ['San Francisco Airport', 'SFO'], dep: '2027-03-04T20:15:00-08:00', to: ['John F. Kennedy International Airport', 'JFK'], arr: '2027-03-05T06:30:00-05:00' }))),
|
||||
},
|
||||
{
|
||||
id: 'doubtful-unknown-types-only', sender: 'newsletter.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld([{ '@context': C, '@type': 'PromotionCard', image: 'https://cdn.example.com/a.png' }, { '@context': C, '@type': 'DiscountOffer', description: '20% off' }])),
|
||||
},
|
||||
{
|
||||
id: 'doubtful-json-beyond-repair', sender: 'airline-a.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(script('{"@context": "http://schema.org", "@type": "FlightReservation", "reservationNumber": "BAD007", "reservationFor": {"@type": "Flight"')),
|
||||
},
|
||||
{
|
||||
id: 'doubtful-foreign-context', sender: 'airline-a.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld({ ...flightRes({ ref: 'BAD008', name: 'Erika Mustermann', ...out1 }), '@context': 'http://vocab.example.com/' })),
|
||||
},
|
||||
{
|
||||
id: 'doubtful-zone-mismatch', sender: 'airline-a.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld(flightRes({ ref: 'BAD009', name: 'Erika Mustermann', ...out1, dep: '2026-12-01T10:15:00+00:00', arr: '2026-12-01T13:05:00-05:00' }))),
|
||||
},
|
||||
{
|
||||
// One leg of a two-leg booking lacks its arrival: the whole booking goes.
|
||||
id: 'doubtful-one-bad-leg', sender: 'airline-a.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld([
|
||||
flightRes({ ref: 'BAD010', name: 'Erika Mustermann', ...out1 }),
|
||||
flightRes({ ref: 'BAD010', name: 'Erika Mustermann', ...back1, arr: '' }),
|
||||
])),
|
||||
},
|
||||
{
|
||||
// The same leg twice with different times: the parts disagree.
|
||||
id: 'doubtful-conflicting-parts', sender: 'airline-a.example', source: 'synthetic', mailDate: MAIL_DATE,
|
||||
html: page(ld([
|
||||
flightRes({ ref: 'BAD011', name: 'Erika Mustermann', ...out1 }),
|
||||
flightRes({ ref: 'BAD011', name: 'Max Mustermann', ...out1, arr: '2026-12-01T14:05:00-05:00' }),
|
||||
])),
|
||||
},
|
||||
];
|
||||
@@ -0,0 +1,189 @@
|
||||
// Mirrors webmail lib/structured-mail/__tests__/format.test.ts at 972e5f621; keep the two in step.
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
||||
import {
|
||||
formatAddress,
|
||||
formatMoment,
|
||||
formatMomentDate,
|
||||
formatMomentTime,
|
||||
formatMoney,
|
||||
isOtherZone,
|
||||
isWebLink,
|
||||
legPlaceLabel,
|
||||
legService,
|
||||
momentOffsetMinutes,
|
||||
nightsBetween,
|
||||
routeTitle,
|
||||
shipmentItems,
|
||||
summarizeItems,
|
||||
} from '../format';
|
||||
import type { Moment, Shipment, TransportBooking, TransportLeg } from '../types';
|
||||
|
||||
const NOW = new Date('2026-10-30T12:00:00Z');
|
||||
const en = { locale: 'en-US', hour12: false, now: NOW } as const;
|
||||
|
||||
// SFO 20:15 −08:00 = 04:15Z the next day; JFK 06:30 −05:00.
|
||||
const SFO_DEP: Moment = { date: '2026-12-03', time: '20:15:00', offsetMinutes: -480, utc: '2026-12-04T04:15:00Z', zone: 'America/Los_Angeles' };
|
||||
const BERLIN: Moment = { date: '2026-12-01', time: '10:15:00', offsetMinutes: 60, utc: '2026-12-01T09:15:00Z', zone: 'Europe/Berlin' };
|
||||
// A floating time the airport table resolved: no offset in the mail, utc from the zone.
|
||||
const NRT_RESOLVED: Moment = { date: '2026-12-02', time: '17:40:00', utc: '2026-12-02T08:40:00Z', zone: 'Asia/Tokyo' };
|
||||
|
||||
const originalTz = process.env.TZ;
|
||||
afterEach(() => {
|
||||
process.env.TZ = originalTz;
|
||||
});
|
||||
|
||||
describe('wall-clock formatting', () => {
|
||||
for (const tz of ['UTC', 'America/Los_Angeles', 'Europe/Berlin', 'Asia/Tokyo', 'Pacific/Kiritimati']) {
|
||||
it(`keeps 20:15 −08:00 at 20:15 on 3 Dec with TZ=${tz}`, () => {
|
||||
process.env.TZ = tz;
|
||||
expect(formatMomentTime(SFO_DEP, en)).toBe('20:15');
|
||||
expect(formatMomentDate(SFO_DEP, en)).toBe('Thu, Dec 3');
|
||||
expect(formatMoment(SFO_DEP, en)).toBe('Thu, Dec 3, 20:15');
|
||||
});
|
||||
}
|
||||
|
||||
it('shows the clock the mail gave whatever the offset', () => {
|
||||
const tokyo: Moment = { date: '2026-12-02', time: '07:05:00', offsetMinutes: 540, utc: '2026-12-01T22:05:00Z' };
|
||||
const kolkata: Moment = { date: '2026-12-02', time: '23:50:00', offsetMinutes: 330, utc: '2026-12-02T18:20:00Z' };
|
||||
expect(formatMoment(tokyo, en)).toBe('Wed, Dec 2, 07:05');
|
||||
expect(formatMoment(kolkata, en)).toBe('Wed, Dec 2, 23:50');
|
||||
});
|
||||
|
||||
it('formats a date-only moment without a time', () => {
|
||||
const d: Moment = { date: '2027-06-01' };
|
||||
expect(formatMomentTime(d, en)).toBeUndefined();
|
||||
// Another year than "now": the year is shown.
|
||||
expect(formatMoment(d, en)).toBe('Tue, Jun 1, 2027');
|
||||
expect(formatMomentDate({ date: '2026-11-03' }, en)).toBe('Tue, Nov 3');
|
||||
});
|
||||
|
||||
it('follows the UI locale and the 12-hour setting', () => {
|
||||
expect(formatMoment(BERLIN, { locale: 'de', hour12: false, now: NOW })).toBe('Di., 1. Dez., 10:15');
|
||||
expect(formatMomentTime(SFO_DEP, { locale: 'en-US', hour12: true, now: NOW })).toBe('8:15 PM');
|
||||
});
|
||||
|
||||
it('accepts HH:mm and falls back to the raw value for a malformed moment', () => {
|
||||
expect(formatMomentTime({ date: '2026-12-01', time: '09:05' }, en)).toBe('09:05');
|
||||
expect(formatMomentDate({ date: 'soon' }, en)).toBe('soon');
|
||||
});
|
||||
});
|
||||
|
||||
describe('local-time hint', () => {
|
||||
it('knows the offset of a moment from the mail or from its resolved zone', () => {
|
||||
expect(momentOffsetMinutes(SFO_DEP)).toBe(-480);
|
||||
expect(momentOffsetMinutes(NRT_RESOLVED)).toBe(540);
|
||||
expect(momentOffsetMinutes({ date: '2026-12-01', time: '10:00:00' })).toBeUndefined();
|
||||
expect(momentOffsetMinutes({ date: '2026-12-01' })).toBeUndefined();
|
||||
});
|
||||
|
||||
it('marks a time whose place keeps another clock than the reader', () => {
|
||||
expect(isOtherZone(SFO_DEP, 'Europe/Berlin')).toBe(true);
|
||||
expect(isOtherZone(SFO_DEP, 'America/Los_Angeles')).toBe(false);
|
||||
expect(isOtherZone(NRT_RESOLVED, 'Asia/Tokyo')).toBe(false);
|
||||
expect(isOtherZone(NRT_RESOLVED, 'Europe/Berlin')).toBe(true);
|
||||
});
|
||||
|
||||
it('compares at the instant, so DST on the reader side counts', () => {
|
||||
// 10:15 +01:00 in December: Berlin is +01:00 then, London +00:00.
|
||||
expect(isOtherZone(BERLIN, 'Europe/Berlin')).toBe(false);
|
||||
expect(isOtherZone(BERLIN, 'Europe/London')).toBe(true);
|
||||
// A +02:00 time in July matches Berlin's summer time.
|
||||
expect(isOtherZone({ date: '2026-07-18', time: '14:00:00', offsetMinutes: 120, utc: '2026-07-18T12:00:00Z' }, 'Europe/Berlin')).toBe(false);
|
||||
expect(isOtherZone(BERLIN, 'x', () => 60)).toBe(false);
|
||||
expect(isOtherZone(BERLIN, 'x', () => 0)).toBe(true);
|
||||
});
|
||||
|
||||
it('never marks a floating or date-only moment', () => {
|
||||
expect(isOtherZone({ date: '2026-12-01', time: '10:00:00', zone: null }, 'Asia/Tokyo')).toBe(false);
|
||||
expect(isOtherZone({ date: '2026-12-01' }, 'Asia/Tokyo')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('card text helpers', () => {
|
||||
const leg = (from: string, to: string, extra: Partial<TransportLeg> = {}): TransportLeg => ({
|
||||
carrier: 'Lufthansa', carrierCode: 'LH', number: '400',
|
||||
departure: { place: { code: from, name: `${from} Airport` }, at: BERLIN },
|
||||
arrival: { place: { code: to }, at: SFO_DEP },
|
||||
seats: [], ...extra,
|
||||
});
|
||||
const flight = (legs: TransportLeg[]): TransportBooking => ({ key: 'k', kind: 'flight', passengers: [], links: {}, source: 'json-ld', legs });
|
||||
|
||||
it('titles a flight with airport codes, chaining connecting legs', () => {
|
||||
expect(routeTitle(flight([leg('FRA', 'JFK')]))).toBe('FRA → JFK');
|
||||
expect(routeTitle(flight([leg('FRA', 'JFK'), leg('JFK', 'FRA')]))).toBe('FRA → JFK → FRA');
|
||||
expect(routeTitle(flight([leg('FRA', 'JFK'), leg('BOS', 'FRA')]))).toBe('FRA → JFK, BOS → FRA');
|
||||
});
|
||||
|
||||
it('titles a train with towns when every stop has one, else station names', () => {
|
||||
const station = (name: string, locality?: string) => ({ name, ...(locality ? { address: { locality } } : {}) });
|
||||
const train = (a: ReturnType<typeof station>, b: ReturnType<typeof station>): TransportBooking => ({
|
||||
key: 'k', kind: 'train', passengers: [], links: {}, source: 'json-ld',
|
||||
legs: [{ departure: { place: a, at: BERLIN }, arrival: { place: b, at: BERLIN }, seats: [] }],
|
||||
});
|
||||
expect(routeTitle(train(station('Hamburg Hbf', 'Hamburg'), station('München Hbf', 'München')))).toBe('Hamburg → München');
|
||||
expect(routeTitle(train(station('Hamburg Hbf', 'Hamburg'), station('München Hbf')))).toBe('Hamburg Hbf → München Hbf');
|
||||
expect(routeTitle(train(station('Berlin Hbf', 'Berlin'), station('Berlin Südkreuz', 'Berlin')))).toBe('Berlin Hbf → Berlin Südkreuz');
|
||||
});
|
||||
|
||||
it('labels airports and services', () => {
|
||||
expect(legPlaceLabel({ code: 'FRA', name: 'Frankfurt Airport' }, 'flight')).toBe('Frankfurt Airport (FRA)');
|
||||
expect(legPlaceLabel({ code: 'FRA' }, 'flight')).toBe('FRA');
|
||||
expect(legPlaceLabel({ code: 'JFK', name: 'New York JFK' }, 'flight')).toBe('New York JFK');
|
||||
expect(legPlaceLabel({ name: 'Hamburg Hbf', code: 'X' }, 'train')).toBe('Hamburg Hbf');
|
||||
expect(legService(leg('FRA', 'JFK'))).toBe('Lufthansa LH 400');
|
||||
expect(legService({ ...leg('FRA', 'JFK'), carrierCode: undefined, carrier: 'Flix', number: 'FLX30' })).toBe('Flix FLX30');
|
||||
});
|
||||
|
||||
it('counts nights by date', () => {
|
||||
expect(nightsBetween({ date: '2026-12-04', time: '15:00:00' }, { date: '2026-12-06', time: '11:00:00' })).toBe(2);
|
||||
expect(nightsBetween({ date: '2027-03-27' }, { date: '2027-03-29' })).toBe(2);
|
||||
expect(nightsBetween({ date: '2027-03-29' }, { date: '2027-03-29' })).toBeUndefined();
|
||||
});
|
||||
|
||||
it('formats money in the UI locale with the card currency', () => {
|
||||
expect(formatMoney({ amount: '129.90', currency: 'EUR' }, 'en-US')).toBe('€129.90');
|
||||
expect(formatMoney({ amount: '1299.00', currency: 'EUR' }, 'de').replace(/\s/g, ' ')).toBe('1.299,00 €');
|
||||
expect(formatMoney({ amount: '25.99', currency: 'USD' }, 'en-US')).toBe('$25.99');
|
||||
expect(formatMoney({ amount: '5.00', currency: 'XX1' }, 'en-US')).toBe('5.00 XX1');
|
||||
});
|
||||
|
||||
it('writes an address on one line without repeating a combined street', () => {
|
||||
expect(formatAddress({ street: 'Marktplatz 1', postalCode: '10178', locality: 'Berlin', country: 'DE' }, 'en')).toBe('Marktplatz 1, 10178 Berlin, Germany');
|
||||
expect(formatAddress({ street: 'Kurfürstendamm 18, 10719 Berlin' }, 'en')).toBe('Kurfürstendamm 18, 10719 Berlin');
|
||||
expect(formatAddress({}, 'en')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('lists the first three items and counts pieces', () => {
|
||||
const items = [{ name: 'A', quantity: 2 }, { name: 'B' }, { name: 'C' }, { name: 'D' }, { name: 'E', quantity: 3 }];
|
||||
const s = summarizeItems(items);
|
||||
expect(s.shown.map((i) => i.name)).toEqual(['A', 'B', 'C']);
|
||||
expect(s.more).toBe(2);
|
||||
expect(s.count).toBe(8);
|
||||
});
|
||||
|
||||
it('takes item images from the parcel when the order has none', () => {
|
||||
const shipment: Shipment = {
|
||||
kind: 'shipment', key: 'k', items: [{ name: 'Shoes', quantity: 1 }], source: 'json-ld',
|
||||
parcels: [{ items: [{ name: 'Shoes', image: { url: 'https://img.example.com/1.jpg', host: 'img.example.com' } }] }],
|
||||
};
|
||||
expect(shipmentItems(shipment)[0].image?.host).toBe('img.example.com');
|
||||
expect(shipmentItems({ ...shipment, items: [] }).map((i) => i.name)).toEqual(['Shoes']);
|
||||
});
|
||||
|
||||
it('accepts only http(s) links', () => {
|
||||
expect(isWebLink({ url: 'https://dhl.de/x' })).toBe(true);
|
||||
expect(isWebLink({ url: 'http://dhl.de/x' })).toBe(true);
|
||||
expect(isWebLink({ url: 'javascript:alert(1)' })).toBe(false);
|
||||
expect(isWebLink({ url: 'data:text/html,x' })).toBe(false);
|
||||
expect(isWebLink({ url: 'not a url' })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatting does not depend on the process zone', () => {
|
||||
beforeEach(() => {
|
||||
process.env.TZ = 'Pacific/Honolulu';
|
||||
});
|
||||
it('keeps a Berlin morning a Berlin morning in Honolulu', () => {
|
||||
expect(formatMoment(BERLIN, en)).toBe('Tue, Dec 1, 10:15');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
// Mirrors webmail lib/structured-mail/__tests__/json-repair.test.ts at 76b0c2fba; keep the two in step.
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { parseLenientJson } from '../json-repair';
|
||||
|
||||
const A = { '@type': 'FlightReservation', reservationNumber: 'A1' };
|
||||
const B = { '@type': 'FlightReservation', reservationNumber: 'B2' };
|
||||
|
||||
describe('parseLenientJson', () => {
|
||||
it('parses valid JSON unchanged', () => {
|
||||
expect(parseLenientJson(JSON.stringify([A, B]))).toEqual([A, B]);
|
||||
});
|
||||
|
||||
it('adds the commas Eurowings leaves out between array objects', () => {
|
||||
expect(parseLenientJson(`[${JSON.stringify(A)}\n${JSON.stringify(B)}]`)).toEqual([A, B]);
|
||||
expect(parseLenientJson(`[${JSON.stringify(A)}${JSON.stringify(B)}]`)).toEqual([A, B]);
|
||||
});
|
||||
|
||||
it('adds the brackets Volotea leaves out, with or without commas', () => {
|
||||
expect(parseLenientJson(`${JSON.stringify(A)},\n${JSON.stringify(B)}`)).toEqual([A, B]);
|
||||
expect(parseLenientJson(`${JSON.stringify(A)}\n${JSON.stringify(B)}`)).toEqual([A, B]);
|
||||
});
|
||||
|
||||
it('drops the trailing commas Eventbrite writes', () => {
|
||||
expect(parseLenientJson('{"@type": "EventReservation", "reservationNumber": "E1", }')).toEqual({ '@type': 'EventReservation', reservationNumber: 'E1' });
|
||||
expect(parseLenientJson('[{"a": [1, 2, ], }, ]')).toEqual([{ a: [1, 2] }]);
|
||||
});
|
||||
|
||||
it('decodes the XML entities Airbnb encodes its JSON with', () => {
|
||||
const encoded = JSON.stringify(A).replace(/"/g, '"');
|
||||
expect(parseLenientJson(encoded)).toEqual(A);
|
||||
expect(parseLenientJson('{"name": "Café & Bar"}')).toEqual({ name: 'Café & Bar' });
|
||||
});
|
||||
|
||||
it('strips comment and CDATA wrappers and escapes raw newlines in strings', () => {
|
||||
expect(parseLenientJson(`<!--\n${JSON.stringify(A)}\n-->`)).toEqual(A);
|
||||
expect(parseLenientJson(`<![CDATA[${JSON.stringify(A)}]]>`)).toEqual(A);
|
||||
expect(parseLenientJson('{"name": "two\nlines"}')).toEqual({ name: 'two\nlines' });
|
||||
});
|
||||
|
||||
it('leaves commas and braces inside strings alone', () => {
|
||||
expect(parseLenientJson('{"name": "a, } { b", }')).toEqual({ name: 'a, } { b' });
|
||||
});
|
||||
|
||||
it('gives up on input it cannot repair', () => {
|
||||
expect(parseLenientJson('{"a": ')).toBeUndefined();
|
||||
expect(parseLenientJson('not json at all')).toBeUndefined();
|
||||
expect(parseLenientJson('')).toBeUndefined();
|
||||
expect(parseLenientJson(`[${'x'.repeat(200_001)}]`)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
// Mirrors webmail lib/structured-mail/__tests__/merge.test.ts at fa324170a; keep the two in step.
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { readStructuredMail } from '..';
|
||||
import type { Shipment, TransportBooking } from '../types';
|
||||
|
||||
const MAIL = { mailDate: new Date('2026-10-30T09:00:00Z') };
|
||||
const C = 'http://schema.org';
|
||||
const page = (...blocks: unknown[]) => `<html><head>${blocks.map((b) => `<script type="application/ld+json">${JSON.stringify(b)}</script>`).join('')}</head><body></body></html>`;
|
||||
|
||||
function leg(name: string, number: string, dep: string, arr: string, status = 'ReservationConfirmed', seat?: string) {
|
||||
return {
|
||||
'@context': C, '@type': 'FlightReservation', reservationNumber: 'QX7K2M', underName: { name }, reservationStatus: status,
|
||||
...(seat ? { airplaneSeat: seat } : {}),
|
||||
reservationFor: {
|
||||
'@type': 'Flight', flightNumber: number, airline: { name: 'Lufthansa', iataCode: 'LH' },
|
||||
departureAirport: { iataCode: number === '400' ? 'FRA' : 'JFK' }, departureTime: dep,
|
||||
arrivalAirport: { iataCode: number === '400' ? 'JFK' : 'FRA' }, arrivalTime: arr,
|
||||
},
|
||||
};
|
||||
}
|
||||
const OUT: [string, string] = ['2026-12-01T10:15:00+01:00', '2026-12-01T13:05:00-05:00'];
|
||||
const BACK: [string, string] = ['2026-12-08T17:30:00-05:00', '2026-12-09T07:20:00+01:00'];
|
||||
|
||||
describe('merging', () => {
|
||||
it('folds two passengers on two legs into one booking with two legs and two names', () => {
|
||||
const cards = readStructuredMail(page([
|
||||
leg('Erika Mustermann', '400', ...OUT, undefined, '12A'), leg('Max Mustermann', '400', ...OUT, undefined, '12B'),
|
||||
leg('Erika Mustermann', '401', ...BACK), leg('Max Mustermann', '401', ...BACK),
|
||||
]), MAIL) as TransportBooking[];
|
||||
expect(cards).toHaveLength(1);
|
||||
expect(cards[0].passengers).toEqual(['Erika Mustermann', 'Max Mustermann']);
|
||||
expect(cards[0].legs.map((l) => l.number)).toEqual(['400', '401']);
|
||||
expect(cards[0].legs[0].seats).toEqual([{ passenger: 'Erika Mustermann', seat: '12A' }, { passenger: 'Max Mustermann', seat: '12B' }]);
|
||||
});
|
||||
|
||||
it('sorts legs by departure even when the mail lists them backwards', () => {
|
||||
const [card] = readStructuredMail(page([leg('Erika Mustermann', '401', ...BACK), leg('Erika Mustermann', '400', ...OUT)]), MAIL) as TransportBooking[];
|
||||
expect(card.legs.map((l) => l.number)).toEqual(['400', '401']);
|
||||
});
|
||||
|
||||
it('keeps a leg live when only one of its passengers is cancelled', () => {
|
||||
const [card] = readStructuredMail(page([
|
||||
leg('Erika Mustermann', '400', ...OUT), leg('Max Mustermann', '400', ...OUT, 'ReservationCancelled'),
|
||||
]), MAIL) as TransportBooking[];
|
||||
expect(card.legs[0].status).toBe('confirmed');
|
||||
expect(card.status).toBe('confirmed');
|
||||
});
|
||||
|
||||
it('cancels a leg, and the booking, only when every part is cancelled', () => {
|
||||
const [partly] = readStructuredMail(page([
|
||||
leg('Erika Mustermann', '400', ...OUT, 'ReservationCancelled'), leg('Erika Mustermann', '401', ...BACK),
|
||||
]), MAIL) as TransportBooking[];
|
||||
expect(partly.legs.map((l) => l.status)).toEqual(['cancelled', 'confirmed']);
|
||||
expect(partly.status).toBe('confirmed');
|
||||
const [all] = readStructuredMail(page([
|
||||
leg('Erika Mustermann', '400', ...OUT, 'ReservationCancelled'), leg('Erika Mustermann', '401', ...BACK, 'Cancelled'),
|
||||
]), MAIL) as TransportBooking[];
|
||||
expect(all.status).toBe('cancelled');
|
||||
});
|
||||
|
||||
it('drops the booking when two parts disagree about one leg, or one part is invalid', () => {
|
||||
expect(readStructuredMail(page([leg('Erika Mustermann', '400', ...OUT), leg('Max Mustermann', '400', OUT[0], '2026-12-01T14:05:00-05:00')]), MAIL)).toEqual([]);
|
||||
expect(readStructuredMail(page([leg('Erika Mustermann', '400', ...OUT), leg('Erika Mustermann', '401', BACK[0], '')]), MAIL)).toEqual([]);
|
||||
});
|
||||
|
||||
it('collapses the same booking given as JSON-LD and as microdata', () => {
|
||||
const microdata = `<div itemscope itemtype="http://schema.org/Order"><meta itemprop="orderNumber" content="SO-1"><div itemprop="merchant" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="Shop"></div></div>`;
|
||||
const html = page({ '@context': C, '@type': 'Order', orderNumber: 'SO-1', merchant: 'Shop', price: 10, priceCurrency: 'EUR' }).replace('<body></body>', `<body>${microdata}</body>`);
|
||||
const cards = readStructuredMail(html, MAIL) as Shipment[];
|
||||
expect(cards).toHaveLength(1);
|
||||
expect(cards[0].total).toEqual({ amount: '10.00', currency: 'EUR' });
|
||||
});
|
||||
|
||||
it('makes one card of an Order and a ParcelDelivery for the same order number', () => {
|
||||
const cards = readStructuredMail(page(
|
||||
{ '@context': C, '@type': 'Order', orderNumber: 'SO-1', merchant: 'Shop', orderStatus: 'OrderProcessing', acceptedOffer: { itemOffered: { name: 'Schuhe' } } },
|
||||
{ '@context': C, '@type': 'ParcelDelivery', trackingNumber: 'T1', carrier: 'DHL', partOfOrder: { '@type': 'Order', orderNumber: 'SO-1', orderStatus: 'OrderInTransit' } },
|
||||
{ '@context': C, '@type': 'ParcelDelivery', trackingNumber: 'T2', carrier: 'DHL', partOfOrder: { '@type': 'Order', orderNumber: 'SO-1' } },
|
||||
), MAIL) as Shipment[];
|
||||
expect(cards).toHaveLength(1);
|
||||
expect(cards[0]).toMatchObject({ orderNumber: 'SO-1', merchant: 'Shop', status: 'in_transit', items: [{ name: 'Schuhe' }] });
|
||||
expect(cards[0].parcels.map((p) => p.trackingNumber)).toEqual(['T1', 'T2']);
|
||||
});
|
||||
|
||||
it('keeps orders with different numbers apart', () => {
|
||||
const cards = readStructuredMail(page(
|
||||
{ '@context': C, '@type': 'Order', orderNumber: 'SO-1', merchant: 'Shop' },
|
||||
{ '@context': C, '@type': 'Order', orderNumber: 'SO-2', merchant: 'Shop' },
|
||||
), MAIL);
|
||||
expect(cards).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,203 @@
|
||||
// Mirrors webmail lib/structured-mail/__tests__/normalize.test.ts at 916434924; keep the two in step.
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { normalizeItems } from '../normalize';
|
||||
import type { LodgingBooking, Shipment, TransportBooking } from '../types';
|
||||
import { address, amount, lenientDate, link, moment, orderStatus, reservationStatus } from '../values';
|
||||
|
||||
const ctx = 'http://schema.org';
|
||||
const norm = (...items: unknown[]) => normalizeItems(items.map((item) => ({ item, source: 'json-ld' as const })));
|
||||
|
||||
describe('value readers', () => {
|
||||
it('reads ISO times with every offset form and computes the instant', () => {
|
||||
expect(moment('2027-03-04T20:15:00-08:00')).toEqual({ date: '2027-03-04', time: '20:15:00', offsetMinutes: -480, utc: '2027-03-05T04:15:00Z' });
|
||||
expect(moment('2024-06-04T11:44:00+0200')).toEqual({ date: '2024-06-04', time: '11:44:00', offsetMinutes: 120, utc: '2024-06-04T09:44:00Z' });
|
||||
expect(moment('2026-01-01T10:00+05')).toMatchObject({ offsetMinutes: 300, utc: '2026-01-01T05:00:00Z' });
|
||||
expect(moment('2026-01-01T10:00:00.123Z')).toMatchObject({ time: '10:00:00', offsetMinutes: 0, utc: '2026-01-01T10:00:00Z' });
|
||||
expect(moment('2026-01-01T10:00:00')).toEqual({ date: '2026-01-01', time: '10:00:00' });
|
||||
expect(moment('2026-01-01')).toEqual({ date: '2026-01-01' });
|
||||
});
|
||||
|
||||
it('refuses broken times instead of guessing', () => {
|
||||
expect(moment('2024-10-13CEST03:10:23')).toBeUndefined();
|
||||
expect(moment('2026-02-30T10:00:00Z')).toBeUndefined();
|
||||
expect(moment('2026-01-01T25:00:00Z')).toBeUndefined();
|
||||
expect(moment('2026-01-01T10:00:00+15:00')).toBeUndefined();
|
||||
expect(moment('next Friday')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('keeps only the date of a broken value where a date is enough', () => {
|
||||
expect(lenientDate('2024-10-13CEST03:10:23')).toEqual({ date: '2024-10-13' });
|
||||
expect(lenientDate('2026-03-12T12:00:00-08:00')).toEqual({ date: '2026-03-12' });
|
||||
expect(lenientDate('20241013')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('reads both forms of the status enums over http and https', () => {
|
||||
expect(reservationStatus('http://schema.org/ReservationConfirmed')).toBe('confirmed');
|
||||
expect(reservationStatus('https://schema.org/Confirmed')).toBe('confirmed');
|
||||
expect(reservationStatus('ReservationCancelled')).toBe('cancelled');
|
||||
expect(reservationStatus(['', 'http://schema.org/Cancelled'])).toBe('cancelled');
|
||||
expect(reservationStatus('http://schema.org/Bogus')).toBeUndefined();
|
||||
expect(orderStatus(['', 'http://schema.org/OrderProcessing'])).toBe('processing');
|
||||
expect(orderStatus('http://schema.org/OrderStatus/InTransit')).toBe('in_transit');
|
||||
expect(orderStatus('OrderDelivered')).toBe('delivered');
|
||||
expect(orderStatus('Processing')).toBe('processing');
|
||||
});
|
||||
|
||||
it('reads prices in dot and German notation and refuses ambiguous ones', () => {
|
||||
expect(amount(25.99)).toBe('25.99');
|
||||
expect(amount('14.9700')).toBe('14.97');
|
||||
expect(amount('399,00 €')).toBe('399.00');
|
||||
expect(amount('1.299,00 EUR')).toBe('1299.00');
|
||||
expect(amount('$1,299.50')).toBe('1299.50');
|
||||
expect(amount('1,299')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('keeps only http(s) links with a real host', () => {
|
||||
expect(link('https://www.fedex.com/track/1')).toEqual({ url: 'https://www.fedex.com/track/1', host: 'fedex.com' });
|
||||
expect(link({ '@type': 'ViewAction', target: 'http://shop.example.com/o/1' })?.host).toBe('shop.example.com');
|
||||
expect(link('http://default/buchung/VK4XKD')).toBeUndefined();
|
||||
expect(link('javascript:alert(1)')).toBeUndefined();
|
||||
expect(link('https://user:pw@example.com/')).toBeUndefined();
|
||||
expect(link('http://192.168.1.1/x')).toBeUndefined();
|
||||
expect(link('http://localhost:3000/x')).toBeUndefined();
|
||||
expect(link('/relative')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('reads addresses given as objects, strings and country objects', () => {
|
||||
expect(address('Unter den Linden 77, Berlin')).toEqual({ street: 'Unter den Linden 77, Berlin' });
|
||||
expect(address({ streetAddress: 'Koppenstr. 3', addressLocality: 'Berlin', postalCode: '10243', addressCountry: 'de', addressRegion: '' }))
|
||||
.toEqual({ street: 'Koppenstr. 3', locality: 'Berlin', postalCode: '10243', country: 'DE' });
|
||||
expect(address({ addressLocality: 'Paris', addressCountry: { '@type': 'Country', name: 'France' } }))
|
||||
.toEqual({ locality: 'Paris', countryName: 'France' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeItems', () => {
|
||||
const flight = {
|
||||
'@context': ctx, '@type': 'FlightReservation', reservationNumber: 'QX7K2M',
|
||||
reservationStatus: 'http://schema.org/ReservationConfirmed',
|
||||
underName: { '@type': 'Person', name: 'Erika Mustermann' }, airplaneSeat: '12C',
|
||||
checkinUrl: 'https://www.lufthansa.example.com/checkin',
|
||||
reservationFor: {
|
||||
'@type': 'Flight', flightNumber: 'LH400',
|
||||
airline: { '@type': 'Airline', name: 'Lufthansa', iataCode: 'LH' },
|
||||
departureAirport: { '@type': 'Airport', name: 'Frankfurt', iataCode: 'fra' }, departureTime: '2026-12-01T10:15:00+01:00',
|
||||
arrivalAirport: { '@type': 'Airport', name: 'New York JFK', iataCode: 'JFK' }, arrivalTime: '2026-12-01T13:05:00-05:00',
|
||||
},
|
||||
};
|
||||
|
||||
it('reads a flight reservation, splitting the airline code off the flight number', () => {
|
||||
const [card] = norm(flight) as TransportBooking[];
|
||||
expect(card).toEqual({
|
||||
key: 'flight|ref:qx7k2m', kind: 'flight', reference: 'QX7K2M', status: 'confirmed', passengers: ['Erika Mustermann'],
|
||||
provider: 'Lufthansa', links: { checkin: { url: 'https://www.lufthansa.example.com/checkin', host: 'lufthansa.example.com' } },
|
||||
source: 'json-ld',
|
||||
legs: [{
|
||||
carrier: 'Lufthansa', carrierCode: 'LH', number: '400',
|
||||
departure: { place: { name: 'Frankfurt', code: 'FRA' }, at: { date: '2026-12-01', time: '10:15:00', offsetMinutes: 60, utc: '2026-12-01T09:15:00Z' } },
|
||||
arrival: { place: { name: 'New York JFK', code: 'JFK' }, at: { date: '2026-12-01', time: '13:05:00', offsetMinutes: -300, utc: '2026-12-01T18:05:00Z' } },
|
||||
seats: [{ passenger: 'Erika Mustermann', seat: '12C' }], status: 'confirmed',
|
||||
}],
|
||||
});
|
||||
});
|
||||
|
||||
it('flattens arrays, @graph and reservationFor arrays', () => {
|
||||
const two = { ...flight, reservationFor: [flight.reservationFor, { ...flight.reservationFor, flightNumber: '401' }] };
|
||||
expect(norm([flight, flight]).length).toBe(2);
|
||||
expect(norm({ '@context': ctx, '@graph': [flight, flight] }).length).toBe(2);
|
||||
const legs = norm(two) as TransportBooking[];
|
||||
expect(legs.map((c) => c.legs[0].number)).toEqual(['400', '401']);
|
||||
});
|
||||
|
||||
it('accepts https contexts, @vocab and several @types, and drops foreign or missing contexts', () => {
|
||||
expect(norm({ ...flight, '@context': 'https://schema.org/' })).toHaveLength(1);
|
||||
expect(norm({ ...flight, '@context': { '@vocab': 'http://schema.org/' } })).toHaveLength(1);
|
||||
expect(norm({ ...flight, '@type': ['Thing', 'FlightReservation'] })).toHaveLength(1);
|
||||
expect(norm({ ...flight, '@context': 'http://example.com' })).toHaveLength(0);
|
||||
const { '@context': _dropped, ...noContext } = flight;
|
||||
expect(norm(noContext)).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('reads bus trips the way FlixBus writes them', () => {
|
||||
const [card] = norm({
|
||||
'@context': ctx, '@type': 'BusReservation', reservationNumber: 3165152579,
|
||||
underName: { '@type': 'Person', name: 'Max Mustermann' },
|
||||
reservationFor: {
|
||||
'@type': 'BusTrip', busNumber: 'FLX30', busName: 'Berlin Ostbahnhof', busCompany: { '@type': 'Organization', name: 'Flix' },
|
||||
departureBusStop: { '@type': 'BusStop', name: 'Hannover Hauptbahnhof', address: { '@type': 'PostalAddress', addressLocality: 'Hannover', addressCountry: 'DE' } },
|
||||
departureTime: '2024-06-04T11:44:00+0200',
|
||||
arrivalBusStop: { '@type': 'BusStop', name: 'Berlin Ostbahnhof' }, arrivalTime: '2024-06-04T13:44:00+0200',
|
||||
},
|
||||
reservedTicket: { '@type': 'Ticket', ticketedSeat: { '@type': 'Seat', seatingType: 'Coach', seatNumber: '6D' } },
|
||||
}) as TransportBooking[];
|
||||
expect(card.reference).toBe('3165152579');
|
||||
expect(card.legs[0]).toMatchObject({ carrier: 'Flix', number: 'FLX30', seats: [{ passenger: 'Max Mustermann', seat: '6D' }] });
|
||||
expect(card.legs[0].departure.place).toEqual({ name: 'Hannover Hauptbahnhof', address: { locality: 'Hannover', country: 'DE' } });
|
||||
expect(JSON.stringify(card)).not.toContain('busName');
|
||||
});
|
||||
|
||||
it('reads hotels from Hotel subtypes with legacy names', () => {
|
||||
const [card] = norm({
|
||||
'@context': ctx, '@type': 'LodgingReservation', reservationId: 'h-1',
|
||||
reservationFor: { '@type': 'Hotel', name: 'Hotel Adlon', address: 'Unter den Linden 77, 10117 Berlin', telephone: '+49 30 1' },
|
||||
checkinTime: '2026-12-04T15:00:00+01:00', checkoutTime: '2026-12-06T12:00:00+01:00', Url: 'https://hotel.example.com/b/h-1',
|
||||
}) as LodgingBooking[];
|
||||
expect(card).toMatchObject({
|
||||
kind: 'lodging', reference: 'h-1', name: 'Hotel Adlon', phone: '+49 30 1', address: { street: 'Unter den Linden 77, 10117 Berlin' },
|
||||
checkin: { date: '2026-12-04', time: '15:00:00' }, checkout: { date: '2026-12-06', time: '12:00:00' },
|
||||
links: { manage: { host: 'hotel.example.com' } },
|
||||
});
|
||||
});
|
||||
|
||||
it('reads restaurants, events with a date-only end, and rental cars', () => {
|
||||
const [restaurant, event, car] = norm(
|
||||
{ '@context': ctx, '@type': 'FoodEstablishmentReservation', reservationNumber: 'R9', partySize: '2', startTime: '2026-12-05T19:30:00+01:00', reservationFor: { '@type': 'Restaurant', name: 'Tim Raue' } },
|
||||
{ '@context': ctx, '@type': 'EventReservation', reservationNumber: 'E9', reservationFor: { '@type': 'MusicEvent', name: 'Konzert', startDate: '2026-12-05T20:00:00+01:00', endDate: '2026-12-05', location: { '@type': 'Place', name: 'Halle', address: { addressCountry: 'DE' } } } },
|
||||
{ '@context': ctx, '@type': 'RentalCarReservation', reservationNumber: 'C9', reservationFor: { '@type': 'Car', brand: { name: 'VW' }, model: 'Golf', rentalCompany: { name: 'Sixt' } }, pickupTime: '2026-12-04T09:00:00+01:00', dropoffTime: '2026-12-07T18:00:00+01:00', pickupLocation: { name: 'BER' }, dropoffLocation: 'BER' },
|
||||
);
|
||||
expect(restaurant).toMatchObject({ kind: 'restaurant', name: 'Tim Raue', partySize: 2, start: { time: '19:30:00' } });
|
||||
expect(event).toMatchObject({ kind: 'event', name: 'Konzert', end: { date: '2026-12-05', time: '23:59:59' }, venue: { name: 'Halle' } });
|
||||
expect(car).toMatchObject({ kind: 'rentalcar', company: 'Sixt', car: 'VW Golf', pickup: { place: { name: 'BER' } }, dropoff: { place: { name: 'BER' } } });
|
||||
});
|
||||
|
||||
it('reads orders with German prices and parcels with their order', () => {
|
||||
const [orderCard, parcelCard] = norm(
|
||||
{
|
||||
'@context': ctx, '@type': 'Order', merchant: { '@type': 'Organization', name: 'Kaufland.de' }, orderNumber: 'M1234AB',
|
||||
price: '399,00 €', priceCurrency: 'EUR', orderStatus: ['', 'http://schema.org/OrderProcessing'],
|
||||
acceptedOffer: { '@type': 'Offer', itemOffered: { '@type': 'Product', name: '3D-Drucker', image: 'https://media.example.com/p.jpg' }, eligibleQuantity: { value: '1' } },
|
||||
url: 'https://www.kaufland.de/account/orders',
|
||||
},
|
||||
{
|
||||
'@context': ctx, '@type': 'ParcelDelivery', carrier: 'DHL', trackingNumber: '0034', trackingUrl: 'https://www.dhl.de/t?id=0034',
|
||||
expectedArrivalUntil: '2024-10-13CEST03:10:23', itemShipped: [{ '@type': 'Product', name: 'A' }, 'B'],
|
||||
partOfOrder: { '@type': 'Order', orderNumber: 'M1234AB', merchant: 'Kaufland.de', orderStatus: 'http://schema.org/OrderInTransit' },
|
||||
},
|
||||
) as Shipment[];
|
||||
expect(orderCard).toEqual({
|
||||
kind: 'shipment', key: 'shipment|order:m1234ab', merchant: 'Kaufland.de', orderNumber: 'M1234AB', status: 'processing',
|
||||
items: [{ name: '3D-Drucker', quantity: 1, image: { url: 'https://media.example.com/p.jpg', host: 'media.example.com' } }],
|
||||
total: { amount: '399.00', currency: 'EUR' }, orderUrl: { url: 'https://www.kaufland.de/account/orders', host: 'kaufland.de' },
|
||||
parcels: [], source: 'json-ld',
|
||||
});
|
||||
expect(parcelCard).toMatchObject({
|
||||
key: 'shipment|order:m1234ab', merchant: 'Kaufland.de', status: 'in_transit',
|
||||
parcels: [{ carrier: 'DHL', trackingNumber: '0034', trackingUrl: { host: 'dhl.de' }, expectedBy: { date: '2024-10-13' }, items: [{ name: 'A' }, { name: 'B' }] }],
|
||||
});
|
||||
});
|
||||
|
||||
it('takes the expected arrival only from its latest bound', () => {
|
||||
const parcel = (extra: Record<string, unknown>) => norm({ '@context': ctx, '@type': 'ParcelDelivery', trackingNumber: '1', carrier: 'DHL', ...extra })[0] as Shipment;
|
||||
// The earliest possible delivery day is not an "arrives by" date.
|
||||
expect(parcel({ expectedArrivalFrom: '2026-11-03' }).parcels[0].expectedBy).toBeUndefined();
|
||||
expect(parcel({ expectedArrivalFrom: '2026-11-03', expectedArrivalUntil: '2026-11-05' }).parcels[0].expectedBy).toEqual({ date: '2026-11-05' });
|
||||
});
|
||||
|
||||
it('drops types it does not read', () => {
|
||||
expect(norm(
|
||||
{ '@context': ctx, '@type': 'PromotionCard', image: 'https://x.example.com/a.png' },
|
||||
{ '@context': ctx, '@type': 'EmailMessage', potentialAction: { '@type': 'ViewAction' } },
|
||||
{ '@context': ctx, '@type': 'Invoice', totalPaymentDue: 3 },
|
||||
)).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
// Mirrors webmail lib/structured-mail/__tests__/structured-mail-fixtures.test.ts at fa324170a; keep the two in step.
|
||||
/**
|
||||
* Every fixture through the whole pipeline (extract, normalise, validate,
|
||||
* merge, resolve zones), compared field by field with the reviewed cards in
|
||||
* expected-cards.json. Real samples are the 11 card-relevant mails from
|
||||
* local-data/schema examples, redacted; synthetic ones are self-written.
|
||||
* Doubtful fixtures must give no card at all.
|
||||
*/
|
||||
import { afterAll, describe, expect, it } from 'vitest';
|
||||
import { loadAirportZones, needsAirportZones, readStructuredMail, resolveCards, type StructuredCard } from '..';
|
||||
import expected from './fixtures/expected-cards.json';
|
||||
import real from './fixtures/real-mails.json';
|
||||
import { syntheticMails, type MailFixture } from './fixtures/synthetic-mails';
|
||||
|
||||
const fixtures: MailFixture[] = [...(real as MailFixture[]), ...syntheticMails];
|
||||
const expectedById = expected as Record<string, StructuredCard[]>;
|
||||
|
||||
async function cardsOf(fixture: MailFixture): Promise<StructuredCard[]> {
|
||||
const cards = readStructuredMail(fixture.html, { mailDate: new Date(fixture.mailDate) });
|
||||
return needsAirportZones(cards) ? resolveCards(cards, await loadAirportZones()) : resolveCards(cards);
|
||||
}
|
||||
|
||||
type Score = { mails: number; found: number; correct: number; wrong: number; none: number };
|
||||
const scores = new Map<string, Score>();
|
||||
|
||||
describe('structured mail fixtures', () => {
|
||||
it('has an expectation for every fixture and no stray ones', () => {
|
||||
expect(Object.keys(expectedById).sort()).toEqual(fixtures.map((f) => f.id).sort());
|
||||
});
|
||||
|
||||
it.each(fixtures.map((f) => [f.id, f] as const))('%s', async (_id, fixture) => {
|
||||
const cards = await cardsOf(fixture);
|
||||
const want = expectedById[fixture.id];
|
||||
const score = scores.get(fixture.sender) ?? { mails: 0, found: 0, correct: 0, wrong: 0, none: 0 };
|
||||
score.mails++;
|
||||
const equal = JSON.stringify(cards) === JSON.stringify(want);
|
||||
if (cards.length === 0) score.none++;
|
||||
else {
|
||||
score.found++;
|
||||
if (equal) score.correct++;
|
||||
else score.wrong++;
|
||||
}
|
||||
scores.set(fixture.sender, score);
|
||||
expect(cards).toEqual(want);
|
||||
});
|
||||
|
||||
it('gives nothing for every doubtful fixture', async () => {
|
||||
for (const fixture of fixtures.filter((f) => f.id.startsWith('doubtful-'))) {
|
||||
expect(await cardsOf(fixture), fixture.id).toEqual([]);
|
||||
}
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
const rows = [...scores.entries()].sort(([a], [b]) => a.localeCompare(b));
|
||||
const lines = rows.map(([sender, s]) => `${sender.padEnd(28)} mails ${s.mails} found ${s.found} correct ${s.correct} wrong ${s.wrong} none ${s.none}`);
|
||||
const total = rows.reduce((t, [, s]) => ({ found: t.found + s.found, correct: t.correct + s.correct, wrong: t.wrong + s.wrong }), { found: 0, correct: 0, wrong: 0 });
|
||||
console.log(`structured mail score per sender\n${lines.join('\n')}\ntotal found ${total.found}, correct ${total.correct}, wrong ${total.wrong}`);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,128 @@
|
||||
// Mirrors webmail lib/structured-mail/__tests__/time-zones.test.ts at a529e6e57; keep the two in step.
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import table from '../data/airport-timezones.json';
|
||||
import {
|
||||
countryZone,
|
||||
fixedOffsetZone,
|
||||
isValidTimeZone,
|
||||
loadAirportZones,
|
||||
resolveZone,
|
||||
wallClockAt,
|
||||
zonedTimeToUtc,
|
||||
zoneOffsetAt,
|
||||
} from '../time-zones';
|
||||
|
||||
const utc = (iso: string) => Date.parse(iso);
|
||||
|
||||
describe('airport time zone table', () => {
|
||||
it('maps IATA codes to their IANA zones', async () => {
|
||||
const zoneOf = await loadAirportZones();
|
||||
expect(zoneOf('FRA')).toBe('Europe/Berlin');
|
||||
expect(zoneOf('JFK')).toBe('America/New_York');
|
||||
expect(zoneOf('SFO')).toBe('America/Los_Angeles');
|
||||
expect(zoneOf('NRT')).toBe('Asia/Tokyo');
|
||||
expect(zoneOf('HNL')).toBe('Pacific/Honolulu');
|
||||
expect(zoneOf('syd')).toBe('Australia/Sydney');
|
||||
});
|
||||
|
||||
it('knows nothing about codes it does not have', async () => {
|
||||
const zoneOf = await loadAirportZones();
|
||||
expect(zoneOf('QQQ')).toBeUndefined();
|
||||
expect(zoneOf('FRAX')).toBeUndefined();
|
||||
expect(zoneOf(undefined)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('holds only valid zone names and indexes inside the zone list', () => {
|
||||
expect(Object.keys(table.airports).length).toBeGreaterThan(7000);
|
||||
for (const zone of table.zones) expect(isValidTimeZone(zone), zone).toBe(true);
|
||||
for (const index of Object.values(table.airports)) {
|
||||
expect(index).toBeGreaterThanOrEqual(0);
|
||||
expect(index).toBeLessThan(table.zones.length);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('fixedOffsetZone', () => {
|
||||
it('inverts the sign for whole hours', () => {
|
||||
expect(fixedOffsetZone(-8 * 60)).toBe('Etc/GMT+8');
|
||||
expect(fixedOffsetZone(5 * 60)).toBe('Etc/GMT-5');
|
||||
expect(fixedOffsetZone(14 * 60)).toBe('Etc/GMT-14');
|
||||
expect(fixedOffsetZone(-12 * 60)).toBe('Etc/GMT+12');
|
||||
});
|
||||
|
||||
it('keeps the UTC instant for half hours and offsets without an Etc zone', () => {
|
||||
expect(fixedOffsetZone(0)).toBe('Etc/UTC');
|
||||
expect(fixedOffsetZone(330)).toBe('Etc/UTC');
|
||||
expect(fixedOffsetZone(345)).toBe('Etc/UTC');
|
||||
expect(fixedOffsetZone(-13 * 60)).toBe('Etc/UTC');
|
||||
});
|
||||
|
||||
it('produces zones whose offset is the one asked for', () => {
|
||||
for (const hours of [-12, -8, -1, 1, 5, 9, 14]) {
|
||||
const zone = fixedOffsetZone(hours * 60);
|
||||
expect(zoneOffsetAt(zone, utc('2026-07-01T00:00:00Z'))).toBe(hours * 60);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('zoneOffsetAt and wall clocks', () => {
|
||||
it('follows DST', () => {
|
||||
expect(zoneOffsetAt('Europe/Berlin', utc('2026-01-15T12:00:00Z'))).toBe(60);
|
||||
expect(zoneOffsetAt('Europe/Berlin', utc('2026-07-15T12:00:00Z'))).toBe(120);
|
||||
expect(zoneOffsetAt('America/Los_Angeles', utc('2027-03-05T04:15:00Z'))).toBe(-480);
|
||||
expect(zoneOffsetAt('Asia/Kathmandu', utc('2026-07-15T12:00:00Z'))).toBe(345);
|
||||
});
|
||||
|
||||
it('turns an instant into the local date and time across the date line', () => {
|
||||
expect(wallClockAt(utc('2027-03-05T04:15:00Z'), 'America/Los_Angeles')).toEqual({ date: '2027-03-04', time: '20:15:00' });
|
||||
expect(wallClockAt(utc('2026-12-01T00:30:00Z'), 'Asia/Tokyo')).toEqual({ date: '2026-12-01', time: '09:30:00' });
|
||||
expect(wallClockAt(utc('2026-12-01T00:30:00Z'), 'Pacific/Honolulu')).toEqual({ date: '2026-11-30', time: '14:30:00' });
|
||||
});
|
||||
|
||||
it('turns a local time back into the instant, and refuses DST gaps and overlaps', () => {
|
||||
expect(zonedTimeToUtc('2027-03-04', '20:15', 'America/Los_Angeles')).toBe(utc('2027-03-05T04:15:00Z'));
|
||||
expect(zonedTimeToUtc('2026-12-01', '09:30:00', 'Asia/Tokyo')).toBe(utc('2026-12-01T00:30:00Z'));
|
||||
// 02:30 on 29 March 2026 doesn't exist in Berlin; 02:30 on 25 October happens twice.
|
||||
expect(zonedTimeToUtc('2026-03-29', '02:30', 'Europe/Berlin')).toBeNull();
|
||||
expect(zonedTimeToUtc('2026-10-25', '02:30', 'Europe/Berlin')).toBeNull();
|
||||
expect(zonedTimeToUtc('2026-13-01', '10:00', 'Europe/Berlin')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('countryZone', () => {
|
||||
it('names the zone of single-zone countries only', () => {
|
||||
expect(countryZone('DE')).toBe('Europe/Berlin');
|
||||
expect(countryZone(' de ')).toBe('Europe/Berlin');
|
||||
expect(countryZone('US')).toBeUndefined();
|
||||
expect(countryZone('ES')).toBeUndefined();
|
||||
expect(countryZone('Germany')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('does not trust France for a time without an offset', () => {
|
||||
expect(countryZone('FR')).toBe('Europe/Paris');
|
||||
expect(countryZone('FR', { floating: true })).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('resolveZone', () => {
|
||||
const at = utc('2026-06-04T09:44:00Z');
|
||||
|
||||
it('accepts a named zone that agrees with the offset', () => {
|
||||
expect(resolveZone({ named: { zone: 'Europe/Berlin', source: 'country' }, offsetMinutes: 120, utcMs: at }))
|
||||
.toEqual({ zone: 'Europe/Berlin', source: 'country' });
|
||||
});
|
||||
|
||||
it('reports a mismatch when table and mail disagree', () => {
|
||||
expect(resolveZone({ named: { zone: 'Europe/Berlin', source: 'airport' }, offsetMinutes: 60, utcMs: at })).toBe('mismatch');
|
||||
});
|
||||
|
||||
it('falls back to a fixed offset, then to UTC, without a named zone', () => {
|
||||
expect(resolveZone({ offsetMinutes: -480, utcMs: at })).toEqual({ zone: 'Etc/GMT+8', source: 'offset' });
|
||||
expect(resolveZone({ offsetMinutes: 330, utcMs: at })).toEqual({ zone: 'Etc/UTC', source: 'utc' });
|
||||
});
|
||||
|
||||
it('reads a time without offset in the named zone, or leaves it floating', () => {
|
||||
expect(resolveZone({ named: { zone: 'Asia/Tokyo', source: 'airport' } })).toEqual({ zone: 'Asia/Tokyo', source: 'airport' });
|
||||
expect(resolveZone({})).toEqual({ zone: null, source: 'floating' });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
// The trust gate on the app's Email shape: only the raw `headers` list from
|
||||
// Email/get, no pre-parsed Authentication-Results (the webmail's full fetch
|
||||
// adds those; the ported trust tests pass them in).
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { Email } from '../../../api/types';
|
||||
import { getStructuredMailTrust } from '../trust';
|
||||
import { isStructuredMailEnabled } from '../gate';
|
||||
|
||||
const HTML = '<script type="application/ld+json">{"@context":"http://schema.org","@type":"Order","orderNumber":"SO-1"}</script>';
|
||||
const RECEIVED = { name: 'Received', value: 'from mail.shop.example (mail.shop.example [192.0.2.1]) by mx.example.org (Stalwart SMTP) with ESMTPS id 1' };
|
||||
const PASS = 'mx.example.org; dkim=pass header.d=shop.example header.s=s1; spf=pass smtp.mailfrom=bounce@shop.example; dmarc=pass header.from=shop.example';
|
||||
|
||||
function mail(headers: { name: string; value: string }[], from = 'orders@shop.example'): Email {
|
||||
return {
|
||||
id: 'm1', threadId: 't1', mailboxIds: {}, keywords: {}, size: 1, hasAttachment: false,
|
||||
receivedAt: '2026-10-30T09:00:00Z', subject: 'Your order SO-1', from: [{ email: from }], headers,
|
||||
htmlBody: [{ partId: '2', type: 'text/html' }], textBody: [],
|
||||
bodyValues: { '2': { value: HTML } },
|
||||
};
|
||||
}
|
||||
|
||||
const reason = (email: Email) => {
|
||||
const trust = getStructuredMailTrust(email);
|
||||
return trust.trusted ? 'trusted' : trust.reason;
|
||||
};
|
||||
|
||||
describe('getStructuredMailTrust from raw headers', () => {
|
||||
it('trusts a delivered mail whose topmost Authentication-Results pass for the From domain', () => {
|
||||
expect(reason(mail([RECEIVED, { name: 'Authentication-Results', value: PASS }]))).toBe('trusted');
|
||||
// Header names come in any case.
|
||||
expect(reason(mail([{ name: 'received', value: RECEIVED.value }, { name: 'authentication-results', value: PASS }]))).toBe('trusted');
|
||||
});
|
||||
|
||||
it('refuses without results, with failing ones, or when only a lower header passes', () => {
|
||||
expect(reason(mail([RECEIVED]))).toBe('no_authentication_results');
|
||||
expect(reason(mail([RECEIVED, { name: 'Authentication-Results', value: 'mx.example.org; spf=fail smtp.mailfrom=shop.example; dmarc=fail header.from=shop.example' }])))
|
||||
.toBe('sender_not_verified');
|
||||
// The receiving server's header is the topmost; one below it may be the sender's own.
|
||||
expect(reason(mail([
|
||||
RECEIVED,
|
||||
{ name: 'Authentication-Results', value: 'mx.example.org; spf=none smtp.mailfrom=shop.example; dmarc=none' },
|
||||
{ name: 'Authentication-Results', value: PASS },
|
||||
]))).toBe('sender_not_verified');
|
||||
});
|
||||
|
||||
it('refuses results the sender wrote above every Received line', () => {
|
||||
expect(reason(mail([{ name: 'Authentication-Results', value: PASS }, RECEIVED]))).toBe('not_written_on_receipt');
|
||||
});
|
||||
|
||||
it('is on while the app has no admin policy', () => {
|
||||
expect(isStructuredMailEnabled()).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,183 @@
|
||||
// Mirrors webmail lib/structured-mail/__tests__/trust.test.ts at 8098039d6; keep the two in step.
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { parseAuthenticationResults } from '../../email-headers';
|
||||
import { authenticationResultsOnReceipt } from '../receipt';
|
||||
import type { Email } from '../../../api/types';
|
||||
import type { EmailBodyPart } from '../trust';
|
||||
import { hasForwardPrefix } from '../forward-prefix';
|
||||
import { readStructuredMail } from '..';
|
||||
import { getStructuredMailTrust, isPersonalMailboxDomain } from '../trust';
|
||||
|
||||
const HTML = `<html><head><script type="application/ld+json">${JSON.stringify({
|
||||
'@context': 'http://schema.org', '@type': 'Order', orderNumber: 'SO-1', merchant: 'Shop', orderStatus: 'OrderProcessing',
|
||||
})}</script></head><body><p>Thanks for your order.</p></body></html>`;
|
||||
|
||||
const AR = {
|
||||
pass: 'mx.example.org; dkim=pass header.d=shop.example header.s=s1; spf=pass smtp.mailfrom=bounce@shop.example; dmarc=pass header.from=shop.example',
|
||||
fail: 'mx.example.org; dkim=fail header.d=shop.example; spf=fail smtp.mailfrom=shop.example; dmarc=fail header.from=shop.example',
|
||||
unverified: 'mx.example.org; dkim=pass header.d=bulk-sender.example; spf=pass smtp.mailfrom=bounce@bulk-sender.example; dmarc=none',
|
||||
};
|
||||
|
||||
type Headers = { name: string; value: string }[];
|
||||
|
||||
function mail(o: {
|
||||
from?: string; ar?: string; headers?: Headers; subject?: string; html?: string; text?: string;
|
||||
truncated?: boolean; bodyStructure?: EmailBodyPart; onReceipt?: boolean;
|
||||
} = {}): Email {
|
||||
const from = o.from ?? 'orders@shop.example';
|
||||
const headers: Headers = o.headers ?? [
|
||||
{ name: 'Received', value: 'from mail.shop.example (mail.shop.example [192.0.2.1]) by mx.example.org (Stalwart SMTP) with ESMTPS id 1' },
|
||||
...(o.ar ? [{ name: 'Authentication-Results', value: o.ar }] : []),
|
||||
{ name: 'From', value: `<${from}>` },
|
||||
];
|
||||
const arValues = headers.filter((h) => h.name.toLowerCase() === 'authentication-results').map((h) => h.value);
|
||||
return {
|
||||
id: 'm1', threadId: 't1', mailboxIds: {}, keywords: {}, size: 1, receivedAt: '2026-10-30T09:00:00Z',
|
||||
from: [{ email: from }], subject: o.subject ?? 'Your order SO-1',
|
||||
headers: headers as unknown as Email['headers'],
|
||||
...(arValues.length > 0 ? { authenticationResults: parseAuthenticationResults(arValues) } : {}),
|
||||
...(o.onReceipt !== undefined ? { authenticationResultsOnReceipt: o.onReceipt } : {}),
|
||||
htmlBody: [{ partId: '2', type: 'text/html' } as EmailBodyPart],
|
||||
textBody: o.text !== undefined ? [{ partId: '1', type: 'text/plain' } as EmailBodyPart] : [],
|
||||
bodyValues: {
|
||||
'2': { value: o.html ?? HTML, isTruncated: !!o.truncated, isEncodingProblem: false },
|
||||
...(o.text !== undefined ? { '1': { value: o.text, isTruncated: false, isEncodingProblem: false } } : {}),
|
||||
},
|
||||
...(o.bodyStructure ? { bodyStructure: o.bodyStructure } : {}),
|
||||
} as unknown as Email;
|
||||
}
|
||||
|
||||
/** What the reader does: the trust gate, then the parser. */
|
||||
function cardsFor(email: Email) {
|
||||
if (!getStructuredMailTrust(email).trusted) return [];
|
||||
const html = email.bodyValues!['2'].value;
|
||||
return readStructuredMail(html, { mailDate: new Date(email.receivedAt) });
|
||||
}
|
||||
|
||||
const reason = (email: Email) => {
|
||||
const trust = getStructuredMailTrust(email);
|
||||
return trust.trusted ? 'trusted' : trust.reason;
|
||||
};
|
||||
|
||||
describe('the same markup under each Authentication-Results verdict', () => {
|
||||
it('gives a card only when SPF or DKIM passes for the From domain', () => {
|
||||
expect(cardsFor(mail({ ar: AR.pass }))).toHaveLength(1);
|
||||
expect(cardsFor(mail({ ar: AR.fail }))).toEqual([]);
|
||||
expect(cardsFor(mail({ ar: AR.unverified }))).toEqual([]);
|
||||
expect(cardsFor(mail({}))).toEqual([]);
|
||||
// Internal: a colleague on the reader's own domain, handed in without any header.
|
||||
expect(cardsFor(mail({ from: 'travel@example.org' }))).toEqual([]);
|
||||
});
|
||||
|
||||
it('names the reason', () => {
|
||||
expect(reason(mail({ ar: AR.pass }))).toBe('trusted');
|
||||
expect(reason(mail({ ar: AR.fail }))).toBe('sender_not_verified');
|
||||
expect(reason(mail({ ar: AR.unverified }))).toBe('sender_not_verified');
|
||||
expect(reason(mail({}))).toBe('no_authentication_results');
|
||||
});
|
||||
|
||||
it('accepts a DKIM pass from a subdomain of the From domain (Uber signs as ibt.uber.com)', () => {
|
||||
const ar = 'mx.google.com; dkim=pass header.i=@ibt.shop.example header.s=smtp3; spf=pass smtp.mailfrom=bounces@ibt.shop.example; dmarc=pass header.from=shop.example';
|
||||
expect(reason(mail({ ar }))).toBe('trusted');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Authentication-Results written by the sender', () => {
|
||||
it('refuses a passing header with no Received above it (submitted over JMAP)', () => {
|
||||
const headers = [{ name: 'Authentication-Results', value: AR.pass }, { name: 'From', value: '<orders@shop.example>' }];
|
||||
expect(reason(mail({ headers }))).toBe('not_written_on_receipt');
|
||||
});
|
||||
|
||||
it('refuses a passing header right below an authenticated submission hop', () => {
|
||||
const headers = [
|
||||
{ name: 'Received', value: 'from [10.0.0.5] (user@example.org) by mx.example.org with ESMTPSA id 7' },
|
||||
{ name: 'Authentication-Results', value: AR.pass },
|
||||
];
|
||||
expect(reason(mail({ headers }))).toBe('not_written_on_receipt');
|
||||
});
|
||||
|
||||
it('trusts the flag the full message fetch recorded', () => {
|
||||
expect(reason(mail({ ar: AR.pass, onReceipt: false }))).toBe('not_written_on_receipt');
|
||||
expect(reason(mail({ ar: AR.pass, headers: [{ name: 'Authentication-Results', value: AR.pass }], onReceipt: true }))).toBe('trusted');
|
||||
});
|
||||
});
|
||||
|
||||
describe('authenticationResultsOnReceipt', () => {
|
||||
it('accepts the header order Stalwart writes on delivery', () => {
|
||||
expect(authenticationResultsOnReceipt([
|
||||
{ name: 'Delivered-To', value: 'info@example.org' },
|
||||
{ name: 'X-Spam-Status', value: 'No' },
|
||||
{ name: 'Received', value: 'from smh-01.example.net (smh-01.example.net [178.254.0.201]) (using TLSv1.3 with cipher TLS13_AES_256_GCM_SHA384) by mail.example.org (Stalwart SMTP) with ESMTPS id 1' },
|
||||
{ name: 'Authentication-Results', value: 'mail.example.org; dkim=pass header.d=shop.example' },
|
||||
{ name: 'Received-SPF', value: 'pass' },
|
||||
])).toBe(true);
|
||||
});
|
||||
|
||||
it('accepts the header order Gmail writes', () => {
|
||||
expect(authenticationResultsOnReceipt([
|
||||
{ name: 'Delivered-To', value: 'someone@gmail.com' },
|
||||
{ name: 'Received', value: 'by 2002:a05:6a10:1b4a:b0:5b1:2::1 with SMTP id 10csp1;' },
|
||||
{ name: 'X-Received', value: 'by 2002:a17:906 with SMTP id 1' },
|
||||
{ name: 'ARC-Authentication-Results', value: 'i=1; mx.google.com; dkim=pass' },
|
||||
{ name: 'Return-Path', value: '<bounce@shop.example>' },
|
||||
{ name: 'Received', value: 'from mail.shop.example (mail.shop.example. [192.0.2.1]) by mx.google.com with ESMTPS id 2' },
|
||||
{ name: 'Received-SPF', value: 'pass' },
|
||||
{ name: 'Authentication-Results', value: 'mx.google.com; dkim=pass header.i=@shop.example' },
|
||||
])).toBe(true);
|
||||
});
|
||||
|
||||
it('refuses headers above every Received, after a submission hop, or with no Received at all', () => {
|
||||
expect(authenticationResultsOnReceipt([{ name: 'Authentication-Results', value: 'x' }, { name: 'Received', value: 'from a by b with ESMTPS' }])).toBe(false);
|
||||
expect(authenticationResultsOnReceipt([{ name: 'Received', value: 'from a by b with LMTPSA' }, { name: 'Authentication-Results', value: 'x' }])).toBe(false);
|
||||
expect(authenticationResultsOnReceipt([{ name: 'Received', value: 'from a by b with utf8esmtpa' }, { name: 'Authentication-Results', value: 'x' }])).toBe(false);
|
||||
expect(authenticationResultsOnReceipt([{ name: 'From', value: 'x' }, { name: 'Authentication-Results', value: 'x' }])).toBe(false);
|
||||
expect(authenticationResultsOnReceipt([{ name: 'Received', value: 'from a by b with ESMTPS' }])).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('forwarded and personal mail', () => {
|
||||
it('gives no card for a forward prefix anywhere in the prefix chain', () => {
|
||||
expect(reason(mail({ ar: AR.pass, subject: 'Fwd: Your order SO-1' }))).toBe('forwarded');
|
||||
expect(reason(mail({ ar: AR.pass, subject: 'Re: WG: Ihre Bestellung' }))).toBe('forwarded');
|
||||
expect(reason(mail({ ar: AR.pass, subject: 'Re: Your order SO-1' }))).toBe('trusted');
|
||||
});
|
||||
|
||||
it('gives no card when the body carries a forward separator or Outlook header block', () => {
|
||||
const separator = HTML.replace('<p>Thanks', '<p>---------- Forwarded message ----------</p><p>Thanks');
|
||||
expect(reason(mail({ ar: AR.pass, html: separator }))).toBe('forwarded');
|
||||
const outlook = 'FYI\n\nVon: Shop <orders@shop.example>\nGesendet: Freitag, 30. Oktober 2026 10:00\nAn: Erika\nBetreff: Ihre Bestellung\n';
|
||||
expect(reason(mail({ ar: AR.pass, text: outlook }))).toBe('forwarded');
|
||||
expect(reason(mail({ ar: AR.pass, text: 'From: Frankfurt\nTo: New York\nDate: 1 Dec' }))).toBe('trusted');
|
||||
});
|
||||
|
||||
it('gives no card for mail from a personal mailbox, even when it passes', () => {
|
||||
const ar = 'mx.example.org; dkim=pass header.d=gmail.com; spf=pass smtp.mailfrom=friend@gmail.com; dmarc=pass header.from=gmail.com';
|
||||
expect(reason(mail({ from: 'friend@gmail.com', ar }))).toBe('personal_mailbox');
|
||||
expect(isPersonalMailboxDomain('outlook.de')).toBe(true);
|
||||
expect(isPersonalMailboxDomain('yahoo.co.uk')).toBe(true);
|
||||
expect(isPersonalMailboxDomain('lufthansa.com')).toBe(false);
|
||||
});
|
||||
|
||||
it('detects forward prefixes on their own', () => {
|
||||
expect(hasForwardPrefix('FW: x')).toBe(true);
|
||||
expect(hasForwardPrefix('AW: Antw: Fwd: x')).toBe(true);
|
||||
expect(hasForwardPrefix('Forwarding your booking')).toBe(false);
|
||||
expect(hasForwardPrefix(undefined)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('attached and truncated mail', () => {
|
||||
it('gives no card when the HTML part sits inside an attached message', () => {
|
||||
const bodyStructure = {
|
||||
type: 'multipart/mixed', subParts: [
|
||||
{ partId: '1', type: 'text/plain' },
|
||||
{ type: 'message/rfc822', partId: '3', subParts: [{ partId: '2', type: 'text/html' }] },
|
||||
],
|
||||
} as unknown as EmailBodyPart;
|
||||
expect(reason(mail({ ar: AR.pass, bodyStructure }))).toBe('attached_message');
|
||||
});
|
||||
|
||||
it('gives no card when the HTML body value was cut off', () => {
|
||||
expect(reason(mail({ ar: AR.pass, truncated: true }))).toBe('truncated');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,151 @@
|
||||
// Mirrors webmail lib/structured-mail/__tests__/validate-resolve.test.ts at 90d7afc34; keep the two in step.
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { normalizeItems } from '../normalize';
|
||||
import { needsAirportZones, resolveCards } from '../resolve';
|
||||
import { loadAirportZones } from '../time-zones';
|
||||
import type { StructuredCard, TransportBooking } from '../types';
|
||||
import { validateCard } from '../validate';
|
||||
|
||||
const ctx = 'http://schema.org';
|
||||
const MAIL = new Date('2026-10-30T10:00:00Z');
|
||||
const one = (item: unknown) => normalizeItems([{ item, source: 'json-ld' }])[0];
|
||||
const problems = (item: unknown, mailDate = MAIL) => validateCard(one(item), mailDate).problems;
|
||||
|
||||
function flight(dep: [string, string, string], arr: [string, string, string], extra: Record<string, unknown> = {}) {
|
||||
return {
|
||||
'@context': ctx, '@type': 'FlightReservation', reservationNumber: 'T1', underName: 'Erika Mustermann',
|
||||
reservationFor: {
|
||||
'@type': 'Flight', flightNumber: '100', airline: { name: 'Test Air', iataCode: 'TA' },
|
||||
departureAirport: { name: dep[0], iataCode: dep[1] }, departureTime: dep[2],
|
||||
arrivalAirport: { name: arr[0], iataCode: arr[1] }, arrivalTime: arr[2],
|
||||
},
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
const sfoJfk = flight(['San Francisco', 'SFO', '2026-12-04T20:15:00-08:00'], ['New York', 'JFK', '2026-12-05T06:30:00-05:00']);
|
||||
|
||||
describe('validateCard', () => {
|
||||
it('accepts a complete flight near the mail date', () => {
|
||||
expect(problems(sfoJfk)).toEqual([]);
|
||||
});
|
||||
|
||||
it('rejects flights with missing times or places', () => {
|
||||
const noArrival = flight(['San Francisco', 'SFO', '2026-12-04T20:15:00-08:00'], ['New York', 'JFK', '']);
|
||||
const bareDate = flight(['San Francisco', 'SFO', '2026-12-04'], ['New York', 'JFK', '2026-12-05T06:30:00-05:00']);
|
||||
const noPlace = flight(['', '', '2026-12-04T20:15:00-08:00'], ['New York', 'JFK', '2026-12-05T06:30:00-05:00']);
|
||||
expect(problems(noArrival)).toContain('arrival time missing');
|
||||
expect(problems(bareDate)).toContain('departure time missing');
|
||||
expect(problems(noPlace)).toContain('departure place missing');
|
||||
});
|
||||
|
||||
it('rejects bookings years away from the mail', () => {
|
||||
expect(problems(sfoJfk, new Date('2024-11-12T00:00:00Z'))).toContain('date too far from the mail');
|
||||
expect(problems(sfoJfk, new Date('2028-01-01T00:00:00Z'))).toContain('date too far from the mail');
|
||||
});
|
||||
|
||||
it('rejects copies of Google documentation examples', () => {
|
||||
expect(problems({ ...sfoJfk, reservationNumber: 'RXJ34P' })).toContain('documentation example');
|
||||
expect(problems({ '@context': ctx, '@type': 'ParcelDelivery', trackingNumber: '3453291231', carrier: 'FedEx' })).toContain('documentation example');
|
||||
});
|
||||
|
||||
it('rejects stays that end before they start', () => {
|
||||
const stay = (checkin: string, checkout: string) => ({
|
||||
'@context': ctx, '@type': 'LodgingReservation', reservationNumber: 'H', reservationFor: { '@type': 'Hotel', name: 'Hotel' },
|
||||
checkinDate: checkin, checkoutDate: checkout,
|
||||
});
|
||||
expect(problems(stay('2026-12-04', '2026-12-06'))).toEqual([]);
|
||||
expect(problems(stay('2026-12-06', '2026-12-04'))).toContain('check-out not after check-in');
|
||||
expect(problems(stay('2026-12-04', '2026-12-04'))).toContain('check-out not after check-in');
|
||||
});
|
||||
|
||||
it('rejects orders with nothing to identify them, like Uber\'s empty Order', () => {
|
||||
expect(problems({ '@context': ctx, '@type': 'Order', merchant: { name: 'UberEATS' } })).toContain('neither order number nor tracking');
|
||||
expect(problems({ '@context': ctx, '@type': 'Order', merchant: 'K4G', orderNumber: 'A1' })).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('resolveCards', () => {
|
||||
const resolved = async (item: unknown): Promise<StructuredCard[]> => {
|
||||
const cards = [one(item)];
|
||||
return needsAirportZones(cards) ? resolveCards(cards, await loadAirportZones()) : resolveCards(cards);
|
||||
};
|
||||
|
||||
it('gives flight ends their airport zones and keeps the instants', async () => {
|
||||
const [card] = (await resolved(sfoJfk)) as TransportBooking[];
|
||||
expect(card.legs[0].departure.at).toMatchObject({ zone: 'America/Los_Angeles', utc: '2026-12-05T04:15:00Z' });
|
||||
expect(card.legs[0].arrival?.at).toMatchObject({ zone: 'America/New_York', utc: '2026-12-05T11:30:00Z' });
|
||||
});
|
||||
|
||||
it('accepts eastbound date-line flights that land earlier on the wall clock', async () => {
|
||||
const nrtHnl = flight(['Tokyo Narita', 'NRT', '2026-12-04T21:00:00+09:00'], ['Honolulu', 'HNL', '2026-12-04T09:15:00-10:00']);
|
||||
const sydLax = flight(['Sydney', 'SYD', '2026-12-04T09:50:00+11:00'], ['Los Angeles', 'LAX', '2026-12-04T06:20:00-08:00']);
|
||||
expect(await resolved(nrtHnl)).toHaveLength(1);
|
||||
expect(await resolved(sydLax)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('rejects arrival before departure and flights over 20 hours', async () => {
|
||||
const backwards = flight(['Frankfurt', 'FRA', '2026-12-01T10:15:00+01:00'], ['New York', 'JFK', '2026-12-01T03:05:00-05:00']);
|
||||
const tooLong = flight(['Frankfurt', 'FRA', '2026-12-01T10:15:00+01:00'], ['Sydney', 'SYD', '2026-12-02T23:00:00+11:00']);
|
||||
expect(await resolved(backwards)).toEqual([]);
|
||||
expect(await resolved(tooLong)).toEqual([]);
|
||||
});
|
||||
|
||||
it('drops a flight whose offset contradicts the airport zone', async () => {
|
||||
const wrongOffset = flight(['Frankfurt', 'FRA', '2026-12-01T10:15:00+00:00'], ['New York', 'JFK', '2026-12-01T13:05:00-05:00']);
|
||||
expect(await resolved(wrongOffset)).toEqual([]);
|
||||
});
|
||||
|
||||
it('reads a flight time without offset in the airport zone, and drops it when the airport is unknown', async () => {
|
||||
const floating = flight(['Frankfurt', 'FRA', '2026-12-01T10:15:00'], ['New York', 'JFK', '2026-12-01T13:05:00']);
|
||||
const [card] = (await resolved(floating)) as TransportBooking[];
|
||||
expect(card.legs[0].departure.at).toMatchObject({ zone: 'Europe/Berlin', utc: '2026-12-01T09:15:00Z' });
|
||||
const unknown = flight(['Nowhere', 'QQQ', '2026-12-01T10:15:00'], ['New York', 'JFK', '2026-12-01T13:05:00']);
|
||||
expect(await resolved(unknown)).toEqual([]);
|
||||
});
|
||||
|
||||
it('never shows a flight before the airport table is there', () => {
|
||||
expect(resolveCards([one(sfoJfk)])).toEqual([]);
|
||||
});
|
||||
|
||||
it('uses a fixed-offset zone where no airport code is known', async () => {
|
||||
const noCode = flight(['San Francisco', '', '2026-12-04T20:15:00-08:00'], ['New York', 'JFK', '2026-12-05T06:30:00-05:00']);
|
||||
const [card] = (await resolved(noCode)) as TransportBooking[];
|
||||
expect(card.legs[0].departure.at.zone).toBe('Etc/GMT+8');
|
||||
});
|
||||
|
||||
it('treats hotel times without offset or known country as dates only', async () => {
|
||||
const [card] = await resolved({
|
||||
'@context': ctx, '@type': 'LodgingReservation', reservationNumber: 'H', reservationFor: { '@type': 'Hotel', name: 'Hotel', address: { addressCountry: 'US' } },
|
||||
checkinDate: '2026-12-04T15:00:00', checkoutDate: '2026-12-06T11:00:00',
|
||||
});
|
||||
expect(card).toMatchObject({ checkin: { date: '2026-12-04' }, checkout: { date: '2026-12-06' } });
|
||||
expect((card as { checkin: { time?: string } }).checkin.time).toBeUndefined();
|
||||
});
|
||||
|
||||
it('ends a date-only event end at midnight local time even across a DST change', async () => {
|
||||
const event = (country?: string) => ({
|
||||
'@context': ctx, '@type': 'EventReservation', reservationNumber: 'EV1',
|
||||
reservationFor: {
|
||||
'@type': 'Event', name: 'Herbstmesse', startDate: '2026-10-23T18:00:00+02:00', endDate: '2026-10-26',
|
||||
location: { '@type': 'Place', name: 'Messe', ...(country ? { address: { addressCountry: country } } : {}) },
|
||||
},
|
||||
});
|
||||
const [withVenue] = await resolved(event('DE'));
|
||||
expect(withVenue).toMatchObject({ end: { date: '2026-10-26', time: '23:59:59', zone: 'Europe/Berlin', utc: '2026-10-26T22:59:59Z' } });
|
||||
const [noVenueCountry] = await resolved(event());
|
||||
expect(noVenueCountry).toMatchObject({ end: { date: '2026-10-26', time: '23:59:59', zone: 'Etc/GMT-2', utc: '2026-10-26T21:59:59Z' } });
|
||||
});
|
||||
|
||||
it('reads bus times in the single zone of the stop\'s country', async () => {
|
||||
const [card] = (await resolved({
|
||||
'@context': ctx, '@type': 'BusReservation', reservationNumber: 'B',
|
||||
reservationFor: {
|
||||
'@type': 'BusTrip', busNumber: 'FLX30',
|
||||
departureBusStop: { name: 'Hannover', address: { addressCountry: 'DE' } }, departureTime: '2026-06-04T11:44:00+0200',
|
||||
arrivalBusStop: { name: 'Berlin', address: { addressCountry: 'DE' } }, arrivalTime: '2026-06-04T13:44:00+0200',
|
||||
},
|
||||
})) as TransportBooking[];
|
||||
expect(card.legs[0].departure.at.zone).toBe('Europe/Berlin');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,697 @@
|
||||
// Mirrors webmail lib/structured-mail/calendar.ts at 87d9f8923; keep the two in step.
|
||||
/**
|
||||
* Calendar events for booking cards: one JSCalendar event per transport leg
|
||||
* and one per hotel stay, rental car, event or table booking. Plain
|
||||
* TypeScript, no React and no stores, so the native app can reuse it.
|
||||
*
|
||||
* Times: a timed event starts at the departure's wall clock in its zone and
|
||||
* lasts the exact hours and minutes between the two UTC instants (never a
|
||||
* `D` duration, so a DST change in between can't move the end). The arrival
|
||||
* zone goes on the arrival location (`relativeTo: "end"`). Floating times
|
||||
* (no offset, no known zone) stay floating. Every event gets a uid derived
|
||||
* from the sender's organisational domain and the booking's identity, so a
|
||||
* later mail about the same booking finds it again and a mail from another
|
||||
* domain never can.
|
||||
*/
|
||||
import type { CalendarEvent, EventLocation as CalendarLocation } from '../../api/types';
|
||||
import { normalizeCalendarEventLike, sanitizeOutgoingCalendarEventData } from './event-normalization';
|
||||
import { fixedOffsetZone, wallClockAt } from './time-zones';
|
||||
import type {
|
||||
Address,
|
||||
Booking,
|
||||
Moment,
|
||||
Place,
|
||||
StructuredCard,
|
||||
TransportBooking,
|
||||
TransportLeg,
|
||||
} from './types';
|
||||
|
||||
const DAY = 86_400_000;
|
||||
const UID_DOMAIN = 'structured.bulwark';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// uid
|
||||
|
||||
/**
|
||||
* Public suffixes of two labels that commercial senders sit under. Not the
|
||||
* whole Public Suffix List: a suffix missing here (`gouv.fr`) makes every
|
||||
* domain under it share one organisational domain. That is acceptable for
|
||||
* uids, which also need the same booking reference (or, without one, the
|
||||
* same leg and date) to match, and booking mail from such domains is rare.
|
||||
*/
|
||||
const TWO_LABEL_SUFFIXES = new Set([
|
||||
'co.uk', 'org.uk', 'ac.uk', 'gov.uk', 'me.uk', 'ltd.uk', 'plc.uk',
|
||||
'com.au', 'net.au', 'org.au',
|
||||
'co.jp', 'ne.jp', 'or.jp',
|
||||
'com.br', 'co.nz', 'co.za', 'com.mx', 'com.tr', 'co.in', 'com.cn', 'com.sg', 'com.hk', 'co.kr', 'co.il',
|
||||
'com.ar', 'com.tw', 'co.th', 'co.id', 'com.my', 'com.ph', 'co.at', 'or.at', 'com.es', 'com.pl', 'com.pt',
|
||||
]);
|
||||
|
||||
/**
|
||||
* The organisational domain of a sender domain: the last two labels, or three
|
||||
* under a two-label public suffix (`co.uk`, `com.au`).
|
||||
* `booking.flixbus.com` → `flixbus.com`, `shop.example.co.uk` → `example.co.uk`.
|
||||
*/
|
||||
export function organizationalDomain(fromDomain: string): string {
|
||||
const labels = fromDomain.trim().toLowerCase().replace(/\.+$/, '').split('.').filter(Boolean);
|
||||
const count = labels.length >= 3 && TWO_LABEL_SUFFIXES.has(labels.slice(-2).join('.')) ? 3 : 2;
|
||||
return labels.slice(-count).join('.');
|
||||
}
|
||||
|
||||
/** The domain part of an address (`orders@booking.flixbus.com` → `booking.flixbus.com`). */
|
||||
export function senderDomainOf(address: string | undefined): string {
|
||||
if (!address) return '';
|
||||
const at = address.lastIndexOf('@');
|
||||
return (at >= 0 ? address.slice(at + 1) : address).trim().toLowerCase();
|
||||
}
|
||||
|
||||
function utf8Bytes(text: string): number[] {
|
||||
const bytes: number[] = [];
|
||||
for (const char of text) {
|
||||
const cp = char.codePointAt(0) ?? 0;
|
||||
if (cp < 0x80) bytes.push(cp);
|
||||
else if (cp < 0x800) bytes.push(0xc0 | (cp >> 6), 0x80 | (cp & 0x3f));
|
||||
else if (cp < 0x10000) bytes.push(0xe0 | (cp >> 12), 0x80 | ((cp >> 6) & 0x3f), 0x80 | (cp & 0x3f));
|
||||
else bytes.push(0xf0 | (cp >> 18), 0x80 | ((cp >> 12) & 0x3f), 0x80 | ((cp >> 6) & 0x3f), 0x80 | (cp & 0x3f));
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 64-bit FNV-1a of the UTF-8 bytes, as 16 hex digits. Synchronous and on
|
||||
* plain numbers (four 16-bit limbs), so it runs without crypto.subtle (Lite
|
||||
* on plain http) and without BigInt.
|
||||
*/
|
||||
export function fnv1a64(text: string): string {
|
||||
// Offset basis 0xcbf29ce484222325, lowest limb first.
|
||||
let h0 = 0x2325, h1 = 0x8422, h2 = 0x9ce4, h3 = 0xcbf2;
|
||||
for (const byte of utf8Bytes(text)) {
|
||||
h0 ^= byte;
|
||||
// Multiply by the prime 0x100000001b3 = 2^40 + 0x1b3, modulo 2^64.
|
||||
let r0 = h0 * 0x1b3;
|
||||
let r1 = h1 * 0x1b3;
|
||||
let r2 = h2 * 0x1b3 + h0 * 0x100;
|
||||
let r3 = h3 * 0x1b3 + h1 * 0x100;
|
||||
r1 += r0 >>> 16; r0 &= 0xffff;
|
||||
r2 += r1 >>> 16; r1 &= 0xffff;
|
||||
r3 += r2 >>> 16; r2 &= 0xffff;
|
||||
r3 &= 0xffff;
|
||||
h0 = r0; h1 = r1; h2 = r2; h3 = r3;
|
||||
}
|
||||
return [h3, h2, h1, h0].map((limb) => limb.toString(16).padStart(4, '0')).join('');
|
||||
}
|
||||
|
||||
const norm = (value: string | undefined) => (value ?? '').toLowerCase().replace(/\s+/g, ' ').trim();
|
||||
|
||||
function uidFrom(kind: Booking['kind'], parts: (string | undefined)[]): string {
|
||||
return `bulwark-sm-${kind}-${fnv1a64(parts.map(norm).join('|'))}@${UID_DOMAIN}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* A leg's number as an identity: no spaces ("ICE 577" = "ICE577") and no
|
||||
* leading zeros ("LX0072" in the markup is flight 72 on the boarding pass;
|
||||
* legKey in normalize.ts strips them the same way).
|
||||
*/
|
||||
function legNumber(number: string | undefined): string {
|
||||
return norm(number).replace(/\s+/g, '').replace(/^0+(?=\d)/, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* The identity of a leg, from the leg alone: reference, carrier IATA code
|
||||
* (the name only without one), number and departure code or name. Without a
|
||||
* reference the departure's local date is added. Never the time, and with a
|
||||
* reference not the date either, so a schedule change (even one that moves
|
||||
* the departure past midnight) updates the same event.
|
||||
*
|
||||
* It never depends on the booking's other legs, so a later mail that lists
|
||||
* only some of them finds the same events. The price: two legs of one
|
||||
* booking with the same reference, flight and departure (the same commuter
|
||||
* flight on two days) share a uid, and since one uid holds one event,
|
||||
* bookingEventSlots offers neither of them.
|
||||
*/
|
||||
function legParts(booking: TransportBooking, leg: TransportLeg): (string | undefined)[] {
|
||||
return [
|
||||
booking.reference,
|
||||
leg.carrierCode ?? leg.carrier,
|
||||
legNumber(leg.number),
|
||||
leg.departure.place.code ?? leg.departure.place.name,
|
||||
...(booking.reference ? [] : [leg.departure.at.date]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The uid of the event for a booking (or, for flights, trains and buses, for
|
||||
* one of its legs; the first leg when none is given):
|
||||
* `bulwark-sm-<kind>-<16 hex>@structured.bulwark`, hashed over a key that
|
||||
* starts with the sender's organisational domain.
|
||||
*/
|
||||
export function bookingEventUid(booking: Booking, senderDomain: string, leg?: TransportLeg): string {
|
||||
const domain = organizationalDomain(senderDomainOf(senderDomain));
|
||||
// With a reference the date stays out, so a moved booking keeps its event;
|
||||
// without one the date is part of what identifies it.
|
||||
const dateUnlessReference = (date: string) => (booking.reference ? [] : [date]);
|
||||
switch (booking.kind) {
|
||||
case 'flight': case 'train': case 'bus':
|
||||
return uidFrom(booking.kind, [domain, ...legParts(booking, leg ?? booking.legs[0])]);
|
||||
case 'lodging':
|
||||
return uidFrom('lodging', [domain, booking.reference, booking.name, ...dateUnlessReference(booking.checkin.date)]);
|
||||
case 'rentalcar':
|
||||
return uidFrom('rentalcar', [domain, booking.reference, booking.company ?? booking.provider, ...dateUnlessReference(booking.pickup.at.date)]);
|
||||
case 'event': case 'restaurant':
|
||||
// One order can cover several events: the name stays in the key.
|
||||
return uidFrom(booking.kind, [domain, booking.reference, booking.name, ...dateUnlessReference(booking.start.date)]);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// times
|
||||
|
||||
interface Instant {
|
||||
/** Local wall clock, `YYYY-MM-DDTHH:mm:ss`, in `timeZone`. */
|
||||
start: string;
|
||||
/** IANA zone, or null for a floating time. */
|
||||
timeZone: string | null;
|
||||
/** UTC instant in ms; null when floating. */
|
||||
utcMs: number | null;
|
||||
/** The wall clock as the mail wrote it, read as if it were UTC (for floating differences). */
|
||||
naiveMs: number;
|
||||
}
|
||||
|
||||
function naiveMs(m: Moment): number {
|
||||
return Date.parse(`${m.date}T${m.time ?? '00:00:00'}Z`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Where a timed moment sits: its resolved zone and instant. A moment that
|
||||
* resolveCards hasn't seen but that carries an offset gets the fixed-offset
|
||||
* zone; one without offset or zone stays floating.
|
||||
*/
|
||||
function instantOf(m: Moment | undefined): Instant | null {
|
||||
if (!m?.time || !/^\d{4}-\d{2}-\d{2}$/.test(m.date)) return null;
|
||||
const naive = naiveMs(m);
|
||||
if (!Number.isFinite(naive)) return null;
|
||||
const zone = m.zone !== undefined ? m.zone : (m.offsetMinutes !== undefined ? fixedOffsetZone(m.offsetMinutes) : null);
|
||||
if (zone === null) return { start: `${m.date}T${m.time}`, timeZone: null, utcMs: null, naiveMs: naive };
|
||||
const utcMs = m.utc ? Date.parse(m.utc) : NaN;
|
||||
if (!Number.isFinite(utcMs)) return null;
|
||||
const wall = wallClockAt(utcMs, zone);
|
||||
return { start: `${wall.date}T${wall.time}`, timeZone: zone, utcMs, naiveMs: naive };
|
||||
}
|
||||
|
||||
/** Hours, minutes and seconds only (`PT7H15M`, `PT43H`, `PT0S`). */
|
||||
function timedDuration(ms: number): string {
|
||||
const total = Math.round(ms / 1000);
|
||||
if (total <= 0) return 'PT0S';
|
||||
const h = Math.floor(total / 3600);
|
||||
const m = Math.floor((total % 3600) / 60);
|
||||
const s = total % 60;
|
||||
return `PT${h ? `${h}H` : ''}${m ? `${m}M` : ''}${s ? `${s}S` : ''}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* The exact span between two timed moments: from the UTC instants when both
|
||||
* have one, else from the wall clocks as written. Null when the end lies
|
||||
* before the start (resolveCards drops such bookings; this is a guard).
|
||||
*/
|
||||
function spanBetween(start: Instant, end: Instant): number | null {
|
||||
const ms = start.utcMs !== null && end.utcMs !== null ? end.utcMs - start.utcMs : end.naiveMs - start.naiveMs;
|
||||
return ms >= 0 ? ms : null;
|
||||
}
|
||||
|
||||
function dayNumber(date: string): number | null {
|
||||
const ms = Date.parse(`${date}T00:00:00Z`);
|
||||
return Number.isFinite(ms) ? Math.round(ms / DAY) : null;
|
||||
}
|
||||
|
||||
/** An all-day span from `first` to `last`, both days included. */
|
||||
function allDaySpan(first: string, last: string): { start: string; duration: string } | null {
|
||||
const a = dayNumber(first);
|
||||
const b = dayNumber(last);
|
||||
if (a === null || b === null || b < a) return null;
|
||||
return { start: `${first}T00:00:00`, duration: `P${b - a + 1}D` };
|
||||
}
|
||||
|
||||
/**
|
||||
* When a moment is over, in ms: its instant; a floating time read in the
|
||||
* runtime's zone; a date-only value at the end of that day (local).
|
||||
*/
|
||||
function momentOverAt(m: Moment | undefined): number | null {
|
||||
if (!m || !/^\d{4}-\d{2}-\d{2}$/.test(m.date)) return null;
|
||||
if (m.time && m.utc) return Date.parse(m.utc);
|
||||
const [y, mo, d] = m.date.split('-').map(Number);
|
||||
if (m.time) {
|
||||
const [h, mi, s] = m.time.split(':').map(Number);
|
||||
return new Date(y, mo - 1, d, h, mi, s || 0).getTime();
|
||||
}
|
||||
return new Date(y, mo - 1, d + 1).getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* When the booking is over (ms): the last arrival (or departure, for a leg
|
||||
* without arrival), check-out, drop-off, the event's end or start, the
|
||||
* table's time. Null when no date can be read.
|
||||
*/
|
||||
export function bookingEndsAt(booking: Booking): number | null {
|
||||
const ends: (number | null)[] = [];
|
||||
switch (booking.kind) {
|
||||
case 'flight': case 'train': case 'bus':
|
||||
for (const leg of booking.legs) ends.push(momentOverAt(leg.arrival?.at ?? leg.departure.at));
|
||||
break;
|
||||
case 'lodging': ends.push(momentOverAt(booking.checkout)); break;
|
||||
case 'rentalcar': ends.push(momentOverAt(booking.dropoff.at)); break;
|
||||
case 'event': ends.push(momentOverAt(booking.end ?? booking.start)); break;
|
||||
case 'restaurant': ends.push(momentOverAt(booking.start)); break;
|
||||
}
|
||||
const known = ends.filter((e): e is number => e !== null && Number.isFinite(e));
|
||||
return known.length > 0 ? Math.max(...known) : null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// text
|
||||
|
||||
/** Description lines and the one title word that need the user's language. */
|
||||
export interface BookingEventLabels {
|
||||
reference: (reference: string) => string;
|
||||
passengers: (names: string) => string;
|
||||
seats: (seats: string) => string;
|
||||
checkin: (url: string) => string;
|
||||
manage: (url: string) => string;
|
||||
ticket: (url: string) => string;
|
||||
phone: (phone: string) => string;
|
||||
car: (car: string) => string;
|
||||
tickets: (numbers: string) => string;
|
||||
partySize: (count: number) => string;
|
||||
rentalCar: (company: string | undefined) => string;
|
||||
addedFrom: (domain: string, subject: string | undefined) => string;
|
||||
}
|
||||
|
||||
export const ENGLISH_LABELS: BookingEventLabels = {
|
||||
reference: (reference) => `Booking reference: ${reference}`,
|
||||
passengers: (names) => `Passengers: ${names}`,
|
||||
seats: (seats) => `Seats: ${seats}`,
|
||||
checkin: (url) => `Check in: ${url}`,
|
||||
manage: (url) => `Manage booking: ${url}`,
|
||||
ticket: (url) => `Ticket: ${url}`,
|
||||
phone: (phone) => `Phone: ${phone}`,
|
||||
car: (car) => `Car: ${car}`,
|
||||
tickets: (numbers) => `Tickets: ${numbers}`,
|
||||
partySize: (count) => `Party size: ${count}`,
|
||||
rentalCar: (company) => (company ? `Rental car: ${company}` : 'Rental car'),
|
||||
addedFrom: (domain, subject) => (subject ? `Added from the mail "${subject}" from ${domain}` : `Added from a mail from ${domain}`),
|
||||
};
|
||||
|
||||
function addressLines(address: Address | undefined): string[] {
|
||||
if (!address) return [];
|
||||
const city = [address.postalCode, address.locality].filter(Boolean).join(' ');
|
||||
return [address.street, city].filter((line): line is string => !!line && line.trim() !== '');
|
||||
}
|
||||
|
||||
/** "Name, Street 1, 12345 City", skipping parts that repeat. */
|
||||
function joinParts(parts: (string | undefined)[]): string {
|
||||
const out: string[] = [];
|
||||
for (const part of parts) {
|
||||
const value = part?.trim();
|
||||
if (value && !out.some((p) => p.toLowerCase() === value.toLowerCase())) out.push(value);
|
||||
}
|
||||
return out.join(', ');
|
||||
}
|
||||
|
||||
/** "Frankfurt Airport (FRA)" for airports, name and address elsewhere. */
|
||||
function placeName(place: Place | undefined, kind: Booking['kind']): string {
|
||||
if (!place) return '';
|
||||
if (kind === 'flight') {
|
||||
if (place.name && place.code) return place.name.toUpperCase().includes(`(${place.code})`) ? place.name : `${place.name} (${place.code})`;
|
||||
return place.code ?? place.name ?? '';
|
||||
}
|
||||
return joinParts([place.name, ...addressLines(place.address)]);
|
||||
}
|
||||
|
||||
function shortPlace(place: Place | undefined, kind: Booking['kind']): string {
|
||||
if (!place) return '';
|
||||
return (kind === 'flight' ? place.code ?? place.name : place.name ?? place.code) ?? '';
|
||||
}
|
||||
|
||||
function legTitle(kind: TransportBooking['kind'], leg: TransportLeg): string {
|
||||
let service: string;
|
||||
if (kind === 'flight') {
|
||||
service = [leg.carrierCode ?? leg.carrier, leg.number].filter(Boolean).join(' ');
|
||||
} else if (leg.number && /[a-z]/i.test(leg.number)) {
|
||||
service = leg.number;
|
||||
} else {
|
||||
service = [leg.carrier, leg.number].filter(Boolean).join(' ');
|
||||
}
|
||||
const route = [shortPlace(leg.departure.place, kind), shortPlace(leg.arrival?.place, kind)].filter(Boolean).join(' → ');
|
||||
return [service, route].filter(Boolean).join(' ');
|
||||
}
|
||||
|
||||
function passengerLines(booking: Booking, leg: TransportLeg | undefined, labels: BookingEventLabels): string[] {
|
||||
const seats = leg?.seats ?? [];
|
||||
const seatsOf = (name: string) => seats.filter((s) => s.passenger === name).map((s) => s.seat);
|
||||
const names = booking.passengers.map((name) => {
|
||||
const own = seatsOf(name);
|
||||
return own.length > 0 ? `${name} (${own.join(', ')})` : name;
|
||||
});
|
||||
const unnamed = seats.filter((s) => !s.passenger || !booking.passengers.includes(s.passenger)).map((s) => s.seat);
|
||||
const lines: string[] = [];
|
||||
if (names.length > 0) lines.push(labels.passengers(names.join(', ')));
|
||||
if (unnamed.length > 0) lines.push(labels.seats(unnamed.join(', ')));
|
||||
return lines;
|
||||
}
|
||||
|
||||
function linkLines(booking: Booking, labels: BookingEventLabels): string[] {
|
||||
const lines: string[] = [];
|
||||
if (booking.links.checkin) lines.push(labels.checkin(booking.links.checkin.url));
|
||||
if (booking.links.manage) lines.push(labels.manage(booking.links.manage.url));
|
||||
if (booking.links.ticket) lines.push(labels.ticket(booking.links.ticket.url));
|
||||
return lines;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// events
|
||||
|
||||
export interface BookingEventOptions {
|
||||
/** Domain (or address) of the mail's From: names the sender and keys the uid. */
|
||||
senderDomain: string;
|
||||
/** Subject of the mail, named in the description so the user can find it. */
|
||||
mailSubjectOrProvider?: string;
|
||||
/** Translated description lines; English by default. */
|
||||
labels?: Partial<BookingEventLabels>;
|
||||
}
|
||||
|
||||
/** One event the booking wants in the calendar, or, for a cancelled leg, one it wants gone. */
|
||||
export interface BookingEventSlot {
|
||||
uid: string;
|
||||
/** The leg or booking is cancelled: never added, removed when present. */
|
||||
cancelled: boolean;
|
||||
/** The event to create or compare with; null when the times can't be made into one. */
|
||||
event: Partial<CalendarEvent> | null;
|
||||
}
|
||||
|
||||
type Locations = Record<string, CalendarLocation>;
|
||||
|
||||
function location(name: string, timeZone: string | null | undefined, relativeTo?: 'start' | 'end'): CalendarLocation {
|
||||
return {
|
||||
'@type': 'Location',
|
||||
name,
|
||||
...(timeZone ? { timeZone } : {}),
|
||||
...(relativeTo ? { relativeTo } : {}),
|
||||
} as CalendarLocation;
|
||||
}
|
||||
|
||||
function event(
|
||||
uid: string,
|
||||
title: string,
|
||||
when: { start: string; timeZone: string | null; duration: string; showWithoutTime: boolean },
|
||||
locations: Locations,
|
||||
description: string[],
|
||||
extra: Partial<CalendarEvent> = {},
|
||||
): Partial<CalendarEvent> {
|
||||
return {
|
||||
'@type': 'Event',
|
||||
uid,
|
||||
title,
|
||||
start: when.start,
|
||||
timeZone: when.timeZone,
|
||||
duration: when.duration,
|
||||
showWithoutTime: when.showWithoutTime,
|
||||
...(Object.keys(locations).length > 0 ? { locations } : {}),
|
||||
description: description.filter(Boolean).join('\n'),
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
/** A timed span from `from` to `to` (or zero length without `to`), or null when it can't be told exactly. */
|
||||
function timedSpan(from: Moment, to: Moment | undefined) {
|
||||
const start = instantOf(from);
|
||||
if (!start) return null;
|
||||
if (!to) return { start, end: null, when: { start: start.start, timeZone: start.timeZone, duration: 'PT0S', showWithoutTime: false } };
|
||||
const end = instantOf(to);
|
||||
if (!end) return null;
|
||||
const ms = spanBetween(start, end);
|
||||
if (ms === null) return null;
|
||||
return { start, end, when: { start: start.start, timeZone: start.timeZone, duration: timedDuration(ms), showWithoutTime: false } };
|
||||
}
|
||||
|
||||
function legEvent(booking: TransportBooking, leg: TransportLeg, uid: string, tail: string[], labels: BookingEventLabels): Partial<CalendarEvent> | null {
|
||||
const span = timedSpan(leg.departure.at, leg.arrival?.at);
|
||||
if (!span) return null;
|
||||
const locations: Locations = {
|
||||
dep: location(placeName(leg.departure.place, booking.kind), span.start.timeZone, 'start'),
|
||||
};
|
||||
if (leg.arrival && placeName(leg.arrival.place, booking.kind)) {
|
||||
locations.arr = location(placeName(leg.arrival.place, booking.kind), span.end?.timeZone, 'end');
|
||||
}
|
||||
return event(uid, legTitle(booking.kind, leg), span.when, locations, [
|
||||
booking.reference ? labels.reference(booking.reference) : '',
|
||||
...passengerLines(booking, leg, labels),
|
||||
...linkLines(booking, labels),
|
||||
...tail,
|
||||
]);
|
||||
}
|
||||
|
||||
function slotsOf(booking: Booking, options: BookingEventOptions): BookingEventSlot[] {
|
||||
const labels: BookingEventLabels = { ...ENGLISH_LABELS, ...options.labels };
|
||||
const domain = organizationalDomain(senderDomainOf(options.senderDomain));
|
||||
const tail = [labels.addedFrom(domain, options.mailSubjectOrProvider?.trim() || undefined)];
|
||||
const reference = booking.reference ? labels.reference(booking.reference) : '';
|
||||
const cancelled = booking.status === 'cancelled';
|
||||
|
||||
switch (booking.kind) {
|
||||
case 'flight': case 'train': case 'bus': {
|
||||
const uids = booking.legs.map((leg) => bookingEventUid(booking, options.senderDomain, leg));
|
||||
return booking.legs.map((leg, i) => {
|
||||
// Legs sharing a uid can't each have their own event: offer nothing
|
||||
// for them, neither adding nor removing.
|
||||
const shared = uids.indexOf(uids[i]) !== uids.lastIndexOf(uids[i]);
|
||||
return {
|
||||
uid: uids[i],
|
||||
cancelled: !shared && (cancelled || leg.status === 'cancelled'),
|
||||
event: shared ? null : legEvent(booking, leg, uids[i], tail, labels),
|
||||
};
|
||||
});
|
||||
}
|
||||
case 'lodging': {
|
||||
const uid = bookingEventUid(booking, options.senderDomain);
|
||||
const hotel = joinParts([booking.name, ...addressLines(booking.address)]);
|
||||
const description = [
|
||||
reference,
|
||||
...passengerLines(booking, undefined, labels),
|
||||
booking.phone ? labels.phone(booking.phone) : '',
|
||||
...linkLines(booking, labels),
|
||||
...tail,
|
||||
];
|
||||
const timed = booking.checkin.time && booking.checkout.time ? timedSpan(booking.checkin, booking.checkout) : null;
|
||||
if (timed) {
|
||||
return [{
|
||||
uid, cancelled,
|
||||
event: event(uid, booking.name, timed.when, { hotel: location(hotel, timed.start.timeZone) }, description, { freeBusyStatus: 'free' }),
|
||||
}];
|
||||
}
|
||||
const days = allDaySpan(booking.checkin.date, booking.checkout.date);
|
||||
return [{
|
||||
uid, cancelled,
|
||||
event: days
|
||||
? event(uid, booking.name, { ...days, timeZone: null, showWithoutTime: true }, { hotel: location(hotel, null) }, description, { freeBusyStatus: 'free' })
|
||||
: null,
|
||||
}];
|
||||
}
|
||||
case 'rentalcar': {
|
||||
const uid = bookingEventUid(booking, options.senderDomain);
|
||||
const span = timedSpan(booking.pickup.at, booking.dropoff.at);
|
||||
const locations: Locations = {};
|
||||
if (span) {
|
||||
const pickup = placeName(booking.pickup.place, booking.kind);
|
||||
const dropoff = placeName(booking.dropoff.place, booking.kind);
|
||||
if (pickup) locations.pickup = location(pickup, span.start.timeZone, 'start');
|
||||
if (dropoff) locations.dropoff = location(dropoff, span.end?.timeZone, 'end');
|
||||
}
|
||||
return [{
|
||||
uid, cancelled,
|
||||
event: span
|
||||
? event(uid, labels.rentalCar(booking.company ?? booking.provider), span.when, locations, [
|
||||
reference,
|
||||
booking.car ? labels.car(booking.car) : '',
|
||||
...linkLines(booking, labels),
|
||||
...tail,
|
||||
], { freeBusyStatus: 'free' })
|
||||
: null,
|
||||
}];
|
||||
}
|
||||
case 'event': {
|
||||
const uid = bookingEventUid(booking, options.senderDomain);
|
||||
const description = [
|
||||
reference,
|
||||
booking.tickets.length > 0 ? labels.tickets(booking.tickets.join(', ')) : '',
|
||||
...linkLines(booking, labels),
|
||||
...tail,
|
||||
];
|
||||
let made: Partial<CalendarEvent> | null = null;
|
||||
if (booking.start.time) {
|
||||
// An end date without time already became 23:59:59 in the parser; no
|
||||
// end at all is zero length (Stalwart would stretch a missing
|
||||
// duration to the end of the day).
|
||||
const span = timedSpan(booking.start, booking.end);
|
||||
const venue = placeName(booking.venue, booking.kind);
|
||||
if (span) made = event(uid, booking.name, span.when, venue ? { venue: location(venue, span.start.timeZone) } : {}, description);
|
||||
} else {
|
||||
const days = allDaySpan(booking.start.date, booking.end?.date ?? booking.start.date);
|
||||
const venue = placeName(booking.venue, booking.kind);
|
||||
if (days) made = event(uid, booking.name, { ...days, timeZone: null, showWithoutTime: true }, venue ? { venue: location(venue, null) } : {}, description);
|
||||
}
|
||||
return [{ uid, cancelled, event: made }];
|
||||
}
|
||||
case 'restaurant': {
|
||||
const uid = bookingEventUid(booking, options.senderDomain);
|
||||
const span = timedSpan(booking.start, undefined);
|
||||
const place = joinParts([booking.name, ...addressLines(booking.address)]);
|
||||
return [{
|
||||
uid, cancelled,
|
||||
event: span
|
||||
? event(uid, booking.name, span.when, { restaurant: location(place, span.start.timeZone) }, [
|
||||
reference,
|
||||
booking.partySize ? labels.partySize(booking.partySize) : '',
|
||||
...linkLines(booking, labels),
|
||||
...tail,
|
||||
])
|
||||
: null,
|
||||
}];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Every event a booking stands for, cancelled ones included (the caller
|
||||
* offers to remove those). Nothing for shipments.
|
||||
*/
|
||||
export function bookingEventSlots(card: StructuredCard, options: BookingEventOptions): BookingEventSlot[] {
|
||||
if (card.kind === 'shipment') return [];
|
||||
return slotsOf(card, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* The events to add for a booking: one per live leg, or one for the stay,
|
||||
* car, event or table. Cancelled legs give none. No calendarIds (the caller
|
||||
* picks the calendar), no participants, never scheduling messages.
|
||||
*/
|
||||
export function bookingToEvents(booking: Booking, options: BookingEventOptions): Partial<CalendarEvent>[] {
|
||||
return slotsOf(booking, options)
|
||||
.filter((slot) => !slot.cancelled && slot.event)
|
||||
.map((slot) => slot.event as Partial<CalendarEvent>);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// comparing with what the calendar holds
|
||||
|
||||
const DURATION = /^P(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?)?$/;
|
||||
|
||||
/** Nominal days and exact seconds of a duration; P1D and PT24H differ. */
|
||||
function durationParts(value: string | undefined | null): string {
|
||||
const match = value ? DURATION.exec(value) : null;
|
||||
if (!match) return value ?? '';
|
||||
const days = Number(match[1] ?? 0) * 7 + Number(match[2] ?? 0);
|
||||
const seconds = Number(match[3] ?? 0) * 3600 + Number(match[4] ?? 0) * 60 + Number(match[5] ?? 0);
|
||||
return `${days}d${seconds}s`;
|
||||
}
|
||||
|
||||
const localDateTime = (value: string | undefined | null) => {
|
||||
const v = value ?? '';
|
||||
return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/.test(v) ? `${v}:00` : v.slice(0, 19);
|
||||
};
|
||||
|
||||
/** Location ids this module writes; other locations on the event are the user's. */
|
||||
const OWN_LOCATION_IDS = ['dep', 'arr', 'hotel', 'pickup', 'dropoff', 'venue', 'restaurant'];
|
||||
|
||||
/**
|
||||
* An event as the calendar store saves it and reads it back: a timed event
|
||||
* from midnight that lasts whole days becomes all-day without a zone
|
||||
* (sanitizeOutgoingCalendarEventData on the way out, normalizeCalendarEventLike
|
||||
* on the way in). Comparing against this keeps such an event from looking
|
||||
* changed for ever.
|
||||
*/
|
||||
export function asStoredEvent(event: Partial<CalendarEvent>): Partial<CalendarEvent> {
|
||||
return normalizeCalendarEventLike(sanitizeOutgoingCalendarEventData({ ...event }));
|
||||
}
|
||||
|
||||
/** Start, duration, all-day flag or zone differ (the zone only matters for timed events). */
|
||||
function whenDiffers(have: Partial<CalendarEvent>, want: Partial<CalendarEvent>): boolean {
|
||||
if (localDateTime(have.start) !== localDateTime(want.start)) return true;
|
||||
if (durationParts(have.duration ?? 'PT0S') !== durationParts(want.duration ?? 'PT0S')) return true;
|
||||
if (Boolean(have.showWithoutTime) !== Boolean(want.showWithoutTime)) return true;
|
||||
if (want.showWithoutTime) return false;
|
||||
return (have.timeZone ?? null) !== (want.timeZone ?? null);
|
||||
}
|
||||
|
||||
/** A location this module writes is missing, extra, or has another name, zone or anchor. */
|
||||
function locationsDiffer(have: Partial<CalendarEvent>, want: Partial<CalendarEvent>): boolean {
|
||||
const a = have.locations ?? {};
|
||||
const b = want.locations ?? {};
|
||||
return OWN_LOCATION_IDS.some((id) => {
|
||||
const x = a[id];
|
||||
const y = b[id];
|
||||
if (!x && !y) return false;
|
||||
if (!x || !y) return true;
|
||||
return (x.name ?? '') !== (y.name ?? '')
|
||||
|| (x.timeZone ?? null) !== (y.timeZone ?? null)
|
||||
|| (x.relativeTo ?? null) !== (y.relativeTo ?? null);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the stored event sits somewhere else than the booking now says:
|
||||
* start, duration, all-day flag, zone (timed events only) and the name, zone
|
||||
* and anchor of each location this module writes. Field by field, because
|
||||
* the server adds defaults (`iCalendar`, nulls). Title and description are
|
||||
* not compared: the user may have renamed the event or written notes, and a
|
||||
* title in another language is no reason to offer "Update".
|
||||
*/
|
||||
export function eventNeedsUpdate(existing: Partial<CalendarEvent>, wanted: Partial<CalendarEvent>): boolean {
|
||||
const have = normalizeCalendarEventLike({ ...existing });
|
||||
const want = asStoredEvent(wanted);
|
||||
return whenDiffers(have, want) || locationsDiffer(have, want);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a stored description is still the one this module wrote: its last
|
||||
* line names the sender's organisational domain ("Added from a mail from
|
||||
* lufthansa.com"). Notes the user added below it make it theirs.
|
||||
*/
|
||||
function isOwnDescription(description: string | null | undefined, senderDomain: string | undefined): boolean {
|
||||
if (!description || !senderDomain) return false;
|
||||
const domain = organizationalDomain(senderDomainOf(senderDomain));
|
||||
const lines = description.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
|
||||
return domain !== '' && (lines[lines.length - 1] ?? '').includes(domain);
|
||||
}
|
||||
|
||||
/**
|
||||
* The patch that brings a stored event to the booking's current state,
|
||||
* touching only what differs: the times as one group (start, duration, zone,
|
||||
* all-day flag, so the store can't half-convert them), and the locations this
|
||||
* module writes (the user's own locations stay). Title and description change
|
||||
* only while the description is still the generated one (see
|
||||
* isOwnDescription), so user edits survive. `senderDomain` is the mail's From
|
||||
* domain; without it title and description are never touched.
|
||||
*/
|
||||
export function bookingEventPatch(existing: Partial<CalendarEvent>, wanted: Partial<CalendarEvent>, senderDomain?: string): Partial<CalendarEvent> {
|
||||
const have = normalizeCalendarEventLike({ ...existing });
|
||||
const want = asStoredEvent(wanted);
|
||||
const patch: Partial<CalendarEvent> = {};
|
||||
if (whenDiffers(have, want)) {
|
||||
patch.start = want.start;
|
||||
patch.duration = want.duration;
|
||||
patch.timeZone = want.timeZone ?? null;
|
||||
patch.showWithoutTime = Boolean(want.showWithoutTime);
|
||||
}
|
||||
if (locationsDiffer(have, want)) {
|
||||
const locations: Locations = {};
|
||||
for (const [id, loc] of Object.entries(existing.locations ?? {})) {
|
||||
if (!OWN_LOCATION_IDS.includes(id)) locations[id] = loc;
|
||||
}
|
||||
Object.assign(locations, want.locations ?? {});
|
||||
patch.locations = Object.keys(locations).length > 0 ? locations : null;
|
||||
}
|
||||
if (isOwnDescription(existing.description, senderDomain)) {
|
||||
if (want.title !== undefined && want.title !== existing.title) patch.title = want.title;
|
||||
if (want.description !== undefined && want.description !== existing.description) patch.description = want.description;
|
||||
}
|
||||
return patch;
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,161 @@
|
||||
// Mirrors webmail lib/calendar-event-normalization.ts at 49beaef85; keep the two in step.
|
||||
import { parseISO } from 'date-fns';
|
||||
import type { CalendarEvent } from '../../api/types';
|
||||
|
||||
const DURATION_RE = /^P(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/;
|
||||
|
||||
function isDateOnlyValue(value: string): boolean {
|
||||
return /^\d{4}-\d{2}-\d{2}$/.test(value);
|
||||
}
|
||||
|
||||
function isMidnightValue(value: string): boolean {
|
||||
if (isDateOnlyValue(value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const match = value.match(/T(\d{2}):(\d{2})(?::(\d{2})(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})?$/);
|
||||
if (!match) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return match[1] === '00' && match[2] === '00' && (match[3] ?? '00') === '00';
|
||||
}
|
||||
|
||||
export function parseDurationSeconds(duration: string | undefined): number | null {
|
||||
if (!duration) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const match = DURATION_RE.exec(duration);
|
||||
if (!match) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const weeks = parseInt(match[1] || '0', 10);
|
||||
const days = parseInt(match[2] || '0', 10);
|
||||
const hours = parseInt(match[3] || '0', 10);
|
||||
const minutes = parseInt(match[4] || '0', 10);
|
||||
const seconds = parseInt(match[5] || '0', 10);
|
||||
|
||||
return (((weeks * 7 + days) * 24 + hours) * 60 + minutes) * 60 + seconds;
|
||||
}
|
||||
|
||||
export function normalizeAllDayDurationValue(duration: string | undefined): string | undefined {
|
||||
const totalSeconds = parseDurationSeconds(duration);
|
||||
if (totalSeconds === null || totalSeconds < 86400 || totalSeconds % 86400 !== 0) {
|
||||
return duration;
|
||||
}
|
||||
|
||||
return `P${totalSeconds / 86400}D`;
|
||||
}
|
||||
|
||||
export function isAllDayEventLike(event: Pick<Partial<CalendarEvent>, 'start' | 'duration' | 'showWithoutTime'>): boolean {
|
||||
if (event.showWithoutTime) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!event.start || !event.duration || !isMidnightValue(event.start)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const totalSeconds = parseDurationSeconds(event.duration);
|
||||
if (totalSeconds === null || totalSeconds < 86400 || totalSeconds % 86400 !== 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const start = parseISO(event.start);
|
||||
if (Number.isNaN(start.getTime())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const end = new Date(start.getTime() + totalSeconds * 1000);
|
||||
return end.getHours() === 0
|
||||
&& end.getMinutes() === 0
|
||||
&& end.getSeconds() === 0
|
||||
&& end.getMilliseconds() === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stalwart returns "recurrenceRule" (singular) instead of RFC 8984 "recurrenceRules" (plural).
|
||||
* Normalize server responses to match the client's internal type.
|
||||
*/
|
||||
function normalizeStalwartPropertyNames<T extends Partial<CalendarEvent>>(event: T): T {
|
||||
const raw = event as Record<string, unknown>;
|
||||
let patched = false;
|
||||
const updates: Partial<CalendarEvent> = {};
|
||||
|
||||
if ('recurrenceRule' in raw && !('recurrenceRules' in raw)) {
|
||||
// JSCalendar 2.0 (jscalendarbis-15) defines recurrenceRule as a single object,
|
||||
// but Stalwart may also return it as an array (for JMAP-created events).
|
||||
// Normalize both forms to our internal array type.
|
||||
const val = raw.recurrenceRule;
|
||||
if (val != null && !Array.isArray(val) && typeof val === 'object') {
|
||||
updates.recurrenceRules = [val] as CalendarEvent['recurrenceRules'];
|
||||
} else {
|
||||
updates.recurrenceRules = val as CalendarEvent['recurrenceRules'];
|
||||
}
|
||||
patched = true;
|
||||
}
|
||||
if ('excludedRecurrenceRule' in raw && !('excludedRecurrenceRules' in raw)) {
|
||||
const val = raw.excludedRecurrenceRule;
|
||||
if (val != null && !Array.isArray(val) && typeof val === 'object') {
|
||||
updates.excludedRecurrenceRules = [val] as CalendarEvent['excludedRecurrenceRules'];
|
||||
} else {
|
||||
updates.excludedRecurrenceRules = val as CalendarEvent['excludedRecurrenceRules'];
|
||||
}
|
||||
patched = true;
|
||||
}
|
||||
|
||||
if (!patched) return event;
|
||||
|
||||
const result = { ...event, ...updates } as T;
|
||||
delete (result as Record<string, unknown>).recurrenceRule;
|
||||
delete (result as Record<string, unknown>).excludedRecurrenceRule;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* A timed event without a duration lasts zero seconds (RFC 8984 default
|
||||
* PT0S). Stalwart drops a DTEND equal to DTSTART and then ends such an
|
||||
* event at 23:59:59 of its start day, so its utcEnd is wrong. Only server
|
||||
* objects carry utcStart, which keeps partial update patches untouched. (#837)
|
||||
*/
|
||||
function withZeroDurationEnd<T extends Partial<CalendarEvent>>(event: T): T {
|
||||
if (event.showWithoutTime || event.duration || !event.start || !event.utcStart) {
|
||||
return event;
|
||||
}
|
||||
return { ...event, duration: 'PT0S', utcEnd: event.utcStart } as T;
|
||||
}
|
||||
|
||||
export function normalizeCalendarEventLike<T extends Partial<CalendarEvent>>(event: T): T {
|
||||
// First normalize Stalwart's singular property names to RFC 8984 plural forms
|
||||
const normalized = normalizeStalwartPropertyNames(event);
|
||||
|
||||
if (!isAllDayEventLike(normalized)) {
|
||||
return withZeroDurationEnd(normalized);
|
||||
}
|
||||
|
||||
return {
|
||||
...normalized,
|
||||
showWithoutTime: true,
|
||||
duration: normalizeAllDayDurationValue(normalized.duration),
|
||||
} as T;
|
||||
}
|
||||
|
||||
export function sanitizeOutgoingCalendarEventData<T extends Partial<CalendarEvent>>(event: T): T {
|
||||
const normalized = normalizeCalendarEventLike(event);
|
||||
if (!normalized.showWithoutTime) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
const normalizedStart = normalized.start
|
||||
? `${normalized.start.slice(0, 10)}T00:00:00`
|
||||
: normalized.start;
|
||||
|
||||
return {
|
||||
...normalized,
|
||||
start: normalizedStart,
|
||||
duration: normalizeAllDayDurationValue(normalized.duration),
|
||||
timeZone: null,
|
||||
} as T;
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
// Mirrors webmail lib/structured-mail/extract.ts at 76b0c2fba; keep the two in step.
|
||||
/**
|
||||
* Pull schema.org data out of a mail's raw HTML: JSON-LD blocks and
|
||||
* microdata. The HTML is parsed into a detached document (DOMParser runs no
|
||||
* scripts and loads nothing) that is never attached to the page; the sanitizer
|
||||
* that renders the mail is a separate path and isn't touched.
|
||||
*/
|
||||
import { parseHtmlDocument } from './html-parser';
|
||||
import { parseLenientJson } from './json-repair';
|
||||
|
||||
export type RawItem = Record<string, unknown>;
|
||||
export interface ExtractedItem {
|
||||
item: unknown;
|
||||
source: 'json-ld' | 'microdata';
|
||||
}
|
||||
|
||||
const MAX_JSONLD_BLOCKS = 50;
|
||||
const MAX_MICRODATA_ITEMS = 500;
|
||||
const MAX_MICRODATA_DEPTH = 12;
|
||||
const MAX_HTML_LENGTH = 8_000_000;
|
||||
|
||||
/** Cheap test that a body may hold markup this feature reads. */
|
||||
const TYPE_HINT = /Reservation|ParcelDelivery|Order/;
|
||||
const SCHEMA_ITEMTYPE = /^https?:\/\/(?:www\.)?schema\.org\/([A-Za-z]+)\/?$/i;
|
||||
|
||||
/**
|
||||
* Mail clients mark what they quote or forward with these containers;
|
||||
* markup inside them belongs to someone else's mail.
|
||||
*/
|
||||
const QUOTED_CONTAINERS = 'blockquote, .gmail_quote, .moz-forward-container, #appendonsend, #divRplyFwdMsg';
|
||||
|
||||
// App: React Native has no DOMParser; html-parser.ts builds the detached tree.
|
||||
export function defaultParseHtml(html: string): Document {
|
||||
return parseHtmlDocument(html);
|
||||
}
|
||||
|
||||
export function mayContainStructuredData(html: string): boolean {
|
||||
return TYPE_HINT.test(html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Elements that sit inside a quoted or forwarded part: inside one of the
|
||||
* containers, or after Outlook's `#divRplyFwdMsg` separator (Outlook puts
|
||||
* the forwarded message after it, not inside it).
|
||||
*/
|
||||
function isQuoted(element: Element, doc: Document): boolean {
|
||||
if (element.closest(QUOTED_CONTAINERS)) return true;
|
||||
const separator = doc.getElementById('divRplyFwdMsg');
|
||||
return !!separator && !!(separator.compareDocumentPosition(element) & 4 /* FOLLOWING */);
|
||||
}
|
||||
|
||||
function itemTypes(element: Element): string[] {
|
||||
return (element.getAttribute('itemtype') ?? '')
|
||||
.split(/\s+/)
|
||||
.map((value) => SCHEMA_ITEMTYPE.exec(value)?.[1])
|
||||
.filter((value): value is string => !!value);
|
||||
}
|
||||
|
||||
function propertyValue(element: Element, depth: number): unknown {
|
||||
if (element.hasAttribute('itemscope')) return microdataItem(element, depth + 1);
|
||||
const content = element.getAttribute('content');
|
||||
if (content !== null) return content.trim();
|
||||
const tag = element.tagName.toLowerCase();
|
||||
const attr = (name: string) => (element.getAttribute(name) ?? '').trim();
|
||||
switch (tag) {
|
||||
case 'a': case 'link': case 'area': return attr('href');
|
||||
case 'img': case 'audio': case 'video': case 'source': case 'embed': case 'iframe': case 'track': return attr('src');
|
||||
case 'object': return attr('data');
|
||||
case 'time': return element.hasAttribute('datetime') ? attr('datetime') : (element.textContent ?? '').trim();
|
||||
case 'data': case 'meter': return attr('value');
|
||||
default: return (element.textContent ?? '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One microdata item: properties are descendants with `itemprop` whose
|
||||
* nearest `itemscope` ancestor is this item. A space-separated itemprop gives
|
||||
* the value to each name; a repeated name becomes an array.
|
||||
*/
|
||||
function microdataItem(scope: Element, depth: number): RawItem | undefined {
|
||||
if (depth > MAX_MICRODATA_DEPTH) return undefined;
|
||||
const item: RawItem = {};
|
||||
const types = itemTypes(scope);
|
||||
if (types.length > 0) item['@type'] = types.length === 1 ? types[0] : types;
|
||||
const add = (name: string, value: unknown) => {
|
||||
if (value === undefined || value === '') return;
|
||||
const existing = item[name];
|
||||
if (existing === undefined) item[name] = value;
|
||||
else if (Array.isArray(existing)) existing.push(value);
|
||||
else item[name] = [existing, value];
|
||||
};
|
||||
for (const element of Array.from(scope.querySelectorAll('[itemprop]'))) {
|
||||
const owner = element.parentElement?.closest('[itemscope]');
|
||||
if (owner !== scope) continue;
|
||||
const value = propertyValue(element, depth);
|
||||
for (const name of (element.getAttribute('itemprop') ?? '').split(/\s+/).filter(Boolean)) add(name, value);
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
export function extractStructuredData(
|
||||
html: string,
|
||||
parseHtml: (html: string) => Document = defaultParseHtml,
|
||||
): ExtractedItem[] {
|
||||
if (!html || html.length > MAX_HTML_LENGTH || !mayContainStructuredData(html)) return [];
|
||||
const doc = parseHtml(html);
|
||||
const found: ExtractedItem[] = [];
|
||||
|
||||
const scripts = Array.from(doc.querySelectorAll('script')).filter(
|
||||
(script) => (script.getAttribute('type') ?? '').trim().toLowerCase() === 'application/ld+json',
|
||||
);
|
||||
for (const script of scripts.slice(0, MAX_JSONLD_BLOCKS)) {
|
||||
if (isQuoted(script, doc)) continue;
|
||||
const value = parseLenientJson(script.textContent ?? '');
|
||||
if (value !== undefined && value !== null) found.push({ item: value, source: 'json-ld' });
|
||||
}
|
||||
|
||||
const tops = Array.from(doc.querySelectorAll('[itemscope]')).filter((element) => !element.hasAttribute('itemprop'));
|
||||
for (const scope of tops.slice(0, MAX_MICRODATA_ITEMS)) {
|
||||
// Only items typed on schema.org count; an untyped or foreign item is noise.
|
||||
if (itemTypes(scope).length === 0 || isQuoted(scope, doc)) continue;
|
||||
const item = microdataItem(scope, 0);
|
||||
if (item) found.push({ item: { '@context': 'https://schema.org', ...item }, source: 'microdata' });
|
||||
}
|
||||
return found;
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
// Mirrors webmail lib/structured-mail/format.ts at b0782af2d; keep the two in step.
|
||||
/**
|
||||
* Text for the reader cards. Plain TypeScript, no React.
|
||||
*
|
||||
* A card shows a time the way the mail gave it: the wall clock at the place
|
||||
* (departure airport, hotel, venue), never converted to the reader's zone.
|
||||
* The local date and time are formatted as if they were UTC, with
|
||||
* `timeZone: 'UTC'`, so neither the browser's zone nor DST can move them.
|
||||
*/
|
||||
import { zoneOffsetAt } from './time-zones';
|
||||
import type { Address, Item, Moment, Place, Shipment, TransportBooking, TransportLeg } from './types';
|
||||
|
||||
const MINUTE = 60_000;
|
||||
const DAY = 24 * 60 * MINUTE;
|
||||
|
||||
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
||||
const TIME_RE = /^\d{2}:\d{2}(?::\d{2})?$/;
|
||||
|
||||
export interface FormatOptions {
|
||||
/** UI locale ("en", "de", ...). */
|
||||
locale: string;
|
||||
/** true for a 12-hour clock, false for 24 hours, undefined for the locale's own. */
|
||||
hour12?: boolean;
|
||||
/** "Now", to decide whether the year is shown. Defaults to the current date. */
|
||||
now?: Date;
|
||||
}
|
||||
|
||||
/** The moment's local wall clock as a Date whose UTC fields hold it; null when malformed. */
|
||||
export function wallClockDate(m: Moment): Date | null {
|
||||
if (!DATE_RE.test(m.date)) return null;
|
||||
if (m.time !== undefined && !TIME_RE.test(m.time)) return null;
|
||||
const time = m.time ? (m.time.length === 5 ? `${m.time}:00` : m.time) : '00:00:00';
|
||||
const d = new Date(`${m.date}T${time}Z`);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function formatter(options: Intl.DateTimeFormatOptions, opts: FormatOptions): Intl.DateTimeFormat {
|
||||
try {
|
||||
return new Intl.DateTimeFormat(opts.locale, { ...options, timeZone: 'UTC' });
|
||||
} catch {
|
||||
return new Intl.DateTimeFormat('en', { ...options, timeZone: 'UTC' });
|
||||
}
|
||||
}
|
||||
|
||||
function clockOptions(opts: FormatOptions): Intl.DateTimeFormatOptions {
|
||||
if (opts.hour12 === true) return { hour: 'numeric', minute: '2-digit', hour12: true };
|
||||
if (opts.hour12 === false) return { hour: '2-digit', minute: '2-digit', hourCycle: 'h23' };
|
||||
return { hour: 'numeric', minute: '2-digit' };
|
||||
}
|
||||
|
||||
function dateOptions(m: Moment, opts: FormatOptions): Intl.DateTimeFormatOptions {
|
||||
const year = Number(m.date.slice(0, 4));
|
||||
const currentYear = (opts.now ?? new Date()).getUTCFullYear();
|
||||
return { weekday: 'short', day: 'numeric', month: 'short', ...(year !== currentYear ? { year: 'numeric' } : {}) };
|
||||
}
|
||||
|
||||
/** "Tue, Dec 1" (the year only when it isn't this year). */
|
||||
export function formatMomentDate(m: Moment, opts: FormatOptions): string {
|
||||
const d = wallClockDate(m);
|
||||
return d ? formatter(dateOptions(m, opts), opts).format(d) : m.date;
|
||||
}
|
||||
|
||||
/** "10:15", or undefined for a date-only moment. */
|
||||
export function formatMomentTime(m: Moment, opts: FormatOptions): string | undefined {
|
||||
if (!m.time) return undefined;
|
||||
const d = wallClockDate(m);
|
||||
return d ? formatter(clockOptions(opts), opts).format(d) : m.time.slice(0, 5);
|
||||
}
|
||||
|
||||
/** "Tue, Dec 1, 10:15" in the locale's own order; the date alone for a date-only moment. */
|
||||
export function formatMoment(m: Moment, opts: FormatOptions): string {
|
||||
if (!m.time) return formatMomentDate(m, opts);
|
||||
const d = wallClockDate(m);
|
||||
if (!d) return `${m.date} ${m.time.slice(0, 5)}`;
|
||||
return formatter({ ...dateOptions(m, opts), ...clockOptions(opts) }, opts).format(d);
|
||||
}
|
||||
|
||||
/** Whether two moments fall on the same local date. */
|
||||
export function sameLocalDate(a: Moment, b: Moment): boolean {
|
||||
return a.date === b.date;
|
||||
}
|
||||
|
||||
/**
|
||||
* The moment's UTC offset in minutes east of UTC: the one the mail gave, or
|
||||
* the one its resolved zone implies. Undefined for a floating time (shown as
|
||||
* written, no zone known) or a date-only moment.
|
||||
*/
|
||||
export function momentOffsetMinutes(m: Moment): number | undefined {
|
||||
if (!m.time) return undefined;
|
||||
if (m.offsetMinutes !== undefined) return m.offsetMinutes;
|
||||
if (!m.utc) return undefined;
|
||||
const wall = wallClockDate(m);
|
||||
const instant = Date.parse(m.utc);
|
||||
if (!wall || Number.isNaN(instant)) return undefined;
|
||||
return Math.round((wall.getTime() - instant) / MINUTE);
|
||||
}
|
||||
|
||||
function instantOf(m: Moment, offset: number): number | undefined {
|
||||
if (m.utc) {
|
||||
const t = Date.parse(m.utc);
|
||||
if (!Number.isNaN(t)) return t;
|
||||
}
|
||||
const wall = wallClockDate(m);
|
||||
return wall ? wall.getTime() - offset * MINUTE : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a timed moment's place keeps a different clock than the reader
|
||||
* at that instant, so the card marks the time as local to the place.
|
||||
* `readerOffsetAt` gives the reader's offset (minutes east of UTC) at an
|
||||
* instant; by default the offset of `readerZone`.
|
||||
*/
|
||||
export function isOtherZone(m: Moment, readerZone: string, readerOffsetAt?: (utcMs: number) => number): boolean {
|
||||
const offset = momentOffsetMinutes(m);
|
||||
if (offset === undefined) return false;
|
||||
const instant = instantOf(m, offset);
|
||||
if (instant === undefined) return false;
|
||||
try {
|
||||
const reader = readerOffsetAt ? readerOffsetAt(instant) : zoneOffsetAt(readerZone, instant);
|
||||
return reader !== offset;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Nights between check-in and check-out dates; undefined when it doesn't add up. */
|
||||
export function nightsBetween(checkin: Moment, checkout: Moment): number | undefined {
|
||||
const a = wallClockDate({ date: checkin.date });
|
||||
const b = wallClockDate({ date: checkout.date });
|
||||
if (!a || !b) return undefined;
|
||||
const nights = Math.round((b.getTime() - a.getTime()) / DAY);
|
||||
return nights > 0 ? nights : undefined;
|
||||
}
|
||||
|
||||
/** "€129.90" in the UI locale; amount and code as given when Intl can't format them. */
|
||||
export function formatMoney(total: { amount: string; currency: string }, locale: string): string {
|
||||
const n = Number(total.amount);
|
||||
if (!Number.isFinite(n)) return `${total.amount} ${total.currency}`;
|
||||
try {
|
||||
return new Intl.NumberFormat(locale, { style: 'currency', currency: total.currency }).format(n);
|
||||
} catch {
|
||||
return `${total.amount} ${total.currency}`;
|
||||
}
|
||||
}
|
||||
|
||||
function regionName(code: string | undefined, locale: string): string | undefined {
|
||||
if (!code) return undefined;
|
||||
try {
|
||||
return new Intl.DisplayNames([locale], { type: 'region' }).of(code) ?? code;
|
||||
} catch {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
/** One line: "Marktplatz 1, 10178 Berlin, Germany". */
|
||||
export function formatAddress(address: Address | undefined, locale: string): string | undefined {
|
||||
if (!address) return undefined;
|
||||
const town = [address.postalCode, address.locality].filter(Boolean).join(' ');
|
||||
const country = address.countryName ?? regionName(address.country, locale);
|
||||
const parts = [address.street, town, address.region, country].filter((p): p is string => !!p && p.trim() !== '');
|
||||
// A street given as one string may already hold the town.
|
||||
const unique = parts.filter((p, i) => !parts.slice(0, i).some((q) => q.includes(p)));
|
||||
return unique.length ? unique.join(', ') : undefined;
|
||||
}
|
||||
|
||||
/** A place on one line: "Stadtpark, Hamburg"; the name alone when it already names the town. */
|
||||
export function formatPlace(place: Place | undefined): string | undefined {
|
||||
if (!place) return undefined;
|
||||
const name = place.name?.trim();
|
||||
const town = place.address?.locality?.trim();
|
||||
if (name && town && !name.toLowerCase().includes(town.toLowerCase())) return `${name}, ${town}`;
|
||||
return name || town || place.code;
|
||||
}
|
||||
|
||||
/** An airport or station in a leg row: "Frankfurt Airport (FRA)", "Hamburg Hbf". */
|
||||
export function legPlaceLabel(place: Place, kind: TransportBooking['kind']): string {
|
||||
const name = place.name?.trim() || place.address?.locality?.trim();
|
||||
if (kind === 'flight' && place.code) {
|
||||
if (!name) return place.code;
|
||||
// "Frankfurt" holds the letters FRA; only a code standing on its own counts.
|
||||
const code = place.code.toUpperCase().replace(/[^A-Z0-9]/g, '');
|
||||
const hasCode = new RegExp(`(^|[^A-Z0-9])${code}([^A-Z0-9]|$)`).test(name.toUpperCase());
|
||||
return hasCode ? name : `${name} (${place.code})`;
|
||||
}
|
||||
return name || place.code || '';
|
||||
}
|
||||
|
||||
/** Carrier and number: "Lufthansa LH 400", "Rail Co ICE 577". */
|
||||
export function legService(leg: TransportLeg): string | undefined {
|
||||
const number = leg.carrierCode && leg.number && !leg.number.toUpperCase().startsWith(leg.carrierCode.toUpperCase())
|
||||
? `${leg.carrierCode} ${leg.number}`
|
||||
: leg.number;
|
||||
const parts = [leg.carrier, number].filter((p): p is string => !!p);
|
||||
return parts.length ? parts.join(' ') : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* The stops of a route, for its title: airport codes for flights, city or
|
||||
* station names otherwise. Connecting legs form one run (FRA, JFK, FRA);
|
||||
* a leg that doesn't connect starts the next run.
|
||||
*/
|
||||
export function routeRuns(booking: TransportBooking): string[][] {
|
||||
const legs = booking.legs;
|
||||
const towns = legs.flatMap((leg) => [leg.departure.place, leg.arrival?.place]).filter((p): p is Place => !!p)
|
||||
.map((p) => p.address?.locality?.trim());
|
||||
const useTowns = booking.kind !== 'flight' && towns.every(Boolean)
|
||||
&& legs.every((leg) => !leg.arrival || leg.departure.place.address?.locality !== leg.arrival.place.address?.locality);
|
||||
const label = (place: Place): string => {
|
||||
if (booking.kind === 'flight' && place.code) return place.code;
|
||||
if (useTowns && place.address?.locality) return place.address.locality.trim();
|
||||
return place.name?.trim() || place.address?.locality?.trim() || place.code || '?';
|
||||
};
|
||||
const runs: string[][] = [];
|
||||
for (const leg of legs) {
|
||||
const from = label(leg.departure.place);
|
||||
const to = leg.arrival ? label(leg.arrival.place) : undefined;
|
||||
const run = runs[runs.length - 1];
|
||||
if (run && run[run.length - 1] === from) {
|
||||
if (to) run.push(to);
|
||||
} else {
|
||||
runs.push(to ? [from, to] : [from]);
|
||||
}
|
||||
}
|
||||
return runs;
|
||||
}
|
||||
|
||||
/** The route as plain text, "FRA → JFK → FRA"; runs separated by a comma. */
|
||||
export function routeTitle(booking: TransportBooking): string {
|
||||
return routeRuns(booking).map((run) => run.join(' → ')).join(', ');
|
||||
}
|
||||
|
||||
/** The items a shipment card lists: the order's, else the parcels', with images from either. */
|
||||
export function shipmentItems(shipment: Shipment): Item[] {
|
||||
const parcelItems = shipment.parcels.flatMap((p) => p.items);
|
||||
if (shipment.items.length === 0) {
|
||||
const seen = new Set<string>();
|
||||
return parcelItems.filter((item) => (seen.has(item.name) ? false : (seen.add(item.name), true)));
|
||||
}
|
||||
return shipment.items.map((item) => {
|
||||
if (item.image) return item;
|
||||
const image = parcelItems.find((p) => p.name === item.name && p.image)?.image;
|
||||
return image ? { ...item, image } : item;
|
||||
});
|
||||
}
|
||||
|
||||
/** The first `max` item names, how many more there are, and the number of pieces. */
|
||||
export function summarizeItems(items: Item[], max = 3): { shown: Item[]; more: number; count: number } {
|
||||
const count = items.reduce((sum, item) => sum + (item.quantity && item.quantity > 0 ? item.quantity : 1), 0);
|
||||
return { shown: items.slice(0, max), more: Math.max(0, items.length - max), count };
|
||||
}
|
||||
|
||||
/** Whether a link may be shown and opened: http(s) only, checked again here. */
|
||||
export function isWebLink(link: { url: string } | undefined): boolean {
|
||||
if (!link) return false;
|
||||
try {
|
||||
const { protocol } = new URL(link.url);
|
||||
return protocol === 'http:' || protocol === 'https:';
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
// Mirrors hasForwardPrefix and the prefix tokens of webmail lib/subject-prefix.ts
|
||||
// at 8098039d6; keep the two in step. The app's own src/lib/subject-prefix.ts
|
||||
// has the same token lists but no forward check.
|
||||
|
||||
const REPLY_TOKENS = [
|
||||
"Re", // English, Italian, French (also generic ISO)
|
||||
"RE", // Outlook variant
|
||||
"AW", // German (Antwort)
|
||||
"Antw", // German verbose
|
||||
"Sv", // Danish / Swedish / Norwegian (Svar)
|
||||
"Yn", // Turkish (Yanit)
|
||||
"Yanit", // Turkish verbose
|
||||
"Odp", // Polish (Odpowiedz)
|
||||
"Ответ", // Russian
|
||||
"Resp", // Spanish/Portuguese variant
|
||||
"Vá", // Hungarian
|
||||
"回复", // Chinese
|
||||
"回覆", // Chinese traditional
|
||||
"답장", // Korean
|
||||
// NB: deliberately no bare "R" token — a single letter would strip the first
|
||||
// word of legitimate subjects like "R: budget 2024". The full "Re" covers
|
||||
// the common Italian/English case anyway.
|
||||
];
|
||||
|
||||
const FORWARD_TOKENS = [
|
||||
"Fwd", // English standard
|
||||
"Fw", // English short / Polish / German short
|
||||
"WG", // German (Weitergeleitet)
|
||||
"Tr", // French (Transfert)
|
||||
"Vs", // Danish (Videresend)
|
||||
"Enc", // Portuguese (Encaminhar)
|
||||
"ENC", // Portuguese caps
|
||||
"Rv", // Spanish (Reenviar)
|
||||
"RV", // Spanish caps
|
||||
"Rvf", // Spanish variant
|
||||
"Inol", // Italian (Inoltro)
|
||||
// NB: deliberately no bare "I" token — see the REPLY_TOKENS note above.
|
||||
"PD", // Polish (Przekazane Dalej)
|
||||
"PR", // Czech (Preposlat)
|
||||
"İlt", // Turkish (Ilet)
|
||||
"Ilt", // Turkish ASCII
|
||||
"Пересл", // Russian (Peresylka)
|
||||
"Пер", // Russian short
|
||||
"转发", // Chinese
|
||||
"轉寄", // Chinese traditional
|
||||
"전달", // Korean
|
||||
];
|
||||
|
||||
// Match a single prefix token + optional [N] counter (Outlook) or *N (Eudora)
|
||||
// + colon + whitespace. Case-insensitive. The non-capturing groups keep the
|
||||
// regex composable for stripping multiple prefixes in a row.
|
||||
function buildPrefixRegex(tokens: string[]): RegExp {
|
||||
// Escape regex specials in tokens (none currently, but be defensive)
|
||||
const escaped = tokens.map((t) => t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
|
||||
// Sort by length DESC so longer tokens (e.g. "Пересл") win over their
|
||||
// shorter prefixes (e.g. "Пер") during alternation matching.
|
||||
escaped.sort((a, b) => b.length - a.length);
|
||||
// Accept both the ASCII colon and the full-width colon ":" (U+FF1A) that CJK
|
||||
// mail clients emit after a localized prefix (e.g. "回复:foo").
|
||||
return new RegExp(
|
||||
`^\\s*(?:${escaped.join("|")})(?:\\[\\d+\\]|\\*\\d*)?\\s*[:\\uFF1A]\\s*`,
|
||||
"i",
|
||||
);
|
||||
}
|
||||
|
||||
const FORWARD_RE = buildPrefixRegex(FORWARD_TOKENS);
|
||||
const REPLY_RE = buildPrefixRegex(REPLY_TOKENS);
|
||||
|
||||
/**
|
||||
* Whether the subject's leading prefix chain marks a forward anywhere in it
|
||||
* ("Fwd:", "Re: Fwd:", "AW: WG:"), case-insensitively. Booking cards use it:
|
||||
* a forwarded booking mail was authenticated for the forwarder, not for the
|
||||
* airline or shop it quotes.
|
||||
*/
|
||||
export function hasForwardPrefix(subject: string | undefined | null): boolean {
|
||||
let rest = (subject ?? "").trimStart();
|
||||
for (let i = 0; i < 20; i++) {
|
||||
const forward = FORWARD_RE.exec(rest);
|
||||
if (forward && forward[0].length > 0) return true;
|
||||
const reply = REPLY_RE.exec(rest);
|
||||
if (!reply || reply[0].length === 0) return false;
|
||||
rest = rest.slice(reply[0].length).trimStart();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Mirrors webmail lib/structured-mail-gate.ts at 8098039d6, which reads the
|
||||
// admin policy's `structuredMailEnabled`. Off: no booking, parcel or order
|
||||
// cards, no parsing of markup and no .pkpass download.
|
||||
//
|
||||
// App: there is no org policy in the app yet, so the gate is on. When the
|
||||
// policy arrives, read it here; every caller already goes through this.
|
||||
|
||||
/** Whether booking cards are shown, outside React. */
|
||||
export function isStructuredMailEnabled(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Whether booking cards are shown, for components. */
|
||||
export function useStructuredMailEnabled(): boolean {
|
||||
return isStructuredMailEnabled();
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// Mirrors webmail lib/structured-mail/index.ts at ba6d5e448; keep the two in step.
|
||||
/**
|
||||
* Structured mail: read the schema.org markup that airlines, hotels, shops
|
||||
* and carriers put into their HTML mail, and turn it into cards. Plain
|
||||
* TypeScript; the only DOM dependency is the HTML parser, which a caller
|
||||
* without DOMParser (the native app) passes in.
|
||||
*
|
||||
* const cards = readStructuredMail(html, { mailDate });
|
||||
* const shown = needsAirportZones(cards)
|
||||
* ? resolveCards(cards, await loadAirportZones())
|
||||
* : resolveCards(cards);
|
||||
*/
|
||||
import { extractStructuredData } from './extract';
|
||||
import { mergeCandidates } from './merge';
|
||||
import { hasReservation, normalizeItems } from './normalize';
|
||||
import type { StructuredCard } from './types';
|
||||
import { validateCard } from './validate';
|
||||
|
||||
export interface ReadOptions {
|
||||
/** When the mail was sent (else received): bookings far from it are doubtful. */
|
||||
mailDate: Date;
|
||||
parseHtml?: (html: string) => Document;
|
||||
}
|
||||
|
||||
export interface DetailedRead {
|
||||
/** Validated cards, merged to one per booking, zones not yet resolved. */
|
||||
cards: StructuredCard[];
|
||||
/**
|
||||
* Whether the markup holds any reservation, valid or rejected. Such a mail
|
||||
* is read from its markup alone: a pass must not bring back a booking the
|
||||
* markup rejected or cancelled, nor add a second card for it.
|
||||
*/
|
||||
hadBookingMarkup: boolean;
|
||||
}
|
||||
|
||||
/** readStructuredMail, plus whether the markup described a booking at all. */
|
||||
export function readStructuredMailDetailed(html: string, options: ReadOptions): DetailedRead {
|
||||
const extracted = extractStructuredData(html, options.parseHtml);
|
||||
const cards = normalizeItems(extracted);
|
||||
return {
|
||||
cards: mergeCandidates(cards.map((card) => validateCard(card, options.mailDate))),
|
||||
hadBookingMarkup: hasReservation(extracted),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Cards from one HTML body: validated and merged to one card per booking,
|
||||
* with zones not yet resolved (resolveCards).
|
||||
*/
|
||||
export function readStructuredMail(html: string, options: ReadOptions): StructuredCard[] {
|
||||
return readStructuredMailDetailed(html, options).cards;
|
||||
}
|
||||
|
||||
export { extractStructuredData, mayContainStructuredData } from './extract';
|
||||
export { normalizeItems } from './normalize';
|
||||
export { validateCard } from './validate';
|
||||
export { mergeCandidates } from './merge';
|
||||
export { needsAirportZones, resolveCards } from './resolve';
|
||||
export { loadAirportZones } from './time-zones';
|
||||
export * from './types';
|
||||
@@ -0,0 +1,130 @@
|
||||
// Mirrors webmail lib/structured-mail/json-repair.ts at 76b0c2fba; keep the two in step.
|
||||
/**
|
||||
* Parse the JSON-LD a sender wrote, including the broken forms real booking
|
||||
* mail carries: missing commas between objects (Eurowings), several objects
|
||||
* without the array brackets (Volotea), trailing commas (Eventbrite) and
|
||||
* XML-entity-encoded JSON (Airbnb). Returns undefined when nothing sensible
|
||||
* comes out; never throws.
|
||||
*/
|
||||
|
||||
const MAX_BLOCK_LENGTH = 200_000;
|
||||
|
||||
function tryParse(text: string): { ok: true; value: unknown } | { ok: false } {
|
||||
try {
|
||||
return { ok: true, value: JSON.parse(text) };
|
||||
} catch {
|
||||
return { ok: false };
|
||||
}
|
||||
}
|
||||
|
||||
function stripWrappers(text: string): string {
|
||||
return text
|
||||
.trim()
|
||||
.replace(/^<!--/, '')
|
||||
.replace(/-->$/, '')
|
||||
.replace(/^<!\[CDATA\[/, '')
|
||||
.replace(/\]\]>$/, '')
|
||||
.trim();
|
||||
}
|
||||
|
||||
const NAMED_ENTITIES: Record<string, string> = { quot: '"', amp: '&', lt: '<', gt: '>', apos: "'", nbsp: ' ' };
|
||||
|
||||
function decodeEntities(text: string): string {
|
||||
return text.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);/gi, (match, body: string) => {
|
||||
if (body[0] === '#') {
|
||||
const code = body[1] === 'x' || body[1] === 'X' ? parseInt(body.slice(2), 16) : parseInt(body.slice(1), 10);
|
||||
return Number.isFinite(code) && code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : match;
|
||||
}
|
||||
return NAMED_ENTITIES[body.toLowerCase()] ?? match;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* One string-aware pass: drop commas right before `}` or `]`, put a comma
|
||||
* between a closing `}` or `]` and an opening `{` or `[` (only where a
|
||||
* comma or nothing could go), and escape raw control characters inside
|
||||
* strings. Returns the text and how many values sit at the top level.
|
||||
*/
|
||||
function repairStructure(text: string): { text: string; topLevel: number } {
|
||||
let out = '';
|
||||
let inString = false;
|
||||
let depth = 0;
|
||||
let topLevel = 0;
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
const c = text[i];
|
||||
if (inString) {
|
||||
if (c === '\\') {
|
||||
out += c + (text[i + 1] ?? '');
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if (c === '"') inString = false;
|
||||
if (c < ' ') {
|
||||
out += c === '\n' ? '\\n' : c === '\r' ? '\\r' : c === '\t' ? '\\t' : '';
|
||||
continue;
|
||||
}
|
||||
out += c;
|
||||
continue;
|
||||
}
|
||||
if (c === '"') {
|
||||
if (depth === 0) topLevel++;
|
||||
inString = true;
|
||||
out += c;
|
||||
continue;
|
||||
}
|
||||
if (c === ',') {
|
||||
let j = i + 1;
|
||||
while (j < text.length && /\s/.test(text[j])) j++;
|
||||
if (text[j] === '}' || text[j] === ']' || j >= text.length) continue;
|
||||
out += c;
|
||||
continue;
|
||||
}
|
||||
if (c === '{' || c === '[') {
|
||||
if (depth === 0) {
|
||||
topLevel++;
|
||||
} else {
|
||||
// `}{` or `]{` inside an array: the comma is missing.
|
||||
const prev = out.trimEnd().slice(-1);
|
||||
if (prev === '}' || prev === ']') out = out.trimEnd() + ',';
|
||||
}
|
||||
depth++;
|
||||
out += c;
|
||||
continue;
|
||||
}
|
||||
if (c === '}' || c === ']') {
|
||||
depth = Math.max(0, depth - 1);
|
||||
out += c;
|
||||
continue;
|
||||
}
|
||||
out += c;
|
||||
}
|
||||
return { text: out, topLevel };
|
||||
}
|
||||
|
||||
export function parseLenientJson(input: string): unknown {
|
||||
if (!input || input.length > MAX_BLOCK_LENGTH) return undefined;
|
||||
const direct = tryParse(input);
|
||||
if (direct.ok) return direct.value;
|
||||
|
||||
let text = stripWrappers(input);
|
||||
let attempt = tryParse(text);
|
||||
if (attempt.ok) return attempt.value;
|
||||
|
||||
if (/&(quot|#34|#x22);/i.test(text)) {
|
||||
text = decodeEntities(text);
|
||||
attempt = tryParse(text);
|
||||
if (attempt.ok) return attempt.value;
|
||||
}
|
||||
|
||||
const repaired = repairStructure(text);
|
||||
attempt = tryParse(repaired.text);
|
||||
if (attempt.ok) return attempt.value;
|
||||
|
||||
if (repaired.topLevel > 1) {
|
||||
// Several top-level values: the brackets are missing. The commas between
|
||||
// them may be missing too, which the pass inside brackets adds.
|
||||
attempt = tryParse(repairStructure(`[${repaired.text}]`).text);
|
||||
if (attempt.ok) return attempt.value;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
// Mirrors webmail lib/structured-mail/merge.ts at fa324170a; keep the two in step.
|
||||
/**
|
||||
* One card per booking. Senders write one FlightReservation per passenger
|
||||
* and leg, an Order next to its ParcelDelivery, or the same booking in both
|
||||
* JSON-LD and microdata; equal merge keys (normalize.ts cardKey) are folded
|
||||
* together here. If any part of a booking failed validation, or two parts
|
||||
* disagree about the same leg, the whole booking is dropped.
|
||||
*/
|
||||
import { legKey } from './normalize';
|
||||
import type {
|
||||
Booking,
|
||||
BookingLinks,
|
||||
Candidate,
|
||||
Item,
|
||||
Moment,
|
||||
OrderStatus,
|
||||
Parcel,
|
||||
ReservationStatus,
|
||||
Seat,
|
||||
Shipment,
|
||||
StructuredCard,
|
||||
TransportBooking,
|
||||
TransportLeg,
|
||||
} from './types';
|
||||
|
||||
const KIND_ORDER: StructuredCard['kind'][] = ['flight', 'train', 'bus', 'lodging', 'rentalcar', 'event', 'restaurant', 'shipment'];
|
||||
|
||||
function sameMoment(a: Moment | undefined, b: Moment | undefined): boolean {
|
||||
if (!a || !b) return true;
|
||||
if (a.utc && b.utc) return a.utc === b.utc;
|
||||
return a.date === b.date && (a.time ?? '') === (b.time ?? '');
|
||||
}
|
||||
|
||||
function mergeLinks(all: BookingLinks[]): BookingLinks {
|
||||
const out: BookingLinks = {};
|
||||
for (const links of all) {
|
||||
out.checkin ??= links.checkin;
|
||||
out.manage ??= links.manage;
|
||||
out.ticket ??= links.ticket;
|
||||
}
|
||||
for (const key of Object.keys(out) as (keyof BookingLinks)[]) if (!out[key]) delete out[key];
|
||||
return out;
|
||||
}
|
||||
|
||||
function unique<T>(values: (T | undefined)[]): T[] {
|
||||
return [...new Set(values.filter((v): v is T => v !== undefined))];
|
||||
}
|
||||
|
||||
function latest(values: (string | undefined)[]): string | undefined {
|
||||
return unique(values).sort().pop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Status of a leg or booking several reservations contributed to: cancelled
|
||||
* only when every one of them says so. One cancelled passenger on a leg the
|
||||
* others still hold leaves the leg live.
|
||||
*/
|
||||
function combinedStatus(statuses: (ReservationStatus | undefined)[]): ReservationStatus | undefined {
|
||||
if (statuses.length > 0 && statuses.every((s) => s === 'cancelled')) return 'cancelled';
|
||||
return statuses.find((s) => s && s !== 'cancelled');
|
||||
}
|
||||
|
||||
function mergeTransport(group: TransportBooking[]): TransportBooking | null {
|
||||
const legs = new Map<string, { legs: TransportLeg[] }>();
|
||||
for (const booking of group) {
|
||||
for (const leg of booking.legs) {
|
||||
const key = legKey(leg);
|
||||
const entry = legs.get(key) ?? { legs: [] };
|
||||
entry.legs.push(leg);
|
||||
legs.set(key, entry);
|
||||
}
|
||||
}
|
||||
const merged: TransportLeg[] = [];
|
||||
for (const { legs: parts } of legs.values()) {
|
||||
const [first] = parts;
|
||||
for (const other of parts.slice(1)) {
|
||||
if (!sameMoment(first.departure.at, other.departure.at) || !sameMoment(first.arrival?.at, other.arrival?.at)) return null;
|
||||
}
|
||||
const seats: Seat[] = [];
|
||||
const seen = new Set<string>();
|
||||
for (const seat of parts.flatMap((p) => p.seats)) {
|
||||
const id = `${seat.passenger ?? ''}|${seat.seat}`;
|
||||
if (!seen.has(id)) {
|
||||
seen.add(id);
|
||||
seats.push(seat);
|
||||
}
|
||||
}
|
||||
const status = combinedStatus(parts.map((p) => p.status));
|
||||
const withArrival = parts.find((p) => p.arrival?.at) ?? first;
|
||||
const { status: _ignored, ...rest } = first;
|
||||
merged.push({ ...rest, ...(withArrival.arrival ? { arrival: withArrival.arrival } : {}), seats, ...(status ? { status } : {}) });
|
||||
}
|
||||
merged.sort((a, b) => (a.departure.at.utc ?? `${a.departure.at.date}T${a.departure.at.time}`).localeCompare(b.departure.at.utc ?? `${b.departure.at.date}T${b.departure.at.time}`));
|
||||
const [head] = group;
|
||||
const allCancelled = merged.length > 0 && merged.every((leg) => leg.status === 'cancelled');
|
||||
const bookingStatus = allCancelled ? 'cancelled' : combinedStatus(group.map((b) => b.status).filter((s) => s !== 'cancelled'));
|
||||
const { status: _s, modified: _m, provider: _p, ...base } = head;
|
||||
const modified = latest(group.map((b) => b.modified));
|
||||
const provider = group.find((b) => b.provider)?.provider;
|
||||
return {
|
||||
...base,
|
||||
...(bookingStatus ? { status: bookingStatus } : {}),
|
||||
passengers: unique(group.flatMap((b) => b.passengers)),
|
||||
...(provider ? { provider } : {}),
|
||||
links: mergeLinks(group.map((b) => b.links)),
|
||||
...(modified ? { modified } : {}),
|
||||
legs: merged,
|
||||
} as TransportBooking;
|
||||
}
|
||||
|
||||
function mergeOther(group: Booking[]): Booking {
|
||||
const [head] = group;
|
||||
const status = combinedStatus(group.map((b) => b.status));
|
||||
const modified = latest(group.map((b) => b.modified));
|
||||
const { status: _s, modified: _m, ...base } = head;
|
||||
const merged = {
|
||||
...base,
|
||||
...(status ? { status } : {}),
|
||||
passengers: unique(group.flatMap((b) => b.passengers)),
|
||||
links: mergeLinks(group.map((b) => b.links)),
|
||||
...(modified ? { modified } : {}),
|
||||
} as Booking;
|
||||
if (merged.kind === 'event') merged.tickets = unique(group.flatMap((b) => (b.kind === 'event' ? b.tickets : [])));
|
||||
return merged;
|
||||
}
|
||||
|
||||
/** Later shipping states win over earlier ones; a problem state over all. */
|
||||
const ORDER_PROGRESS: OrderStatus[] = ['payment_due', 'processing', 'in_transit', 'pickup_available', 'delivered', 'returned', 'problem', 'cancelled'];
|
||||
|
||||
function mergeItems(lists: Item[][]): Item[] {
|
||||
const byName = new Map<string, Item>();
|
||||
for (const item of lists.flat()) {
|
||||
const key = item.name.toLowerCase();
|
||||
const known = byName.get(key);
|
||||
byName.set(key, known ? { ...item, ...known, image: known.image ?? item.image } : item);
|
||||
}
|
||||
return [...byName.values()].map((item) => {
|
||||
if (!item.image) delete item.image;
|
||||
return item;
|
||||
});
|
||||
}
|
||||
|
||||
function mergeShipments(group: Shipment[]): Shipment {
|
||||
const parcels: Parcel[] = [];
|
||||
for (const parcel of group.flatMap((s) => s.parcels)) {
|
||||
const same = parcel.trackingNumber ? parcels.find((p) => p.trackingNumber === parcel.trackingNumber) : undefined;
|
||||
if (same) {
|
||||
same.carrier ??= parcel.carrier;
|
||||
same.trackingUrl ??= parcel.trackingUrl;
|
||||
same.expectedBy ??= parcel.expectedBy;
|
||||
same.items = mergeItems([same.items, parcel.items]);
|
||||
} else {
|
||||
parcels.push({ ...parcel, items: [...parcel.items] });
|
||||
}
|
||||
}
|
||||
const statuses = group.map((s) => s.status).filter((s): s is OrderStatus => !!s);
|
||||
const status = statuses.sort((a, b) => ORDER_PROGRESS.indexOf(a) - ORDER_PROGRESS.indexOf(b)).pop();
|
||||
const withOrder = group.find((s) => s.items.length > 0 || s.total) ?? group[0];
|
||||
const merged: Shipment = {
|
||||
kind: 'shipment',
|
||||
key: group[0].key,
|
||||
...(group.find((s) => s.merchant) ? { merchant: group.find((s) => s.merchant)!.merchant } : {}),
|
||||
...(group.find((s) => s.orderNumber) ? { orderNumber: group.find((s) => s.orderNumber)!.orderNumber } : {}),
|
||||
...(status ? { status } : {}),
|
||||
items: mergeItems(group.map((s) => s.items)),
|
||||
...(withOrder.total ? { total: withOrder.total } : {}),
|
||||
...(group.find((s) => s.orderUrl) ? { orderUrl: group.find((s) => s.orderUrl)!.orderUrl } : {}),
|
||||
parcels,
|
||||
source: group[0].source,
|
||||
};
|
||||
return merged;
|
||||
}
|
||||
|
||||
function startOf(card: StructuredCard): string {
|
||||
switch (card.kind) {
|
||||
case 'flight': case 'train': case 'bus': return card.legs[0]?.departure.at.utc ?? card.legs[0]?.departure.at.date ?? '';
|
||||
case 'lodging': return card.checkin.date;
|
||||
case 'rentalcar': return card.pickup.at.utc ?? card.pickup.at.date;
|
||||
case 'event': case 'restaurant': return card.start.utc ?? card.start.date;
|
||||
case 'shipment': return '';
|
||||
}
|
||||
}
|
||||
|
||||
/** Fold candidates with equal keys into one card each; drop doubtful groups. */
|
||||
export function mergeCandidates(candidates: Candidate[]): StructuredCard[] {
|
||||
const groups = new Map<string, Candidate[]>();
|
||||
for (const candidate of candidates) {
|
||||
const group = groups.get(candidate.card.key) ?? [];
|
||||
group.push(candidate);
|
||||
groups.set(candidate.card.key, group);
|
||||
}
|
||||
const out: StructuredCard[] = [];
|
||||
for (const group of groups.values()) {
|
||||
if (group.some((c) => c.problems.length > 0)) continue;
|
||||
const cards = group.map((c) => c.card);
|
||||
const [head] = cards;
|
||||
if (head.kind === 'shipment') out.push(mergeShipments(cards as Shipment[]));
|
||||
else if (head.kind === 'flight' || head.kind === 'train' || head.kind === 'bus') {
|
||||
const merged = mergeTransport(cards as TransportBooking[]);
|
||||
if (merged) out.push(merged);
|
||||
} else out.push(mergeOther(cards as Booking[]));
|
||||
}
|
||||
return out.sort((a, b) => KIND_ORDER.indexOf(a.kind) - KIND_ORDER.indexOf(b.kind) || startOf(a).localeCompare(startOf(b)));
|
||||
}
|
||||
@@ -0,0 +1,426 @@
|
||||
// Mirrors webmail lib/structured-mail/normalize.ts at 916434924; keep the two in step.
|
||||
/**
|
||||
* Turn extracted schema.org objects into the card model. Unknown types and
|
||||
* properties are dropped; validation (validate.ts) decides afterwards which
|
||||
* cards are believable.
|
||||
*/
|
||||
import type { ExtractedItem } from './extract';
|
||||
import type {
|
||||
BookingKind,
|
||||
BookingLinks,
|
||||
CardSource,
|
||||
Item,
|
||||
Moment,
|
||||
Parcel,
|
||||
Place,
|
||||
Seat,
|
||||
Shipment,
|
||||
StructuredCard,
|
||||
TransportBooking,
|
||||
TransportKind,
|
||||
TransportLeg,
|
||||
} from './types';
|
||||
import {
|
||||
address,
|
||||
all,
|
||||
amount,
|
||||
currency,
|
||||
hasType,
|
||||
isObj,
|
||||
isSchemaContext,
|
||||
link,
|
||||
moment,
|
||||
lenientDate,
|
||||
nameOf,
|
||||
obj,
|
||||
orderStatus,
|
||||
people,
|
||||
positiveInt,
|
||||
reservationStatus,
|
||||
str,
|
||||
typesOf,
|
||||
type Obj,
|
||||
} from './values';
|
||||
|
||||
const RESERVATION_KINDS: Record<string, BookingKind> = {
|
||||
FlightReservation: 'flight',
|
||||
TrainReservation: 'train',
|
||||
BusReservation: 'bus',
|
||||
LodgingReservation: 'lodging',
|
||||
RentalCarReservation: 'rentalcar',
|
||||
EventReservation: 'event',
|
||||
FoodEstablishmentReservation: 'restaurant',
|
||||
};
|
||||
|
||||
const MAX_OBJECTS = 200;
|
||||
|
||||
interface Found {
|
||||
object: Obj;
|
||||
source: CardSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten what the extractor found into typed top-level objects: arrays,
|
||||
* `@graph`, ReservationPackage `subReservation`, and `reservationFor` arrays
|
||||
* (one reservation per element). Only objects under a schema.org context
|
||||
* count; nested objects inherit it.
|
||||
*/
|
||||
function flatten(extracted: ExtractedItem[]): Found[] {
|
||||
const out: Found[] = [];
|
||||
const visit = (value: unknown, source: CardSource, inherited: boolean, depth: number) => {
|
||||
if (out.length >= MAX_OBJECTS || depth > 6) return;
|
||||
if (Array.isArray(value)) {
|
||||
for (const v of value) visit(v, source, inherited, depth + 1);
|
||||
return;
|
||||
}
|
||||
if (!isObj(value)) return;
|
||||
const inContext = inherited || isSchemaContext(value['@context']);
|
||||
if (Array.isArray(value['@graph'])) {
|
||||
visit(value['@graph'], source, inContext, depth + 1);
|
||||
return;
|
||||
}
|
||||
if (!inContext) return;
|
||||
if (hasType(value, ['ReservationPackage'])) {
|
||||
visit(value.subReservation, source, true, depth + 1);
|
||||
return;
|
||||
}
|
||||
const reservationFor = value.reservationFor;
|
||||
if (Array.isArray(reservationFor) && reservationFor.length > 1) {
|
||||
for (const one of reservationFor) out.push({ object: { ...value, reservationFor: one }, source });
|
||||
return;
|
||||
}
|
||||
out.push({ object: value, source });
|
||||
};
|
||||
for (const { item, source } of extracted) visit(item, source, false, 0);
|
||||
return out;
|
||||
}
|
||||
|
||||
const lower = (s: string | undefined) => (s ?? '').toLowerCase().replace(/\s+/g, ' ').trim();
|
||||
|
||||
function place(value: unknown): Place {
|
||||
const v = obj(value);
|
||||
if (!v) {
|
||||
const name = str(value);
|
||||
return name ? { name } : {};
|
||||
}
|
||||
const result: Place = {};
|
||||
const name = str(v.name);
|
||||
if (name) result.name = name;
|
||||
const code = str(v.iataCode)?.toUpperCase();
|
||||
if (code && /^[A-Z]{3}$/.test(code)) result.code = code;
|
||||
const addr = address(v.address);
|
||||
if (addr) result.address = addr;
|
||||
return result;
|
||||
}
|
||||
|
||||
function bookingLinks(res: Obj, extra: Obj | undefined): BookingLinks {
|
||||
const links: BookingLinks = {};
|
||||
const checkin = link(res.checkinUrl);
|
||||
const manage = link(res.modifyReservationUrl) ?? link(res.url) ?? link(res.Url);
|
||||
const ticket = link(res.ticketDownloadUrl) ?? link(res.ticketPrintUrl) ?? link(extra?.ticketDownloadUrl);
|
||||
if (checkin) links.checkin = checkin;
|
||||
if (manage) links.manage = manage;
|
||||
if (ticket) links.ticket = ticket;
|
||||
return links;
|
||||
}
|
||||
|
||||
function reference(res: Obj): string | undefined {
|
||||
return str(res.reservationNumber) ?? str(res.reservationId) ?? str(res.confirmationNumber);
|
||||
}
|
||||
|
||||
function tickets(res: Obj): Obj[] {
|
||||
return all(res.reservedTicket).filter(isObj);
|
||||
}
|
||||
|
||||
function seatOf(res: Obj): string | undefined {
|
||||
const direct = str(res.airplaneSeat);
|
||||
if (direct) return direct;
|
||||
for (const ticket of tickets(res)) {
|
||||
const seat = obj(ticket.ticketedSeat);
|
||||
const number = str(seat?.seatNumber) ?? str(ticket.ticketedSeat);
|
||||
if (number) {
|
||||
const section = str(seat?.seatSection);
|
||||
return section ? `${section}/${number}` : number;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function splitFlightNumber(raw: string | undefined, airlineCode: string | undefined): { code?: string; number?: string } {
|
||||
if (!raw) return { code: airlineCode };
|
||||
const compact = raw.replace(/\s+/g, '').toUpperCase();
|
||||
if (airlineCode && compact.startsWith(airlineCode) && /^\d{1,4}[A-Z]?$/.test(compact.slice(airlineCode.length))) {
|
||||
return { code: airlineCode, number: compact.slice(airlineCode.length) };
|
||||
}
|
||||
const match = /^([A-Z0-9]{2})(\d{1,4}[A-Z]?)$/.exec(compact);
|
||||
if (match && /[A-Z]/.test(match[1]) && (!airlineCode || airlineCode === match[1])) return { code: match[1], number: match[2] };
|
||||
return { code: airlineCode, number: compact };
|
||||
}
|
||||
|
||||
function transport(kind: TransportKind, res: Obj, source: CardSource): TransportBooking {
|
||||
const trip = obj(res.reservationFor) ?? {};
|
||||
const passengers = people(res.underName);
|
||||
let carrier: string | undefined;
|
||||
let carrierCode: string | undefined;
|
||||
let number: string | undefined;
|
||||
let departure: Place;
|
||||
let arrival: Place;
|
||||
if (kind === 'flight') {
|
||||
const airline = obj(trip.airline);
|
||||
const airlineCode = str(airline?.iataCode)?.toUpperCase();
|
||||
carrier = nameOf(trip.airline) ?? nameOf(trip.provider) ?? nameOf(res.provider);
|
||||
const split = splitFlightNumber(str(trip.flightNumber), airlineCode && /^[A-Z0-9]{2}$/.test(airlineCode) ? airlineCode : undefined);
|
||||
carrierCode = split.code;
|
||||
number = split.number;
|
||||
departure = place(trip.departureAirport);
|
||||
arrival = place(trip.arrivalAirport);
|
||||
} else if (kind === 'train') {
|
||||
carrier = nameOf(trip.provider) ?? nameOf(trip.trainCompany) ?? nameOf(res.provider);
|
||||
number = str(trip.trainNumber) ?? str(trip.trainName);
|
||||
departure = place(trip.departureStation);
|
||||
arrival = place(trip.arrivalStation);
|
||||
} else {
|
||||
carrier = nameOf(trip.busCompany) ?? nameOf(trip.provider) ?? nameOf(res.provider);
|
||||
// busName is ignored: FlixBus puts the destination there.
|
||||
number = str(trip.busNumber);
|
||||
departure = place(trip.departureBusStop ?? trip.departureStation);
|
||||
arrival = place(trip.arrivalBusStop ?? trip.arrivalStation);
|
||||
}
|
||||
const depAt = moment(trip.departureTime);
|
||||
const arrAt = moment(trip.arrivalTime);
|
||||
const seat = seatOf(res);
|
||||
const status = reservationStatus(res.reservationStatus);
|
||||
const seats: Seat[] = seat ? [{ ...(passengers[0] ? { passenger: passengers[0] } : {}), seat }] : [];
|
||||
const leg: TransportLeg = {
|
||||
...(carrier ? { carrier } : {}),
|
||||
...(carrierCode ? { carrierCode } : {}),
|
||||
...(number ? { number } : {}),
|
||||
departure: { place: departure, at: depAt ?? { date: '' } },
|
||||
arrival: { place: arrival, ...(arrAt ? { at: arrAt } : {}) },
|
||||
seats,
|
||||
...(status ? { status } : {}),
|
||||
};
|
||||
const ref = reference(res);
|
||||
return {
|
||||
key: '',
|
||||
kind,
|
||||
...(ref ? { reference: ref } : {}),
|
||||
...(status ? { status } : {}),
|
||||
passengers,
|
||||
...(carrier ? { provider: carrier } : {}),
|
||||
links: bookingLinks(res, tickets(res)[0]),
|
||||
...(str(res.modifiedTime) ? { modified: str(res.modifiedTime) } : {}),
|
||||
source,
|
||||
legs: [leg],
|
||||
};
|
||||
}
|
||||
|
||||
function base(kind: BookingKind, res: Obj, source: CardSource) {
|
||||
const ref = reference(res);
|
||||
const status = reservationStatus(res.reservationStatus);
|
||||
return {
|
||||
key: '',
|
||||
kind,
|
||||
...(ref ? { reference: ref } : {}),
|
||||
...(status ? { status } : {}),
|
||||
passengers: people(res.underName),
|
||||
links: bookingLinks(res, tickets(res)[0]),
|
||||
...(str(res.modifiedTime) ? { modified: str(res.modifiedTime) } : {}),
|
||||
source,
|
||||
};
|
||||
}
|
||||
|
||||
const MISSING: Moment = { date: '' };
|
||||
|
||||
function reservation(kind: BookingKind, res: Obj, source: CardSource): StructuredCard | undefined {
|
||||
if (kind === 'flight' || kind === 'train' || kind === 'bus') return transport(kind, res, source);
|
||||
const target = obj(res.reservationFor) ?? {};
|
||||
const name = str(target.name) ?? (typeof res.reservationFor === 'string' ? str(res.reservationFor) : undefined);
|
||||
switch (kind) {
|
||||
case 'lodging': {
|
||||
const addr = address(target.address);
|
||||
const phone = str(target.telephone);
|
||||
return {
|
||||
...base(kind, res, source),
|
||||
kind,
|
||||
name: name ?? '',
|
||||
...(name ? { provider: name } : {}),
|
||||
...(addr ? { address: addr } : {}),
|
||||
...(phone ? { phone } : {}),
|
||||
checkin: moment(res.checkinDate) ?? moment(res.checkinTime) ?? MISSING,
|
||||
checkout: moment(res.checkoutDate) ?? moment(res.checkoutTime) ?? MISSING,
|
||||
};
|
||||
}
|
||||
case 'rentalcar': {
|
||||
const company = nameOf(target.rentalCompany) ?? nameOf(res.provider) ?? nameOf(target.brand);
|
||||
const car = [nameOf(target.brand), str(target.model) ?? str(target.name)].filter(Boolean).join(' ') || undefined;
|
||||
return {
|
||||
...base(kind, res, source),
|
||||
kind,
|
||||
...(company ? { company, provider: company } : {}),
|
||||
...(car ? { car } : {}),
|
||||
pickup: { place: place(res.pickupLocation), at: moment(res.pickupTime) ?? MISSING },
|
||||
dropoff: { place: place(res.dropoffLocation), at: moment(res.dropoffTime) ?? MISSING },
|
||||
};
|
||||
}
|
||||
case 'event': {
|
||||
const venue = place(target.location);
|
||||
const ticketNumbers = [...new Set([str(res.ticketNumber), ...tickets(res).map((t) => str(t.ticketNumber))].filter((t): t is string => !!t))];
|
||||
const start = moment(target.startDate);
|
||||
let end = moment(target.endDate);
|
||||
// An end date without a time means the end of that day, when the
|
||||
// event starts at a time; with a date-only start it's an all-day span.
|
||||
// The end carries no offset: the day may end on the other side of a
|
||||
// DST change, so resolve.ts reads it in the zone the start resolves to.
|
||||
if (end && !end.time && start?.time) end = { ...end, time: '23:59:59' };
|
||||
return {
|
||||
...base(kind, res, source),
|
||||
kind,
|
||||
name: name ?? '',
|
||||
...(venue.name || venue.address ? { venue } : {}),
|
||||
start: start ?? MISSING,
|
||||
...(end ? { end } : {}),
|
||||
tickets: ticketNumbers,
|
||||
...(nameOf(res.provider) ? { provider: nameOf(res.provider) } : {}),
|
||||
};
|
||||
}
|
||||
case 'restaurant': {
|
||||
const addr = address(target.address);
|
||||
const partySize = positiveInt(res.partySize);
|
||||
return {
|
||||
...base(kind, res, source),
|
||||
kind,
|
||||
name: name ?? '',
|
||||
...(name ? { provider: name } : {}),
|
||||
...(addr ? { address: addr } : {}),
|
||||
start: moment(res.startTime) ?? moment(res.startDate) ?? MISSING,
|
||||
...(partySize ? { partySize } : {}),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function items(products: unknown, quantityOf?: (o: Obj) => number | undefined): Item[] {
|
||||
const result: Item[] = [];
|
||||
for (const p of all(products)) {
|
||||
const name = nameOf(p);
|
||||
if (!name) continue;
|
||||
const product = isObj(p) ? p : undefined;
|
||||
const image = product ? link(isObj(product.image) ? product.image.url ?? product.image.contentUrl : product.image) : undefined;
|
||||
const quantity = product && quantityOf ? quantityOf(product) : undefined;
|
||||
result.push({ name, ...(quantity ? { quantity } : {}), ...(image ? { image } : {}) });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function orderItems(order: Obj): Item[] {
|
||||
const result: Item[] = [];
|
||||
for (const offer of all(order.acceptedOffer).filter(isObj)) {
|
||||
const quantity = positiveInt(offer.eligibleQuantity);
|
||||
for (const item of items(offer.itemOffered)) result.push(quantity ? { ...item, quantity } : item);
|
||||
}
|
||||
for (const entry of all(order.orderedItem).filter(isObj)) {
|
||||
if (hasType(entry, ['OrderItem'])) {
|
||||
const quantity = positiveInt(entry.orderQuantity);
|
||||
for (const item of items(entry.orderedItem)) result.push(quantity ? { ...item, quantity } : item);
|
||||
} else {
|
||||
result.push(...items(entry));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function order(o: Obj, source: CardSource): Shipment {
|
||||
const merchant = nameOf(o.merchant) ?? nameOf(o.seller) ?? nameOf(o.broker);
|
||||
const orderNumber = str(o.orderNumber);
|
||||
const status = orderStatus(o.orderStatus);
|
||||
const total = amount(o.price) ?? amount(o.totalPrice);
|
||||
const cur = currency(o.priceCurrency);
|
||||
const action = all(o.potentialAction).filter(isObj).find((a) => hasType(a, ['ViewAction']));
|
||||
const orderUrl = link(o.url) ?? link(o.Url) ?? link(action?.target) ?? link(action?.url);
|
||||
return {
|
||||
kind: 'shipment',
|
||||
key: '',
|
||||
...(merchant ? { merchant } : {}),
|
||||
...(orderNumber ? { orderNumber } : {}),
|
||||
...(status ? { status } : {}),
|
||||
items: orderItems(o),
|
||||
...(total && cur ? { total: { amount: total, currency: cur } } : {}),
|
||||
...(orderUrl ? { orderUrl } : {}),
|
||||
parcels: [],
|
||||
source,
|
||||
};
|
||||
}
|
||||
|
||||
function parcel(p: Obj, source: CardSource): Shipment {
|
||||
const partOf = obj(p.partOfOrder);
|
||||
const base: Shipment = partOf ? order(partOf, source) : { kind: 'shipment', key: '', items: [], parcels: [], source };
|
||||
const carrier = nameOf(p.carrier) ?? nameOf(p.provider);
|
||||
const trackingNumber = str(p.trackingNumber);
|
||||
const trackingUrl = link(p.trackingUrl);
|
||||
// Only the latest bound is an "arrives by" date; expectedArrivalFrom is the
|
||||
// earliest one and would promise too much.
|
||||
const expectedBy = lenientDate(p.expectedArrivalUntil);
|
||||
const shipped: Parcel = {
|
||||
...(carrier ? { carrier } : {}),
|
||||
...(trackingNumber ? { trackingNumber } : {}),
|
||||
...(trackingUrl ? { trackingUrl } : {}),
|
||||
...(expectedBy ? { expectedBy } : {}),
|
||||
items: items(p.itemShipped),
|
||||
};
|
||||
return { ...base, parcels: [shipped] };
|
||||
}
|
||||
|
||||
/** One card per top-level object, unmerged and unvalidated, with a merge key. */
|
||||
export function normalizeItems(extracted: ExtractedItem[]): StructuredCard[] {
|
||||
const cards: StructuredCard[] = [];
|
||||
for (const { object, source } of flatten(extracted)) {
|
||||
const types = typesOf(object);
|
||||
const kindType = types.find((t) => RESERVATION_KINDS[t]);
|
||||
let card: StructuredCard | undefined;
|
||||
if (kindType) card = reservation(RESERVATION_KINDS[kindType], object, source);
|
||||
else if (types.includes('ParcelDelivery')) card = parcel(object, source);
|
||||
else if (types.includes('Order')) card = order(object, source);
|
||||
if (card) cards.push({ ...card, key: cardKey(card) });
|
||||
}
|
||||
return cards;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the markup holds a reservation of any type (known or not, valid or
|
||||
* not) under a schema.org context. A mail that describes its booking in
|
||||
* markup is read from the markup alone; its Wallet passes are not consulted.
|
||||
*/
|
||||
export function hasReservation(extracted: ExtractedItem[]): boolean {
|
||||
return flatten(extracted).some(({ object }) => typesOf(object).some((t) => /Reservation$/.test(t)));
|
||||
}
|
||||
|
||||
/** The merge key: equal keys describe the same booking or order. */
|
||||
export function cardKey(card: StructuredCard): string {
|
||||
switch (card.kind) {
|
||||
case 'flight': case 'train': case 'bus': {
|
||||
const leg = card.legs[0];
|
||||
return card.reference ? `${card.kind}|ref:${lower(card.reference)}` : `${card.kind}|leg:${legKey(leg)}`;
|
||||
}
|
||||
case 'lodging':
|
||||
return `lodging|${lower(card.reference)}|${lower(card.name)}|${card.checkin.date}`;
|
||||
case 'rentalcar':
|
||||
return `rentalcar|${lower(card.reference)}|${lower(card.company)}|${card.pickup.at.date}`;
|
||||
case 'event': case 'restaurant':
|
||||
return `${card.kind}|${lower(card.reference)}|${lower(card.name)}|${card.start.date}`;
|
||||
case 'shipment': {
|
||||
if (card.orderNumber) return `shipment|order:${lower(card.orderNumber)}`;
|
||||
const tracking = card.parcels.find((p) => p.trackingNumber)?.trackingNumber;
|
||||
return tracking ? `shipment|parcel:${lower(tracking)}` : `shipment|anon:${lower(card.merchant)}|${card.parcels.length}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A leg's identity inside a booking: carrier, number, departure day and place.
|
||||
* Leading zeros of the number don't count (BA0123 in a mail, 123 in its
|
||||
* Wallet pass).
|
||||
*/
|
||||
export function legKey(leg: TransportLeg): string {
|
||||
return [lower(leg.carrierCode ?? leg.carrier), lower(leg.number).replace(/^0+(?=\d)/, ''), leg.departure.at.date, lower(leg.departure.place.code ?? leg.departure.place.name)].join('|');
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// Mirrors authenticationResultsOnReceipt of webmail lib/email-headers.ts at
|
||||
// 609464471; keep the two in step. The app's src/lib/email-headers.ts has
|
||||
// no receipt check yet.
|
||||
|
||||
/**
|
||||
* `with` protocols (RFC 3848) that record an authenticated submission: the
|
||||
* client handed the message in after logging in, so whatever
|
||||
* Authentication-Results it carries below that hop are the client's own text.
|
||||
*/
|
||||
const SUBMISSION_PROTOCOL_RE = /\bwith\s+(?:UTF8)?(?:E?SMTP|LMTP)S?A\b/i;
|
||||
|
||||
/**
|
||||
* Whether the topmost Authentication-Results header looks like it was
|
||||
* written by a server that received the message, rather than by whoever
|
||||
* composed it. A receiving server writes its Received line first and its
|
||||
* Authentication-Results below it (Stalwart and Gmail both do), so: a
|
||||
* Received header must sit above the topmost Authentication-Results, and the
|
||||
* nearest one must not record an authenticated submission. Takes the headers
|
||||
* in message order.
|
||||
*
|
||||
* This only catches a forged header on its own. Stalwart (0.16 and 1.0)
|
||||
* keeps both headers when a signed-in user writes them into a mail they
|
||||
* submit over JMAP, and adds nothing above them, so a forged Received line
|
||||
* followed by a forged "dkim=pass" arrives exactly like mail from outside.
|
||||
* Only the server can tell them apart.
|
||||
*/
|
||||
export function authenticationResultsOnReceipt(headers: readonly { name: string; value: string }[]): boolean {
|
||||
const index = headers.findIndex((header) => header.name.toLowerCase() === 'authentication-results');
|
||||
if (index < 0) return false;
|
||||
for (let i = index - 1; i >= 0; i--) {
|
||||
if (headers[i].name.toLowerCase() !== 'received') continue;
|
||||
return !SUBMISSION_PROTOCOL_RE.test(headers[i].value);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
// Mirrors webmail lib/structured-mail/resolve.ts at 90d7afc34; keep the two in step.
|
||||
/**
|
||||
* Second validation pass, once zones can be looked up: give every time its
|
||||
* zone and instant, then check order and durations on UTC instants (an
|
||||
* eastbound flight over the date line lands "earlier" on the wall clock).
|
||||
* A booking whose times don't hold together is dropped.
|
||||
*/
|
||||
import { countryZone, resolveZone, wallClockAt, zoneOffsetAt, zonedTimeToUtc, type ResolvedZone } from './time-zones';
|
||||
import type { Moment, Place, StructuredCard, TransportBooking, TransportLeg } from './types';
|
||||
|
||||
type ZoneOf = (code: string | undefined) => string | undefined;
|
||||
|
||||
const MINUTE = 60_000;
|
||||
const HOUR = 60 * MINUTE;
|
||||
const LIMITS = {
|
||||
flight: { min: 10 * MINUTE, max: 20 * HOUR },
|
||||
train: { min: MINUTE, max: 48 * HOUR },
|
||||
bus: { min: MINUTE, max: 48 * HOUR },
|
||||
} as const;
|
||||
|
||||
/** Whether resolving these cards needs the airport table. */
|
||||
export function needsAirportZones(cards: StructuredCard[]): boolean {
|
||||
return cards.some((card) => card.kind === 'flight');
|
||||
}
|
||||
|
||||
interface Named { zone: string; source: 'airport' | 'country' }
|
||||
|
||||
function namedZone(place: Place | undefined, kind: StructuredCard['kind'], zoneOf: ZoneOf | undefined, floating: boolean): Named | undefined {
|
||||
if (kind === 'flight') {
|
||||
const zone = zoneOf?.(place?.code);
|
||||
return zone ? { zone, source: 'airport' } : undefined;
|
||||
}
|
||||
const zone = countryZone(place?.address?.country, { floating });
|
||||
return zone ? { zone, source: 'country' } : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach zone and instant to a timed moment. 'drop' when table and mail
|
||||
* disagree, or when a floating local time falls into a DST gap or overlap.
|
||||
* An instant without a known wall clock (a pass's UTC time, `instantOnly`)
|
||||
* takes date, time and offset from the place's zone, or is dropped without one.
|
||||
*/
|
||||
function resolveMoment(m: Moment, named: Named | undefined): Moment | 'drop' {
|
||||
if (m.instantOnly) {
|
||||
if (!named || !m.utc) return 'drop';
|
||||
const ms = Date.parse(m.utc);
|
||||
const local = wallClockAt(ms, named.zone);
|
||||
return { date: local.date, time: local.time, offsetMinutes: zoneOffsetAt(named.zone, ms), utc: m.utc, zone: named.zone };
|
||||
}
|
||||
if (!m.time) return m;
|
||||
const utcMs = m.utc ? Date.parse(m.utc) : undefined;
|
||||
const resolved: ResolvedZone | 'mismatch' = resolveZone({ named, offsetMinutes: m.offsetMinutes, utcMs });
|
||||
if (resolved === 'mismatch') return 'drop';
|
||||
if (m.utc || resolved.zone === null) return { ...m, zone: resolved.zone };
|
||||
const utc = zonedTimeToUtc(m.date, m.time, resolved.zone);
|
||||
if (utc === null) return 'drop';
|
||||
return { ...m, zone: resolved.zone, utc: new Date(utc).toISOString().replace('.000Z', 'Z') };
|
||||
}
|
||||
|
||||
function resolveLeg(kind: TransportBooking['kind'], leg: TransportLeg, zoneOf: ZoneOf | undefined): TransportLeg | null {
|
||||
const dep = resolveMoment(leg.departure.at, namedZone(leg.departure.place, kind, zoneOf, leg.departure.at.offsetMinutes === undefined));
|
||||
if (dep === 'drop' || !dep.utc) return null;
|
||||
let arrivalAt: Moment | undefined;
|
||||
if (leg.arrival?.at) {
|
||||
const arr = resolveMoment(leg.arrival.at, namedZone(leg.arrival.place, kind, zoneOf, leg.arrival.at.offsetMinutes === undefined));
|
||||
if (arr === 'drop' || !arr.utc) return null;
|
||||
const duration = Date.parse(arr.utc) - Date.parse(dep.utc);
|
||||
if (duration < LIMITS[kind].min || duration > LIMITS[kind].max) return null;
|
||||
arrivalAt = arr;
|
||||
}
|
||||
return {
|
||||
...leg,
|
||||
departure: { ...leg.departure, at: dep },
|
||||
...(leg.arrival ? { arrival: { ...leg.arrival, ...(arrivalAt ? { at: arrivalAt } : {}) } } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
/** Order two resolved moments: by instant when both have one, else by wall clock. */
|
||||
function notBefore(later: Moment, earlier: Moment): boolean {
|
||||
if (later.utc && earlier.utc) return Date.parse(later.utc) >= Date.parse(earlier.utc);
|
||||
return `${later.date}T${later.time ?? '00:00:00'}` >= `${earlier.date}T${earlier.time ?? '00:00:00'}`;
|
||||
}
|
||||
|
||||
function resolveCard(card: StructuredCard, zoneOf: ZoneOf | undefined): StructuredCard | null {
|
||||
switch (card.kind) {
|
||||
case 'flight': case 'train': case 'bus': {
|
||||
const legs: TransportLeg[] = [];
|
||||
for (const leg of card.legs) {
|
||||
const resolved = resolveLeg(card.kind, leg, zoneOf);
|
||||
if (!resolved) return null;
|
||||
legs.push(resolved);
|
||||
}
|
||||
return { ...card, legs };
|
||||
}
|
||||
case 'lodging': {
|
||||
const place: Place = { address: card.address };
|
||||
const resolveEnd = (m: Moment): Moment | 'drop' => {
|
||||
const named = namedZone(place, card.kind, zoneOf, m.offsetMinutes === undefined);
|
||||
// A time with neither offset nor a known zone says nothing reliable:
|
||||
// keep the date only.
|
||||
if (m.time && m.offsetMinutes === undefined && !named) return { date: m.date };
|
||||
return resolveMoment(m, named);
|
||||
};
|
||||
const checkin = resolveEnd(card.checkin);
|
||||
const checkout = resolveEnd(card.checkout);
|
||||
if (checkin === 'drop' || checkout === 'drop') return null;
|
||||
if (checkin.time && checkout.time && !notBefore(checkout, checkin)) return null;
|
||||
return { ...card, checkin, checkout };
|
||||
}
|
||||
case 'rentalcar': {
|
||||
const pickupAt = resolveMoment(card.pickup.at, namedZone(card.pickup.place, card.kind, zoneOf, card.pickup.at.offsetMinutes === undefined));
|
||||
const dropoffAt = resolveMoment(card.dropoff.at, namedZone(card.dropoff.place, card.kind, zoneOf, card.dropoff.at.offsetMinutes === undefined));
|
||||
if (pickupAt === 'drop' || dropoffAt === 'drop') return null;
|
||||
if (!notBefore(dropoffAt, pickupAt) || notBefore(pickupAt, dropoffAt)) return null;
|
||||
return { ...card, pickup: { ...card.pickup, at: pickupAt }, dropoff: { ...card.dropoff, at: dropoffAt } };
|
||||
}
|
||||
case 'event': {
|
||||
const named = (m: Moment) => namedZone(card.venue, card.kind, zoneOf, m.offsetMinutes === undefined);
|
||||
const start = resolveMoment(card.start, named(card.start));
|
||||
if (start === 'drop') return null;
|
||||
// An end without its own offset (a date-only end turned into the end of
|
||||
// that day) belongs to the start's zone when the venue names none.
|
||||
const endNamed = card.end && card.end.offsetMinutes === undefined && !named(card.end) && start.zone
|
||||
? { zone: start.zone, source: 'country' as const }
|
||||
: card.end ? named(card.end) : undefined;
|
||||
const end = card.end ? resolveMoment(card.end, endNamed) : undefined;
|
||||
if (end === 'drop') return null;
|
||||
if (end && !notBefore(end, start)) return null;
|
||||
return { ...card, start, ...(end ? { end } : {}) };
|
||||
}
|
||||
case 'restaurant': {
|
||||
const start = resolveMoment(card.start, namedZone({ address: card.address }, card.kind, zoneOf, card.start.offsetMinutes === undefined));
|
||||
if (start === 'drop') return null;
|
||||
return { ...card, start };
|
||||
}
|
||||
case 'shipment':
|
||||
return card;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve zones and drop cards whose times don't hold together. `zoneOf` is
|
||||
* the airport lookup from loadAirportZones(); without it, flight cards are
|
||||
* dropped (the card must not show before its zones are known).
|
||||
*/
|
||||
export function resolveCards(cards: StructuredCard[], zoneOf?: ZoneOf): StructuredCard[] {
|
||||
const out: StructuredCard[] = [];
|
||||
for (const card of cards) {
|
||||
if (card.kind === 'flight' && !zoneOf) continue;
|
||||
const resolved = resolveCard(card, zoneOf);
|
||||
if (resolved) out.push(resolved);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
// Mirrors webmail lib/structured-mail/time-zones.ts at a529e6e57; keep the two in step.
|
||||
/**
|
||||
* Time zones for booking cards. Markup gives local times with a UTC offset
|
||||
* and never an IANA zone; the card shows the place's local time and the
|
||||
* calendar wants a zone that stays right across DST changes. Plain
|
||||
* TypeScript on Intl, no React, so the native app can reuse it.
|
||||
*/
|
||||
|
||||
const MINUTE = 60_000;
|
||||
|
||||
/**
|
||||
* Countries that use one IANA zone throughout, so an address in them names
|
||||
* the zone. Countries whose territory spans zones (US, CA, RU, BR, AU, MX,
|
||||
* ID, ES with the Canaries, PT with the Azores, ...) are left out on purpose.
|
||||
*/
|
||||
const SINGLE_ZONE_COUNTRIES: Record<string, string> = {
|
||||
AD: 'Europe/Andorra', AE: 'Asia/Dubai', AL: 'Europe/Tirane', AR: 'America/Argentina/Buenos_Aires',
|
||||
AT: 'Europe/Vienna', BA: 'Europe/Sarajevo', BE: 'Europe/Brussels', BG: 'Europe/Sofia',
|
||||
BY: 'Europe/Minsk', CH: 'Europe/Zurich', CN: 'Asia/Shanghai', CO: 'America/Bogota',
|
||||
CR: 'America/Costa_Rica', CU: 'America/Havana', CY: 'Asia/Nicosia', CZ: 'Europe/Prague',
|
||||
DE: 'Europe/Berlin', DK: 'Europe/Copenhagen', DO: 'America/Santo_Domingo', EE: 'Europe/Tallinn',
|
||||
EG: 'Africa/Cairo', FI: 'Europe/Helsinki', FR: 'Europe/Paris', GB: 'Europe/London',
|
||||
GR: 'Europe/Athens', HK: 'Asia/Hong_Kong', HR: 'Europe/Zagreb', HU: 'Europe/Budapest',
|
||||
IE: 'Europe/Dublin', IL: 'Asia/Jerusalem', IN: 'Asia/Kolkata', IS: 'Atlantic/Reykjavik',
|
||||
IT: 'Europe/Rome', JM: 'America/Jamaica', JP: 'Asia/Tokyo', KE: 'Africa/Nairobi',
|
||||
KR: 'Asia/Seoul', LI: 'Europe/Vaduz', LK: 'Asia/Colombo', LT: 'Europe/Vilnius',
|
||||
LU: 'Europe/Luxembourg', LV: 'Europe/Riga', MA: 'Africa/Casablanca', MC: 'Europe/Monaco',
|
||||
MD: 'Europe/Chisinau', ME: 'Europe/Podgorica', MK: 'Europe/Skopje', MT: 'Europe/Malta',
|
||||
MY: 'Asia/Kuala_Lumpur', NL: 'Europe/Amsterdam', NO: 'Europe/Oslo', NP: 'Asia/Kathmandu',
|
||||
NZ: 'Pacific/Auckland', PA: 'America/Panama', PE: 'America/Lima', PH: 'Asia/Manila',
|
||||
PL: 'Europe/Warsaw', QA: 'Asia/Qatar', RO: 'Europe/Bucharest', RS: 'Europe/Belgrade',
|
||||
SE: 'Europe/Stockholm', SG: 'Asia/Singapore', SI: 'Europe/Ljubljana', SK: 'Europe/Bratislava',
|
||||
SM: 'Europe/San_Marino', TH: 'Asia/Bangkok', TN: 'Africa/Tunis', TR: 'Europe/Istanbul',
|
||||
TW: 'Asia/Taipei', UY: 'America/Montevideo', VA: 'Europe/Vatican', VN: 'Asia/Ho_Chi_Minh',
|
||||
ZA: 'Africa/Johannesburg',
|
||||
};
|
||||
|
||||
/**
|
||||
* Countries in the table whose postal addresses also cover overseas places
|
||||
* in other zones (a hotel on Réunion often writes "France"). Good enough
|
||||
* when the mail's offset confirms the zone, not for a time without one.
|
||||
*/
|
||||
const NOT_FOR_FLOATING_TIMES = new Set(['FR']);
|
||||
|
||||
/** The single IANA zone of a country (ISO 3166-1 alpha-2), if it has one. */
|
||||
export function countryZone(country: string | undefined, opts: { floating?: boolean } = {}): string | undefined {
|
||||
const code = country?.trim().toUpperCase();
|
||||
if (!code || !/^[A-Z]{2}$/.test(code)) return undefined;
|
||||
if (opts.floating && NOT_FOR_FLOATING_TIMES.has(code)) return undefined;
|
||||
return SINGLE_ZONE_COUNTRIES[code];
|
||||
}
|
||||
|
||||
const offsetFormatters = new Map<string, Intl.DateTimeFormat>();
|
||||
|
||||
function partsFormatter(zone: string): Intl.DateTimeFormat {
|
||||
let formatter = offsetFormatters.get(zone);
|
||||
if (!formatter) {
|
||||
formatter = new Intl.DateTimeFormat('en-US', {
|
||||
timeZone: zone,
|
||||
hourCycle: 'h23',
|
||||
year: 'numeric', month: '2-digit', day: '2-digit',
|
||||
hour: '2-digit', minute: '2-digit', second: '2-digit',
|
||||
});
|
||||
offsetFormatters.set(zone, formatter);
|
||||
}
|
||||
return formatter;
|
||||
}
|
||||
|
||||
export function isValidTimeZone(zone: string): boolean {
|
||||
try {
|
||||
partsFormatter(zone);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
interface WallClock { year: number; month: number; day: number; hour: number; minute: number; second: number }
|
||||
|
||||
function wallClockParts(utcMs: number, zone: string): WallClock {
|
||||
const parts: Record<string, number> = {};
|
||||
for (const part of partsFormatter(zone).formatToParts(new Date(utcMs))) {
|
||||
if (part.type !== 'literal') parts[part.type] = Number(part.value);
|
||||
}
|
||||
return { year: parts.year, month: parts.month, day: parts.day, hour: parts.hour % 24, minute: parts.minute, second: parts.second };
|
||||
}
|
||||
|
||||
/** UTC offset of `zone` at `utcMs`, in minutes east of UTC (Berlin in winter: 60). */
|
||||
export function zoneOffsetAt(zone: string, utcMs: number): number {
|
||||
const w = wallClockParts(utcMs, zone);
|
||||
const asUtc = Date.UTC(w.year, w.month - 1, w.day, w.hour, w.minute, w.second);
|
||||
return Math.round((asUtc - Math.floor(utcMs / 1000) * 1000) / MINUTE);
|
||||
}
|
||||
|
||||
const pad = (n: number, width = 2) => String(n).padStart(width, '0');
|
||||
|
||||
/** Local date and time in `zone` at `utcMs`: `{ date: 'YYYY-MM-DD', time: 'HH:mm:ss' }`. */
|
||||
export function wallClockAt(utcMs: number, zone: string): { date: string; time: string } {
|
||||
const w = wallClockParts(utcMs, zone);
|
||||
return {
|
||||
date: `${pad(w.year, 4)}-${pad(w.month)}-${pad(w.day)}`,
|
||||
time: `${pad(w.hour)}:${pad(w.minute)}:${pad(w.second)}`,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The UTC instant of a local wall-clock time in `zone`. A time that falls in
|
||||
* a DST gap or overlap has no single answer; returns null then, so a caller
|
||||
* never shows a guessed instant.
|
||||
*/
|
||||
export function zonedTimeToUtc(date: string, time: string, zone: string): number | null {
|
||||
const d = /^(\d{4})-(\d{2})-(\d{2})$/.exec(date);
|
||||
const t = /^(\d{2}):(\d{2})(?::(\d{2}))?$/.exec(time);
|
||||
if (!d || !t) return null;
|
||||
const naive = Date.UTC(+d[1], +d[2] - 1, +d[3], +t[1], +t[2], +(t[3] ?? 0));
|
||||
const check = new Date(naive);
|
||||
if (check.getUTCMonth() !== +d[2] - 1 || check.getUTCDate() !== +d[3] || check.getUTCHours() !== +t[1] || +t[2] > 59 || +(t[3] ?? 0) > 59) {
|
||||
return null;
|
||||
}
|
||||
const candidates = new Set<number>();
|
||||
for (const probe of [naive - 14 * 60 * MINUTE, naive, naive + 14 * 60 * MINUTE]) {
|
||||
const offset = zoneOffsetAt(zone, probe);
|
||||
const utc = naive - offset * MINUTE;
|
||||
if (zoneOffsetAt(zone, utc) === offset) candidates.add(utc);
|
||||
}
|
||||
return candidates.size === 1 ? [...candidates][0] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A zone that has a fixed UTC offset. Whole hours map to `Etc/GMT∓N`, whose
|
||||
* sign is inverted (UTC−8 is `Etc/GMT+8`). Anything else, a half-hour offset
|
||||
* or one outside `Etc/GMT-14` … `Etc/GMT+12`, has no Etc zone: the caller
|
||||
* keeps the UTC instant and gets `Etc/UTC`.
|
||||
*/
|
||||
export function fixedOffsetZone(offsetMinutes: number): string {
|
||||
if (offsetMinutes === 0 || offsetMinutes % 60 !== 0) return 'Etc/UTC';
|
||||
const hours = offsetMinutes / 60;
|
||||
if (hours > 14 || hours < -12) return 'Etc/UTC';
|
||||
return hours > 0 ? `Etc/GMT-${hours}` : `Etc/GMT+${-hours}`;
|
||||
}
|
||||
|
||||
type AirportTable = { zones: string[]; airports: Record<string, number> };
|
||||
let airportTable: Promise<AirportTable> | null = null;
|
||||
|
||||
/**
|
||||
* Load the IATA → IANA table (scripts/generate-airport-timezones.mjs). It is
|
||||
* its own chunk, fetched from the app's origin the first time a flight card
|
||||
* needs it. A failed load is forgotten so the next card can retry.
|
||||
*/
|
||||
export function loadAirportZones(): Promise<(code: string | undefined) => string | undefined> {
|
||||
if (!airportTable) {
|
||||
airportTable = import('./data/airport-timezones.json').then((mod) => (mod.default ?? mod) as AirportTable);
|
||||
airportTable.catch(() => { airportTable = null; });
|
||||
}
|
||||
return airportTable.then((table) => (code) => {
|
||||
const key = code?.trim().toUpperCase();
|
||||
if (!key || !/^[A-Z]{3}$/.test(key)) return undefined;
|
||||
const index = table.airports[key];
|
||||
return index === undefined ? undefined : table.zones[index];
|
||||
});
|
||||
}
|
||||
|
||||
export type ZoneSource = 'airport' | 'country' | 'offset' | 'utc' | 'floating';
|
||||
|
||||
export interface ResolvedZone {
|
||||
/** IANA zone, or null for a floating time that stays as written. */
|
||||
zone: string | null;
|
||||
source: ZoneSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pick the zone for one end of a booking. `named` is the zone the place
|
||||
* implies (airport table or single-zone country). With an offset from the
|
||||
* mail, the named zone must give that same offset at that instant, otherwise
|
||||
* the mail and the table disagree and the result is `mismatch` (the caller
|
||||
* drops the booking: we can't tell which of the two is wrong). Without a
|
||||
* named zone the offset gives a fixed-offset zone. Without an offset the
|
||||
* time is read in the named zone, or stays floating.
|
||||
*/
|
||||
export function resolveZone(input: {
|
||||
named?: { zone: string; source: 'airport' | 'country' };
|
||||
offsetMinutes?: number;
|
||||
utcMs?: number;
|
||||
}): ResolvedZone | 'mismatch' {
|
||||
const { named, offsetMinutes, utcMs } = input;
|
||||
if (offsetMinutes !== undefined && utcMs !== undefined) {
|
||||
if (named) {
|
||||
return zoneOffsetAt(named.zone, utcMs) === offsetMinutes ? { zone: named.zone, source: named.source } : 'mismatch';
|
||||
}
|
||||
const zone = fixedOffsetZone(offsetMinutes);
|
||||
return { zone, source: zone === 'Etc/UTC' && offsetMinutes !== 0 ? 'utc' : 'offset' };
|
||||
}
|
||||
if (named) return { zone: named.zone, source: named.source };
|
||||
return { zone: null, source: 'floating' };
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
// Mirrors webmail lib/structured-mail/trust.ts at 8098039d6; keep the two in step.
|
||||
/**
|
||||
* Whether a message may show booking cards at all. A card looks official and
|
||||
* a calendar entry outlives the mail, so the bar is high: the receiving
|
||||
* server must have tied the mail to its From domain, the mail must not be a
|
||||
* forward, and the markup must come from the message itself. Plain
|
||||
* TypeScript on the Email shape; the admin gate is checked by the caller.
|
||||
*/
|
||||
// App: the app's Email carries only the raw `headers` list, so the
|
||||
// Authentication-Results are parsed here (src/lib/email-headers.ts) unless
|
||||
// the caller passes them, as the webmail's full message fetch does.
|
||||
import {
|
||||
getSenderVerification,
|
||||
hasAlignedAuthenticationPass,
|
||||
headerValues,
|
||||
parseAuthenticationResults,
|
||||
type AuthenticationResults,
|
||||
} from '../email-headers';
|
||||
import type { BodyPart, Email } from '../../api/types';
|
||||
import { hasForwardPrefix } from './forward-prefix';
|
||||
import { authenticationResultsOnReceipt } from './receipt';
|
||||
|
||||
/** A body part with its children (RFC 8621 bodyStructure), which the app's BodyPart leaves out. */
|
||||
export type EmailBodyPart = BodyPart & { subParts?: EmailBodyPart[] };
|
||||
|
||||
export type UntrustedReason =
|
||||
| 'no_authentication_results'
|
||||
| 'not_written_on_receipt'
|
||||
| 'sender'
|
||||
| 'sender_not_verified'
|
||||
| 'personal_mailbox'
|
||||
| 'forwarded'
|
||||
| 'attached_message'
|
||||
| 'truncated';
|
||||
|
||||
export type StructuredMailTrust = { trusted: true } | { trusted: false; reason: UntrustedReason };
|
||||
|
||||
export type TrustInput = Pick<Email, 'from' | 'subject' | 'headers' | 'htmlBody' | 'textBody' | 'bodyValues'> & {
|
||||
bodyStructure?: EmailBodyPart;
|
||||
/** Parsed Authentication-Results; read from `headers` when absent. */
|
||||
authenticationResults?: AuthenticationResults;
|
||||
/** See authenticationResultsOnReceipt; read from `headers` when absent. */
|
||||
authenticationResultsOnReceipt?: boolean;
|
||||
};
|
||||
|
||||
/** The message's Authentication-Results, in message order, parsed; undefined without any. */
|
||||
function authenticationResultsOf(email: TrustInput): AuthenticationResults | undefined {
|
||||
if (email.authenticationResults) return email.authenticationResults;
|
||||
const values = headerValues(headerList(email.headers), 'Authentication-Results');
|
||||
return values.length > 0 ? parseAuthenticationResults(values) : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Personal mailbox providers. A booking mail from one of these is someone
|
||||
* forwarding it (perhaps with the "Fwd:" removed); airlines and shops don't
|
||||
* send from them.
|
||||
*/
|
||||
const PERSONAL_MAILBOX_DOMAINS = [
|
||||
'gmail.com', 'googlemail.com', 'msn.com', 'aol.com', 'icloud.com', 'me.com', 'mac.com',
|
||||
'web.de', 't-online.de', 'freenet.de', 'posteo.de', 'posteo.net', 'mailbox.org', 'proton.me',
|
||||
'protonmail.com', 'protonmail.ch', 'pm.me', 'mail.ru', 'gmx.com', 'gmx.net', 'gmx.de', 'gmx.at', 'gmx.ch',
|
||||
'tutanota.com', 'tuta.io', 'zoho.com', 'fastmail.com', 'hey.com',
|
||||
];
|
||||
const PERSONAL_MAILBOX_FAMILIES = /^(?:outlook|hotmail|live|yahoo|ymail|yandex)\.[a-z.]+$/;
|
||||
|
||||
export function isPersonalMailboxDomain(domain: string): boolean {
|
||||
const d = domain.toLowerCase();
|
||||
return PERSONAL_MAILBOX_DOMAINS.includes(d) || PERSONAL_MAILBOX_FAMILIES.test(d);
|
||||
}
|
||||
|
||||
/** Separator lines mail clients put above a forwarded message. */
|
||||
const FORWARD_SEPARATOR_RE = new RegExp([
|
||||
'forwarded message', 'begin forwarded message', 'original message', 'weitergeleitete nachricht',
|
||||
'ursprüngliche nachricht', 'message transféré', 'message d\'origine', 'mensaje reenviado', 'mensaje original',
|
||||
'messaggio inoltrato', 'messaggio originale', 'doorgestuurd bericht', 'oorspronkelijk bericht', 'mensagem encaminhada',
|
||||
].map((s) => `-{2,}\\s*${s}\\s*-{2,}|^\\s*${s}:?\\s*$`).join('|'), 'im');
|
||||
|
||||
/** Outlook's forward header block: From / Sent / To ... Subject, in a few languages. */
|
||||
const OUTLOOK_HEADER_RE = /^\s*(?:from|von|de|da|van|od):[^\n]*\n\s*(?:sent|gesendet|envoyé|inviato|enviado|verzonden|wysłano):[^\n]*\n(?:[^\n]*\n){0,3}\s*(?:subject|betreff|objet|oggetto|asunto|onderwerp|temat):/im;
|
||||
|
||||
function htmlToLines(html: string): string {
|
||||
return html
|
||||
.replace(/<(?:script|style)[\s\S]*?<\/(?:script|style)>/gi, ' ')
|
||||
.replace(/<(?:br|\/p|\/div|\/tr|\/li|\/h\d|\/td)\b[^>]*>/gi, '\n')
|
||||
.replace(/<[^>]+>/g, ' ')
|
||||
.replace(/ /gi, ' ')
|
||||
.replace(/[ \t]+/g, ' ');
|
||||
}
|
||||
|
||||
function looksForwarded(text: string): boolean {
|
||||
return FORWARD_SEPARATOR_RE.test(text) || OUTLOOK_HEADER_RE.test(text);
|
||||
}
|
||||
|
||||
function partValue(email: TrustInput, part: EmailBodyPart | undefined): { value: string; truncated: boolean } | undefined {
|
||||
if (!part?.partId) return undefined;
|
||||
const value = email.bodyValues?.[part.partId];
|
||||
return value ? { value: value.value ?? '', truncated: !!value.isTruncated } : undefined;
|
||||
}
|
||||
|
||||
/** The HTML part a card reads: the message's own first text/html part. */
|
||||
export function structuredHtmlPart(email: Pick<Email, 'htmlBody' | 'bodyValues'>): { value: string; truncated: boolean } | undefined {
|
||||
const part = email.htmlBody?.find((p) => p.type === 'text/html');
|
||||
if (!part?.partId) return undefined;
|
||||
const value = email.bodyValues?.[part.partId];
|
||||
return value ? { value: value.value ?? '', truncated: !!value.isTruncated } : undefined;
|
||||
}
|
||||
|
||||
/** Whether `partId` sits inside an attached message (message/rfc822) in the body tree. */
|
||||
function insideAttachedMessage(root: EmailBodyPart | undefined, partId: string | undefined): boolean {
|
||||
if (!root || !partId) return false;
|
||||
const walk = (part: EmailBodyPart, inMessage: boolean): boolean | undefined => {
|
||||
const nested = inMessage || (part !== root && part.type?.toLowerCase() === 'message/rfc822');
|
||||
if (part.partId === partId) return nested;
|
||||
for (const child of part.subParts ?? []) {
|
||||
const found = walk(child, nested);
|
||||
if (found !== undefined) return found;
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
return walk(root, false) ?? false;
|
||||
}
|
||||
|
||||
function headerList(headers: TrustInput['headers']): { name: string; value: string }[] | undefined {
|
||||
return Array.isArray(headers) ? (headers as unknown as { name: string; value: string }[]) : undefined;
|
||||
}
|
||||
|
||||
export function getStructuredMailTrust(email: TrustInput): StructuredMailTrust {
|
||||
const auth = authenticationResultsOf(email);
|
||||
if (!auth) return { trusted: false, reason: 'no_authentication_results' };
|
||||
|
||||
const list = headerList(email.headers);
|
||||
const onReceipt = email.authenticationResultsOnReceipt ?? (list ? authenticationResultsOnReceipt(list) : false);
|
||||
if (!onReceipt) return { trusted: false, reason: 'not_written_on_receipt' };
|
||||
|
||||
const senders = email.from ?? [];
|
||||
const from = senders[0]?.email ?? '';
|
||||
const domain = from.slice(from.lastIndexOf('@') + 1).toLowerCase();
|
||||
if (senders.length !== 1 || !from.includes('@') || !domain.includes('.')) return { trusted: false, reason: 'sender' };
|
||||
|
||||
if (getSenderVerification(auth, from) !== null || !hasAlignedAuthenticationPass(auth, from)) {
|
||||
return { trusted: false, reason: 'sender_not_verified' };
|
||||
}
|
||||
if (isPersonalMailboxDomain(domain)) return { trusted: false, reason: 'personal_mailbox' };
|
||||
|
||||
const html = structuredHtmlPart(email);
|
||||
const htmlPartId = email.htmlBody?.find((p) => p.type === 'text/html')?.partId;
|
||||
if (insideAttachedMessage(email.bodyStructure, htmlPartId)) return { trusted: false, reason: 'attached_message' };
|
||||
if (html?.truncated) return { trusted: false, reason: 'truncated' };
|
||||
|
||||
if (hasForwardPrefix(email.subject)) return { trusted: false, reason: 'forwarded' };
|
||||
const text = partValue(email, email.textBody?.find((p) => p.type === 'text/plain'));
|
||||
if ((html && looksForwarded(htmlToLines(html.value))) || (text && looksForwarded(text.value))) {
|
||||
return { trusted: false, reason: 'forwarded' };
|
||||
}
|
||||
return { trusted: true };
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
// Mirrors webmail lib/structured-mail/types.ts at 1d4cae961; keep the two in step.
|
||||
/**
|
||||
* The model booking, shipping and order mail is read into. Plain data, no
|
||||
* React and no app imports, so the native app can reuse the parser.
|
||||
*/
|
||||
|
||||
export type BookingKind = 'flight' | 'train' | 'bus' | 'lodging' | 'rentalcar' | 'event' | 'restaurant';
|
||||
export type TransportKind = 'flight' | 'train' | 'bus';
|
||||
export type ReservationStatus = 'confirmed' | 'pending' | 'hold' | 'cancelled';
|
||||
export type OrderStatus =
|
||||
| 'payment_due' | 'processing' | 'problem' | 'in_transit'
|
||||
| 'pickup_available' | 'returned' | 'delivered' | 'cancelled';
|
||||
|
||||
/** A time as the mail wrote it, local to the place it belongs to. */
|
||||
export interface Moment {
|
||||
/** YYYY-MM-DD, local. */
|
||||
date: string;
|
||||
/** HH:mm:ss, local; absent for a date-only value. */
|
||||
time?: string;
|
||||
/** Minutes east of UTC, from the value; absent when the mail gave none. */
|
||||
offsetMinutes?: number;
|
||||
/** ISO instant; set when the time and its zone or offset are known. */
|
||||
utc?: string;
|
||||
/**
|
||||
* IANA zone after resolution (resolveCards): airport, country or a
|
||||
* fixed-offset zone; null for a time that stays floating. Absent before.
|
||||
*/
|
||||
zone?: string | null;
|
||||
/**
|
||||
* Set by the .pkpass reader for a UTC time with no zone for its place:
|
||||
* `utc` is the instant, but date, time and offset are UTC, not local.
|
||||
* Resolution takes the wall clock from the airport's zone and clears it.
|
||||
*/
|
||||
instantOnly?: true;
|
||||
}
|
||||
|
||||
export interface Address {
|
||||
street?: string;
|
||||
locality?: string;
|
||||
region?: string;
|
||||
postalCode?: string;
|
||||
/** ISO 3166-1 alpha-2, when the mail gave one. */
|
||||
country?: string;
|
||||
/** Country as the mail named it, when that wasn't a code. */
|
||||
countryName?: string;
|
||||
}
|
||||
|
||||
export interface Place {
|
||||
name?: string;
|
||||
/** IATA airport code for airports, upper case. */
|
||||
code?: string;
|
||||
address?: Address;
|
||||
}
|
||||
|
||||
/** A link a card may show: http(s) only, host shown next to it. */
|
||||
export interface Link {
|
||||
url: string;
|
||||
host: string;
|
||||
}
|
||||
|
||||
export interface Seat {
|
||||
passenger?: string;
|
||||
seat: string;
|
||||
}
|
||||
|
||||
export interface TransportLeg {
|
||||
carrier?: string;
|
||||
/** IATA airline code for flights. */
|
||||
carrierCode?: string;
|
||||
number?: string;
|
||||
departure: { place: Place; at: Moment };
|
||||
arrival?: { place: Place; at?: Moment };
|
||||
seats: Seat[];
|
||||
status?: ReservationStatus;
|
||||
}
|
||||
|
||||
export interface BookingLinks {
|
||||
checkin?: Link;
|
||||
manage?: Link;
|
||||
ticket?: Link;
|
||||
}
|
||||
|
||||
export type CardSource = 'json-ld' | 'microdata' | 'pkpass';
|
||||
|
||||
interface BookingBase {
|
||||
/** Merge key: equal keys are the same booking. */
|
||||
key: string;
|
||||
kind: BookingKind;
|
||||
reference?: string;
|
||||
status?: ReservationStatus;
|
||||
passengers: string[];
|
||||
provider?: string;
|
||||
links: BookingLinks;
|
||||
modified?: string;
|
||||
source: CardSource;
|
||||
}
|
||||
|
||||
export interface TransportBooking extends BookingBase {
|
||||
kind: TransportKind;
|
||||
legs: TransportLeg[];
|
||||
}
|
||||
|
||||
export interface LodgingBooking extends BookingBase {
|
||||
kind: 'lodging';
|
||||
name: string;
|
||||
address?: Address;
|
||||
phone?: string;
|
||||
checkin: Moment;
|
||||
checkout: Moment;
|
||||
}
|
||||
|
||||
export interface RentalCarBooking extends BookingBase {
|
||||
kind: 'rentalcar';
|
||||
company?: string;
|
||||
car?: string;
|
||||
pickup: { place: Place; at: Moment };
|
||||
dropoff: { place: Place; at: Moment };
|
||||
}
|
||||
|
||||
export interface EventBooking extends BookingBase {
|
||||
kind: 'event';
|
||||
name: string;
|
||||
venue?: Place;
|
||||
start: Moment;
|
||||
end?: Moment;
|
||||
tickets: string[];
|
||||
}
|
||||
|
||||
export interface RestaurantBooking extends BookingBase {
|
||||
kind: 'restaurant';
|
||||
name: string;
|
||||
address?: Address;
|
||||
start: Moment;
|
||||
partySize?: number;
|
||||
}
|
||||
|
||||
export type Booking = TransportBooking | LodgingBooking | RentalCarBooking | EventBooking | RestaurantBooking;
|
||||
|
||||
export interface Item {
|
||||
name: string;
|
||||
quantity?: number;
|
||||
/** Remote image; a card shows it only when remote content may load. */
|
||||
image?: Link;
|
||||
}
|
||||
|
||||
export interface Parcel {
|
||||
carrier?: string;
|
||||
trackingNumber?: string;
|
||||
trackingUrl?: Link;
|
||||
expectedBy?: Moment;
|
||||
items: Item[];
|
||||
}
|
||||
|
||||
/** An Order and/or the ParcelDeliveries for one order number. */
|
||||
export interface Shipment {
|
||||
kind: 'shipment';
|
||||
key: string;
|
||||
merchant?: string;
|
||||
orderNumber?: string;
|
||||
status?: OrderStatus;
|
||||
items: Item[];
|
||||
/** Amount normalised to a dot decimal ("399.00"). */
|
||||
total?: { amount: string; currency: string };
|
||||
orderUrl?: Link;
|
||||
parcels: Parcel[];
|
||||
source: CardSource;
|
||||
}
|
||||
|
||||
export type StructuredCard = Booking | Shipment;
|
||||
|
||||
/** A card plus the reasons validation found to doubt it (empty = fine). */
|
||||
export interface Candidate {
|
||||
card: StructuredCard;
|
||||
problems: string[];
|
||||
}
|
||||
|
||||
export function isBooking(card: StructuredCard): card is Booking {
|
||||
return card.kind !== 'shipment';
|
||||
}
|
||||
|
||||
export function isTransport(card: StructuredCard): card is TransportBooking {
|
||||
return card.kind === 'flight' || card.kind === 'train' || card.kind === 'bus';
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
// Mirrors webmail lib/structured-mail/validate.ts at 76b0c2fba; keep the two in step.
|
||||
/**
|
||||
* Plausibility checks that don't need time zones. A card with any problem is
|
||||
* dropped, together with every other part of the same booking (merge.ts):
|
||||
* a wrong card is worse than no card. Time order and durations are checked
|
||||
* once the zones are known (resolve.ts).
|
||||
*/
|
||||
import type { Candidate, Moment, StructuredCard, TransportLeg } from './types';
|
||||
|
||||
const DAY = 86_400_000;
|
||||
const PAST_WINDOW_DAYS = 366;
|
||||
const TRANSPORT_FUTURE_DAYS = 400;
|
||||
const OTHER_FUTURE_DAYS = 760;
|
||||
|
||||
/**
|
||||
* Values from Google's markup documentation examples. Senders paste those
|
||||
* examples into newsletters (zave.it sent the "Eva Green" flight in a
|
||||
* cashback mail, with a passing DKIM signature); they are never real.
|
||||
*/
|
||||
const DOCUMENTATION_VALUES = new Set([
|
||||
'rxj34p', 'abc456', 'e123456789', 'ot12345', '546323', '3453291231', '176057', '123-4567890-1234567',
|
||||
]);
|
||||
|
||||
function dayNumber(date: string): number | undefined {
|
||||
const ms = Date.parse(`${date}T00:00:00Z`);
|
||||
return Number.isFinite(ms) ? Math.floor(ms / DAY) : undefined;
|
||||
}
|
||||
|
||||
function hasDate(m: Moment | undefined): m is Moment {
|
||||
return !!m && /^\d{4}-\d{2}-\d{2}$/.test(m.date);
|
||||
}
|
||||
|
||||
function hasTime(m: Moment | undefined): boolean {
|
||||
return hasDate(m) && !!m.time;
|
||||
}
|
||||
|
||||
function firstDate(card: StructuredCard): string | undefined {
|
||||
switch (card.kind) {
|
||||
case 'flight': case 'train': case 'bus': return card.legs[0]?.departure.at.date;
|
||||
case 'lodging': return card.checkin.date;
|
||||
case 'rentalcar': return card.pickup.at.date;
|
||||
case 'event': case 'restaurant': return card.start.date;
|
||||
case 'shipment': return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function legProblems(kind: 'flight' | 'train' | 'bus', leg: TransportLeg, fromPass: boolean): string[] {
|
||||
const problems: string[] = [];
|
||||
if (!hasTime(leg.departure.at)) problems.push('departure time missing');
|
||||
if (!fromPass && !hasTime(leg.arrival?.at)) problems.push('arrival time missing');
|
||||
const named = (p: { code?: string; name?: string } | undefined) => !!(p && (kind === 'flight' ? p.code || p.name : p.name));
|
||||
if (!named(leg.departure.place)) problems.push('departure place missing');
|
||||
if (!named(leg.arrival?.place)) problems.push('arrival place missing');
|
||||
if (kind === 'flight' && !(leg.carrierCode || leg.carrier)) problems.push('airline missing');
|
||||
if (kind === 'flight' && !leg.number) problems.push('flight number missing');
|
||||
return problems;
|
||||
}
|
||||
|
||||
function cardProblems(card: StructuredCard): string[] {
|
||||
switch (card.kind) {
|
||||
case 'flight': case 'train': case 'bus':
|
||||
if (card.legs.length === 0) return ['no legs'];
|
||||
return card.legs.flatMap((leg) => legProblems(card.kind as 'flight' | 'train' | 'bus', leg, card.source === 'pkpass'));
|
||||
case 'lodging': {
|
||||
const problems: string[] = [];
|
||||
if (!card.name) problems.push('hotel name missing');
|
||||
if (!hasDate(card.checkin) || !hasDate(card.checkout)) problems.push('stay dates missing');
|
||||
else {
|
||||
const nights = dayNumber(card.checkout.date)! - dayNumber(card.checkin.date)!;
|
||||
if (nights < 1) problems.push('check-out not after check-in');
|
||||
if (nights > 365) problems.push('stay longer than a year');
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
case 'rentalcar': {
|
||||
const problems: string[] = [];
|
||||
if (!hasTime(card.pickup.at) || !hasTime(card.dropoff.at)) problems.push('rental times missing');
|
||||
else {
|
||||
const days = dayNumber(card.dropoff.at.date)! - dayNumber(card.pickup.at.date)!;
|
||||
if (days < 0) problems.push('dropoff before pickup');
|
||||
if (days > 365) problems.push('rental longer than a year');
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
case 'event': {
|
||||
const problems: string[] = [];
|
||||
if (!card.name) problems.push('event name missing');
|
||||
if (!hasDate(card.start)) problems.push('event start missing');
|
||||
else if (card.end && hasDate(card.end)) {
|
||||
const days = dayNumber(card.end.date)! - dayNumber(card.start.date)!;
|
||||
if (days < 0) problems.push('event ends before it starts');
|
||||
if (days > 31) problems.push('event longer than a month');
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
case 'restaurant': {
|
||||
const problems: string[] = [];
|
||||
if (!card.name) problems.push('restaurant name missing');
|
||||
if (!hasTime(card.start)) problems.push('reservation time missing');
|
||||
return problems;
|
||||
}
|
||||
case 'shipment': {
|
||||
const problems: string[] = [];
|
||||
const tracked = card.parcels.some((p) => p.trackingNumber || p.trackingUrl);
|
||||
if (!card.orderNumber && !tracked) problems.push('neither order number nor tracking');
|
||||
const showable = card.merchant || card.items.length > 0 || card.parcels.some((p) => p.carrier || p.items.length > 0 || p.expectedBy);
|
||||
if (!showable) problems.push('nothing to show');
|
||||
return problems;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function identifiers(card: StructuredCard): string[] {
|
||||
if (card.kind === 'shipment') {
|
||||
return [card.orderNumber, ...card.parcels.map((p) => p.trackingNumber)].filter((v): v is string => !!v);
|
||||
}
|
||||
return card.reference ? [card.reference] : [];
|
||||
}
|
||||
|
||||
/** Check one card against the mail's date. */
|
||||
export function validateCard(card: StructuredCard, mailDate: Date): Candidate {
|
||||
const problems = cardProblems(card);
|
||||
if (identifiers(card).some((id) => DOCUMENTATION_VALUES.has(id.toLowerCase()))) {
|
||||
problems.push('documentation example');
|
||||
}
|
||||
const date = firstDate(card);
|
||||
const mailDay = Math.floor(mailDate.getTime() / DAY);
|
||||
const day = date ? dayNumber(date) : undefined;
|
||||
if (date !== undefined && day !== undefined && Number.isFinite(mailDay)) {
|
||||
const future = card.kind === 'flight' || card.kind === 'train' || card.kind === 'bus' ? TRANSPORT_FUTURE_DAYS : OTHER_FUTURE_DAYS;
|
||||
if (day < mailDay - PAST_WINDOW_DAYS || day > mailDay + future) problems.push('date too far from the mail');
|
||||
}
|
||||
return { card, problems };
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
// Mirrors webmail lib/structured-mail/values.ts at 76b0c2fba; keep the two in step.
|
||||
/**
|
||||
* Readers for single schema.org values, tolerant of the shapes real senders
|
||||
* use: a string where an object belongs, an array where one value belongs,
|
||||
* numbers for identifiers, German prices, offsets without a colon.
|
||||
*/
|
||||
import type { Address, Link, Moment, OrderStatus, ReservationStatus } from './types';
|
||||
|
||||
export type Obj = Record<string, unknown>;
|
||||
|
||||
export function isObj(value: unknown): value is Obj {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
/** The values of a property: an array as is, anything else as one element. */
|
||||
export function all(value: unknown): unknown[] {
|
||||
if (value === undefined || value === null) return [];
|
||||
return Array.isArray(value) ? value : [value];
|
||||
}
|
||||
|
||||
/** The first object among the values. */
|
||||
export function obj(value: unknown): Obj | undefined {
|
||||
return all(value).find(isObj);
|
||||
}
|
||||
|
||||
/** The first non-empty string (or number, as a string). */
|
||||
export function str(value: unknown): string | undefined {
|
||||
for (const v of all(value)) {
|
||||
if (typeof v === 'number' && Number.isFinite(v)) return String(v);
|
||||
if (typeof v === 'string') {
|
||||
const trimmed = v.replace(/\s+/g, ' ').trim();
|
||||
if (trimmed) return trimmed;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** A name: a plain string, or an object's `name` (Organization, Person, Place). */
|
||||
export function nameOf(value: unknown): string | undefined {
|
||||
for (const v of all(value)) {
|
||||
if (isObj(v)) {
|
||||
const name = str(v.name) ?? [str(v.givenName), str(v.familyName)].filter(Boolean).join(' ');
|
||||
if (name) return name;
|
||||
} else {
|
||||
const s = str(v);
|
||||
if (s) return s;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** Types of an object, as their last path segment ("FlightReservation"). */
|
||||
export function typesOf(value: unknown): string[] {
|
||||
if (!isObj(value)) return [];
|
||||
return all(value['@type'])
|
||||
.map((t) => (typeof t === 'string' ? t.trim().replace(/^schema:/i, '').split(/[/#]/).pop() ?? '' : ''))
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
export function hasType(value: unknown, names: readonly string[]): boolean {
|
||||
return typesOf(value).some((t) => names.includes(t));
|
||||
}
|
||||
|
||||
const SCHEMA_CONTEXT = /^https?:\/\/(?:www\.)?schema\.org\/?$/i;
|
||||
|
||||
/** Whether an `@context` names schema.org (string, array or `@vocab`). */
|
||||
export function isSchemaContext(context: unknown): boolean {
|
||||
return all(context).some((c) =>
|
||||
(typeof c === 'string' && SCHEMA_CONTEXT.test(c.trim()))
|
||||
|| (isObj(c) && typeof c['@vocab'] === 'string' && SCHEMA_CONTEXT.test(c['@vocab'].trim())));
|
||||
}
|
||||
|
||||
/** The last segment of an enum value: a URL or a bare token. */
|
||||
function enumToken(value: unknown): string | undefined {
|
||||
const s = str(value);
|
||||
if (!s) return undefined;
|
||||
return s.split(/[/#:]/).filter(Boolean).pop();
|
||||
}
|
||||
|
||||
const RESERVATION_STATUS: Record<string, ReservationStatus> = {
|
||||
confirmed: 'confirmed', cancelled: 'cancelled', canceled: 'cancelled', pending: 'pending', hold: 'hold',
|
||||
};
|
||||
|
||||
/** ReservationConfirmed / Confirmed, full URL (http/https) or token. The first value that maps wins. */
|
||||
export function reservationStatus(value: unknown): ReservationStatus | undefined {
|
||||
for (const v of all(value)) {
|
||||
const token = enumToken(isObj(v) ? v['@id'] ?? v.name : v)?.toLowerCase().replace(/^reservation/, '');
|
||||
if (token && RESERVATION_STATUS[token]) return RESERVATION_STATUS[token];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const ORDER_STATUS: Record<string, OrderStatus> = {
|
||||
paymentdue: 'payment_due', processing: 'processing', problem: 'problem', intransit: 'in_transit',
|
||||
pickupavailable: 'pickup_available', returned: 'returned', delivered: 'delivered',
|
||||
cancelled: 'cancelled', canceled: 'cancelled',
|
||||
};
|
||||
|
||||
export function orderStatus(value: unknown): OrderStatus | undefined {
|
||||
for (const v of all(value)) {
|
||||
const token = enumToken(isObj(v) ? v['@id'] ?? v.name : v)?.toLowerCase().replace(/^order(status)?/, '');
|
||||
if (token && ORDER_STATUS[token]) return ORDER_STATUS[token];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const ISO_MOMENT = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:[.,]\d+)?)?\s*(Z|[+-]\d{2}(?::?\d{2})?)?)?$/i;
|
||||
|
||||
function validDate(y: number, m: number, d: number): boolean {
|
||||
const date = new Date(Date.UTC(y, m - 1, d));
|
||||
return date.getUTCFullYear() === y && date.getUTCMonth() === m - 1 && date.getUTCDate() === d;
|
||||
}
|
||||
|
||||
function parseOffset(text: string): number | undefined {
|
||||
if (/^z$/i.test(text)) return 0;
|
||||
const match = /^([+-])(\d{2}):?(\d{2})?$/.exec(text);
|
||||
if (!match) return undefined;
|
||||
const hours = Number(match[2]);
|
||||
const minutes = Number(match[3] ?? 0);
|
||||
if (hours > 14 || minutes > 59) return undefined;
|
||||
return (match[1] === '-' ? -1 : 1) * (hours * 60 + minutes);
|
||||
}
|
||||
|
||||
/**
|
||||
* An ISO 8601 date or date-time. Offsets `Z`, `+01:00`, `+0100` and `+01`;
|
||||
* fractional seconds dropped. Anything else is undefined, so a doubtful value
|
||||
* counts as missing.
|
||||
*/
|
||||
export function moment(value: unknown): Moment | undefined {
|
||||
const s = str(value);
|
||||
if (!s) return undefined;
|
||||
const match = ISO_MOMENT.exec(s);
|
||||
if (!match) return undefined;
|
||||
const [, y, mo, d, h, mi, se, off] = match;
|
||||
if (!validDate(+y, +mo, +d)) return undefined;
|
||||
const result: Moment = { date: `${y}-${mo}-${d}` };
|
||||
if (h === undefined) return result;
|
||||
if (+h > 23 || +mi > 59 || +(se ?? 0) > 59) return undefined;
|
||||
result.time = `${h}:${mi}:${se ?? '00'}`;
|
||||
if (off !== undefined) {
|
||||
const offset = parseOffset(off);
|
||||
if (offset === undefined) return undefined;
|
||||
result.offsetMinutes = offset;
|
||||
const local = Date.UTC(+y, +mo - 1, +d, +h, +mi, +(se ?? 0));
|
||||
result.utc = new Date(local - offset * 60_000).toISOString().replace('.000Z', 'Z');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only the date of a value whose time part is broken
|
||||
* (`2024-10-13CEST03:10:23`, Kaufland). Used where a date is all a card
|
||||
* shows (expected parcel arrival); nowhere else.
|
||||
*/
|
||||
export function lenientDate(value: unknown): Moment | undefined {
|
||||
const parsed = moment(value);
|
||||
if (parsed) return { date: parsed.date };
|
||||
const s = str(value);
|
||||
const match = s ? /^(\d{4})-(\d{2})-(\d{2})(?![\d])/.exec(s) : null;
|
||||
if (!match || !validDate(+match[1], +match[2], +match[3])) return undefined;
|
||||
return { date: `${match[1]}-${match[2]}-${match[3]}` };
|
||||
}
|
||||
|
||||
/** A PostalAddress, or a plain string kept as the street line. */
|
||||
export function address(value: unknown): Address | undefined {
|
||||
const v = all(value)[0];
|
||||
if (typeof v === 'string') {
|
||||
const street = str(v);
|
||||
return street ? { street } : undefined;
|
||||
}
|
||||
if (!isObj(v)) return undefined;
|
||||
const result: Address = {};
|
||||
const set = (key: keyof Address, raw: unknown) => {
|
||||
const s = str(raw);
|
||||
if (s) result[key] = s;
|
||||
};
|
||||
set('street', v.streetAddress);
|
||||
set('locality', v.addressLocality);
|
||||
set('region', v.addressRegion);
|
||||
set('postalCode', v.postalCode);
|
||||
const country = isObj(v.addressCountry) ? str(v.addressCountry.identifier) ?? nameOf(v.addressCountry) : str(v.addressCountry);
|
||||
if (country) {
|
||||
if (/^[A-Za-z]{2}$/.test(country)) result.country = country.toUpperCase();
|
||||
else result.countryName = country;
|
||||
}
|
||||
return Object.keys(result).length > 0 ? result : undefined;
|
||||
}
|
||||
|
||||
const IPV4 = /^\d{1,3}(?:\.\d{1,3}){3}$/;
|
||||
|
||||
/**
|
||||
* A link a card may show: absolute http(s), a real host name (no single
|
||||
* label like Ahoi's `http://default/`, no IP literal, no localhost), no
|
||||
* credentials. Anything else is dropped.
|
||||
*/
|
||||
export function link(value: unknown): Link | undefined {
|
||||
for (const v of all(value)) {
|
||||
const raw = isObj(v) ? str(v.url) ?? str(v.target) ?? str(v['@id']) : str(v);
|
||||
if (!raw) continue;
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(raw);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (url.protocol !== 'https:' && url.protocol !== 'http:') continue;
|
||||
if (url.username || url.password) continue;
|
||||
const host = url.hostname.toLowerCase();
|
||||
if (!host.includes('.') || host.startsWith('[') || IPV4.test(host) || host === 'localhost' || host.endsWith('.localhost')) continue;
|
||||
if (!/^[a-z0-9.-]+$/.test(host) || host.endsWith('.')) continue;
|
||||
return { url: url.href, host: host.replace(/^www\./, '') };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* A price as a dot-decimal string: numbers, "29.99", "14.9700", and German
|
||||
* "399,00 €" / "1.299,00 EUR". Ambiguous values ("1,299") give undefined.
|
||||
*/
|
||||
export function amount(value: unknown): string | undefined {
|
||||
for (const v of all(value)) {
|
||||
if (typeof v === 'number' && Number.isFinite(v) && v >= 0) return v.toFixed(2);
|
||||
if (typeof v !== 'string') continue;
|
||||
const s = v.replace(/[^\d.,]/g, '');
|
||||
if (!s) continue;
|
||||
let normalised: string | undefined;
|
||||
if (/^\d+(\.\d+)?$/.test(s)) normalised = s;
|
||||
else if (/^\d{1,3}(\.\d{3})+,\d{1,2}$/.test(s) || /^\d+,\d{1,2}$/.test(s)) normalised = s.replace(/\./g, '').replace(',', '.');
|
||||
else if (/^\d{1,3}(,\d{3})+\.\d{1,2}$/.test(s)) normalised = s.replace(/,/g, '');
|
||||
if (normalised === undefined) continue;
|
||||
const n = Number(normalised);
|
||||
if (Number.isFinite(n)) return n.toFixed(2);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function currency(value: unknown): string | undefined {
|
||||
const s = str(value);
|
||||
return s && /^[A-Za-z]{3}$/.test(s) ? s.toUpperCase() : undefined;
|
||||
}
|
||||
|
||||
export function positiveInt(value: unknown): number | undefined {
|
||||
for (const v of all(value)) {
|
||||
const raw = isObj(v) ? v.value : v;
|
||||
const n = typeof raw === 'number' ? raw : typeof raw === 'string' && /^\s*\d+\s*$/.test(raw) ? Number(raw) : NaN;
|
||||
if (Number.isInteger(n) && n > 0 && n < 10_000) return n;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** Names of people: Person objects, strings, arrays; duplicates dropped. */
|
||||
export function people(value: unknown): string[] {
|
||||
const names = all(value).map((v) => nameOf(v)).filter((n): n is string => !!n);
|
||||
return [...new Set(names)];
|
||||
}
|
||||
Reference in New Issue
Block a user