db-vendo-client/test/vbb.js

518 lines
14 KiB
JavaScript
Raw Normal View History

'use strict'
const a = require('assert')
const isRoughlyEqual = require('is-roughly-equal')
const stations = require('vbb-stations-autocomplete')
2017-11-14 02:59:17 +01:00
const tapePromise = require('tape-promise').default
const tape = require('tape')
const shorten = require('vbb-short-station-name')
2018-01-23 01:49:41 +01:00
const co = require('./co')
const createClient = require('..')
const vbbProfile = require('../p/vbb')
const {
assertValidStation: _assertValidStation,
assertValidPoi,
assertValidAddress,
assertValidLocation,
2017-12-11 14:51:09 +01:00
assertValidLine: _assertValidLine,
2017-11-20 15:08:30 +01:00
assertValidStopover,
hour, createWhen,
2017-12-12 23:15:06 +01:00
assertValidWhen,
assertValidTicket
} = require('./util')
const when = createWhen('Europe/Berlin', 'de-DE')
const assertValidStation = (t, s, coordsOptional = false) => {
_assertValidStation(t, s, coordsOptional)
t.equal(s.name, shorten(s.name))
}
const assertValidStationProducts = (t, p) => {
t.ok(p)
t.equal(typeof p.suburban, 'boolean')
t.equal(typeof p.subway, 'boolean')
t.equal(typeof p.tram, 'boolean')
t.equal(typeof p.bus, 'boolean')
t.equal(typeof p.ferry, 'boolean')
t.equal(typeof p.express, 'boolean')
t.equal(typeof p.regional, 'boolean')
}
2017-12-11 14:51:09 +01:00
const assertValidLine = (t, l) => {
_assertValidLine(t, l)
if (l.symbol !== null) t.equal(typeof l.symbol, 'string')
if (l.nr !== null) t.equal(typeof l.nr, 'number')
if (l.metro !== null) t.equal(typeof l.metro, 'boolean')
if (l.express !== null) t.equal(typeof l.express, 'boolean')
if (l.night !== null) t.equal(typeof l.night, 'boolean')
}
2018-01-23 00:50:21 +01:00
const findStation = (query) => stations(query, true, false)[0]
2017-11-14 02:59:17 +01:00
const test = tapePromise(tape)
const client = createClient(vbbProfile)
2017-11-20 01:05:48 +01:00
const amrumerStr = '900000009101'
const spichernstr = '900000042101'
const bismarckstr = '900000024201'
2018-01-23 01:49:41 +01:00
test('journeys  station to station', co(function* (t) {
2017-11-29 02:27:31 +01:00
const journeys = yield client.journeys(spichernstr, amrumerStr, {
results: 3, when, passedStations: true
})
2017-11-14 02:59:17 +01:00
t.ok(Array.isArray(journeys))
t.strictEqual(journeys.length, 3)
for (let journey of journeys) {
t.equal(journey.type, 'journey')
2017-11-14 02:59:17 +01:00
assertValidStation(t, journey.origin)
assertValidStationProducts(t, journey.origin.products)
2017-11-14 02:59:17 +01:00
t.ok(journey.origin.name.indexOf('(Berlin)') === -1)
2017-11-20 01:05:48 +01:00
t.strictEqual(journey.origin.id, spichernstr)
assertValidWhen(t, journey.departure, when)
2017-11-14 02:59:17 +01:00
assertValidStation(t, journey.destination)
assertValidStationProducts(t, journey.destination.products)
2017-11-20 01:05:48 +01:00
t.strictEqual(journey.destination.id, amrumerStr)
assertValidWhen(t, journey.arrival, when)
2017-11-14 02:59:17 +01:00
t.ok(Array.isArray(journey.legs))
t.strictEqual(journey.legs.length, 1)
const leg = journey.legs[0]
t.equal(typeof leg.id, 'string')
t.ok(leg.id)
assertValidStation(t, leg.origin)
assertValidStationProducts(t, leg.origin.products)
t.ok(leg.origin.name.indexOf('(Berlin)') === -1)
t.strictEqual(leg.origin.id, spichernstr)
assertValidWhen(t, leg.departure, when)
assertValidStation(t, leg.destination)
assertValidStationProducts(t, leg.destination.products)
t.strictEqual(leg.destination.id, amrumerStr)
assertValidWhen(t, leg.arrival, when)
assertValidLine(t, leg.line)
if (!findStation(leg.direction)) {
const err = new Error('unknown direction: ' + leg.direction)
err.stack = err.stack.split('\n').slice(0, 2).join('\n')
console.error(err)
}
t.ok(leg.direction.indexOf('(Berlin)') === -1)
t.ok(Array.isArray(leg.passed))
for (let passed of leg.passed) assertValidStopover(t, passed)
2017-12-11 15:49:58 +01:00
// todo: find a journey where there ticket info is always available
if (journey.tickets) {
t.ok(Array.isArray(journey.tickets))
for (let ticket of journey.tickets) assertValidTicket(t, ticket)
}
2017-11-14 02:59:17 +01:00
}
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-01-23 01:49:41 +01:00
test('journeys  only subway', co(function* (t) {
2017-11-29 02:27:31 +01:00
const journeys = yield client.journeys(spichernstr, bismarckstr, {
results: 20, when,
products: {
suburban: false,
subway: true,
tram: false,
bus: false,
ferry: false,
express: false,
regional: false
}
})
2017-11-14 02:59:17 +01:00
t.ok(Array.isArray(journeys))
t.ok(journeys.length > 1)
for (let journey of journeys) {
for (let leg of journey.legs) {
if (leg.line) {
assertValidLine(t, leg.line)
t.equal(leg.line.mode, 'train')
t.equal(leg.line.product, 'subway')
}
}
2017-11-14 02:59:17 +01:00
}
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-01-23 01:49:41 +01:00
test('journeys  fails with no product', co(function* (t) {
2017-11-14 02:59:17 +01:00
try {
2018-03-02 16:34:22 +01:00
client.journeys(spichernstr, bismarckstr, {
2017-11-14 02:59:17 +01:00
when,
products: {
suburban: false,
subway: false,
tram: false,
bus: false,
ferry: false,
express: false,
regional: false
}
})
2018-03-02 16:34:22 +01:00
// silence rejections, we're only interested in exceptions
.catch(() => {})
2017-11-14 02:59:17 +01:00
} catch (err) {
t.ok(err, 'error thrown')
t.end()
2017-11-14 02:59:17 +01:00
}
2017-11-29 02:27:31 +01:00
}))
2018-03-04 19:54:21 +01:00
test('earlier/later journeys', co(function* (t) {
const model = yield client.journeys(spichernstr, bismarckstr, {
results: 3, when
})
t.equal(typeof model.earlierRef, 'string')
t.ok(model.earlierRef)
t.equal(typeof model.laterRef, 'string')
t.ok(model.laterRef)
2018-03-04 19:54:21 +01:00
// when and earlierThan/laterThan should be mutually exclusive
t.throws(() => {
client.journeys(spichernstr, bismarckstr, {
when, earlierThan: model.earlierRef
})
})
t.throws(() => {
client.journeys(spichernstr, bismarckstr, {
when, laterThan: model.laterRef
})
})
2018-03-04 19:54:21 +01:00
let earliestDep = Infinity, latestDep = -Infinity
for (let j of model) {
const dep = +new Date(j.departure)
if (dep < earliestDep) earliestDep = dep
else if (dep > latestDep) latestDep = dep
}
const earlier = yield client.journeys(spichernstr, bismarckstr, {
results: 3,
// todo: single journey ref?
earlierThan: model.earlierRef
2018-03-04 19:54:21 +01:00
})
for (let j of earlier) {
t.ok(new Date(j.departure) < earliestDep)
}
const later = yield client.journeys(spichernstr, bismarckstr, {
results: 3,
// todo: single journey ref?
laterThan: model.laterRef
2018-03-04 19:54:21 +01:00
})
for (let j of later) {
t.ok(new Date(j.departure) > latestDep)
}
t.end()
}))
2018-01-23 01:49:41 +01:00
test('journey leg details', co(function* (t) {
2017-11-29 02:27:31 +01:00
const journeys = yield client.journeys(spichernstr, amrumerStr, {
2017-11-14 02:59:17 +01:00
results: 1, when
})
const p = journeys[0].legs[0]
2017-11-14 02:59:17 +01:00
t.ok(p.id, 'precondition failed')
t.ok(p.line.name, 'precondition failed')
const leg = yield client.journeyLeg(p.id, p.line.name, {when})
t.equal(typeof leg.id, 'string')
t.ok(leg.id)
assertValidLine(t, leg.line)
2017-11-14 02:59:17 +01:00
t.equal(typeof leg.direction, 'string')
t.ok(leg.direction)
2017-11-14 02:59:17 +01:00
t.ok(Array.isArray(leg.passed))
for (let passed of leg.passed) assertValidStopover(t, passed)
2017-11-14 02:59:17 +01:00
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-01-23 01:49:41 +01:00
test('journeys  station to address', co(function* (t) {
2017-11-29 02:27:31 +01:00
const journeys = yield client.journeys(spichernstr, {
type: 'location',
address: 'Torfstr. 17, Berlin',
latitude: 52.541797, longitude: 13.350042
}, {results: 1, when})
2017-11-14 02:59:17 +01:00
t.ok(Array.isArray(journeys))
t.strictEqual(journeys.length, 1)
const journey = journeys[0]
const leg = journey.legs[journey.legs.length - 1]
assertValidStation(t, leg.origin)
assertValidStationProducts(t, leg.origin.products)
assertValidWhen(t, leg.departure, when)
2017-11-14 02:59:17 +01:00
const dest = leg.destination
2017-11-14 02:59:17 +01:00
assertValidAddress(t, dest)
t.strictEqual(dest.address, '13353 Berlin-Wedding, Torfstr. 17')
t.ok(isRoughlyEqual(.0001, dest.latitude, 52.541797))
t.ok(isRoughlyEqual(.0001, dest.longitude, 13.350042))
assertValidWhen(t, leg.arrival, when)
2017-11-14 02:59:17 +01:00
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-01-23 01:49:41 +01:00
test('journeys  station to POI', co(function* (t) {
2017-11-29 02:27:31 +01:00
const journeys = yield client.journeys(spichernstr, {
type: 'location',
id: '900980720',
name: 'Berlin, Atze Musiktheater für Kinder',
latitude: 52.543333, longitude: 13.351686
}, {results: 1, when})
2017-11-14 02:59:17 +01:00
t.ok(Array.isArray(journeys))
t.strictEqual(journeys.length, 1)
const journey = journeys[0]
const leg = journey.legs[journey.legs.length - 1]
assertValidStation(t, leg.origin)
assertValidStationProducts(t, leg.origin.products)
assertValidWhen(t, leg.departure, when)
2017-11-14 02:59:17 +01:00
const dest = leg.destination
2017-11-14 02:59:17 +01:00
assertValidPoi(t, dest)
t.strictEqual(dest.id, '900980720')
t.strictEqual(dest.name, 'Berlin, Atze Musiktheater für Kinder')
t.ok(isRoughlyEqual(.0001, dest.latitude, 52.543333))
t.ok(isRoughlyEqual(.0001, dest.longitude, 13.351686))
assertValidWhen(t, leg.arrival, when)
2017-11-14 02:59:17 +01:00
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-03-16 14:30:49 +01:00
test('journeys: via works with detour', co(function* (t) {
// Going from Westhafen to Wedding via Württembergalle without detour
2018-03-16 14:34:37 +01:00
// is currently impossible. We check if the routing engine computes a detour.
const westhafen = '900000001201'
const wedding = '900000009104'
const württembergallee = '900000026153'
const [journey] = yield client.journeys(westhafen, wedding, {
via: württembergallee,
results: 1,
2018-03-16 14:30:49 +01:00
when,
passedStations: true
})
t.ok(journey)
2018-03-16 14:30:49 +01:00
const l = journey.legs.some(l => l.passed && l.passed.some(p => p.station.id === württembergallee))
2018-03-16 14:38:08 +01:00
t.ok(l, 'Württembergalle is not being passed')
t.end()
}))
2018-03-16 14:30:49 +01:00
test('journeys: via works without detour', co(function* (t) {
// When going from Ruhleben to Zoo via Kastanienallee, there is *no need*
2018-03-16 14:30:49 +01:00
// to change trains / no need for a "detour".
const ruhleben = '900000025202'
const zoo = '900000023201'
const kastanienallee = '900000020152'
const [journey] = yield client.journeys(ruhleben, zoo, {
via: kastanienallee,
results: 1,
when,
passedStations: true
})
t.ok(journey)
2018-03-16 14:30:49 +01:00
const l = journey.legs.some(l => l.passed && l.passed.some(p => p.station.id === kastanienallee))
2018-03-16 14:38:08 +01:00
t.ok(l, 'Kastanienallee is not being passed')
t.end()
}))
2018-01-23 01:49:41 +01:00
test('departures', co(function* (t) {
2017-11-29 02:27:31 +01:00
const deps = yield client.departures(spichernstr, {duration: 5, when})
2017-11-14 02:59:17 +01:00
t.ok(Array.isArray(deps))
t.deepEqual(deps, deps.sort((a, b) => t.when > b.when))
for (let dep of deps) {
t.equal(typeof dep.journeyId, 'string')
t.ok(dep.journeyId)
2017-11-14 02:59:17 +01:00
t.equal(dep.station.name, 'U Spichernstr.')
assertValidStation(t, dep.station)
assertValidStationProducts(t, dep.station.products)
2017-11-20 01:05:48 +01:00
t.strictEqual(dep.station.id, spichernstr)
2017-11-14 02:59:17 +01:00
assertValidWhen(t, dep.when, when)
if (!findStation(dep.direction)) {
const err = new Error('unknown direction: ' + dep.direction)
err.stack = err.stack.split('\n').slice(0, 2).join('\n')
console.error(err)
}
2017-11-14 02:59:17 +01:00
assertValidLine(t, dep.line)
}
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-01-23 01:49:41 +01:00
test('departures with station object', co(function* (t) {
yield client.departures({
type: 'station',
id: spichernstr,
name: 'U Spichernstr',
location: {
type: 'location',
latitude: 1.23,
longitude: 2.34
}
}, {when})
t.ok('did not fail')
t.end()
}))
2018-01-23 01:49:41 +01:00
test('departures at 7-digit station', co(function* (t) {
const eisenach = '8010097' // see derhuerst/vbb-hafas#22
2017-12-16 03:22:36 +01:00
yield client.departures(eisenach, {when})
2017-11-14 02:59:17 +01:00
t.pass('did not fail')
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-01-23 01:49:41 +01:00
test('nearby', co(function* (t) {
2017-11-14 02:59:17 +01:00
// Berliner Str./Bundesallee
2018-01-05 14:53:03 +01:00
const nearby = yield client.nearby({
type: 'location',
latitude: 52.4873452,
longitude: 13.3310411
}, {distance: 200})
2017-11-14 02:59:17 +01:00
t.ok(Array.isArray(nearby))
2017-12-11 19:53:26 +01:00
for (let n of nearby) {
if (n.type === 'station') assertValidStation(t, n)
else assertValidLocation(t, n, false)
}
2017-11-14 02:59:17 +01:00
t.equal(nearby[0].id, '900000044201')
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, '900000043252')
t.equal(nearby[1].name, 'Landhausstr.')
t.ok(nearby[1].distance > 100)
t.ok(nearby[1].distance < 200)
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-01-23 01:49:41 +01:00
test('locations', co(function* (t) {
2018-02-15 17:31:43 +01:00
const locations = yield client.locations('Alexanderplatz', {results: 20})
2017-11-14 02:59:17 +01:00
t.ok(Array.isArray(locations))
t.ok(locations.length > 0)
2018-02-15 17:31:43 +01:00
t.ok(locations.length <= 20)
2017-12-11 19:53:26 +01:00
for (let l of locations) {
if (l.type === 'station') assertValidStation(t, l)
else assertValidLocation(t, l)
}
t.ok(locations.find(s => s.type === 'station'))
t.ok(locations.find(s => s.id && s.name)) // POIs
t.ok(locations.find(s => !s.name && s.address)) // addresses
2017-11-14 02:59:17 +01:00
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-01-26 17:08:07 +01:00
test('location', co(function* (t) {
const loc = yield client.location(spichernstr)
assertValidStation(t, loc)
t.equal(loc.id, spichernstr)
t.ok(Array.isArray(loc.lines))
if (Array.isArray(loc.lines)) {
for (let line of loc.lines) assertValidLine(t, line)
}
2018-01-26 17:08:07 +01:00
t.end()
}))
2018-01-23 01:49:41 +01:00
test('radar', co(function* (t) {
const vehicles = yield client.radar({
north: 52.52411,
west: 13.41002,
south: 52.51942,
east: 13.41709
}, {
2017-11-14 02:59:17 +01:00
duration: 5 * 60, when
})
2017-11-14 02:59:17 +01:00
t.ok(Array.isArray(vehicles))
t.ok(vehicles.length > 0)
for (let v of vehicles) {
if (!findStation(v.direction)) {
const err = new Error('unknown direction: ' + v.direction)
err.stack = err.stack.split('\n').slice(0, 2).join('\n')
console.error(err)
}
2017-11-14 02:59:17 +01:00
assertValidLine(t, v.line)
t.equal(typeof v.location.latitude, 'number')
t.ok(v.location.latitude <= 55, 'vehicle is too far away')
t.ok(v.location.latitude >= 45, 'vehicle is too far away')
t.equal(typeof v.location.longitude, 'number')
t.ok(v.location.longitude >= 9, 'vehicle is too far away')
t.ok(v.location.longitude <= 15, 'vehicle is too far away')
2017-11-14 02:59:17 +01:00
t.ok(Array.isArray(v.nextStops))
2017-11-20 17:37:08 +01:00
for (let st of v.nextStops) {
assertValidStopover(t, st, true)
t.strictEqual(st.station.name.indexOf('(Berlin)'), -1)
if (st.arrival) {
t.equal(typeof st.arrival, 'string')
const arr = +new Date(st.arrival)
2017-11-14 02:59:17 +01:00
// note that this can be an ICE train
t.ok(isRoughlyEqual(14 * hour, +when, arr))
}
2017-11-20 17:37:08 +01:00
if (st.departure) {
t.equal(typeof st.departure, 'string')
const dep = +new Date(st.departure)
2017-11-14 02:59:17 +01:00
// note that this can be an ICE train
t.ok(isRoughlyEqual(14 * hour, +when, dep))
}
}
2017-11-14 02:59:17 +01:00
t.ok(Array.isArray(v.frames))
for (let f of v.frames) {
2017-11-20 17:37:08 +01:00
assertValidStation(t, f.origin, true)
assertValidStationProducts(t, f.origin.products)
2017-11-20 17:37:08 +01:00
t.strictEqual(f.origin.name.indexOf('(Berlin)'), -1)
assertValidStation(t, f.destination, true)
assertValidStationProducts(t, f.destination.products)
2017-11-20 17:37:08 +01:00
t.strictEqual(f.destination.name.indexOf('(Berlin)'), -1)
2017-11-14 02:59:17 +01:00
t.equal(typeof f.t, 'number')
}
}
t.end()
2017-11-29 02:27:31 +01:00
}))