db-vendo-client/test/e2e/cfl.js

272 lines
6.3 KiB
JavaScript
Raw Normal View History

2018-06-10 20:44:12 +02:00
'use strict'
2021-05-20 16:42:43 +01:00
const tap = require('tap')
2018-06-10 20:44:12 +02:00
const assert = require('assert')
const isRoughlyEqual = require('is-roughly-equal')
const {createWhen} = require('./lib/util')
2019-08-30 18:31:39 +02:00
const createClient = require('../..')
const cflProfile = require('../../p/cfl')
const products = require('../../p/cfl/products')
2018-06-10 20:44:12 +02:00
const {
2018-06-11 20:50:35 +02:00
line: createValidateLine,
journeyLeg: createValidateJourneyLeg,
2018-06-10 20:44:12 +02:00
movement: _validateMovement
} = require('./lib/validators')
const createValidate = require('./lib/validate-fptf-with')
const testJourneysStationToStation = require('./lib/journeys-station-to-station')
const testJourneysStationToAddress = require('./lib/journeys-station-to-address')
const testJourneysStationToPoi = require('./lib/journeys-station-to-poi')
const testEarlierLaterJourneys = require('./lib/earlier-later-journeys')
const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product')
const testDepartures = require('./lib/departures')
const testArrivals = require('./lib/arrivals')
const T_MOCK = 1641897000 * 1000 // 2022-01-11T11:30:00+01
const when = createWhen(cflProfile.timezone, cflProfile.locale, T_MOCK)
2018-06-10 20:44:12 +02:00
const cfg = {
when,
products,
minLatitude: 47.24,
maxLatitude: 52.9,
minLongitude: -0.63,
maxLongitude: 14.07
}
2018-06-11 20:50:35 +02:00
const _validateLine = createValidateLine(cfg)
const validateLine = (validate, l, name) => {
if (!l.direction) l = Object.assign({}, l, {direction: 'foo'})
_validateLine(validate, l, name)
}
const _validateJourneyLeg = createValidateJourneyLeg(cfg)
const validateJourneyLeg = (validate, l, name) => {
if (!l.direction) l = Object.assign({}, l, {direction: 'foo'})
_validateJourneyLeg(validate, l, name)
}
2018-06-10 20:44:12 +02:00
const validateMovement = (val, m, name = 'movement') => {
// todo: fix this upstream
const withFakeLocation = Object.assign({}, m)
withFakeLocation.location = Object.assign({}, m.location, {
latitude: 50,
longitude: 12
})
_validateMovement(val, withFakeLocation, name)
assert.ok(m.location.latitude <= 55, name + '.location.latitude is too small')
assert.ok(m.location.latitude >= 45, name + '.location.latitude is too large')
assert.ok(m.location.longitude >= 1, name + '.location.longitude is too small')
assert.ok(m.location.longitude <= 11, name + '.location.longitude is too small')
}
const validate = createValidate(cfg, {
2018-06-11 20:50:35 +02:00
line: validateLine,
journeyLeg: validateJourneyLeg,
2018-06-10 20:44:12 +02:00
movement: validateMovement
})
const client = createClient(cflProfile, 'public-transport/hafas-client:test')
const ettelbruck = '9258199'
const mersch = '9864348'
2021-08-24 00:43:20 +02:00
const luxembourgGareCentrale = '200405059'
2018-06-10 20:44:12 +02:00
2021-05-20 16:42:43 +01:00
tap.test('journeys  Ettelbruck to Luxembourg', async (t) => {
2021-08-24 00:43:20 +02:00
const res = await client.journeys(ettelbruck, luxembourgGareCentrale, {
2018-06-10 20:44:12 +02:00
results: 4,
departure: when,
stopovers: true
})
await testJourneysStationToStation({
test: t,
res,
validate,
fromId: ettelbruck,
2021-08-24 00:43:20 +02:00
toId: luxembourgGareCentrale
2018-06-10 20:44:12 +02:00
})
t.end()
})
// todo: journeys, only one product
2021-12-29 18:53:50 +01:00
tap.test('journeys fails with no product', async (t) => {
await journeysFailsWithNoProduct({
2018-06-10 20:44:12 +02:00
test: t,
fetchJourneys: client.journeys,
fromId: ettelbruck,
2021-08-24 00:43:20 +02:00
toId: luxembourgGareCentrale,
2018-06-10 20:44:12 +02:00
when,
products
})
t.end()
})
2021-05-20 16:42:43 +01:00
tap.test('Luxembourg to 9071 Ettelbruck, Rue des Romains 4', async (t) => {
2018-06-10 20:44:12 +02:00
const rueDeRomain = {
type: 'location',
address: '9071 Ettelbruck, Rue des Romains 4',
latitude: 49.847469,
longitude: 6.097608
}
2021-08-24 00:43:20 +02:00
const res = await client.journeys(luxembourgGareCentrale, rueDeRomain, {
2018-06-10 20:44:12 +02:00
results: 3,
departure: when
})
await testJourneysStationToAddress({
test: t,
res,
validate,
2021-08-24 00:43:20 +02:00
fromId: luxembourgGareCentrale,
2018-06-10 20:44:12 +02:00
to: rueDeRomain
})
t.end()
})
2021-08-24 00:43:20 +02:00
// Each journey's last leg has a destination without the ID.
tap.skip('Luxembourg to Centre Hospitalier du Nord', async (t) => {
2018-06-10 20:44:12 +02:00
const centreHospitalier = {
type: 'location',
id: '140701020',
poi: true,
name: 'Ettelbruck, Centre Hospitalier du Nord',
latitude: 49.853096,
longitude: 6.094075
}
2021-08-24 00:43:20 +02:00
const res = await client.journeys(luxembourgGareCentrale, centreHospitalier, {
2018-06-10 20:44:12 +02:00
results: 3,
departure: when
})
await testJourneysStationToPoi({
test: t,
res,
validate,
2021-08-24 00:43:20 +02:00
fromId: luxembourgGareCentrale,
2018-06-11 20:50:35 +02:00
to: centreHospitalier
2018-06-10 20:44:12 +02:00
})
t.end()
})
// todo: journeys: via works with detour
// todo: without detour
2021-05-20 16:42:43 +01:00
tap.test('earlier/later journeys', async (t) => {
2018-06-10 20:44:12 +02:00
await testEarlierLaterJourneys({
test: t,
fetchJourneys: client.journeys,
validate,
2021-08-24 00:43:20 +02:00
fromId: luxembourgGareCentrale,
toId: ettelbruck,
when,
2018-06-10 20:44:12 +02:00
})
t.end()
})
2021-05-20 16:42:43 +01:00
tap.test('trip', async (t) => {
2021-08-24 00:43:20 +02:00
const { journeys } = await client.journeys(luxembourgGareCentrale, ettelbruck, {
2018-06-10 20:44:12 +02:00
results: 1, departure: when
})
const p = journeys[0].legs.find(l => !l.walking)
2018-06-10 20:44:12 +02:00
t.ok(p.tripId, 'precondition failed')
t.ok(p.line.name, 'precondition failed')
const tripRes = await client.trip(p.tripId, p.line.name, {when})
validate(t, tripRes, 'tripResult', 'res')
2018-06-10 20:44:12 +02:00
t.end()
})
2021-05-20 16:42:43 +01:00
tap.test('departures at Ettelbruck.', async (t) => {
const res = await client.departures(ettelbruck, {
2018-06-10 20:44:12 +02:00
duration: 20, when
})
await testDepartures({
test: t,
res,
2018-06-10 20:44:12 +02:00
validate,
id: ettelbruck
})
t.end()
})
2021-05-20 16:42:43 +01:00
tap.test('arrivals at Ettelbruck.', async (t) => {
const res = await client.arrivals(ettelbruck, {
2018-06-10 20:44:12 +02:00
duration: 20, when
})
await testArrivals({
test: t,
res,
2018-06-10 20:44:12 +02:00
validate,
id: ettelbruck
})
t.end()
})
2021-05-20 16:42:43 +01:00
tap.test('departures with station object', async (t) => {
const res = await client.departures({
2018-06-10 20:44:12 +02:00
type: 'station',
id: ettelbruck,
name: 'Ettelbruck',
location: {
type: 'location',
latitude: 49.847298,
longitude: 6.106157
}
}, {when})
validate(t, res, 'departuresResponse', 'res')
2018-06-10 20:44:12 +02:00
t.end()
})
// todo: nearby
2021-05-20 16:42:43 +01:00
tap.test('locations named Mersch', async (t) => {
const locations = await client.locations('Mersch', {
2018-06-10 20:44:12 +02:00
results: 20
})
validate(t, locations, 'locations', 'locations')
t.ok(locations.length <= 20)
t.ok(locations.find(s => s.type === 'stop' || s.type === 'station'))
t.ok(locations.find(s => s.poi))
t.ok(locations.some((loc) => {
if (loc.station && loc.station.id === mersch) return true
return loc.id === mersch
2018-06-10 20:44:12 +02:00
}))
t.end()
})
2021-05-20 16:42:43 +01:00
tap.test('stop Mersch', async (t) => {
const s = await client.stop(mersch)
2018-06-10 20:44:12 +02:00
validate(t, s, ['stop', 'station'], 'stop')
t.equal(s.id, mersch)
2018-06-10 20:44:12 +02:00
t.end()
})
2021-05-20 16:42:43 +01:00
tap.test('radar', async (t) => {
const res = await client.radar({
2018-06-10 20:44:12 +02:00
north: 49.9,
west: 6.05,
south: 49.8,
east: 6.15
}, {
duration: 5 * 60, when, results: 10
})
validate(t, res, 'radarResult', 'res')
2018-06-10 20:44:12 +02:00
t.end()
})