2017-11-12 21:03:24 +01:00
|
|
|
|
'use strict'
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const stations = require('db-stations/full.json')
|
|
|
|
|
const a = require('assert')
|
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-04-19 14:55:17 +02:00
|
|
|
|
const {createWhen} = require('./lib/util')
|
2018-04-18 16:15:24 +02:00
|
|
|
|
const co = require('./lib/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-04-19 14:55:17 +02:00
|
|
|
|
const products = require('../p/db/products')
|
2017-11-12 21:03:24 +01:00
|
|
|
|
const {
|
2018-04-19 14:55:17 +02:00
|
|
|
|
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-19 14:55:17 +02:00
|
|
|
|
|
|
|
|
|
const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o)
|
2017-11-12 21:03:24 +01:00
|
|
|
|
|
2017-12-28 22:57:22 +01:00
|
|
|
|
const when = createWhen('Europe/Berlin', 'de-DE')
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const cfg = {
|
|
|
|
|
when,
|
|
|
|
|
stationCoordsOptional: false,
|
|
|
|
|
products
|
2017-12-11 14:41:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const _validateStation = createValidateStation(cfg)
|
|
|
|
|
const validateStation = (validate, s, name) => {
|
|
|
|
|
_validateStation(validate, s, name)
|
|
|
|
|
const match = stations.some(station => (
|
|
|
|
|
station.id === s.id ||
|
|
|
|
|
(station.additionalIds && station.additionalIds.includes(s.id))
|
|
|
|
|
))
|
|
|
|
|
if (!match) {
|
|
|
|
|
console.error(name + `.id: unknown ID "${s.id}"`)
|
|
|
|
|
}
|
2017-11-12 21:23:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const validate = createValidate(cfg, {
|
|
|
|
|
station: validateStation
|
|
|
|
|
})
|
2017-11-12 21:23:29 +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 berlinHbf = '8011160'
|
|
|
|
|
const münchenHbf = '8000261'
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const jungfernheide = '8011167'
|
2018-04-23 13:56:00 +02:00
|
|
|
|
const blnSchwedterStr = '732652'
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const atze = '991598902'
|
|
|
|
|
const westhafen = '008089116'
|
|
|
|
|
const wedding = '008089131'
|
|
|
|
|
const württembergallee = '731084'
|
2018-03-04 20:12:38 +01:00
|
|
|
|
const regensburgHbf = '8000309'
|
|
|
|
|
|
2018-04-23 13:56:00 +02:00
|
|
|
|
test('journeys – Berlin Schwedter Str. to München Hbf', co(function* (t) {
|
|
|
|
|
const journeys = yield client.journeys(blnSchwedterStr, münchenHbf, {
|
|
|
|
|
results: 3, when, passedStations: true
|
2017-11-12 21:03:24 +01:00
|
|
|
|
})
|
|
|
|
|
|
2018-04-23 13:56:00 +02:00
|
|
|
|
yield testJourneysStationToStation({
|
|
|
|
|
test: t,
|
|
|
|
|
journeys,
|
|
|
|
|
validate,
|
|
|
|
|
fromId: blnSchwedterStr,
|
|
|
|
|
toId: münchenHbf
|
|
|
|
|
})
|
|
|
|
|
// todo: find a journey where there pricing info is always available
|
2017-11-12 21:03:24 +01:00
|
|
|
|
for (let journey of journeys) {
|
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-04-19 14:55:17 +02:00
|
|
|
|
// todo: journeys, only one product
|
|
|
|
|
// todo: journeys, fails with no product
|
|
|
|
|
|
2018-01-23 01:49:41 +01:00
|
|
|
|
test('Berlin Jungfernheide to Torfstraße 17', co(function* (t) {
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const latitude = 52.5416823
|
|
|
|
|
const longitude = 13.3491223
|
|
|
|
|
const journeys = yield client.journeys(jungfernheide, {
|
2017-12-12 23:24:55 +01:00
|
|
|
|
type: 'location', address: 'Torfstraße 17',
|
2018-04-19 14:55:17 +02:00
|
|
|
|
latitude, longitude
|
2017-11-12 21:03:24 +01:00
|
|
|
|
}, {when})
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
validate(t, journeys, 'journeys', 'journeys')
|
2017-11-12 21:03:24 +01:00
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const i = journeys[0].legs.length - 1
|
|
|
|
|
const d = journeys[0].legs[i].destination
|
|
|
|
|
const name = `journeys[0].legs[${i}].destination`
|
|
|
|
|
t.equal(d.address, 'Torfstraße 17', name + '.address is invalid')
|
|
|
|
|
t.ok(isRoughlyEqual(.0001, d.latitude, latitude), name + '.latitude is invalid')
|
|
|
|
|
t.ok(isRoughlyEqual(.0001, d.longitude, longitude), name + '.longitude is invalid')
|
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-04-19 14:55:17 +02:00
|
|
|
|
const latitude = 52.542417
|
|
|
|
|
const longitude = 13.350437
|
|
|
|
|
const journeys = yield client.journeys(jungfernheide, {
|
|
|
|
|
type: 'location', id: atze, name: 'ATZE Musiktheater',
|
|
|
|
|
latitude, longitude
|
2017-11-12 21:03:24 +01:00
|
|
|
|
}, {when})
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
validate(t, journeys, 'journeys', 'journeys')
|
2017-11-12 21:03:24 +01:00
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const i = journeys[0].legs.length - 1
|
|
|
|
|
const d = journeys[0].legs[i].destination
|
|
|
|
|
const name = `journeys[0].legs[${i}].destination`
|
|
|
|
|
t.equal(d.name, 'ATZE Musiktheater', name + '.name is invalid')
|
|
|
|
|
t.ok(isRoughlyEqual(.0001, d.latitude, latitude), name + '.latitude is invalid')
|
|
|
|
|
t.ok(isRoughlyEqual(.0001, d.longitude, longitude), name + '.longitude is invalid')
|
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-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.
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const journeys = yield client.journeys(westhafen, wedding, {
|
2018-03-06 02:47:05 +01:00
|
|
|
|
via: württembergallee,
|
|
|
|
|
results: 1,
|
2018-03-16 14:30:49 +01:00
|
|
|
|
when,
|
|
|
|
|
passedStations: true
|
2018-01-19 16:58:39 +01:00
|
|
|
|
})
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
validate(t, journeys, 'journeys', 'journeys')
|
2018-03-06 02:47:05 +01:00
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const leg = journeys[0].legs.some((leg) => {
|
|
|
|
|
return leg.passed && leg.passed.some((passed) => {
|
|
|
|
|
return passed.station.id === württembergallee
|
|
|
|
|
})
|
2018-03-06 02:47:05 +01:00
|
|
|
|
})
|
2018-04-19 14:55:17 +02:00
|
|
|
|
t.ok(leg, 'Württembergalle is not being passed')
|
2018-03-06 02:47:05 +01:00
|
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
}))
|
|
|
|
|
|
2018-03-04 20:12:38 +01:00
|
|
|
|
test('earlier/later journeys, Jungfernheide -> München Hbf', co(function* (t) {
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const model = yield client.journeys(jungfernheide, münchenHbf, {
|
2018-03-04 20:12:38 +01:00
|
|
|
|
results: 3, when
|
|
|
|
|
})
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
// todo: move to journeys validator?
|
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(() => {
|
2018-04-19 14:55:17 +02:00
|
|
|
|
client.journeys(jungfernheide, münchenHbf, {
|
2018-03-04 20:41:26 +01:00
|
|
|
|
when, earlierThan: model.earlierRef
|
|
|
|
|
})
|
2018-04-19 14:55:17 +02:00
|
|
|
|
// silence rejections, we're only interested in exceptions
|
|
|
|
|
.catch(() => {})
|
2018-03-04 20:41:26 +01:00
|
|
|
|
})
|
|
|
|
|
t.throws(() => {
|
2018-04-19 14:55:17 +02:00
|
|
|
|
client.journeys(jungfernheide, münchenHbf, {
|
2018-03-04 20:41:26 +01:00
|
|
|
|
when, laterThan: model.laterRef
|
|
|
|
|
})
|
2018-04-19 14:55:17 +02:00
|
|
|
|
// silence rejections, we're only interested in exceptions
|
|
|
|
|
.catch(() => {})
|
2018-03-04 20:41:26 +01:00
|
|
|
|
})
|
|
|
|
|
|
2018-03-04 20:12:38 +01:00
|
|
|
|
let earliestDep = Infinity, latestDep = -Infinity
|
|
|
|
|
for (let j of model) {
|
2018-03-21 01:53:15 +01:00
|
|
|
|
const dep = +new Date(j.legs[0].departure)
|
2018-03-04 20:12:38 +01:00
|
|
|
|
if (dep < earliestDep) earliestDep = dep
|
|
|
|
|
else if (dep > latestDep) latestDep = dep
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const earlier = yield client.journeys(jungfernheide, münchenHbf, {
|
2018-03-04 20:12:38 +01:00
|
|
|
|
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) {
|
2018-03-21 01:53:15 +01:00
|
|
|
|
t.ok(new Date(j.legs[0].departure) < earliestDep)
|
2018-03-04 20:12:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const later = yield client.journeys(jungfernheide, münchenHbf, {
|
2018-03-04 20:12:38 +01:00
|
|
|
|
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) {
|
2018-03-21 01:53:15 +01:00
|
|
|
|
t.ok(new Date(j.legs[0].departure) > latestDep)
|
2018-03-04 20:12:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
}))
|
|
|
|
|
|
2018-04-19 14:55:51 +02:00
|
|
|
|
test('journey leg details', co(function* (t) {
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const journeys = yield client.journeys(berlinHbf, münchenHbf, {
|
|
|
|
|
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})
|
|
|
|
|
|
|
|
|
|
validate(t, leg, 'journeyLeg', 'leg')
|
|
|
|
|
t.end()
|
|
|
|
|
}))
|
|
|
|
|
|
2018-01-23 01:49:41 +01:00
|
|
|
|
test('departures at Berlin Jungfernheide', co(function* (t) {
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const deps = yield client.departures(jungfernheide, {
|
2017-11-12 21:03:24 +01:00
|
|
|
|
duration: 5, when
|
|
|
|
|
})
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
validate(t, deps, 'departures', 'departures')
|
|
|
|
|
for (let i = 0; i < deps.length; i++) {
|
|
|
|
|
const dep = deps[i]
|
|
|
|
|
const name = `deps[${i}]`
|
|
|
|
|
// todo: make this pass
|
|
|
|
|
// t.equal(dep.station.id, jungfernheide, name + '.station.id is invalid')
|
2017-11-12 21:03:24 +01:00
|
|
|
|
}
|
2018-04-19 14:55:17 +02:00
|
|
|
|
// todo: move into deps validator
|
|
|
|
|
t.deepEqual(deps, deps.sort((a, b) => t.when > b.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-04-19 14:55:17 +02:00
|
|
|
|
const deps = yield client.departures({
|
2018-01-04 16:19:42 +01:00
|
|
|
|
type: 'station',
|
2018-04-19 14:55:17 +02:00
|
|
|
|
id: jungfernheide,
|
2018-01-04 16:19:42 +01:00
|
|
|
|
name: 'Berlin Jungfernheide',
|
|
|
|
|
location: {
|
|
|
|
|
type: 'location',
|
|
|
|
|
latitude: 1.23,
|
|
|
|
|
longitude: 2.34
|
|
|
|
|
}
|
|
|
|
|
}, {when})
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
validate(t, deps, 'departures', 'departures')
|
2018-01-04 16:19:42 +01:00
|
|
|
|
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
|
|
|
|
|
})
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
validate(t, nearby, 'locations', 'nearby')
|
|
|
|
|
|
2017-11-12 21:03:24 +01:00
|
|
|
|
t.equal(nearby.length, 2)
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const s0 = nearby[0]
|
|
|
|
|
// todo: trim IDs
|
|
|
|
|
t.ok(s0.id === '008011167' || s0.id === jungfernheide)
|
|
|
|
|
t.equal(s0.name, 'Berlin Jungfernheide')
|
|
|
|
|
t.ok(isRoughlyEqual(s0.location.latitude, 52.530408, .0005))
|
|
|
|
|
t.ok(isRoughlyEqual(s0.location.longitude, 13.299424, .0005))
|
|
|
|
|
t.ok(s0.distance >= 0)
|
|
|
|
|
t.ok(s0.distance <= 100)
|
2017-12-11 19:53:26 +01:00
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
// todo: nearby[0]
|
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
|
|
|
|
|
})
|
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
validate(t, locations, 'locations', 'locations')
|
2017-11-12 21:03:24 +01:00
|
|
|
|
t.ok(locations.length <= 10)
|
2018-04-19 14:55:17 +02:00
|
|
|
|
t.ok(locations.some((l) => {
|
|
|
|
|
// todo: trim IDs
|
|
|
|
|
return l.id === '008011167' || l.id === jungfernheide
|
|
|
|
|
}), 'Jungfernheide not found')
|
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) {
|
2018-04-19 14:55:17 +02:00
|
|
|
|
const s = yield client.location(regensburgHbf)
|
2018-01-26 17:08:07 +01:00
|
|
|
|
|
2018-04-19 14:55:17 +02:00
|
|
|
|
validate(t, s, 'station', 'station')
|
|
|
|
|
t.equal(s.id, regensburgHbf)
|
2018-01-26 17:40:14 +01:00
|
|
|
|
|
2018-01-26 17:08:07 +01:00
|
|
|
|
t.end()
|
|
|
|
|
}))
|