2017-11-12 21:03:24 +01:00
|
|
|
'use strict'
|
|
|
|
|
2017-11-14 02:50:24 +01:00
|
|
|
const getStations = require('db-stations').full
|
2017-11-12 21:03:24 +01:00
|
|
|
const tapePromise = require('tape-promise').default
|
|
|
|
const tape = require('tape')
|
|
|
|
const isRoughlyEqual = require('is-roughly-equal')
|
|
|
|
|
2018-01-23 01:49:41 +01:00
|
|
|
const co = require('./co')
|
2017-11-12 21:03:24 +01:00
|
|
|
const createClient = require('..')
|
2017-11-12 23:51:39 +01:00
|
|
|
const dbProfile = require('../p/db')
|
2018-03-13 03:23:03 +01:00
|
|
|
const {allProducts} = require('../p/db/modes')
|
2017-11-12 21:03:24 +01:00
|
|
|
const {
|
|
|
|
assertValidStation,
|
|
|
|
assertValidPoi,
|
|
|
|
assertValidAddress,
|
|
|
|
assertValidLocation,
|
|
|
|
assertValidLine,
|
|
|
|
assertValidStopover,
|
2017-12-28 22:57:22 +01:00
|
|
|
createWhen, assertValidWhen
|
2017-11-12 21:03:24 +01:00
|
|
|
} = require('./util.js')
|
|
|
|
|
2017-12-28 22:57:22 +01:00
|
|
|
const when = createWhen('Europe/Berlin', 'de-DE')
|
|
|
|
|
2017-12-11 14:41:28 +01:00
|
|
|
const assertValidStationProducts = (t, p) => {
|
|
|
|
t.ok(p)
|
|
|
|
t.equal(typeof p.nationalExp, 'boolean')
|
|
|
|
t.equal(typeof p.national, 'boolean')
|
|
|
|
t.equal(typeof p.regionalExp, 'boolean')
|
|
|
|
t.equal(typeof p.regional, 'boolean')
|
|
|
|
t.equal(typeof p.suburban, 'boolean')
|
|
|
|
t.equal(typeof p.bus, 'boolean')
|
|
|
|
t.equal(typeof p.ferry, 'boolean')
|
|
|
|
t.equal(typeof p.subway, 'boolean')
|
|
|
|
t.equal(typeof p.tram, 'boolean')
|
|
|
|
t.equal(typeof p.taxi, 'boolean')
|
|
|
|
}
|
|
|
|
|
2017-11-14 02:50:24 +01:00
|
|
|
const findStation = (id) => new Promise((yay, nay) => {
|
|
|
|
const stations = getStations()
|
|
|
|
stations
|
|
|
|
.once('error', nay)
|
|
|
|
.on('data', (s) => {
|
|
|
|
if (
|
|
|
|
s.id === id ||
|
|
|
|
(s.additionalIds && s.additionalIds.includes(id))
|
|
|
|
) {
|
|
|
|
yay(s)
|
|
|
|
stations.destroy()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.once('end', yay)
|
|
|
|
})
|
|
|
|
|
2017-11-12 21:23:29 +01:00
|
|
|
const isJungfernheide = (s) => {
|
|
|
|
return s.type === 'station' &&
|
2018-03-04 20:12:38 +01:00
|
|
|
(s.id === '008011167' || s.id === jungfernh) &&
|
2017-11-12 21:23:29 +01:00
|
|
|
s.name === 'Berlin Jungfernheide' &&
|
2017-12-11 19:25:29 +01:00
|
|
|
s.location &&
|
|
|
|
isRoughlyEqual(s.location.latitude, 52.530408, .0005) &&
|
|
|
|
isRoughlyEqual(s.location.longitude, 13.299424, .0005)
|
2017-11-12 21:23:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const assertIsJungfernheide = (t, s) => {
|
|
|
|
t.equal(s.type, 'station')
|
2018-03-04 20:12:38 +01:00
|
|
|
t.ok(s.id === '008011167' || s.id === jungfernh, 'id should be 8011167')
|
2017-11-12 21:23:29 +01:00
|
|
|
t.equal(s.name, 'Berlin Jungfernheide')
|
2017-12-11 19:25:29 +01:00
|
|
|
t.ok(s.location)
|
|
|
|
t.ok(isRoughlyEqual(s.location.latitude, 52.530408, .0005))
|
|
|
|
t.ok(isRoughlyEqual(s.location.longitude, 13.299424, .0005))
|
2017-11-12 21:23:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 19:40:46 +01:00
|
|
|
// todo: DRY with assertValidStationProducts
|
2018-03-13 03:23:03 +01:00
|
|
|
// todo: DRY with other tests
|
2017-11-13 00:30:14 +01:00
|
|
|
const assertValidProducts = (t, p) => {
|
2018-03-13 03:23:03 +01:00
|
|
|
for (let product of allProducts) {
|
|
|
|
product = product.product // wat
|
|
|
|
t.equal(typeof p[product], 'boolean', 'product ' + p + ' must be a boolean')
|
2017-11-13 00:30:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 16:06:37 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 21:03:24 +01:00
|
|
|
const test = tapePromise(tape)
|
|
|
|
const client = createClient(dbProfile)
|
|
|
|
|
2018-03-04 20:12:38 +01:00
|
|
|
const jungfernh = '8011167'
|
|
|
|
const berlinHbf = '8011160'
|
|
|
|
const münchenHbf = '8000261'
|
|
|
|
const hannoverHbf = '8000152'
|
|
|
|
const regensburgHbf = '8000309'
|
|
|
|
|
2018-01-23 01:49:41 +01:00
|
|
|
test('Berlin Jungfernheide to München Hbf', co(function* (t) {
|
2018-03-04 20:12:38 +01:00
|
|
|
const journeys = yield client.journeys(jungfernh, münchenHbf, {
|
2017-11-12 21:03:24 +01:00
|
|
|
when, passedStations: true
|
|
|
|
})
|
|
|
|
|
|
|
|
t.ok(Array.isArray(journeys))
|
|
|
|
t.ok(journeys.length > 0, 'no journeys')
|
|
|
|
for (let journey of journeys) {
|
2018-03-02 00:35:54 +01:00
|
|
|
t.equal(journey.type, 'journey')
|
|
|
|
|
2017-11-12 21:03:24 +01:00
|
|
|
assertValidStation(t, journey.origin)
|
2017-12-11 14:41:28 +01:00
|
|
|
assertValidStationProducts(t, journey.origin.products)
|
2017-11-29 02:27:31 +01:00
|
|
|
if (!(yield findStation(journey.origin.id))) {
|
2017-11-12 21:03:24 +01:00
|
|
|
console.error('unknown station', journey.origin.id, journey.origin.name)
|
|
|
|
}
|
2017-11-13 00:30:14 +01:00
|
|
|
if (journey.origin.products) {
|
|
|
|
assertValidProducts(t, journey.origin.products)
|
|
|
|
}
|
2017-12-28 22:57:22 +01:00
|
|
|
assertValidWhen(t, journey.departure, when)
|
2017-11-12 21:03:24 +01:00
|
|
|
|
|
|
|
assertValidStation(t, journey.destination)
|
2017-12-11 14:41:28 +01:00
|
|
|
assertValidStationProducts(t, journey.origin.products)
|
2017-11-29 02:27:31 +01:00
|
|
|
if (!(yield findStation(journey.origin.id))) {
|
2017-11-12 21:03:24 +01:00
|
|
|
console.error('unknown station', journey.destination.id, journey.destination.name)
|
|
|
|
}
|
2017-11-13 00:30:14 +01:00
|
|
|
if (journey.destination.products) {
|
|
|
|
assertValidProducts(t, journey.destination.products)
|
|
|
|
}
|
2017-12-28 22:57:22 +01:00
|
|
|
assertValidWhen(t, journey.arrival, when)
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2017-12-28 17:14:53 +01:00
|
|
|
t.ok(Array.isArray(journey.legs))
|
|
|
|
t.ok(journey.legs.length > 0, 'no legs')
|
|
|
|
const leg = journey.legs[0]
|
|
|
|
|
|
|
|
assertValidStation(t, leg.origin)
|
|
|
|
assertValidStationProducts(t, leg.origin.products)
|
|
|
|
if (!(yield findStation(leg.origin.id))) {
|
|
|
|
console.error('unknown station', leg.origin.id, leg.origin.name)
|
2017-11-12 21:03:24 +01:00
|
|
|
}
|
2017-12-28 22:57:22 +01:00
|
|
|
assertValidWhen(t, leg.departure, when)
|
2017-12-28 17:14:53 +01:00
|
|
|
t.equal(typeof leg.departurePlatform, 'string')
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2017-12-28 17:14:53 +01:00
|
|
|
assertValidStation(t, leg.destination)
|
|
|
|
assertValidStationProducts(t, leg.origin.products)
|
|
|
|
if (!(yield findStation(leg.destination.id))) {
|
|
|
|
console.error('unknown station', leg.destination.id, leg.destination.name)
|
2017-11-12 21:03:24 +01:00
|
|
|
}
|
2017-12-28 22:57:22 +01:00
|
|
|
assertValidWhen(t, leg.arrival, when)
|
2017-12-28 17:14:53 +01:00
|
|
|
t.equal(typeof leg.arrivalPlatform, 'string')
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2017-12-28 17:14:53 +01:00
|
|
|
assertValidLine(t, leg.line)
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2017-12-28 17:14:53 +01:00
|
|
|
t.ok(Array.isArray(leg.passed))
|
|
|
|
for (let stopover of leg.passed) assertValidStopover(t, stopover)
|
2017-12-11 16:06:37 +01:00
|
|
|
|
|
|
|
if (journey.price) assertValidPrice(t, journey.price)
|
2017-11-12 21:03:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
t.end()
|
2017-11-29 02:27:31 +01:00
|
|
|
}))
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2018-01-23 01:49:41 +01:00
|
|
|
test('Berlin Jungfernheide to Torfstraße 17', co(function* (t) {
|
2018-03-04 20:12:38 +01:00
|
|
|
const journeys = yield client.journeys(jungfernh, {
|
2017-12-12 23:24:55 +01:00
|
|
|
type: 'location', address: 'Torfstraße 17',
|
2017-11-12 21:03:24 +01:00
|
|
|
latitude: 52.5416823, longitude: 13.3491223
|
|
|
|
}, {when})
|
|
|
|
|
|
|
|
t.ok(Array.isArray(journeys))
|
|
|
|
t.ok(journeys.length >= 1, 'no journeys')
|
|
|
|
const journey = journeys[0]
|
2017-12-28 17:14:53 +01:00
|
|
|
const leg = journey.legs[journey.legs.length - 1]
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2017-12-28 17:14:53 +01:00
|
|
|
assertValidStation(t, leg.origin)
|
|
|
|
assertValidStationProducts(t, leg.origin.products)
|
|
|
|
if (!(yield findStation(leg.origin.id))) {
|
|
|
|
console.error('unknown station', leg.origin.id, leg.origin.name)
|
2017-11-12 21:03:24 +01:00
|
|
|
}
|
2017-12-28 17:14:53 +01:00
|
|
|
if (leg.origin.products) assertValidProducts(t, leg.origin.products)
|
2017-12-28 22:57:22 +01:00
|
|
|
assertValidWhen(t, leg.departure, when)
|
|
|
|
assertValidWhen(t, leg.arrival, when)
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2017-12-28 17:14:53 +01:00
|
|
|
const d = leg.destination
|
2017-11-12 21:03:24 +01:00
|
|
|
assertValidAddress(t, d)
|
2017-12-11 19:40:46 +01:00
|
|
|
t.equal(d.address, 'Torfstraße 17')
|
2017-12-11 19:25:29 +01:00
|
|
|
t.ok(isRoughlyEqual(.0001, d.latitude, 52.5416823))
|
|
|
|
t.ok(isRoughlyEqual(.0001, d.longitude, 13.3491223))
|
2017-11-12 21:03:24 +01:00
|
|
|
|
|
|
|
t.end()
|
2017-11-29 02:27:31 +01:00
|
|
|
}))
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2018-01-23 01:49:41 +01:00
|
|
|
test('Berlin Jungfernheide to ATZE Musiktheater', co(function* (t) {
|
2018-03-04 20:12:38 +01:00
|
|
|
const journeys = yield client.journeys(jungfernh, {
|
2017-12-12 23:24:55 +01:00
|
|
|
type: 'location', id: '991598902', name: 'ATZE Musiktheater',
|
2017-11-12 21:03:24 +01:00
|
|
|
latitude: 52.542417, longitude: 13.350437
|
|
|
|
}, {when})
|
|
|
|
|
|
|
|
t.ok(Array.isArray(journeys))
|
|
|
|
t.ok(journeys.length >= 1, 'no journeys')
|
|
|
|
const journey = journeys[0]
|
2017-12-28 17:14:53 +01:00
|
|
|
const leg = journey.legs[journey.legs.length - 1]
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2017-12-28 17:14:53 +01:00
|
|
|
assertValidStation(t, leg.origin)
|
|
|
|
assertValidStationProducts(t, leg.origin.products)
|
|
|
|
if (!(yield findStation(leg.origin.id))) {
|
|
|
|
console.error('unknown station', leg.origin.id, leg.origin.name)
|
2017-11-12 21:03:24 +01:00
|
|
|
}
|
2017-12-28 17:14:53 +01:00
|
|
|
if (leg.origin.products) assertValidProducts(t, leg.origin.products)
|
2017-12-28 22:57:22 +01:00
|
|
|
assertValidWhen(t, leg.departure, when)
|
|
|
|
assertValidWhen(t, leg.arrival, when)
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2017-12-28 17:14:53 +01:00
|
|
|
const d = leg.destination
|
2017-11-12 21:03:24 +01:00
|
|
|
assertValidPoi(t, d)
|
|
|
|
t.equal(d.name, 'ATZE Musiktheater')
|
2017-12-11 19:25:29 +01:00
|
|
|
t.ok(isRoughlyEqual(.0001, d.latitude, 52.542399))
|
|
|
|
t.ok(isRoughlyEqual(.0001, d.longitude, 13.350402))
|
2017-11-12 21:03:24 +01:00
|
|
|
|
|
|
|
t.end()
|
2017-11-29 02:27:31 +01:00
|
|
|
}))
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2018-01-23 01:49:41 +01:00
|
|
|
test('Berlin Hbf to München Hbf with stopover at Hannover Hbf', co(function* (t) {
|
2018-01-19 16:58:39 +01:00
|
|
|
const [journey] = yield client.journeys(berlinHbf, münchenHbf, {
|
|
|
|
via: hannoverHbf,
|
|
|
|
results: 1
|
|
|
|
})
|
|
|
|
|
|
|
|
const i = journey.legs.findIndex(leg => leg.destination.id === hannoverHbf)
|
|
|
|
t.ok(i >= 0, 'no leg with Hannover Hbf as destination')
|
|
|
|
|
|
|
|
const nextLeg = journey.legs[i + 1]
|
|
|
|
t.ok(nextLeg)
|
|
|
|
t.equal(nextLeg.origin.id, hannoverHbf)
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
}))
|
|
|
|
|
2018-03-04 20:12:38 +01:00
|
|
|
test('earlier/later journeys, Jungfernheide -> München Hbf', co(function* (t) {
|
|
|
|
const model = yield client.journeys(jungfernh, münchenHbf, {
|
|
|
|
results: 3, when
|
|
|
|
})
|
|
|
|
|
2018-03-04 20:38:50 +01:00
|
|
|
t.equal(typeof model.earlierRef, 'string')
|
|
|
|
t.ok(model.earlierRef)
|
|
|
|
t.equal(typeof model.laterRef, 'string')
|
|
|
|
t.ok(model.laterRef)
|
2018-03-04 20:12:38 +01:00
|
|
|
|
2018-03-04 20:41:26 +01:00
|
|
|
// when and earlierThan/laterThan should be mutually exclusive
|
|
|
|
t.throws(() => {
|
|
|
|
client.journeys(jungfernh, münchenHbf, {
|
|
|
|
when, earlierThan: model.earlierRef
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.throws(() => {
|
|
|
|
client.journeys(jungfernh, münchenHbf, {
|
|
|
|
when, laterThan: model.laterRef
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-03-04 20:12:38 +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(jungfernh, münchenHbf, {
|
|
|
|
results: 3,
|
|
|
|
// todo: single journey ref?
|
2018-03-04 20:41:26 +01:00
|
|
|
earlierThan: model.earlierRef
|
2018-03-04 20:12:38 +01:00
|
|
|
})
|
|
|
|
for (let j of earlier) {
|
|
|
|
t.ok(new Date(j.departure) < earliestDep)
|
|
|
|
}
|
|
|
|
|
|
|
|
const later = yield client.journeys(jungfernh, münchenHbf, {
|
|
|
|
results: 3,
|
|
|
|
// todo: single journey ref?
|
2018-03-04 20:41:26 +01:00
|
|
|
laterThan: model.laterRef
|
2018-03-04 20:12:38 +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('departures at Berlin Jungfernheide', co(function* (t) {
|
2018-03-04 20:12:38 +01:00
|
|
|
const deps = yield client.departures(jungfernh, {
|
2017-11-12 21:03:24 +01:00
|
|
|
duration: 5, when
|
|
|
|
})
|
|
|
|
|
|
|
|
t.ok(Array.isArray(deps))
|
|
|
|
for (let dep of deps) {
|
|
|
|
assertValidStation(t, dep.station)
|
2017-12-11 14:41:28 +01:00
|
|
|
assertValidStationProducts(t, dep.station.products)
|
2017-11-29 02:27:31 +01:00
|
|
|
if (!(yield findStation(dep.station.id))) {
|
2017-11-12 21:03:24 +01:00
|
|
|
console.error('unknown station', dep.station.id, dep.station.name)
|
|
|
|
}
|
2017-11-13 00:30:14 +01:00
|
|
|
if (dep.station.products) assertValidProducts(t, dep.station.products)
|
2017-12-28 22:57:22 +01:00
|
|
|
assertValidWhen(t, dep.when, when)
|
2017-11-12 21:03:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
t.end()
|
2017-11-29 02:27:31 +01:00
|
|
|
}))
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2018-01-23 01:49:41 +01:00
|
|
|
test('departures with station object', co(function* (t) {
|
2018-01-04 16:19:42 +01:00
|
|
|
yield client.departures({
|
|
|
|
type: 'station',
|
2018-03-04 20:12:38 +01:00
|
|
|
id: jungfernh,
|
2018-01-04 16:19:42 +01:00
|
|
|
name: 'Berlin Jungfernheide',
|
|
|
|
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('nearby Berlin Jungfernheide', co(function* (t) {
|
2018-01-05 14:53:03 +01:00
|
|
|
const nearby = yield client.nearby({
|
|
|
|
type: 'location',
|
|
|
|
latitude: 52.530273,
|
|
|
|
longitude: 13.299433
|
|
|
|
}, {
|
2017-11-12 21:03:24 +01:00
|
|
|
results: 2, distance: 400
|
|
|
|
})
|
|
|
|
|
|
|
|
t.ok(Array.isArray(nearby))
|
|
|
|
t.equal(nearby.length, 2)
|
|
|
|
|
|
|
|
assertIsJungfernheide(t, nearby[0])
|
|
|
|
t.ok(nearby[0].distance >= 0)
|
|
|
|
t.ok(nearby[0].distance <= 100)
|
|
|
|
|
2017-12-11 19:53:26 +01:00
|
|
|
for (let n of nearby) {
|
|
|
|
if (n.type === 'station') assertValidStation(t, n)
|
|
|
|
else assertValidLocation(t, n)
|
|
|
|
}
|
|
|
|
|
2017-11-12 21:03:24 +01:00
|
|
|
t.end()
|
2017-11-29 02:27:31 +01:00
|
|
|
}))
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2018-01-23 01:49:41 +01:00
|
|
|
test('locations named Jungfernheide', co(function* (t) {
|
2017-11-29 02:27:31 +01:00
|
|
|
const locations = yield client.locations('Jungfernheide', {
|
2017-11-12 21:03:24 +01:00
|
|
|
results: 10
|
|
|
|
})
|
|
|
|
|
|
|
|
t.ok(Array.isArray(locations))
|
|
|
|
t.ok(locations.length > 0)
|
|
|
|
t.ok(locations.length <= 10)
|
|
|
|
|
2017-12-11 19:53:26 +01:00
|
|
|
for (let l of locations) {
|
|
|
|
if (l.type === 'station') assertValidStation(t, l)
|
|
|
|
else assertValidLocation(t, l)
|
|
|
|
}
|
2017-11-12 21:23:29 +01:00
|
|
|
t.ok(locations.some(isJungfernheide))
|
2017-11-12 21:03:24 +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(regensburgHbf)
|
|
|
|
|
|
|
|
assertValidStation(t, loc)
|
|
|
|
t.equal(loc.id, regensburgHbf)
|
|
|
|
|
2018-01-26 17:40:14 +01:00
|
|
|
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()
|
|
|
|
}))
|