2024-02-06 22:58:49 +01:00
|
|
|
|
import tap from 'tap';
|
|
|
|
|
import assert from 'assert';
|
|
|
|
|
import isRoughlyEqual from 'is-roughly-equal';
|
2022-05-07 16:17:37 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
import {createWhen} from './lib/util.js';
|
|
|
|
|
import {createClient} from '../../index.js';
|
|
|
|
|
import {profile as cflProfile} from '../../p/cfl/index.js';
|
2022-05-07 16:17:37 +02:00
|
|
|
|
import {
|
|
|
|
|
createValidateLine,
|
|
|
|
|
createValidateJourneyLeg,
|
|
|
|
|
createValidateMovement,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
} from './lib/validators.js';
|
|
|
|
|
import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js';
|
|
|
|
|
import {testJourneysStationToStation} from './lib/journeys-station-to-station.js';
|
|
|
|
|
import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js';
|
|
|
|
|
import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js';
|
|
|
|
|
import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js';
|
|
|
|
|
import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js';
|
|
|
|
|
import {testDepartures} from './lib/departures.js';
|
|
|
|
|
import {testArrivals} from './lib/arrivals.js';
|
|
|
|
|
|
|
|
|
|
const T_MOCK = 1671260400 * 1000; // 2022-12-17T08:00:00+01:00
|
|
|
|
|
const when = createWhen(cflProfile.timezone, cflProfile.locale, T_MOCK);
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
|
|
|
|
const cfg = {
|
|
|
|
|
when,
|
2022-05-07 16:17:37 +02:00
|
|
|
|
products: cflProfile.products,
|
2018-06-10 20:44:12 +02:00
|
|
|
|
minLatitude: 47.24,
|
|
|
|
|
maxLatitude: 52.9,
|
|
|
|
|
minLongitude: -0.63,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
maxLongitude: 14.07,
|
|
|
|
|
};
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
const _validateLine = createValidateLine(cfg);
|
2018-06-11 20:50:35 +02:00
|
|
|
|
const validateLine = (validate, l, name) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
|
if (!l.direction) {
|
|
|
|
|
l = Object.assign({}, l, {direction: 'foo'});
|
|
|
|
|
}
|
|
|
|
|
_validateLine(validate, l, name);
|
|
|
|
|
};
|
2018-06-11 20:50:35 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
const _validateJourneyLeg = createValidateJourneyLeg(cfg);
|
2018-06-11 20:50:35 +02:00
|
|
|
|
const validateJourneyLeg = (validate, l, name) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
|
if (!l.direction) {
|
|
|
|
|
l = Object.assign({}, l, {direction: 'foo'});
|
|
|
|
|
}
|
|
|
|
|
_validateJourneyLeg(validate, l, name);
|
|
|
|
|
};
|
2018-06-11 20:50:35 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
const _validateMovement = createValidateMovement(cfg);
|
2018-06-10 20:44:12 +02:00
|
|
|
|
const validateMovement = (val, m, name = 'movement') => {
|
|
|
|
|
// todo: fix this upstream
|
2024-02-06 22:58:49 +01:00
|
|
|
|
const withFakeLocation = Object.assign({}, m);
|
2018-06-10 20:44:12 +02:00
|
|
|
|
withFakeLocation.location = Object.assign({}, m.location, {
|
|
|
|
|
latitude: 50,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
longitude: 12,
|
|
|
|
|
});
|
|
|
|
|
_validateMovement(val, withFakeLocation, name);
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
assert.ok(m.location.latitude <= 55, name + '.location.latitude is too small');
|
|
|
|
|
assert.ok(m.location.latitude >= 45, name + '.location.latitude is too large');
|
|
|
|
|
assert.ok(m.location.longitude >= 1, name + '.location.longitude is too small');
|
|
|
|
|
assert.ok(m.location.longitude <= 11, name + '.location.longitude is too small');
|
|
|
|
|
};
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
|
|
|
|
const validate = createValidate(cfg, {
|
2018-06-11 20:50:35 +02:00
|
|
|
|
line: validateLine,
|
|
|
|
|
journeyLeg: validateJourneyLeg,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
movement: validateMovement,
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
const client = createClient(cflProfile, 'public-transport/hafas-client:test');
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
const ettelbruck = '9258199';
|
|
|
|
|
const mersch = '9864348';
|
|
|
|
|
const luxembourgGareCentrale = '9217081';
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2021-05-20 16:42:43 +01:00
|
|
|
|
tap.test('journeys – Ettelbruck to Luxembourg', async (t) => {
|
2021-08-24 00:43:20 +02:00
|
|
|
|
const res = await client.journeys(ettelbruck, luxembourgGareCentrale, {
|
2018-06-10 20:44:12 +02:00
|
|
|
|
results: 4,
|
|
|
|
|
departure: when,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
stopovers: true,
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
|
|
|
|
await testJourneysStationToStation({
|
|
|
|
|
test: t,
|
|
|
|
|
res,
|
|
|
|
|
validate,
|
|
|
|
|
fromId: ettelbruck,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
toId: luxembourgGareCentrale,
|
|
|
|
|
});
|
|
|
|
|
t.end();
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
|
|
|
|
// todo: journeys, only one product
|
|
|
|
|
|
2021-12-29 18:53:50 +01:00
|
|
|
|
tap.test('journeys – fails with no product', async (t) => {
|
|
|
|
|
await journeysFailsWithNoProduct({
|
2018-06-10 20:44:12 +02:00
|
|
|
|
test: t,
|
|
|
|
|
fetchJourneys: client.journeys,
|
|
|
|
|
fromId: ettelbruck,
|
2021-08-24 00:43:20 +02:00
|
|
|
|
toId: luxembourgGareCentrale,
|
2018-06-10 20:44:12 +02:00
|
|
|
|
when,
|
2022-05-07 16:17:37 +02:00
|
|
|
|
products: cflProfile.products,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
});
|
|
|
|
|
t.end();
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2021-05-20 16:42:43 +01:00
|
|
|
|
tap.test('Luxembourg to 9071 Ettelbruck, Rue des Romains 4', async (t) => {
|
2018-06-10 20:44:12 +02:00
|
|
|
|
const rueDeRomain = {
|
|
|
|
|
type: 'location',
|
|
|
|
|
address: '9071 Ettelbruck, Rue des Romains 4',
|
|
|
|
|
latitude: 49.847469,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
longitude: 6.097608,
|
|
|
|
|
};
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2021-08-24 00:43:20 +02:00
|
|
|
|
const res = await client.journeys(luxembourgGareCentrale, rueDeRomain, {
|
2018-06-10 20:44:12 +02:00
|
|
|
|
results: 3,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
departure: when,
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
|
|
|
|
await testJourneysStationToAddress({
|
|
|
|
|
test: t,
|
|
|
|
|
res,
|
|
|
|
|
validate,
|
2021-08-24 00:43:20 +02:00
|
|
|
|
fromId: luxembourgGareCentrale,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
to: rueDeRomain,
|
|
|
|
|
});
|
|
|
|
|
t.end();
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2022-11-09 21:17:32 +01:00
|
|
|
|
tap.test('Luxembourg to Centre Hospitalier du Nord', async (t) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
|
const luxembourg = '9217081';
|
2018-06-10 20:44:12 +02:00
|
|
|
|
const centreHospitalier = {
|
|
|
|
|
type: 'location',
|
2022-11-09 21:17:32 +01:00
|
|
|
|
id: '990027653',
|
2018-06-10 20:44:12 +02:00
|
|
|
|
poi: true,
|
2022-11-09 21:17:32 +01:00
|
|
|
|
name: 'Centre Hospitalier du Nord (CHDN), Ettelbruck',
|
|
|
|
|
latitude: 49.853168,
|
|
|
|
|
longitude: 6.096268,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
};
|
2021-08-24 00:43:20 +02:00
|
|
|
|
const res = await client.journeys(luxembourgGareCentrale, centreHospitalier, {
|
2018-06-10 20:44:12 +02:00
|
|
|
|
results: 3,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
departure: when,
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
|
|
|
|
await testJourneysStationToPoi({
|
|
|
|
|
test: t,
|
|
|
|
|
res,
|
|
|
|
|
validate,
|
2022-11-09 21:17:32 +01:00
|
|
|
|
fromId: luxembourg,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
to: centreHospitalier,
|
|
|
|
|
});
|
|
|
|
|
t.end();
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
|
|
|
|
// todo: journeys: via works – with detour
|
|
|
|
|
// todo: without detour
|
|
|
|
|
|
2021-05-20 16:42:43 +01:00
|
|
|
|
tap.test('earlier/later journeys', async (t) => {
|
2018-06-10 20:44:12 +02:00
|
|
|
|
await testEarlierLaterJourneys({
|
|
|
|
|
test: t,
|
|
|
|
|
fetchJourneys: client.journeys,
|
|
|
|
|
validate,
|
2021-08-24 00:43:20 +02:00
|
|
|
|
fromId: luxembourgGareCentrale,
|
2020-05-21 22:21:07 +02:00
|
|
|
|
toId: ettelbruck,
|
|
|
|
|
when,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
t.end();
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2021-05-20 16:42:43 +01:00
|
|
|
|
tap.test('trip', async (t) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
|
const {journeys} = await client.journeys(luxembourgGareCentrale, ettelbruck, {
|
|
|
|
|
results: 1, departure: when,
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
const p = journeys[0].legs.find(l => !l.walking);
|
|
|
|
|
t.ok(p.tripId, 'precondition failed');
|
|
|
|
|
t.ok(p.line.name, 'precondition failed');
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
const tripRes = await client.trip(p.tripId, {when});
|
2021-12-29 21:23:28 +01:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
validate(t, tripRes, 'tripResult', 'res');
|
|
|
|
|
t.end();
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2021-05-20 16:42:43 +01:00
|
|
|
|
tap.test('departures at Ettelbruck.', async (t) => {
|
2021-12-29 21:11:07 +01:00
|
|
|
|
const res = await client.departures(ettelbruck, {
|
2024-02-06 22:58:49 +01:00
|
|
|
|
duration: 20, when,
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
|
|
|
|
await testDepartures({
|
|
|
|
|
test: t,
|
2021-12-29 21:11:07 +01:00
|
|
|
|
res,
|
2018-06-10 20:44:12 +02:00
|
|
|
|
validate,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
id: ettelbruck,
|
|
|
|
|
});
|
|
|
|
|
t.end();
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2021-05-20 16:42:43 +01:00
|
|
|
|
tap.test('arrivals at Ettelbruck.', async (t) => {
|
2021-12-29 21:11:07 +01:00
|
|
|
|
const res = await client.arrivals(ettelbruck, {
|
2024-02-06 22:58:49 +01:00
|
|
|
|
duration: 20, when,
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
|
|
|
|
await testArrivals({
|
|
|
|
|
test: t,
|
2021-12-29 21:11:07 +01:00
|
|
|
|
res,
|
2018-06-10 20:44:12 +02:00
|
|
|
|
validate,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
id: ettelbruck,
|
|
|
|
|
});
|
|
|
|
|
t.end();
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2021-05-20 16:42:43 +01:00
|
|
|
|
tap.test('departures with station object', async (t) => {
|
2021-12-29 21:11:07 +01:00
|
|
|
|
const res = await client.departures({
|
2018-06-10 20:44:12 +02:00
|
|
|
|
type: 'station',
|
|
|
|
|
id: ettelbruck,
|
|
|
|
|
name: 'Ettelbruck',
|
|
|
|
|
location: {
|
|
|
|
|
type: 'location',
|
|
|
|
|
latitude: 49.847298,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
longitude: 6.106157,
|
|
|
|
|
},
|
|
|
|
|
}, {when});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
validate(t, res, 'departuresResponse', 'res');
|
|
|
|
|
t.end();
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
|
|
|
|
// todo: nearby
|
|
|
|
|
|
2021-05-20 16:42:43 +01:00
|
|
|
|
tap.test('locations named Mersch', async (t) => {
|
2019-05-20 19:31:47 +02:00
|
|
|
|
const locations = await client.locations('Mersch', {
|
2024-02-06 22:58:49 +01:00
|
|
|
|
results: 20,
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
validate(t, locations, 'locations', 'locations');
|
|
|
|
|
t.ok(locations.length <= 20);
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
t.ok(locations.find(s => s.type === 'stop' || s.type === 'station'));
|
|
|
|
|
t.ok(locations.find(s => s.poi));
|
2018-06-10 20:44:12 +02:00
|
|
|
|
t.ok(locations.some((loc) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
|
if (loc.station && loc.station.id === mersch) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return loc.id === mersch;
|
|
|
|
|
}));
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
t.end();
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2021-05-20 16:42:43 +01:00
|
|
|
|
tap.test('stop Mersch', async (t) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
|
const s = await client.stop(mersch);
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
validate(t, s, ['stop', 'station'], 'stop');
|
|
|
|
|
t.equal(s.id, mersch);
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
t.end();
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2021-05-20 16:42:43 +01:00
|
|
|
|
tap.test('radar', async (t) => {
|
2021-12-29 21:24:07 +01:00
|
|
|
|
const res = await client.radar({
|
2018-06-10 20:44:12 +02:00
|
|
|
|
north: 49.9,
|
|
|
|
|
west: 6.05,
|
|
|
|
|
south: 49.8,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
east: 6.15,
|
2018-06-10 20:44:12 +02:00
|
|
|
|
}, {
|
2024-02-06 22:58:49 +01:00
|
|
|
|
duration: 5 * 60, when, results: 10,
|
|
|
|
|
});
|
2018-06-10 20:44:12 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
validate(t, res, 'radarResult', 'res');
|
|
|
|
|
t.end();
|
|
|
|
|
});
|