db-vendo-client/test/e2e/oebb.js

407 lines
10 KiB
JavaScript
Raw Normal View History

import tap from 'tap';
import isRoughlyEqual from 'is-roughly-equal';
import validateLine from 'validate-fptf/line.js';
2022-05-07 16:17:37 +02:00
import {createWhen} from './lib/util.js';
import {createClient} from '../../index.js';
import {profile as oebbProfile} from '../../p/oebb/index.js';
2022-05-07 16:17:37 +02:00
import {
createValidateStation,
createValidateStop,
} 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 {testRefreshJourney} from './lib/refresh-journey.js';
import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js';
import {testJourneysWithDetour} from './lib/journeys-with-detour.js';
import {testDepartures} from './lib/departures.js';
import {testDeparturesInDirection} from './lib/departures-in-direction.js';
const T_MOCK = 1671260400 * 1000; // 2022-12-17T08:00:00+01:00
const when = createWhen(oebbProfile.timezone, oebbProfile.locale, T_MOCK);
2017-12-29 10:01:02 +01:00
2018-04-20 11:04:54 +02:00
const cfg = {
when,
stationCoordsOptional: false,
2022-05-07 16:17:37 +02:00
products: oebbProfile.products,
2019-02-15 14:53:01 +01:00
minLatitude: 45.992803,
maxLatitude: 49.453517,
minLongitude: 8.787557,
maxLongitude: 17.491275,
};
2017-12-29 10:01:02 +01:00
2018-04-20 11:04:54 +02:00
// todo validateDirection: search list of stations for direction
2017-12-29 10:01:02 +01:00
const validate = createValidate(cfg);
2017-12-29 10:01:02 +01:00
const _validateStop = createValidateStop(cfg);
2022-05-07 16:17:37 +02:00
2017-12-29 10:01:02 +01:00
const assertValidPrice = (t, p) => {
t.ok(p);
2017-12-29 10:01:02 +01:00
if (p.amount !== null) {
t.equal(typeof p.amount, 'number');
t.ok(p.amount > 0);
2017-12-29 10:01:02 +01:00
}
if (p.hint !== null) {
t.equal(typeof p.hint, 'string');
t.ok(p.hint);
2017-12-29 10:01:02 +01:00
}
};
2017-12-29 10:01:02 +01:00
const client = createClient(oebbProfile, 'public-transport/hafas-client:test');
2017-12-29 10:01:02 +01:00
const salzburgHbf = '8100002';
const wienFickeystr = '911014';
const wien = '1190100';
const wienWestbahnhof = '1291501';
const klagenfurtHbf = '8100085';
const muenchenHbf = '8000261';
const wienRenngasse = '1390186';
const wienKarlsplatz = '1390461';
const wienPilgramgasse = '1390562';
2022-04-28 22:11:34 +02:00
tap.test('journeys Salzburg Hbf to Wien Westbahnhof', async (t) => {
const res = await client.journeys(salzburgHbf, wienFickeystr, {
results: 4,
2018-05-31 13:42:54 +02:00
departure: when,
stopovers: true,
});
2018-11-21 19:07:37 +01:00
await testJourneysStationToStation({
test: t,
res,
validate,
fromId: salzburgHbf,
toId: wienFickeystr,
});
2017-12-29 10:01:02 +01:00
for (let i = 0; i < res.journeys.length; i++) {
const j = res.journeys[i];
if (j.price) {
assertValidPrice(t, j.price, `res.journeys[${i}].price`);
}
2017-12-29 10:01:02 +01:00
}
t.end();
});
2017-12-29 10:01:02 +01:00
2018-04-20 11:04:54 +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({
test: t,
fetchJourneys: client.journeys,
fromId: salzburgHbf,
toId: wienFickeystr,
when,
2022-05-07 16:17:37 +02:00
products: oebbProfile.products,
});
t.end();
});
2018-04-20 11:04:54 +02:00
2021-05-20 16:42:43 +01:00
tap.test('Salzburg Hbf to 1220 Wien, Fischerstrand 7', async (t) => {
2017-12-29 10:01:02 +01:00
const wagramerStr = {
type: 'location',
2022-04-28 22:11:34 +02:00
address: '1220 Wien, Fischerstrand 7',
latitude: 48.236216,
longitude: 16.425863,
};
const res = await client.journeys(salzburgHbf, wagramerStr, {
2018-05-31 13:42:54 +02:00
results: 3,
departure: when,
});
2017-12-29 10:01:02 +01:00
2018-11-21 19:07:37 +01:00
await testJourneysStationToAddress({
test: t,
res,
validate,
fromId: salzburgHbf,
to: wagramerStr,
});
t.end();
});
2017-12-29 10:01:02 +01:00
2021-05-20 16:42:43 +01:00
tap.test('Salzburg Hbf to Uni Wien', async (t) => {
const uniWien = {
2017-12-29 10:01:02 +01:00
type: 'location',
id: '970085780',
2019-02-07 17:47:50 +01:00
poi: true,
name: 'Wien, Donaupark (Parkplatz)',
latitude: 48.240674, longitude: 16.4097,
};
const res = await client.journeys(salzburgHbf, uniWien, {
results: 3, departure: when,
});
2017-12-29 10:01:02 +01:00
2018-11-21 19:07:37 +01:00
await testJourneysStationToPoi({
test: t,
res,
validate,
fromId: salzburgHbf,
to: uniWien,
});
t.end();
});
2017-12-29 10:01:02 +01:00
2021-05-20 16:42:43 +01:00
tap.test('journeys: via works with detour', async (t) => {
2018-03-16 14:22:00 +01:00
// Going from Stephansplatz to Schottenring via Donauinsel without detour
2018-03-16 14:34:37 +01:00
// is currently impossible. We check if the routing engine computes a detour.
const stephansplatz = '1390167';
const schottenring = '1390163';
const donauinsel = '1392277';
const donauinselPassed = '922001';
const res = await client.journeys(stephansplatz, schottenring, {
2018-03-16 14:22:00 +01:00
via: donauinsel,
2018-01-24 23:32:54 +01:00
results: 1,
2018-05-28 20:35:01 +02:00
departure: when,
stopovers: true,
});
2018-11-21 19:07:37 +01:00
await testJourneysWithDetour({
2018-05-13 00:34:26 +02:00
test: t,
res,
2018-05-13 00:34:26 +02:00
validate,
detourIds: [donauinsel, donauinselPassed],
});
t.end();
});
2018-03-16 14:22:00 +01:00
2021-05-20 16:42:43 +01:00
tap.test('journeys: via works without detour', async (t) => {
2018-04-20 11:04:54 +02:00
// When going from Karlsplatz to Praterstern via Museumsquartier, there is
// *no need* to change trains / no need for a "detour".
const karlsplatz = '1390461';
const praterstern = '1290201';
const stephansplatz = '1390167';
const stephansplatzPassed = '901006';
2018-03-16 14:22:00 +01:00
const res = await client.journeys(karlsplatz, praterstern, {
2021-08-24 00:43:20 +02:00
via: stephansplatz,
2018-03-16 14:22:00 +01:00
results: 1,
2018-05-28 20:35:01 +02:00
departure: when,
stopovers: true,
});
2018-03-16 14:22:00 +01:00
validate(t, res, 'journeysResult', 'res');
2018-03-16 14:22:00 +01:00
const l1 = res.journeys[0].legs.some((leg) => {
2018-04-20 11:04:54 +02:00
return (
leg.destination.id === stephansplatz
|| leg.destination.id === stephansplatzPassed
);
});
t.notOk(l1, 'transfer at Museumsquartier');
2018-04-20 11:04:54 +02:00
const l2 = res.journeys[0].legs.some((leg) => {
return leg.stopovers && leg.stopovers.some((stopover) => {
return stopover.stop.id === stephansplatzPassed;
});
});
t.ok(l2, 'Museumsquartier is not being passed');
t.end();
});
2021-05-20 16:42:43 +01:00
tap.test('earlier/later journeys, Salzburg Hbf -> Wien Westbahnhof', async (t) => {
2018-11-21 19:07:37 +01:00
await testEarlierLaterJourneys({
test: t,
fetchJourneys: client.journeys,
validate,
fromId: salzburgHbf,
toId: wienWestbahnhof,
when,
});
t.end();
});
2021-05-20 16:42:43 +01:00
tap.test('refreshJourney', async (t) => {
2018-11-21 19:07:37 +01:00
await testRefreshJourney({
test: t,
fetchJourneys: client.journeys,
refreshJourney: client.refreshJourney,
validate,
fromId: salzburgHbf,
toId: wienWestbahnhof,
when,
});
t.end();
});
2021-05-20 16:42:43 +01:00
tap.test('trip details', async (t) => {
const res = await client.journeys(wienWestbahnhof, muenchenHbf, {
results: 1, departure: when,
});
2017-12-29 10:01:02 +01:00
const p = res.journeys[0].legs.find(l => !l.walking);
t.ok(p.tripId, 'precondition failed');
t.ok(p.line.name, 'precondition failed');
2017-12-29 10:01:02 +01:00
const tripRes = await client.trip(p.tripId, {when});
validate(t, tripRes, 'tripResult', 'res');
t.end();
});
2017-12-29 10:01:02 +01:00
2021-05-20 16:42:43 +01:00
tap.test('departures at Wien Leibenfrostgasse', async (t) => {
const wienLeibenfrostgasse = '1390469';
const ids = [
wienLeibenfrostgasse, // station
'904029', // stop "Wien Leibenfrostgasse (Phorusgasse)s"
'904030', // stop "Wien Leibenfrostgasse (Ziegelofengasse)"
];
const res = await client.departures(wienLeibenfrostgasse, {
duration: 15, when,
});
2017-12-29 10:01:02 +01:00
await testDepartures({
test: t,
res,
validate,
ids,
});
t.end();
});
2017-12-29 10:01:02 +01:00
2021-05-20 16:42:43 +01:00
tap.test('departures with station object', async (t) => {
const res = await client.departures({
2018-04-20 11:04:54 +02:00
type: 'station',
id: salzburgHbf,
name: 'Salzburg Hbf',
location: {
type: 'location',
latitude: 1.23,
longitude: 2.34,
},
}, {when});
2018-04-20 11:04:54 +02:00
validate(t, res, 'departuresResponse', 'res');
t.end();
});
2018-04-20 11:04:54 +02:00
2021-05-20 16:42:43 +01:00
tap.test('departures at Karlsplatz in direction of Pilgramgasse', async (t) => {
const subStops = (await client.stop(wienPilgramgasse, {
subStops: true, entrances: false,
})).stops || [];
2018-11-21 19:07:37 +01:00
await testDeparturesInDirection({
test: t,
fetchDepartures: client.departures,
fetchTrip: client.trip,
id: wienKarlsplatz,
directionIds: [wienPilgramgasse, ...subStops.map(s => s.id)],
when,
validate,
});
t.end();
});
// todo: arrivals
tap.test('nearby Salzburg Hbf', async (t) => {
2018-11-21 19:07:37 +01:00
const nearby = await client.nearby({
2018-01-07 19:01:22 +01:00
type: 'location',
longitude: 13.045605,
latitude: 47.812852,
2018-04-20 11:04:54 +02:00
}, {
results: 5, distance: 400,
});
2017-12-29 10:01:02 +01:00
validate(t, nearby, 'locations', 'nearby');
t.equal(nearby.length, 5);
2017-12-29 10:01:02 +01:00
const s = nearby[0];
t.equal(s.id, salzburgHbf, 'id should be ' + salzburgHbf);
t.equal(s.name, 'Salzburg Hbf');
t.ok(isRoughlyEqual(0.0005, s.location.latitude, 47.812851));
t.ok(isRoughlyEqual(0.0005, s.location.longitude, 13.045604));
t.ok(s.distance >= 0);
t.ok(s.distance <= 100);
2017-12-29 10:01:02 +01:00
t.end();
});
2017-12-29 10:01:02 +01:00
2021-05-20 16:42:43 +01:00
tap.test('locations named Salzburg', async (t) => {
const salzburgVolksgarten = '591161';
2019-02-07 18:06:15 +01:00
const locations = await client.locations('Salzburg volksgarten', {
results: 20,
});
2017-12-29 10:01:02 +01:00
validate(t, locations, 'locations', 'locations');
t.ok(locations.length <= 20);
2017-12-29 10:01:02 +01:00
t.ok(locations.find(s => s.type === 'stop' || s.type === 'station'));
t.ok(locations.find(s => s.poi)); // POIs
2018-07-11 13:25:22 +02:00
t.ok(locations.some((s) => {
return s.station && s.station.id === salzburgVolksgarten || s.id === salzburgVolksgarten;
}));
2017-12-29 10:01:02 +01:00
t.end();
});
2017-12-29 10:01:02 +01:00
2021-05-20 16:42:43 +01:00
tap.test('stop', async (t) => {
const loc = await client.stop(wienRenngasse);
2018-01-26 17:08:07 +01:00
// todo: find a way to always get products from the API
2018-04-20 11:04:54 +02:00
// todo: cfg.stationProductsOptional option
const {products} = oebbProfile;
const allProducts = products.reduce((acc, p) => (acc[p.id] = true, acc), {});
const validateStation = createValidateStation(cfg);
2018-04-20 11:04:54 +02:00
const validate = createValidate(cfg, {
2018-07-11 13:25:22 +02:00
stop: (validate, s, name) => {
const withFakeProducts = Object.assign({products: allProducts}, s);
_validateStop(validate, withFakeProducts, name);
2018-07-11 13:25:22 +02:00
},
2018-04-20 11:04:54 +02:00
station: (validate, s, name) => {
const withFakeProducts = Object.assign({products: allProducts}, s);
validateStation(validate, withFakeProducts, name);
},
});
validate(t, loc, ['stop', 'station'], 'stop');
2018-04-20 11:04:54 +02:00
t.equal(loc.id, wienRenngasse);
2018-01-26 17:08:07 +01:00
t.end();
});
2018-01-26 17:08:07 +01:00
2021-05-20 16:42:43 +01:00
tap.test('radar Salzburg', async (t) => {
let res = await client.radar({
north: 47.827203,
west: 13.001261,
south: 47.773278,
east: 13.07562,
}, {
duration: 5 * 60, when,
});
2017-12-29 10:01:02 +01:00
2018-04-20 11:04:54 +02:00
// todo: find a way to always get frames from the API
res.movements = res.movements.filter(m => m.frames && m.frames.length > 0);
2018-04-20 11:04:54 +02:00
// todo: find a way to always get products from the API
// todo: cfg.stationProductsOptional option
const {products} = oebbProfile;
const allProducts = products.reduce((acc, p) => (acc[p.id] = true, acc), {});
const validateStation = createValidateStation(cfg);
2018-04-20 11:04:54 +02:00
const validate = createValidate(cfg, {
station: (validate, s, name) => {
const withFakeProducts = Object.assign({products: allProducts}, s);
validateStation(validate, withFakeProducts, name);
2018-04-20 11:04:54 +02:00
},
line: (val, line, name = 'line') => {
validateLine(val, {
...line,
// fptf demands a mode
mode: line.mode === null
? 'bus'
: line.mode,
}, name);
},
});
validate(t, res, 'radarResult', 'res');
2017-12-29 10:01:02 +01:00
t.end();
});