Files
TREK/server/tests/unit/nest/booking-import/booking-import.service.test.ts
T
Maurice 71cd6c2de4 place-contrib: record origins for collection copies and booking imports
A place copied from a collection and a venue a booking import creates now
record their details as an import, so a later edit compares against what was
really written. Dawarich and the background enrichment write none of the
shareable fields, so they need nothing.

The admin panel asks again for a few seconds while a fresh installation is
still fetching its key, so the prompt appears without a reload.
2026-10-31 03:25:55 +00:00

518 lines
20 KiB
TypeScript

import { BookingImportService } from '../../../../src/nest/booking-import/booking-import.service';
import { HttpException } from '@nestjs/common';
import { describe, it, expect, vi, beforeEach } from 'vitest';
// Mock the heavy side-effect imports so the service module loads cleanly; the
// preview() path under test only touches the extractor + llmParse deps.
vi.mock('../../../../src/db/database', () => ({
db: { prepare: vi.fn() },
closeDb: () => {},
reinitialize: () => {},
// Trip access reaches these through DatabaseService; preview() never calls
// them, but the module-level import has to resolve.
canAccessTrip: vi.fn(),
isOwner: () => false,
getPlaceWithTags: () => null,
}));
const permissionsStub = { checkPermission: vi.fn(() => true) };
// The origin bookkeeping of the venues a confirm creates; its own suite is place-origins.service.test.ts.
const origins = { recordImported: vi.fn(async () => undefined) };
// Runs the callback in place; the tests that care about the boundary wrap it to record it.
const uowStub = { transactional: <T>(fn: () => Promise<T>) => fn() };
const HOTEL_KI = {
'@type': 'LodgingReservation',
reservationNumber: 'ABC',
reservationFor: { name: 'Hotel X' },
checkinTime: '2026-06-11T15:00',
checkoutTime: '2026-06-12T11:00',
};
const file = (name = 'a.pdf') => ({ buffer: Buffer.from('x'), originalname: name }) as any;
function make(opts: { kit?: boolean; ai?: boolean; extract?: any; parse?: any }) {
const extractor = { isAvailable: () => opts.kit ?? false, extract: vi.fn(opts.extract ?? (async () => [])) };
const llmParse = {
isAvailable: () => opts.ai ?? false,
parse: vi.fn(opts.parse ?? (async () => ({ kiItems: [], warnings: [] }))),
};
const reservations = { create: vi.fn() };
// budget/addons/realtime/maps ride the confirm() path only — the preview()
// tests never reach them, so stubs beyond the positional slots aren't needed.
// geocodeQuery, not searchNominatim: the import asks the TREK index first
// and falls through to OpenStreetMap inside that method, so the service no
// longer knows which of the two answered.
const maps = { geocodeQuery: vi.fn() };
// Places became a constructor dep with the place DI fold (was a path mock of
// services/placeService); only confirm() reaches it, so a bare create stub does.
const places = { create: vi.fn() };
return {
svc: new BookingImportService(
extractor as any,
llmParse as any,
undefined as never,
undefined as never,
reservations as never,
permissionsStub as never,
undefined as never,
undefined as never,
undefined as never,
maps as never,
places as never,
uowStub as never,
origins as never,
),
extractor,
llmParse,
reservations,
maps,
places,
};
}
beforeEach(() => vi.clearAllMocks());
describe('BookingImportService.preview', () => {
it('no-ai: maps kitinerary items, does not force needs_review, reports aiUsed:false', async () => {
const { svc, llmParse } = make({ kit: true, ai: false, extract: async () => [HOTEL_KI] });
const res = await svc.preview([file()], 'no-ai', 1);
expect(res.items).toHaveLength(1);
expect(res.items[0].needs_review).toBeFalsy();
expect(res.files).toEqual([{ fileName: 'a.pdf', aiAvailable: false, aiUsed: false }]);
expect(llmParse.parse).not.toHaveBeenCalled();
});
it('throws 503 when neither parser is available', async () => {
const { svc } = make({ kit: false, ai: false });
try {
await svc.preview([file()], 'no-ai', 1);
throw new Error('expected throw');
} catch (err) {
expect(err).toBeInstanceOf(HttpException);
expect((err as HttpException).getStatus()).toBe(503);
}
});
it('fallback-on-empty: runs the LLM when kitinerary finds nothing and flags needs_review', async () => {
const { svc, extractor, llmParse } = make({
kit: true,
ai: true,
extract: async () => [],
parse: async () => ({ kiItems: [HOTEL_KI], warnings: [] }),
});
const res = await svc.preview([file()], 'fallback-on-empty', 1);
expect(extractor.extract).toHaveBeenCalled();
expect(llmParse.parse).toHaveBeenCalled();
expect(res.items).toHaveLength(1);
expect(res.items[0].needs_review).toBe(true);
expect(res.files![0]).toEqual({ fileName: 'a.pdf', aiAvailable: true, aiUsed: true });
});
it('fallback-on-empty: skips the LLM when kitinerary already found items', async () => {
const { svc, llmParse } = make({ kit: true, ai: true, extract: async () => [HOTEL_KI] });
const res = await svc.preview([file()], 'fallback-on-empty', 1);
expect(llmParse.parse).not.toHaveBeenCalled();
expect(res.files![0].aiUsed).toBe(false);
});
it('fallback-on-empty: a photo goes straight to the LLM, kitinerary is not asked', async () => {
const { svc, extractor, llmParse } = make({
kit: true,
ai: true,
extract: async () => [HOTEL_KI],
parse: async () => ({ kiItems: [HOTEL_KI], warnings: [] }),
});
const res = await svc.preview([file('ticket.jpg')], 'fallback-on-empty', 1);
expect(extractor.extract).not.toHaveBeenCalled();
expect(llmParse.parse).toHaveBeenCalledWith({ buffer: expect.any(Buffer), originalName: 'ticket.jpg' }, 1);
expect(res.items[0].needs_review).toBe(true);
});
it('force-ai: skips kitinerary entirely and uses the LLM', async () => {
const { svc, extractor, llmParse } = make({
kit: true,
ai: true,
parse: async () => ({ kiItems: [HOTEL_KI], warnings: [] }),
});
const res = await svc.preview([file()], 'force-ai', 1);
expect(extractor.extract).not.toHaveBeenCalled();
expect(llmParse.parse).toHaveBeenCalled();
expect(res.items[0].needs_review).toBe(true);
});
});
/**
* Endpoints that arrive without coordinates (#1969).
*
* kitinerary and the LLM name stations, stops, terminals and rental desks but
* rarely geo-locate them — only airports come with coordinates, from the
* mapper's own table. `reservation_endpoints.lat`/`lng` are NOT NULL, so
* `saveEndpoints` drops anything without them.
*
* The lookup that was meant to fill them in lived in confirm(), which nothing
* calls: both the desktop planner and mobile go preview -> review form ->
* ordinary save. So a named endpoint showed up in the review step's From -> To
* summary and then vanished on save, with nothing logged and nothing shown.
*
* These run against preview(), because that is the path that exists.
*/
describe('BookingImportService.preview endpoint geocoding (#1969)', () => {
const TRAIN_KI = {
'@type': 'TrainReservation',
reservationNumber: 'TR1',
reservationFor: {
trainNumber: 'ICE 597',
departureStation: { name: 'Berlin Hbf' },
arrivalStation: { name: 'München Hbf' },
departureTime: '2026-06-11T08:00:00',
arrivalTime: '2026-06-11T12:00:00',
},
};
// geocodeQuery answers with one coordinate or null, not a provider result
// list. Stubbing the old array shape made 'has coordinates' pass on an
// undefined lat, which is exactly the bug the test is meant to catch.
const hit = (lat: number, lng: number) => ({ lat, lng });
it('gives a named station coordinates, so it survives the save', async () => {
const { svc, maps } = make({ kit: true, extract: async () => [TRAIN_KI] });
maps.geocodeQuery.mockResolvedValue(hit(52.525, 13.369));
const res = await svc.preview([file()], 'no-ai', 1);
const endpoints = (res.items[0] as { endpoints?: { lat: number | null }[] }).endpoints ?? [];
expect(endpoints.length).toBeGreaterThan(0);
for (const ep of endpoints) expect(ep.lat).not.toBeNull();
});
it('asks the same station only once, however often it appears', async () => {
const { svc, maps } = make({
kit: true,
// Two legs out of and back into the same station.
extract: async () => [TRAIN_KI, TRAIN_KI],
});
maps.geocodeQuery.mockResolvedValue(hit(52.525, 13.369));
await svc.preview([file()], 'no-ai', 1);
// Two distinct names across four endpoints. The index is fast, but its
// fallback lane is Nominatim at about one request a second, so repeats
// still have to come from the cache.
expect(maps.geocodeQuery).toHaveBeenCalledTimes(2);
});
/*
* The half that made this so hard to notice: the endpoint disappeared without
* a word. It stays on the item now, so the review form can still show and
* edit it, and the preview says what it could not place.
*/
it('keeps an endpoint it cannot place, and says so', async () => {
const { svc, maps } = make({ kit: true, extract: async () => [TRAIN_KI] });
maps.geocodeQuery.mockResolvedValue(null);
const res = await svc.preview([file()], 'no-ai', 1);
const endpoints = (res.items[0] as { endpoints?: { name: string }[] }).endpoints ?? [];
expect(endpoints.map((e) => e.name)).toContain('Berlin Hbf');
expect(res.warnings.some((w) => w.includes('Berlin Hbf'))).toBe(true);
});
it('survives a geocoder that throws, rather than failing the import', async () => {
const { svc, maps } = make({ kit: true, extract: async () => [TRAIN_KI] });
maps.geocodeQuery.mockRejectedValue(new Error('nominatim down'));
const res = await svc.preview([file()], 'no-ai', 1);
expect(res.items).toHaveLength(1);
});
it('does not look up an endpoint that already knows where it is', async () => {
const withGeo = {
...TRAIN_KI,
reservationFor: {
...TRAIN_KI.reservationFor,
departureStation: { name: 'Berlin Hbf', geo: { latitude: 52.525, longitude: 13.369 } },
arrivalStation: { name: 'München Hbf', geo: { latitude: 48.14, longitude: 11.558 } },
},
};
const { svc, maps } = make({ kit: true, extract: async () => [withGeo] });
await svc.preview([file()], 'no-ai', 1);
expect(maps.geocodeQuery).not.toHaveBeenCalled();
});
});
// #2483: a venue's website from a booking mail is one more outside source, and it
// used to reach the place row exactly as the mail or the review form had it.
describe('BookingImportService.confirm venue website (#2483)', () => {
it('BOOKING-IMPORT-2483-01: a bare host is saved with https, a script link or nothing not at all', async () => {
const reservations = {
createWithCostInTx: vi.fn(() => ({
reservation: { id: 1 },
accommodationCreated: false,
costEvents: [],
stayMirror: {},
})),
announceStayMirror: vi.fn(),
announceCost: vi.fn(),
};
const places = { create: vi.fn((_tripId: string, _input: { website?: string }) => ({ id: 7 })) };
// No dates on a restaurant, so the day repositories are never reached.
const svc = new BookingImportService(
{} as never,
{} as never,
undefined as never,
undefined as never,
reservations as never,
permissionsStub as never,
undefined as never,
{ isAddonEnabled: () => false } as never,
{ broadcast: vi.fn() } as never,
{ geocodeQuery: vi.fn() } as never,
places as never,
uowStub as never,
origins as never,
);
const item = (website?: string) => ({
type: 'restaurant',
title: 'Dîner',
_venue: { name: 'Crêperie', lat: 48.03, lng: -3.49, website },
source: { fileName: 'booking.eml', index: 0 },
});
const res = await svc.confirm(
'5',
[item('www.creperie.example/carte'), item('javascript:alert(1)'), item()],
undefined,
);
expect(res.created).toHaveLength(3);
expect(places.create.mock.calls.map(([, input]) => input.website)).toEqual([
'https://www.creperie.example/carte',
undefined,
undefined,
]);
// The venues' details came from the booking: recorded once, as an import, after the loop.
expect(origins.recordImported).toHaveBeenLastCalledWith([7, 7, 7]);
});
});
// The booking and the cost its extracted price becomes go in as one write, through
// ReservationsService.createWithCostInTx with the venue, and the FX rate frozen before it.
describe('BookingImportService.confirm writes the booking with its cost', () => {
function makeConfirm(
opts: {
budget?: boolean;
failWrite?: boolean;
freeze?: (tripId: string, entry: { currency?: string | null; exchange_rate?: number }) => Promise<void>;
} = {},
) {
const order: string[] = [];
const costEvents = [{ event: 'budget:created', payload: { item: { id: 9 } } }];
const reservations = {
createWithCostInTx: vi.fn(async () => {
if (opts.failWrite) throw new Error('write failed');
order.push('write');
return { reservation: { id: 1 }, accommodationCreated: false, costEvents, stayMirror: {} };
}),
announceStayMirror: vi.fn(async () => {
order.push('announceStayMirror');
}),
announceCost: vi.fn(() => {
order.push('announceCost');
}),
};
const places = {
create: vi.fn(async () => {
order.push('place');
return { id: 7 };
}),
};
const uow = {
transactional: vi.fn(async <T>(fn: () => Promise<T>) => {
order.push('begin');
const out = await fn();
order.push('commit');
return out;
}),
};
const budget = {
freezeForeignRate: vi.fn(async (tripId: string, entry: { currency?: string | null; exchange_rate?: number }) => {
order.push('freeze');
await (
opts.freeze ??
(async (_t: string, e: { exchange_rate?: number }) => {
e.exchange_rate = 0.5;
})
)(tripId, entry);
}),
};
const realtime = {
broadcast: vi.fn((_tripId: string, event: string) => {
order.push(event);
}),
};
const svc = new BookingImportService(
{} as never,
{} as never,
undefined as never,
undefined as never,
reservations as never,
permissionsStub as never,
budget as never,
{ isAddonEnabled: async () => opts.budget ?? true } as never,
realtime as never,
{ geocodeQuery: vi.fn() } as never,
places as never,
uow as never,
origins as never,
);
return { svc, reservations, budget, places, realtime, order, costEvents };
}
const priced = {
type: 'train',
title: 'ICE 123',
metadata: { price: '49.9', priceCurrency: 'CHF' },
source: { fileName: 'ticket.pdf', index: 0 },
};
it('BOOKING-IMPORT-TX-001: freezes the rate first, writes booking and cost in one call, announces the cost after the booking', async () => {
const { svc, reservations, order, costEvents } = makeConfirm();
const res = await svc.confirm('5', [priced as never], 'sock');
expect(res.created).toEqual([{ id: 1 }]);
expect(reservations.createWithCostInTx).toHaveBeenCalledWith(
'5',
expect.objectContaining({ title: 'ICE 123', type: 'train' }),
{
total_price: 49.9,
category: 'transport',
currency: 'CHF',
exchange_rate: 0.5,
},
);
expect(reservations.announceCost).toHaveBeenCalledWith('5', costEvents, 'sock');
expect(order).toEqual([
'freeze',
'begin',
'write',
'commit',
'reservation:created',
'announceStayMirror',
'announceCost',
]);
});
it('BOOKING-IMPORT-2895: a rail ticket with changes is one booking with one cost for its fare', async () => {
const segment = (from: string, to: string, departureTime: string, arrivalTime: string, trainNumber: string) => ({
'@type': 'TrainReservation',
reservationNumber: '297199016704',
totalPrice: 71.67,
priceCurrency: 'EUR',
reservationFor: {
trainNumber,
departureStation: { name: from },
arrivalStation: { name: to },
departureTime,
arrivalTime,
},
});
const { svc: importer, maps } = make({
kit: true,
extract: async () => [
segment('Klagenfurt West', 'Villach Hbf', '2026-07-04T06:12:00', '2026-07-04T06:30:00', 'S1'),
segment('Villach Hbf', 'Heidelberg Hbf', '2026-07-04T06:40:00', '2026-07-04T12:50:00', 'ICE 116'),
segment('Heidelberg Hbf', 'Ludwigshafen (Rhein) Mitte', '2026-07-04T13:05:00', '2026-07-04T13:30:00', 'S1'),
segment('Ludwigshafen (Rhein) Mitte', 'Rheingönheim', '2026-07-04T13:41:00', '2026-07-04T13:55:00', 'STR 7'),
],
});
maps.geocodeQuery.mockResolvedValue({ lat: 49, lng: 8 });
const preview = await importer.preview([file('ticket.pdf')], 'no-ai', 1);
expect(preview.items).toHaveLength(1);
const { svc, reservations } = makeConfirm({ freeze: async () => {} });
await svc.confirm('5', preview.items as never, undefined);
expect(reservations.createWithCostInTx).toHaveBeenCalledTimes(1);
expect(reservations.createWithCostInTx).toHaveBeenCalledWith(
'5',
expect.objectContaining({ type: 'train', confirmation_number: '297199016704' }),
{ total_price: 71.67, category: 'transport', currency: 'EUR' },
);
});
it('BOOKING-IMPORT-TX-002: no cost without the Costs addon or without a price', async () => {
const off = makeConfirm({ budget: false });
await off.svc.confirm('5', [priced as never], undefined);
expect(off.budget.freezeForeignRate).not.toHaveBeenCalled();
expect(off.reservations.createWithCostInTx).toHaveBeenCalledWith('5', expect.anything(), undefined);
const free = makeConfirm();
await free.svc.confirm('5', [{ ...priced, metadata: { price: '0' } } as never], undefined);
expect(free.budget.freezeForeignRate).not.toHaveBeenCalled();
expect(free.reservations.createWithCostInTx).toHaveBeenCalledWith('5', expect.anything(), undefined);
});
const atVenue = {
type: 'restaurant',
title: 'Dîner',
_venue: { name: 'Crêperie', lat: 48.03, lng: -3.49 },
source: { fileName: 'booking.eml', index: 0 },
};
it('BOOKING-IMPORT-TX-004: writes the venue in the booking transaction and announces it after the commit', async () => {
const { svc, places, reservations, order } = makeConfirm({ budget: false });
await svc.confirm('5', [atVenue as never], 'sock');
expect(order).toEqual([
'begin',
'place',
'write',
'commit',
'place:created',
'reservation:created',
'announceStayMirror',
'announceCost',
]);
expect(places.create).toHaveBeenCalledWith(
'5',
expect.objectContaining({ name: 'Crêperie', lat: 48.03, lng: -3.49 }),
);
expect(reservations.createWithCostInTx).toHaveBeenCalledWith(
'5',
expect.objectContaining({ place_id: 7 }),
undefined,
);
});
it('BOOKING-IMPORT-TX-005: a booking that fails takes its venue with it and announces nothing', async () => {
const quiet = vi.spyOn(console, 'error').mockImplementation(() => {});
try {
const { svc, realtime, order } = makeConfirm({ budget: false, failWrite: true });
const res = await svc.confirm('5', [atVenue as never], 'sock');
expect(res.created).toEqual([]);
// The place insert ran inside the transaction the failure rolls back, never on its own.
expect(order).toEqual(['begin', 'place']);
expect(realtime.broadcast).not.toHaveBeenCalled();
} finally {
quiet.mockRestore();
}
});
it('BOOKING-IMPORT-TX-003: a rate that cannot be frozen keeps the booking, without its cost', async () => {
const quiet = vi.spyOn(console, 'error').mockImplementation(() => {});
try {
const { svc, reservations } = makeConfirm({
freeze: async () => {
throw new Error('rates down');
},
});
const res = await svc.confirm('5', [priced as never], undefined);
expect(res.created).toEqual([{ id: 1 }]);
expect(reservations.createWithCostInTx).toHaveBeenCalledWith('5', expect.anything(), undefined);
} finally {
quiet.mockRestore();
}
});
});