db-vendo-client/test/e2e/lib/leg-cycle-alternatives.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-05-07 16:17:37 +02:00
import isRoughlyEqual from 'is-roughly-equal'
2022-05-07 16:17:37 +02:00
import {hour} from './util.js'
2018-11-21 19:07:37 +01:00
const testLegCycleAlternatives = async (cfg) => {
const {
test: t,
fetchJourneys,
fromId,
toId
} = cfg
// Apparently HAFAS doesn't return the leg cycle or alternatives more
// than ~2 hours in advance. This is why we don't pass `when` here.
2018-11-21 19:07:37 +01:00
const journeys = await fetchJourneys(fromId, toId, {results: 3})
for (let i = 0; i < journeys.length; i++) {
const journey = journeys[i]
for (let j = 0; j < journey.legs.length; j++) {
const leg = journey.legs[j]
const name = `journeys[${i}].legs[${j}]`
if (!leg.line) continue
t.ok(leg.cycle, name + '.cycle is missing')
t.equal(typeof leg.cycle.min, 'number', name + '.cycle.min is not a number')
t.equal(typeof leg.cycle.max, 'number', name + '.cycle.max is not a number')
t.equal(typeof leg.cycle.nr, 'number', name + '.cycle.nr is not a number')
const lineWhen = +new Date(leg.departure)
t.ok(Array.isArray(leg.alternatives), name + '.alternatives must be an array')
for (let k = 0; k < leg.alternatives.length; k++) {
const a = leg.alternatives[k]
const n = name + `.alternatives[${k}]`
let alternativeWhen = +new Date(a.when)
if ('number' === typeof a.delay) alternativeWhen -= a.delay * 1000
t.ok(isRoughlyEqual(2 * hour, alternativeWhen, lineWhen), n + '.when seems invalid')
}
}
}
2018-11-21 19:07:37 +01:00
}
2022-05-07 16:17:37 +02:00
export {
testLegCycleAlternatives,
}