2018-03-14 14:54:08 +01:00
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
|
|
const tapePromise = require('tape-promise').default
|
|
|
|
|
const tape = require('tape')
|
|
|
|
|
const isRoughlyEqual = require('is-roughly-equal')
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
const {createWhen} = require('./lib/util')
|
2018-04-18 16:15:24 +02:00
|
|
|
|
const co = require('./lib/co')
|
2018-03-14 14:54:08 +01:00
|
|
|
|
const createClient = require('..')
|
|
|
|
|
const nahshProfile = require('../p/nahsh')
|
2018-04-19 18:14:10 +02:00
|
|
|
|
const products = require('../p/nahsh/products')
|
2018-03-14 14:54:08 +01:00
|
|
|
|
const {
|
2018-04-19 18:14:10 +02:00
|
|
|
|
line: createValidateLine,
|
|
|
|
|
station: createValidateStation
|
|
|
|
|
} = require('./lib/validators')
|
|
|
|
|
const createValidate = require('./lib/validate-fptf-with')
|
2018-04-23 13:56:00 +02:00
|
|
|
|
const testJourneysStationToStation = require('./lib/journeys-station-to-station')
|
2018-04-24 15:42:53 +02:00
|
|
|
|
const testJourneysStationToAddress = require('./lib/journeys-station-to-address')
|
2018-04-24 15:24:59 +02:00
|
|
|
|
const testEarlierLaterJourneys = require('./lib/earlier-later-journeys')
|
2018-03-14 14:54:08 +01:00
|
|
|
|
|
|
|
|
|
const when = createWhen('Europe/Berlin', 'de-DE')
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
const cfg = {
|
|
|
|
|
when,
|
|
|
|
|
stationCoordsOptional: false,
|
|
|
|
|
products
|
2018-03-14 14:54:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
const _validateLine = createValidateLine(cfg)
|
|
|
|
|
const validateLine = (validate, l, name) => {
|
|
|
|
|
if (l && l.product === 'onCall') {
|
|
|
|
|
// skip line validation
|
|
|
|
|
// https://github.com/derhuerst/hafas-client/issues/8#issuecomment-355839965
|
|
|
|
|
l = Object.assign({}, l)
|
|
|
|
|
l.mode = 'taxi'
|
|
|
|
|
}
|
|
|
|
|
_validateLine(validate, l, name)
|
2018-03-14 14:54:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
const validate = createValidate(cfg, {
|
|
|
|
|
line: validateLine
|
|
|
|
|
})
|
2018-03-14 14:54:08 +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(nahshProfile)
|
|
|
|
|
|
|
|
|
|
const kielHbf = '8000199'
|
|
|
|
|
const flensburg = '8000103'
|
2018-04-19 18:14:10 +02:00
|
|
|
|
const holstentor = '970003547'
|
2018-03-14 14:54:08 +01:00
|
|
|
|
const luebeckHbf = '8000237'
|
|
|
|
|
const husum = '8000181'
|
|
|
|
|
const schleswig = '8005362'
|
|
|
|
|
|
2018-04-23 13:56:00 +02:00
|
|
|
|
test('journeys – Kiel Hbf to Flensburg', co(function* (t) {
|
2018-03-14 14:54:08 +01:00
|
|
|
|
const journeys = yield client.journeys(kielHbf, flensburg, {
|
2018-04-23 13:56:00 +02:00
|
|
|
|
results: 3, when, passedStations: true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
yield testJourneysStationToStation({
|
|
|
|
|
test: t,
|
|
|
|
|
journeys,
|
|
|
|
|
validate,
|
|
|
|
|
fromId: kielHbf,
|
|
|
|
|
toId: flensburg
|
2018-03-14 14:54:08 +01:00
|
|
|
|
})
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
for (let i = 0; i < journeys.length; i++) {
|
|
|
|
|
const j = journeys[i]
|
|
|
|
|
// todo: find a journey where there pricing info is always available
|
2018-04-23 13:56:00 +02:00
|
|
|
|
if (j.price) assertValidPrice(t, j.price, `journeys[${i}].price`)
|
2018-04-19 18:14:10 +02:00
|
|
|
|
}
|
2018-04-23 13:56:00 +02:00
|
|
|
|
|
2018-03-14 14:54:08 +01:00
|
|
|
|
t.end()
|
|
|
|
|
}))
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
// todo: journeys, only one product
|
|
|
|
|
// todo: journeys, fails with no product
|
|
|
|
|
|
2018-03-14 14:54:08 +01:00
|
|
|
|
test('Kiel Hbf to Husum, Zingel 10', co(function* (t) {
|
|
|
|
|
const zingel = {
|
|
|
|
|
type: 'location',
|
2018-04-19 18:14:10 +02:00
|
|
|
|
address: 'Husum, Zingel 10',
|
2018-04-24 15:42:53 +02:00
|
|
|
|
latitude: 54.475359,
|
|
|
|
|
longitude: 9.050798
|
2018-03-14 14:54:08 +01:00
|
|
|
|
}
|
2018-04-24 15:42:53 +02:00
|
|
|
|
const journeys = yield client.journeys(kielHbf, zingel, {
|
|
|
|
|
results: 3, when
|
|
|
|
|
})
|
2018-03-14 14:54:08 +01:00
|
|
|
|
|
2018-04-24 15:42:53 +02:00
|
|
|
|
yield testJourneysStationToAddress({
|
|
|
|
|
test: t,
|
|
|
|
|
journeys,
|
|
|
|
|
validate,
|
|
|
|
|
fromId: kielHbf,
|
|
|
|
|
to: zingel
|
|
|
|
|
})
|
2018-03-14 14:54:08 +01:00
|
|
|
|
t.end()
|
|
|
|
|
}))
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
test('Kiel Hbf to Holstentor', co(function* (t) {
|
|
|
|
|
const latitude = 53.866321
|
|
|
|
|
const longitude = 10.679976
|
|
|
|
|
const name = 'Hansestadt Lübeck, Holstentor (Denkmal)'
|
|
|
|
|
const journeys = yield client.journeys(kielHbf, {
|
2018-03-14 14:54:08 +01:00
|
|
|
|
type: 'location',
|
2018-04-19 18:14:10 +02:00
|
|
|
|
id: holstentor,
|
|
|
|
|
name,
|
|
|
|
|
latitude, longitude
|
|
|
|
|
}, {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.strictEqual(d.id, holstentor, k + '.id is invalid')
|
|
|
|
|
t.strictEqual(d.name, name, 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')
|
2018-03-14 14:54:08 +01:00
|
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
}))
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
test('Husum to Lübeck Hbf with stopover at Kiel Hbf', co(function* (t) {
|
|
|
|
|
const journeys = yield client.journeys(husum, luebeckHbf, {
|
2018-03-14 14:54:08 +01:00
|
|
|
|
via: kielHbf,
|
2018-04-19 18:14:10 +02:00
|
|
|
|
results: 1, when, passedStations: true
|
2018-03-14 14:54:08 +01:00
|
|
|
|
})
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
validate(t, journeys, 'journeys', 'journeys')
|
2018-03-14 14:54:08 +01:00
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
const leg = journeys[0].legs.some((leg) => {
|
|
|
|
|
return leg.passed && leg.passed.some((passed) => {
|
|
|
|
|
return passed.station.id === kielHbf
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
t.ok(leg, 'Kiel Hbf is not being passed')
|
2018-03-14 14:54:08 +01:00
|
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
test('earlier/later journeys, Kiel Hbf -> Flensburg', co(function* (t) {
|
2018-04-24 15:24:59 +02:00
|
|
|
|
yield testEarlierLaterJourneys({
|
|
|
|
|
test: t,
|
|
|
|
|
fetchJourneys: client.journeys,
|
|
|
|
|
validate,
|
|
|
|
|
fromId: kielHbf,
|
|
|
|
|
toId: flensburg
|
2018-03-14 14:54:08 +01:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
}))
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
test('journey leg details for Flensburg to Husum', co(function* (t) {
|
2018-03-14 14:54:08 +01:00
|
|
|
|
const journeys = yield client.journeys(flensburg, husum, {
|
|
|
|
|
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-19 18:14:10 +02:00
|
|
|
|
validate(t, leg, 'journeyLeg', 'leg')
|
2018-03-14 14:54:08 +01:00
|
|
|
|
t.end()
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
test('departures at Kiel Hbf', co(function* (t) {
|
|
|
|
|
const deps = yield client.departures(kielHbf, {
|
|
|
|
|
duration: 30, when
|
|
|
|
|
})
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
validate(t, deps, 'departures', 'departures')
|
|
|
|
|
for (let i = 0; i < deps.length; i++) {
|
|
|
|
|
const dep = deps[i]
|
|
|
|
|
const name = `deps[${i}]`
|
|
|
|
|
|
|
|
|
|
// todo: fix this
|
|
|
|
|
// t.equal(dep.station.name, 'Kiel Hauptbahnhof', name + '.station.name is invalid')
|
|
|
|
|
// t.equal(dep.station.id, kielHbf, name + '.station.id is invalid')
|
2018-03-14 14:54:08 +01:00
|
|
|
|
}
|
2018-04-19 18:14:10 +02:00
|
|
|
|
// todo: move into deps validator
|
|
|
|
|
t.deepEqual(deps, deps.sort((a, b) => t.when > b.when))
|
2018-03-14 14:54:08 +01:00
|
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
}))
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
test('departures with station object', co(function* (t) {
|
|
|
|
|
const deps = yield client.departures({
|
|
|
|
|
type: 'station',
|
|
|
|
|
id: kielHbf,
|
|
|
|
|
name: 'Kiel Hbf',
|
|
|
|
|
location: {
|
|
|
|
|
type: 'location',
|
|
|
|
|
latitude: 1.23,
|
|
|
|
|
longitude: 2.34
|
|
|
|
|
}
|
|
|
|
|
}, {when})
|
|
|
|
|
|
|
|
|
|
validate(t, deps, 'departures', 'departures')
|
|
|
|
|
t.end()
|
|
|
|
|
}))
|
|
|
|
|
|
2018-03-14 14:54:08 +01:00
|
|
|
|
test('nearby Kiel Hbf', co(function* (t) {
|
|
|
|
|
const kielHbfPosition = {
|
|
|
|
|
type: 'location',
|
|
|
|
|
latitude: 54.314982,
|
|
|
|
|
longitude: 10.131976
|
|
|
|
|
}
|
|
|
|
|
const nearby = yield client.nearby(kielHbfPosition, {
|
|
|
|
|
results: 2, distance: 400
|
|
|
|
|
})
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
validate(t, nearby, 'locations', 'nearby')
|
|
|
|
|
|
2018-03-14 14:54:08 +01:00
|
|
|
|
t.ok(Array.isArray(nearby))
|
|
|
|
|
t.equal(nearby.length, 2)
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
t.equal(nearby[0].id, kielHbf)
|
|
|
|
|
t.equal(nearby[0].name, 'Kiel Hbf')
|
2018-03-14 14:54:08 +01:00
|
|
|
|
t.ok(nearby[0].distance >= 0)
|
|
|
|
|
t.ok(nearby[0].distance <= 100)
|
|
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
test('locations named Kiel', co(function* (t) {
|
|
|
|
|
const locations = yield client.locations('Kiel', {
|
2018-04-19 18:14:10 +02:00
|
|
|
|
results: 20
|
2018-03-14 14:54:08 +01:00
|
|
|
|
})
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
validate(t, locations, 'locations', 'locations')
|
|
|
|
|
t.ok(locations.length <= 20)
|
2018-03-14 14:54:08 +01:00
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
t.ok(locations.find(s => s.type === 'station'))
|
|
|
|
|
t.ok(locations.find(s => s.id && s.name)) // POIs
|
|
|
|
|
t.ok(locations.some(l => l.id === kielHbf))
|
2018-03-14 14:54:08 +01:00
|
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
test('location', co(function* (t) {
|
2018-04-19 18:14:10 +02:00
|
|
|
|
const s = yield client.location(kielHbf)
|
2018-03-14 14:54:08 +01:00
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
validate(t, s, 'station', 'station')
|
|
|
|
|
t.equal(s.id, kielHbf)
|
2018-03-14 14:54:08 +01:00
|
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
}))
|
|
|
|
|
|
2018-04-19 18:14:10 +02:00
|
|
|
|
test('radar', co(function* (t) {
|
|
|
|
|
const vehicles = yield client.radar({
|
|
|
|
|
north: 54.4,
|
|
|
|
|
west: 10.0,
|
|
|
|
|
south: 54.2,
|
|
|
|
|
east: 10.2
|
|
|
|
|
}, {
|
2018-03-17 17:27:43 +01:00
|
|
|
|
duration: 5 * 60, when
|
|
|
|
|
})
|
|
|
|
|
|
2018-04-20 11:04:54 +02:00
|
|
|
|
// todo: cfg.stationProductsOptional option
|
2018-04-19 18:14:10 +02:00
|
|
|
|
const allProducts = products.reduce((acc, p) => (acc[p.id] = true, acc), {})
|
|
|
|
|
const validateStation = createValidateStation(cfg)
|
|
|
|
|
const validate = createValidate(cfg, {
|
|
|
|
|
station: (validate, s, name) => {
|
|
|
|
|
s = Object.assign({
|
|
|
|
|
products: allProducts // todo: fix station.products
|
|
|
|
|
}, s)
|
|
|
|
|
if (!s.name) s.name = 'foo' // todo, see #34
|
|
|
|
|
validateStation(validate, s, name)
|
2018-03-17 17:27:43 +01:00
|
|
|
|
}
|
2018-04-19 18:14:10 +02:00
|
|
|
|
})
|
|
|
|
|
validate(t, vehicles, 'movements', 'vehicles')
|
2018-03-17 17:27:43 +01:00
|
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
}))
|