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')
|
2017-11-29 02:27:31 +01:00
|
|
|
const co = require('co')
|
2017-11-12 21:03:24 +01:00
|
|
|
const isRoughlyEqual = require('is-roughly-equal')
|
|
|
|
|
|
|
|
const createClient = require('..')
|
2017-11-12 23:51:39 +01:00
|
|
|
const dbProfile = require('../p/db')
|
2017-11-13 00:30:14 +01:00
|
|
|
const modes = require('../p/db/modes')
|
2017-11-12 21:03:24 +01:00
|
|
|
const {
|
|
|
|
assertValidStation,
|
|
|
|
assertValidPoi,
|
|
|
|
assertValidAddress,
|
|
|
|
assertValidLocation,
|
|
|
|
assertValidLine,
|
|
|
|
assertValidStopover,
|
2017-12-11 15:20:50 +01:00
|
|
|
when, isValidWhen // todo: timezone
|
2017-11-12 21:03:24 +01:00
|
|
|
} = require('./util.js')
|
|
|
|
|
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' &&
|
|
|
|
(s.id === '008011167' || s.id === '8011167') &&
|
|
|
|
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')
|
|
|
|
t.ok(s.id === '008011167' || s.id === '8011167', 'id should be 8011167')
|
|
|
|
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-11-13 00:30:14 +01:00
|
|
|
const assertValidProducts = (t, p) => {
|
|
|
|
for (let k of Object.keys(modes)) {
|
|
|
|
t.ok('boolean', typeof modes[k], 'mode ' + k + ' must be a boolean')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2017-11-29 02:27:31 +01:00
|
|
|
test('Berlin Jungfernheide to München Hbf', co.wrap(function* (t) {
|
|
|
|
const journeys = yield client.journeys('8011167', '8000261', {
|
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) {
|
|
|
|
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-11-12 21:03:24 +01:00
|
|
|
t.ok(isValidWhen(journey.departure))
|
|
|
|
|
|
|
|
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-11-12 21:03:24 +01:00
|
|
|
t.ok(isValidWhen(journey.arrival))
|
|
|
|
|
|
|
|
t.ok(Array.isArray(journey.parts))
|
|
|
|
t.ok(journey.parts.length > 0, 'no parts')
|
|
|
|
const part = journey.parts[0]
|
|
|
|
|
|
|
|
assertValidStation(t, part.origin)
|
2017-12-11 14:41:28 +01:00
|
|
|
assertValidStationProducts(t, part.origin.products)
|
2017-11-29 02:27:31 +01:00
|
|
|
if (!(yield findStation(part.origin.id))) {
|
2017-11-12 21:03:24 +01:00
|
|
|
console.error('unknown station', part.origin.id, part.origin.name)
|
|
|
|
}
|
|
|
|
t.ok(isValidWhen(part.departure))
|
|
|
|
t.equal(typeof part.departurePlatform, 'string')
|
|
|
|
|
|
|
|
assertValidStation(t, part.destination)
|
2017-12-11 14:41:28 +01:00
|
|
|
assertValidStationProducts(t, part.origin.products)
|
2017-11-29 02:27:31 +01:00
|
|
|
if (!(yield findStation(part.destination.id))) {
|
2017-11-12 21:03:24 +01:00
|
|
|
console.error('unknown station', part.destination.id, part.destination.name)
|
|
|
|
}
|
|
|
|
t.ok(isValidWhen(part.arrival))
|
|
|
|
t.equal(typeof part.arrivalPlatform, 'string')
|
|
|
|
|
|
|
|
assertValidLine(t, part.line)
|
|
|
|
|
|
|
|
t.ok(Array.isArray(part.passed))
|
|
|
|
for (let stopover of part.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
|
|
|
|
2017-11-29 02:27:31 +01:00
|
|
|
test('Berlin Jungfernheide to Torfstraße 17', co.wrap(function* (t) {
|
|
|
|
const journeys = yield client.journeys('8011167', {
|
2017-11-12 21:03:24 +01:00
|
|
|
type: 'address', name: 'Torfstraße 17',
|
|
|
|
latitude: 52.5416823, longitude: 13.3491223
|
|
|
|
}, {when})
|
|
|
|
|
|
|
|
t.ok(Array.isArray(journeys))
|
|
|
|
t.ok(journeys.length >= 1, 'no journeys')
|
|
|
|
const journey = journeys[0]
|
|
|
|
const part = journey.parts[journey.parts.length - 1]
|
|
|
|
|
|
|
|
assertValidStation(t, part.origin)
|
2017-12-11 14:41:28 +01:00
|
|
|
assertValidStationProducts(t, part.origin.products)
|
2017-11-29 02:27:31 +01:00
|
|
|
if (!(yield findStation(part.origin.id))) {
|
2017-11-12 21:03:24 +01:00
|
|
|
console.error('unknown station', part.origin.id, part.origin.name)
|
|
|
|
}
|
2017-11-13 00:30:14 +01:00
|
|
|
if (part.origin.products) assertValidProducts(t, part.origin.products)
|
2017-11-12 21:03:24 +01:00
|
|
|
t.ok(isValidWhen(part.departure))
|
|
|
|
t.ok(isValidWhen(part.arrival))
|
|
|
|
|
|
|
|
const d = part.destination
|
|
|
|
assertValidAddress(t, d)
|
|
|
|
t.equal(d.name, '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
|
|
|
|
2017-11-29 02:27:31 +01:00
|
|
|
test('Berlin Jungfernheide to ATZE Musiktheater', co.wrap(function* (t) {
|
|
|
|
const journeys = yield client.journeys('8011167', {
|
2017-11-12 21:03:24 +01:00
|
|
|
type: 'poi', name: 'ATZE Musiktheater', id: '991598902',
|
|
|
|
latitude: 52.542417, longitude: 13.350437
|
|
|
|
}, {when})
|
|
|
|
|
|
|
|
t.ok(Array.isArray(journeys))
|
|
|
|
t.ok(journeys.length >= 1, 'no journeys')
|
|
|
|
const journey = journeys[0]
|
|
|
|
const part = journey.parts[journey.parts.length - 1]
|
|
|
|
|
|
|
|
assertValidStation(t, part.origin)
|
2017-12-11 14:41:28 +01:00
|
|
|
assertValidStationProducts(t, part.origin.products)
|
2017-11-29 02:27:31 +01:00
|
|
|
if (!(yield findStation(part.origin.id))) {
|
2017-11-12 21:03:24 +01:00
|
|
|
console.error('unknown station', part.origin.id, part.origin.name)
|
|
|
|
}
|
2017-11-13 00:30:14 +01:00
|
|
|
if (part.origin.products) assertValidProducts(t, part.origin.products)
|
2017-11-12 21:03:24 +01:00
|
|
|
t.ok(isValidWhen(part.departure))
|
|
|
|
t.ok(isValidWhen(part.arrival))
|
|
|
|
|
|
|
|
const d = part.destination
|
|
|
|
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
|
|
|
|
2017-11-29 02:27:31 +01:00
|
|
|
test('departures at Berlin Jungfernheide', co.wrap(function* (t) {
|
|
|
|
const deps = yield client.departures('8011167', {
|
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-11-12 21:03:24 +01:00
|
|
|
t.ok(isValidWhen(dep.when))
|
|
|
|
}
|
|
|
|
|
|
|
|
t.end()
|
2017-11-29 02:27:31 +01:00
|
|
|
}))
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2017-11-29 02:27:31 +01:00
|
|
|
test('nearby Berlin Jungfernheide', co.wrap(function* (t) {
|
|
|
|
const nearby = yield client.nearby(52.530273, 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)
|
|
|
|
|
|
|
|
t.end()
|
2017-11-29 02:27:31 +01:00
|
|
|
}))
|
2017-11-12 21:03:24 +01:00
|
|
|
|
2017-11-29 02:27:31 +01:00
|
|
|
test('locations named Jungfernheide', co.wrap(function* (t) {
|
|
|
|
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)
|
|
|
|
|
|
|
|
for (let location of locations) assertValidLocation(t, location)
|
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
|
|
|
}))
|