2022-12-19 13:23:13 +01:00
|
|
|
import tape from 'tape'
|
|
|
|
import _ndjson from 'ndjson'
|
|
|
|
const {parse: ndjsonParser} = _ndjson
|
|
|
|
import {loyaltyCards} from '../lib/loyalty-cards.js'
|
|
|
|
import {fetchWithTestApi} from './util.js'
|
|
|
|
import {pStations as pAllStations} from '../lib/db-stations.js'
|
2022-04-03 13:44:22 +02:00
|
|
|
|
2022-04-02 17:02:25 +02:00
|
|
|
const NO_JOURNEYS = {
|
|
|
|
// todo?
|
|
|
|
journeys: [],
|
|
|
|
}
|
|
|
|
|
|
|
|
tape.test('/journeys?firstClass works', async (t) => {
|
|
|
|
await fetchWithTestApi({
|
|
|
|
journeys: async (from, to, opt = {}) => {
|
|
|
|
t.equal(opt.firstClass, true, 'journeys() called with invalid opt.firstClass')
|
|
|
|
return NO_JOURNEYS
|
|
|
|
}
|
|
|
|
}, {}, '/journeys?from=123&to=234&firstClass=true')
|
|
|
|
})
|
|
|
|
|
|
|
|
tape.test('/journeys?loyaltyCard works', async (t) => {
|
|
|
|
await fetchWithTestApi({
|
|
|
|
journeys: async (from, to, opt = {}) => {
|
|
|
|
t.deepEqual(opt.loyaltyCard, {
|
|
|
|
type: loyaltyCards.SHCARD,
|
|
|
|
}, 'journeys() called with invalid opt.loyaltyCard')
|
|
|
|
return NO_JOURNEYS
|
|
|
|
}
|
|
|
|
}, {}, '/journeys?from=123&to=234&loyaltyCard=shcard')
|
|
|
|
|
|
|
|
await fetchWithTestApi({
|
|
|
|
journeys: async (from, to, opt = {}) => {
|
|
|
|
t.deepEqual(opt.loyaltyCard, {
|
|
|
|
type: loyaltyCards.BAHNCARD,
|
|
|
|
discount: 50,
|
|
|
|
class: 2,
|
|
|
|
}, 'journeys() called with invalid opt.loyaltyCard')
|
|
|
|
return NO_JOURNEYS
|
|
|
|
}
|
|
|
|
}, {}, '/journeys?from=123&to=234&loyaltyCard=bahncard-2nd-50')
|
|
|
|
})
|
2022-11-22 16:01:29 +01:00
|
|
|
|
|
|
|
tape.test('/stations works', async (t) => {
|
2022-11-22 16:05:11 +01:00
|
|
|
const {data: allStations} = await pAllStations
|
|
|
|
const someStationId = Object.keys(allStations)[0]
|
|
|
|
|
2022-11-22 16:01:29 +01:00
|
|
|
{
|
2022-11-22 16:05:11 +01:00
|
|
|
const {headers, data} = await fetchWithTestApi({}, {}, '/stations', {
|
2022-11-22 16:01:29 +01:00
|
|
|
headers: {
|
|
|
|
'accept': 'application/json',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
t.equal(headers['content-type'], 'application/json')
|
2022-11-22 16:05:11 +01:00
|
|
|
t.equal(typeof data, 'object')
|
|
|
|
t.ok(data)
|
|
|
|
t.ok(data[someStationId])
|
|
|
|
t.equal(Object.keys(data).length, Object.keys(allStations).length)
|
2022-11-22 16:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-11-22 16:05:11 +01:00
|
|
|
const {headers, data} = await fetchWithTestApi({}, {}, '/stations', {
|
2022-11-22 16:01:29 +01:00
|
|
|
headers: {
|
|
|
|
'accept': 'application/x-ndjson',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
t.equal(headers['content-type'], 'application/x-ndjson')
|
2022-11-22 16:05:11 +01:00
|
|
|
|
|
|
|
let nrOfStations = 0
|
|
|
|
const parser = ndjsonParser()
|
|
|
|
parser.end(data)
|
|
|
|
for await (const station of parser) nrOfStations++
|
|
|
|
|
|
|
|
t.equal(nrOfStations, Object.keys(allStations).length)
|
2022-11-22 16:01:29 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-12-21 14:47:36 +01:00
|
|
|
tape.test('/stations?query=frankfurt%20ha works', async (t) => {
|
2022-11-22 16:05:11 +01:00
|
|
|
const FRANKFURT_MAIN_HBF = '8000105'
|
|
|
|
|
2022-11-22 16:01:29 +01:00
|
|
|
{
|
2022-12-21 14:47:36 +01:00
|
|
|
const {headers, data} = await fetchWithTestApi({}, {}, '/stations?query=frankfurt%20ha', {
|
2022-11-22 16:01:29 +01:00
|
|
|
headers: {
|
|
|
|
'accept': 'application/json',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
t.equal(headers['content-type'], 'application/json')
|
2022-11-22 16:05:11 +01:00
|
|
|
t.equal(typeof data, 'object')
|
|
|
|
t.ok(data)
|
|
|
|
t.ok(data[FRANKFURT_MAIN_HBF])
|
|
|
|
t.ok(Object.keys(data).length > 0)
|
2022-11-22 16:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-12-21 14:47:36 +01:00
|
|
|
const {headers, data} = await fetchWithTestApi({}, {}, '/stations?query=frankfurt%20ha', {
|
2022-11-22 16:01:29 +01:00
|
|
|
headers: {
|
|
|
|
'accept': 'application/x-ndjson',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
t.equal(headers['content-type'], 'application/x-ndjson')
|
2022-11-22 16:05:11 +01:00
|
|
|
|
|
|
|
const stations = []
|
|
|
|
const parser = ndjsonParser()
|
|
|
|
parser.end(data)
|
|
|
|
for await (const station of parser) stations.push(station)
|
|
|
|
|
|
|
|
t.ok(stations.find(s => s.id === FRANKFURT_MAIN_HBF))
|
2022-12-28 14:16:04 +01:00
|
|
|
t.ok(stations.length > 0)
|
2022-11-22 16:01:29 +01:00
|
|
|
}
|
|
|
|
})
|