-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
82 lines (69 loc) · 3.26 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict'
const tapeWithoutPromise = require('tape')
const addPromiseSupport = require('tape-promise').default
const tape = addPromiseSupport(tapeWithoutPromise)
const { DateTime } = require('luxon')
const validate = require('validate-fptf')()
const fptiTests = require('fpti-tests')
const interrail = require('.')
const pkg = require('./package.json')
tape('interrail fpti tests', async t => {
await t.doesNotReject(fptiTests.packageJson(pkg), 'valid package.json')
t.doesNotThrow(() => fptiTests.packageExports(interrail, ['stations.search', 'journeys']), 'valid module exports')
t.doesNotThrow(() => fptiTests.stationsSearchFeatures(interrail.stations.search.features, ['results']), 'valid stations.search features')
t.doesNotThrow(() => fptiTests.journeysFeatures(interrail.journeys.features, ['when', 'departureAfter', 'results', 'interval', 'transfers']), 'valid journeys features')
t.end()
})
tape('interrail.stations.search', async t => {
const stations = await interrail.stations.search('Berlin')
t.ok(stations.length > 5, 'number of stations')
for (const station of stations) {
validate(station)
t.ok(station.id.slice(0, 2) === '80', 'id')
t.ok(station.id.length === 7, 'id')
t.ok(station.name.slice(0, 6).toLowerCase() === 'berlin', 'name')
t.ok(station.weight >= 0, 'weight')
}
const fewStations = await interrail.stations.search('Berlin', { results: 1 })
t.ok(fewStations.length === 1, 'number of stations')
t.end()
})
tape('interrail.journeys', async t => {
const berlin = '8065969'
const ljubljana = {
type: 'station',
id: '7942300',
name: 'Ljubljana',
}
const hamburg = {
type: 'station',
id: '8002710',
name: 'Hamburg',
}
const when = DateTime.fromJSDate(new Date(), { zone: 'Europe/Berlin' }).plus({ days: 10 }).startOf('day').plus({ hours: 5 }).toJSDate()
const journeys = await interrail.journeys(berlin, ljubljana, { when })
t.ok(journeys.length >= 2, 'number of journeys')
for (const journey of journeys) {
validate(journey)
t.ok(journey.legs[0].origin.name.toLowerCase().includes('berlin'), 'origin')
t.ok(journey.legs[journey.legs.length - 1].destination.name.toLowerCase().includes('ljubljana'), 'destination')
t.ok(journey.legs.find(l => l.line), 'leg with line found')
for (const leg of journey.legs) {
if (leg.line) validate(leg.line)
}
}
const directJourneys = await interrail.journeys(berlin, ljubljana, { when, transfers: 0 })
t.ok(directJourneys.length === 0, 'number of direct journeys')
const twoTransferJourneys = await interrail.journeys(berlin, ljubljana, { when, transfers: 2 })
t.ok(twoTransferJourneys.length <= journeys.length, 'number of 1-transfer journeys')
t.ok(twoTransferJourneys.length > 0, 'number of 1-transfer journeys')
const fewJourneys = await interrail.journeys(berlin, ljubljana, { departureAfter: when, results: 2 })
t.ok(fewJourneys.length === 2, 'number of journeys')
const twoDaysLater = DateTime.fromJSDate(when).plus({ days: 2 }).toJSDate()
const threeDays = 3 * 24 * 60
const manyJourneys = await interrail.journeys(hamburg, berlin, { when, interval: threeDays })
t.ok(manyJourneys.length > 10, 'number of journeys')
const journeysTwoDaysLater = manyJourneys.filter(j => +new Date(j.legs[0].departure) > +twoDaysLater)
t.ok(journeysTwoDaysLater.length >= 2, 'number of journeys')
t.end()
})