2018-11-21 19:07:37 +01:00
|
|
|
const testEarlierLaterJourneys = async (cfg) => {
|
2018-04-24 15:24:59 +02:00
|
|
|
const {
|
|
|
|
test: t,
|
|
|
|
fetchJourneys,
|
|
|
|
fromId,
|
|
|
|
toId,
|
2018-11-20 18:38:48 +01:00
|
|
|
when
|
2018-04-24 15:24:59 +02:00
|
|
|
// todo: validate
|
|
|
|
} = cfg
|
|
|
|
|
2018-11-21 19:07:37 +01:00
|
|
|
const model = await fetchJourneys(fromId, toId, {
|
2018-05-31 13:42:54 +02:00
|
|
|
results: 3, departure: when
|
2018-04-24 15:24:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// todo: move to journeys validator?
|
|
|
|
t.equal(typeof model.earlierRef, 'string')
|
|
|
|
t.ok(model.earlierRef)
|
|
|
|
t.equal(typeof model.laterRef, 'string')
|
|
|
|
t.ok(model.laterRef)
|
|
|
|
|
2018-05-31 13:42:54 +02:00
|
|
|
// departure/arrival and earlierThan/laterThan should be mutually exclusive
|
2021-12-29 18:53:50 +01:00
|
|
|
await t.rejects(async () => {
|
|
|
|
await fetchJourneys(fromId, toId, {
|
2018-05-31 13:42:54 +02:00
|
|
|
departure: when, earlierThan: model.earlierRef
|
2018-04-24 15:24:59 +02:00
|
|
|
})
|
|
|
|
})
|
2021-12-29 18:53:50 +01:00
|
|
|
await t.rejects(async () => {
|
|
|
|
await fetchJourneys(fromId, toId, {
|
2018-05-31 13:42:54 +02:00
|
|
|
departure: when, laterThan: model.laterRef
|
|
|
|
})
|
|
|
|
})
|
2021-12-29 18:53:50 +01:00
|
|
|
await t.rejects(async () => {
|
|
|
|
await fetchJourneys(fromId, toId, {
|
2018-05-31 13:42:54 +02:00
|
|
|
arrival: when, earlierThan: model.earlierRef
|
|
|
|
})
|
|
|
|
})
|
2021-12-29 18:53:50 +01:00
|
|
|
await t.rejects(async () => {
|
|
|
|
await fetchJourneys(fromId, toId, {
|
2018-05-31 13:42:54 +02:00
|
|
|
arrival: when, laterThan: model.laterRef
|
2018-04-24 15:24:59 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
let earliestDep = Infinity, latestDep = -Infinity
|
2019-01-16 21:41:17 +08:00
|
|
|
for (let j of model.journeys) {
|
2018-05-07 12:33:53 +02:00
|
|
|
if (j.legs[0].departure === null) continue
|
2020-09-10 23:57:03 +02:00
|
|
|
const dep = Date.parse(j.legs[0].departure)
|
2018-04-24 15:24:59 +02:00
|
|
|
if (dep < earliestDep) earliestDep = dep
|
|
|
|
else if (dep > latestDep) latestDep = dep
|
|
|
|
}
|
|
|
|
|
2018-11-21 19:07:37 +01:00
|
|
|
const earlier = await fetchJourneys(fromId, toId, {
|
2018-04-24 15:24:59 +02:00
|
|
|
results: 3,
|
|
|
|
// todo: single journey ref?
|
|
|
|
earlierThan: model.earlierRef
|
|
|
|
})
|
2019-01-16 21:41:17 +08:00
|
|
|
for (let j of earlier.journeys) {
|
2018-11-20 18:29:17 +01:00
|
|
|
const firstLeg = j.legs[0]
|
2020-09-10 23:57:03 +02:00
|
|
|
const dep = Date.parse(firstLeg.departure || firstLeg.plannedDeparture)
|
2018-11-20 18:29:17 +01:00
|
|
|
t.ok(dep < earliestDep)
|
2018-04-24 15:24:59 +02:00
|
|
|
}
|
|
|
|
|
2018-11-21 19:07:37 +01:00
|
|
|
const later = await fetchJourneys(fromId, toId, {
|
2018-04-24 15:24:59 +02:00
|
|
|
results: 3,
|
|
|
|
// todo: single journey ref?
|
|
|
|
laterThan: model.laterRef
|
|
|
|
})
|
2019-01-16 21:41:17 +08:00
|
|
|
for (let j of later.journeys) {
|
2018-11-20 18:29:17 +01:00
|
|
|
const firstLeg = j.legs[0]
|
2020-09-10 23:57:03 +02:00
|
|
|
const dep = Date.parse(firstLeg.departure || firstLeg.plannedDeparture)
|
2018-11-20 18:29:17 +01:00
|
|
|
t.ok(dep > latestDep)
|
2018-04-24 15:24:59 +02:00
|
|
|
}
|
2018-11-21 19:07:37 +01:00
|
|
|
}
|
2018-04-24 15:24:59 +02:00
|
|
|
|
2022-05-07 16:17:37 +02:00
|
|
|
export {
|
|
|
|
testEarlierLaterJourneys,
|
|
|
|
}
|