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

441 lines
10 KiB
JavaScript
Raw Normal View History

2018-07-28 13:43:58 +02:00
'use strict'
// todo: DRY with vbb tests
2021-05-20 16:42:43 +01:00
const tap = require('tap')
2018-07-28 13:43:58 +02:00
const isRoughlyEqual = require('is-roughly-equal')
const {DateTime} = require('luxon')
const flatMap = require('lodash/flatMap')
2018-07-28 13:43:58 +02:00
2019-08-30 18:31:39 +02:00
const createClient = require('../..')
const bvgProfile = require('../../p/bvg')
const products = require('../../p/bvg/products')
2018-07-28 13:43:58 +02:00
const createValidate = require('./lib/validate-fptf-with')
const {
cfg,
validateStation,
validateLine,
validateJourneyLeg,
validateDeparture,
validateMovement
} = require('./lib/vbb-bvg-validators')
const testJourneysStationToStation = require('./lib/journeys-station-to-station')
const testJourneysStationToAddress = require('./lib/journeys-station-to-address')
const testJourneysStationToPoi = require('./lib/journeys-station-to-poi')
const testJourneysWalkingSpeed = require('./lib/journeys-walking-speed')
2018-07-28 13:43:58 +02:00
const testEarlierLaterJourneys = require('./lib/earlier-later-journeys')
const testLegCycleAlternatives = require('./lib/leg-cycle-alternatives')
2018-07-28 13:43:58 +02:00
const testRefreshJourney = require('./lib/refresh-journey')
const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product')
const testDepartures = require('./lib/departures')
const testDeparturesInDirection = require('./lib/departures-in-direction')
const testArrivals = require('./lib/arrivals')
const testJourneysWithDetour = require('./lib/journeys-with-detour')
const testReachableFrom = require('./lib/reachable-from')
2018-07-28 13:43:58 +02:00
const when = cfg.when
const validate = createValidate(cfg, {
station: validateStation,
line: validateLine,
journeyLeg: validateJourneyLeg,
departure: validateDeparture,
movement: validateMovement
})
const client = createClient(bvgProfile, 'public-transport/hafas-client:test')
const amrumerStr = '900000009101'
const spichernstr = '900000042101'
const bismarckstr = '900000024201'
const westhafen = '900000001201'
const wedding = '900000009104'
const württembergallee = '900000026153'
const tiergarten = '900000003103'
const jannowitzbrücke = '900000100004'
const hour = 60 * 60 * 1000
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('journeys  Spichernstr. to Bismarckstr.', async (t) => {
const res = await client.journeys(spichernstr, bismarckstr, {
results: 4,
2018-07-28 13:43:58 +02:00
departure: when,
stopovers: true
})
2018-11-21 19:07:37 +01:00
await testJourneysStationToStation({
2018-07-28 13:43:58 +02:00
test: t,
res,
2018-07-28 13:43:58 +02:00
validate,
fromId: spichernstr,
toId: bismarckstr
})
// todo: find a journey where there ticket info is always available
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('journeys  only subway', async (t) => {
const res = await client.journeys(spichernstr, bismarckstr, {
2018-07-28 13:43:58 +02:00
results: 20,
departure: when,
products: {
suburban: false,
subway: true,
tram: false,
bus: false,
ferry: false,
express: false,
regional: false
}
})
validate(t, res, 'journeysResult', 'res')
t.ok(res.journeys.length > 1)
for (let i = 0; i < res.journeys.length; i++) {
const journey = res.journeys[i]
2018-07-28 13:43:58 +02:00
for (let j = 0; j < journey.legs.length; j++) {
const leg = journey.legs[j]
const name = `res.journeys[${i}].legs[${j}].line`
2018-07-28 13:43:58 +02:00
if (leg.line) {
t.equal(leg.line.mode, 'train', name + '.mode is invalid')
t.equal(leg.line.product, 'subway', name + '.product is invalid')
}
t.ok(journey.legs.some(l => l.line), name + '.legs has no subway leg')
}
}
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('journeys  fails with no product', (t) => {
2018-07-28 13:43:58 +02:00
journeysFailsWithNoProduct({
test: t,
fetchJourneys: client.journeys,
fromId: spichernstr,
toId: bismarckstr,
when,
products
})
t.end()
})
// BerlKönig for public use is suspended during COVID-19.
2021-05-20 16:42:43 +01:00
tap.skip('journeys  BerlKönig', async (t) => {
const when = DateTime.fromMillis(Date.now(), {
zone: 'Europe/Berlin',
locale: 'de-De',
}).startOf('day').plus({days: 1, hours: 18}).toISO()
const {journeys} = await client.journeys({
type: 'location',
address: '12101 Berlin-Tempelhof, Peter-Str.r-Weg 1',
latitude: 52.476283,
longitude: 13.384947
}, {
type: 'location',
id: '900981505',
poi: true,
name: 'Berlin, Tempelhofer Park Eingang Oderstr.',
latitude: 52.476688,
longitude: 13.41872
}, {
berlkoenig: true,
departure: when
})
const withBerlkoenig = flatMap(journeys, j => j.legs)
.find(l => l.line && l.line.product === 'berlkoenig')
t.ok(withBerlkoenig, 'journey with BerlKönig not found')
t.ok(withBerlkoenig.line)
t.equal(withBerlkoenig.line.public, true)
t.equal(withBerlkoenig.line.mode, 'taxi')
t.equal(withBerlkoenig.line.product, 'berlkoenig')
t.end()
})
// todo: opt.walkingSpeed doesn't seem to work right now
2021-05-20 16:42:43 +01:00
tap.skip('journeys: walkingSpeed', async (t) => {
const havelchaussee = {
type: 'location',
address: 'Havelchaussee',
latitude: 52.443576,
longitude: 13.198973
}
const wannsee = '900000053301'
await testJourneysWalkingSpeed({
test: t,
journeys: client.journeys,
validate,
from: havelchaussee,
to: wannsee,
products: {bus: false},
minTimeDifference: 5 * 60 * 1000
})
})
2021-05-20 16:42:43 +01:00
tap.test('earlier/later journeys', async (t) => {
2018-11-21 19:07:37 +01:00
await testEarlierLaterJourneys({
2018-07-28 13:43:58 +02:00
test: t,
fetchJourneys: client.journeys,
validate,
fromId: spichernstr,
toId: bismarckstr,
when
2018-07-28 13:43:58 +02:00
})
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.skip('journeys  leg cycle & alternatives', async (t) => {
2018-11-21 19:07:37 +01:00
await testLegCycleAlternatives({
test: t,
fetchJourneys: client.journeys,
fromId: tiergarten,
toId: jannowitzbrücke
})
t.end()
2018-11-21 19:07:37 +01:00
})
2021-05-20 16:42:43 +01:00
tap.test('refreshJourney', async (t) => {
2018-11-21 19:07:37 +01:00
await testRefreshJourney({
2018-07-28 13:43:58 +02:00
test: t,
fetchJourneys: client.journeys,
refreshJourney: client.refreshJourney,
validate,
fromId: spichernstr,
toId: bismarckstr,
when
})
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('trip details', async (t) => {
const res = await client.journeys(spichernstr, amrumerStr, {
2018-07-28 13:43:58 +02:00
results: 1, departure: when
})
const p = res.journeys[0].legs.find(l => !l.walking)
t.ok(p.tripId, 'precondition failed')
2018-07-28 13:43:58 +02:00
t.ok(p.line.name, 'precondition failed')
const trip = await client.trip(p.tripId, p.line.name, {when})
2018-07-28 13:43:58 +02:00
validate(t, trip, 'trip', 'trip')
2018-07-28 13:43:58 +02:00
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('journeys  station to address', async (t) => {
2018-07-28 13:43:58 +02:00
const torfstr = {
type: 'location',
address: '13353 Berlin-Wedding, Torfstr. 17',
latitude: 52.541797,
longitude: 13.350042
}
const res = await client.journeys(spichernstr, torfstr, {
2018-07-28 13:43:58 +02:00
results: 3,
departure: when
})
2018-11-21 19:07:37 +01:00
await testJourneysStationToAddress({
2018-07-28 13:43:58 +02:00
test: t,
res,
2018-07-28 13:43:58 +02:00
validate,
fromId: spichernstr,
to: torfstr
})
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('journeys  station to POI', async (t) => {
2018-07-28 13:43:58 +02:00
const atze = {
type: 'location',
id: '900980720',
2019-02-07 17:47:50 +01:00
poi: true,
2018-07-28 13:43:58 +02:00
name: 'Berlin, Atze Musiktheater für Kinder',
latitude: 52.543333,
longitude: 13.351686
}
const res = await client.journeys(spichernstr, atze, {
2018-07-28 13:43:58 +02:00
results: 3,
departure: when
})
2018-11-21 19:07:37 +01:00
await testJourneysStationToPoi({
2018-07-28 13:43:58 +02:00
test: t,
res,
2018-07-28 13:43:58 +02:00
validate,
fromId: spichernstr,
to: atze
})
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('journeys: via works with detour', async (t) => {
2018-07-28 13:43:58 +02:00
// Going from Westhafen to Wedding via Württembergalle without detour
// is currently impossible. We check if the routing engine computes a detour.
const res = await client.journeys(westhafen, wedding, {
2018-07-28 13:43:58 +02:00
via: württembergallee,
results: 1,
departure: when,
stopovers: true
})
2018-11-21 19:07:37 +01:00
await testJourneysWithDetour({
2018-07-28 13:43:58 +02:00
test: t,
res,
2018-07-28 13:43:58 +02:00
validate,
detourIds: [württembergallee]
})
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
// todo: without detour test
2021-05-20 16:42:43 +01:00
tap.test('departures', async (t) => {
2018-11-21 19:07:37 +01:00
const departures = await client.departures(spichernstr, {
duration: 5, when
2018-07-28 13:43:58 +02:00
})
2018-11-21 19:07:37 +01:00
await testDepartures({
2018-07-28 13:43:58 +02:00
test: t,
departures,
validate,
id: spichernstr
})
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('departures with station object', async (t) => {
2018-11-21 19:07:37 +01:00
const deps = await client.departures({
2018-07-28 13:43:58 +02:00
type: 'station',
id: spichernstr,
name: 'U Spichernstr',
location: {
type: 'location',
latitude: 1.23,
longitude: 2.34
}
}, {when})
validate(t, deps, 'departures', 'departures')
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('departures at Spichernstr. in direction of Westhafen', async (t) => {
2018-11-21 19:07:37 +01:00
await testDeparturesInDirection({
2018-07-28 13:43:58 +02:00
test: t,
fetchDepartures: client.departures,
fetchTrip: client.trip,
id: spichernstr,
directionIds: [westhafen],
when,
validate
})
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('departures at 7-digit station', async (t) => {
2018-07-28 13:43:58 +02:00
const eisenach = '8010097' // see derhuerst/vbb-hafas#22
2018-11-21 19:07:37 +01:00
await client.departures(eisenach, {when})
2018-07-28 13:43:58 +02:00
t.pass('did not fail')
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('arrivals', async (t) => {
2018-11-21 19:07:37 +01:00
const arrivals = await client.arrivals(spichernstr, {
2018-07-28 13:43:58 +02:00
duration: 5, when
})
2018-11-21 19:07:37 +01:00
await testArrivals({
2018-07-28 13:43:58 +02:00
test: t,
arrivals,
validate,
id: spichernstr
})
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('nearby', async (t) => {
2018-07-28 13:43:58 +02:00
const berlinerStr = '900000044201'
const landhausstr = '900000043252'
// Berliner Str./Bundesallee
2018-11-21 19:07:37 +01:00
const nearby = await client.nearby({
2018-07-28 13:43:58 +02:00
type: 'location',
latitude: 52.4873452,
longitude: 13.3310411
2021-08-24 00:43:20 +02:00
}, {distance: 300})
2018-07-28 13:43:58 +02:00
validate(t, nearby, 'locations', 'nearby')
t.equal(nearby[0].id, berlinerStr)
t.equal(nearby[0].name, 'U Berliner Str.')
t.ok(nearby[0].distance > 0)
t.ok(nearby[0].distance < 100)
t.equal(nearby[1].id, landhausstr)
t.equal(nearby[1].name, 'Landhausstr.')
t.ok(nearby[1].distance > 100)
t.ok(nearby[1].distance < 200)
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('locations', async (t) => {
2018-11-21 19:07:37 +01:00
const locations = await client.locations('Alexanderplatz', {results: 20})
2018-07-28 13:43:58 +02:00
validate(t, locations, 'locations', 'locations')
t.ok(locations.length <= 20)
t.ok(locations.find(s => s.type === 'stop' || s.type === 'station'))
2019-02-07 17:47:50 +01:00
t.ok(locations.find(s => s.poi)) // POIs
2018-07-28 13:43:58 +02:00
t.ok(locations.find(s => !s.name && s.address)) // addresses
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('stop', async (t) => {
const s = await client.stop(spichernstr)
2018-07-28 13:43:58 +02:00
validate(t, s, ['stop', 'station'], 'stop')
2018-07-28 13:43:58 +02:00
t.equal(s.id, spichernstr)
t.end()
2018-11-21 19:07:37 +01:00
})
2018-07-28 13:43:58 +02:00
2021-05-20 16:42:43 +01:00
tap.test('radar', async (t) => {
2018-11-21 19:07:37 +01:00
const vehicles = await client.radar({
2018-07-28 13:43:58 +02:00
north: 52.52411,
west: 13.41002,
south: 52.51942,
east: 13.41709
}, {
duration: 5 * 60, when
})
validate(t, vehicles, 'movements', 'vehicles')
t.end()
2018-11-21 19:07:37 +01:00
})
2021-05-20 16:42:43 +01:00
tap.test('reachableFrom', async (t) => {
const torfstr17 = {
type: 'location',
address: '13353 Berlin-Wedding, Torfstr. 17',
latitude: 52.541797,
longitude: 13.350042
}
2018-11-21 19:07:37 +01:00
await testReachableFrom({
test: t,
reachableFrom: client.reachableFrom,
address: torfstr17,
when,
maxDuration: 15,
validate
})
t.end()
2018-11-21 19:07:37 +01:00
})