db-vendo-client/test/oebb.js

340 lines
9 KiB
JavaScript
Raw Normal View History

2017-12-29 10:01:02 +01:00
'use strict'
const tapePromise = require('tape-promise').default
const tape = require('tape')
const isRoughlyEqual = require('is-roughly-equal')
2018-04-20 11:04:54 +02:00
const validateLine = require('validate-fptf/line')
2017-12-29 10:01:02 +01:00
2018-04-20 11:04:54 +02:00
const {createWhen} = require('./lib/util')
const co = require('./lib/co')
2017-12-29 10:01:02 +01:00
const createClient = require('..')
const oebbProfile = require('../p/oebb')
2018-04-20 11:04:54 +02:00
const products = require('../p/oebb/products')
2017-12-29 10:01:02 +01:00
const {
2018-04-20 11:04:54 +02:00
station: createValidateStation
} = require('./lib/validators')
const createValidate = require('./lib/validate-fptf-with')
const testJourneysStationToStation = require('./lib/journeys-station-to-station')
const testEarlierLaterJourneys = require('./lib/earlier-later-journeys')
2017-12-29 10:01:02 +01:00
const when = createWhen('Europe/Vienna', 'de-AT')
2018-04-20 11:04:54 +02:00
const cfg = {
when,
stationCoordsOptional: false,
products
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
2018-04-20 11:04:54 +02:00
const validate = createValidate(cfg, {
line: validateLine // bypass line validator in lib/validators
})
2017-12-29 10:01:02 +01:00
const assertValidPrice = (t, p) => {
t.ok(p)
if (p.amount !== null) {
t.equal(typeof p.amount, 'number')
t.ok(p.amount > 0)
}
if (p.hint !== null) {
t.equal(typeof p.hint, 'string')
t.ok(p.hint)
}
}
const test = tapePromise(tape)
const client = createClient(oebbProfile)
const salzburgHbf = '8100002'
const wienFickeystr = '911014'
const wien = '1190100'
const wienWestbahnhof = '1291501'
const klagenfurtHbf = '8100085'
const muenchenHbf = '8000261'
2018-04-20 11:04:54 +02:00
const wienRenngasse = '1390186'
test('journeys  Salzburg Hbf to Wien Westbahnhof', co(function* (t) {
const journeys = yield client.journeys(salzburgHbf, wienFickeystr, {
results: 3, when, passedStations: true
})
yield testJourneysStationToStation({
test: t,
journeys,
validate,
fromId: salzburgHbf,
toId: wienFickeystr
2017-12-29 10:01:02 +01:00
})
for (let i = 0; i < journeys.length; i++) {
const j = journeys[i]
if (j.price) assertValidPrice(t, j.price, `journeys[${i}].price`)
2017-12-29 10:01:02 +01:00
}
t.end()
}))
2018-04-20 11:04:54 +02:00
// todo: journeys, only one product
// todo: journeys, fails with no product
2018-01-23 01:49:41 +01:00
test('Salzburg Hbf to 1220 Wien, Wagramer Straße 5', co(function* (t) {
2018-04-20 11:04:54 +02:00
const latitude = 48.236216
const longitude = 16.425863
2017-12-29 10:01:02 +01:00
const wagramerStr = {
type: 'location',
2018-04-20 11:04:54 +02:00
address: '1220 Wien, Wagramer Straße 5',
latitude, longitude
2017-12-29 10:01:02 +01:00
}
const journeys = yield client.journeys(salzburgHbf, wagramerStr, {when})
2018-04-20 11:04:54 +02:00
validate(t, journeys, 'journeys', 'journeys')
const i = journeys[0].legs.length - 1
const d = journeys[0].legs[i].destination
const k = `journeys[0].legs[${i}].destination`
t.equal(d.address, '1220 Wien, Wagramer Straße 5', k + '.address is invalid')
t.ok(isRoughlyEqual(.0001, d.latitude, latitude), k + '.latitude is invalid')
t.ok(isRoughlyEqual(.0001, d.longitude, longitude), k + '.longitude is invalid')
2017-12-29 10:01:02 +01:00
t.end()
}))
2018-04-20 11:04:54 +02:00
test('Salzburg Hbf to Albertina', co(function* (t) {
const latitude = 48.204699
const longitude = 16.368404
2017-12-29 10:01:02 +01:00
const albertina = {
type: 'location',
2018-04-20 11:04:54 +02:00
id: '975900003',
2017-12-29 10:01:02 +01:00
name: 'Albertina',
2018-04-20 11:04:54 +02:00
latitude, longitude
2017-12-29 10:01:02 +01:00
}
2018-04-20 11:04:54 +02:00
const journeys = yield client.journeys(salzburgHbf, albertina, {when})
validate(t, journeys, 'journeys', 'journeys')
const i = journeys[0].legs.length - 1
const d = journeys[0].legs[i].destination
const k = `journeys[0].legs[${i}].destination`
t.equal(d.name, 'Albertina', k + '.name is invalid')
t.ok(isRoughlyEqual(.0001, d.latitude, latitude), k + '.latitude is invalid')
t.ok(isRoughlyEqual(.0001, d.longitude, longitude), k + '.longitude is invalid')
2017-12-29 10:01:02 +01:00
t.end()
}))
2018-03-16 14:22:00 +01:00
test('journeys: via works with detour', co(function* (t) {
// 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.
2018-03-16 14:22:00 +01:00
const stephansplatz = '001390167'
const schottenring = '001390163'
const donauinsel = '001392277'
const donauinselPassed = '922001'
2018-04-20 11:04:54 +02:00
const journeys = yield 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-03-16 14:22:00 +01:00
when,
passedStations: true
})
2018-04-20 11:04:54 +02:00
validate(t, journeys, 'journeys', 'journeys')
2018-04-20 11:04:54 +02:00
const leg = journeys[0].legs.some((leg) => {
return leg.passed && leg.passed.some((passed) => {
return (
passed.station.id === donauinsel ||
passed.station.id === donauinselPassed
)
})
})
t.ok(leg, 'Donauinsel is not being passed')
2018-03-16 14:22:00 +01:00
t.end()
}))
test('journeys: via works without detour', co(function* (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".
2018-03-16 14:22:00 +01:00
const karlsplatz = '001390461'
const praterstern = '001290201'
const museumsquartier = '001390171'
const museumsquartierPassed = '901014'
2018-04-20 11:04:54 +02:00
const journeys = yield client.journeys(karlsplatz, praterstern, {
2018-03-16 14:22:00 +01:00
via: museumsquartier,
results: 1,
when,
passedStations: true
})
2018-04-20 11:04:54 +02:00
validate(t, journeys, 'journeys', 'journeys')
2018-03-16 14:22:00 +01:00
2018-04-20 11:04:54 +02:00
const l1 = journeys[0].legs.some((leg) => {
return (
leg.destination.id === museumsquartier ||
leg.destination.id === museumsquartierPassed
)
})
t.notOk(l1, 'transfer at Museumsquartier')
const l2 = journeys[0].legs.some((leg) => {
return leg.passed && leg.passed.some((passed) => {
return passed.station.id === museumsquartierPassed
})
})
t.ok(l2, 'Museumsquartier is not being passed')
t.end()
}))
test('earlier/later journeys, Salzburg Hbf -> Wien Westbahnhof', co(function* (t) {
yield testEarlierLaterJourneys({
test: t,
fetchJourneys: client.journeys,
validate,
fromId: salzburgHbf,
toId: wienWestbahnhof
})
t.end()
}))
2018-01-23 01:49:41 +01:00
test('leg details for Wien Westbahnhof to München Hbf', co(function* (t) {
2017-12-29 10:01:02 +01:00
const journeys = yield client.journeys(wienWestbahnhof, muenchenHbf, {
results: 1, when
})
const p = journeys[0].legs[0]
t.ok(p.id, 'precondition failed')
t.ok(p.line.name, 'precondition failed')
const leg = yield client.journeyLeg(p.id, p.line.name, {when})
2018-04-20 11:04:54 +02:00
validate(t, leg, 'journeyLeg', 'leg')
2017-12-29 10:01:02 +01:00
t.end()
}))
2018-04-20 11:04:54 +02:00
test('departures at Wien Renngasse', co(function* (t) {
const deps = yield client.departures(wienRenngasse, {
2017-12-29 10:01:02 +01:00
duration: 5, when
})
2018-04-20 11:04:54 +02:00
validate(t, deps, 'departures', 'departures')
for (let i = 0; i < deps.length; i++) {
const dep = deps[i]
const name = `deps[${i}]`
// todo: fis this
// ÖBB HAFAS data is just too detailed :P
// t.equal(dep.station.name, 'Wien Renngasse', name + '.station.name is invalid')
// t.equal(dep.station.id, wienRenngasse, name + '.station.id is invalid')
2017-12-29 10:01:02 +01:00
}
2018-04-20 11:04:54 +02:00
// todo: move into deps validator
t.deepEqual(deps, deps.sort((a, b) => t.when > b.when))
2017-12-29 10:01:02 +01:00
t.end()
}))
2018-04-20 11:04:54 +02:00
test('departures with station object', co(function* (t) {
const deps = yield client.departures({
type: 'station',
id: salzburgHbf,
name: 'Salzburg Hbf',
location: {
type: 'location',
latitude: 1.23,
longitude: 2.34
}
}, {when})
validate(t, deps, 'departures', 'departures')
t.end()
}))
2018-01-23 01:49:41 +01:00
test('nearby Salzburg Hbf', co(function* (t) {
2018-04-20 11:04:54 +02:00
const nearby = yield client.nearby({
2018-01-07 19:01:22 +01:00
type: 'location',
2017-12-29 10:01:02 +01:00
longitude: 13.045604,
latitude: 47.812851
2018-04-20 11:04:54 +02:00
}, {
results: 5, distance: 400
2017-12-29 10:01:02 +01:00
})
2018-04-20 11:04:54 +02:00
validate(t, nearby, 'locations', 'nearby')
t.equal(nearby.length, 5)
2017-12-29 10:01:02 +01:00
2018-04-20 11:04:54 +02:00
const s = nearby[0]
t.ok(s.id === '008100002' || s.id === '8100002', 'id should be 8100002')
t.equal(s.name, 'Salzburg Hbf')
t.ok(isRoughlyEqual(s.location.latitude, 47.812851, .0005))
t.ok(isRoughlyEqual(s.location.longitude, 13.045604, .0005))
t.ok(s.distance >= 0)
t.ok(s.distance <= 100)
2017-12-29 10:01:02 +01:00
t.end()
}))
2018-01-23 01:49:41 +01:00
test('locations named Salzburg', co(function* (t) {
2017-12-29 10:01:02 +01:00
const locations = yield client.locations('Salzburg', {
2018-04-20 11:04:54 +02:00
results: 20
2017-12-29 10:01:02 +01:00
})
2018-04-20 11:04:54 +02:00
validate(t, locations, 'locations', 'locations')
t.ok(locations.length <= 20)
2017-12-29 10:01:02 +01:00
2018-04-20 11:04:54 +02:00
t.ok(locations.find(s => s.type === 'station'))
t.ok(locations.find(s => s.id && s.name)) // POIs
t.ok(locations.some(s => s.id === '008100002' || s.id === '8100002'))
2017-12-29 10:01:02 +01:00
t.end()
}))
2018-01-26 17:08:07 +01:00
test('location', co(function* (t) {
2018-04-20 11:04:54 +02:00
const loc = yield client.location(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 allProducts = products.reduce((acc, p) => (acc[p.id] = true, acc), {})
const validateStation = createValidateStation(cfg)
const validate = createValidate(cfg, {
station: (validate, s, name) => {
const withFakeProducts = Object.assign({products: allProducts}, s)
validateStation(validate, withFakeProducts, name)
}
})
validate(t, loc, 'station', 'station')
t.equal(loc.id, wienRenngasse)
2018-01-26 17:08:07 +01:00
t.end()
}))
2018-01-23 01:49:41 +01:00
test('radar Salzburg', co(function* (t) {
2018-04-20 11:04:54 +02:00
let vehicles = yield client.radar({
north: 47.827203,
west: 13.001261,
south: 47.773278,
east: 13.07562
}, {
2018-04-20 11:04:54 +02:00
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
vehicles = vehicles.filter(m => m.frames && m.frames.length > 0)
// todo: find a way to always get products from the API
// todo: cfg.stationProductsOptional option
const allProducts = products.reduce((acc, p) => (acc[p.id] = true, acc), {})
const validateStation = createValidateStation(cfg)
const validate = createValidate(cfg, {
station: (validate, s, name) => {
const withFakeProducts = Object.assign({products: allProducts}, s)
validateStation(validate, withFakeProducts, name)
},
line: validateLine
})
validate(t, vehicles, 'movements', 'vehicles')
2017-12-29 10:01:02 +01:00
t.end()
}))