-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
183 lines (143 loc) · 4.88 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict'
const tapeWithoutPromise = require('tape')
const addPromiseSupport = require('tape-promise').default
const tape = addPromiseSupport(tapeWithoutPromise)
const countries = require('iso-3166-1')
const validate = require('validate-fptf')
const moment = require('moment-timezone')
const uniqBy = require('lodash.uniqby')
const isCurrency = require('is-currency-code')
const ecolines = require('.')
const timezones = moment.tz.names()
tape('@juliuste/ecolines.stations', async (t) => {
const stations = await ecolines.stations()
t.ok(stations.length > 100)
for (let station of stations) {
validate(station)
// regions
t.ok(Array.isArray(station.regions))
t.ok(station.regions.length > 0)
// location & country
validate(station.location)
t.ok(countries.whereAlpha2(station.location.country))
t.ok(!!station.location.longitude)
t.ok(timezones.includes(station.location.timezone))
}
const stationsWithDescription = stations.filter(s => !!s.description)
t.ok(stationsWithDescription.length > 20)
t.end()
})
tape('@juliuste/ecolines.regions', async (t) => {
const regions = await ecolines.regions()
t.ok(regions.length > 100)
for (let region of regions) {
validate(region)
// stations
t.ok(Array.isArray(region.stations))
t.ok(region.stations.length > 0)
// country
t.ok(countries.whereAlpha2(region.country))
}
const regionsWithMultipleStations = regions.filter(r => r.stations.length > 0)
t.ok(regionsWithMultipleStations.length > 1)
t.end()
})
tape('@juliuste/ecolines.stopovers', async (t) => {
const berlin = '211'
const date = moment.tz('Europe/Berlin').add(5, 'days').startOf('day').toDate()
const stopovers = await ecolines.stopovers(berlin, { when: date })
t.ok(stopovers.length > 4)
for (let stopover of stopovers) {
validate(stopover)
t.ok(!!stopover.schedule)
t.ok(!!stopover.line)
t.ok(!!stopover.trip)
if (stopover.arrival) {
t.ok(+new Date(stopover.arrival) - +date > 0)
t.ok(+new Date(stopover.arrival) - +date <= 24 * 60 * 60 * 1000)
}
if (stopover.departure) {
t.ok(+new Date(stopover.departure) - +date > 0)
t.ok(+new Date(stopover.departure) - +date <= 24 * 60 * 60 * 1000)
}
t.ok(stopover.origin)
validate(stopover.origin)
if (stopover.origin.id === berlin) t.ok(!stopover.arrival)
else t.ok(stopover.arrival)
t.ok(stopover.destination)
validate(stopover.destination)
if (stopover.destination.id === berlin) t.ok(!stopover.departure)
else t.ok(stopover.departure)
}
const stopoversByTrip = uniqBy(stopovers, 'trip')
t.ok(stopoversByTrip.length === stopovers.length)
const stopoversFromRiga = stopovers.filter(s => s.origin.id === '1')
t.ok(stopoversFromRiga.length >= 2)
const stopoversToRiga = stopovers.filter(s => s.destination.id === '1')
t.ok(stopoversToRiga.length >= 2)
t.end()
})
tape('@juliuste/ecolines.currencies', async (t) => {
const currencies = await ecolines.currencies()
t.ok(currencies.length >= 4)
for (let currency of currencies) t.ok(isCurrency(currency))
t.ok(currencies.includes('EUR'))
t.ok(currencies.includes('PLN'))
t.end()
})
tape('@juliuste/ecolines.journeys', async (t) => {
const riga = {
type: 'station',
id: '1'
}
const berlin = '211'
const date = moment.tz('Europe/Riga').add(5, 'days').startOf('day').toDate()
const currency = 'PLN'
const journeys = await ecolines.journeys(riga, berlin, { when: date, adults: 2, currency })
t.ok(journeys.length >= 2)
for (let journey of journeys) {
validate(journey)
t.ok(journey.price)
t.ok(journey.price.currency === currency)
t.ok(journey.price.amount > 50)
t.ok(journey.legs[0].origin.id === riga.id)
t.ok(journey.legs[journey.legs.length - 1].destination.id === berlin)
for (let leg of journey.legs) {
t.ok(leg.id)
t.ok(leg.operator === 'ecolines')
t.ok(leg.mode === 'bus')
t.ok(leg.public)
}
}
const directJourneys = journeys.filter(j => j.legs.length === 1)
t.ok(directJourneys.length > 0)
const indirectJourneys = journeys.filter(j => j.legs.length > 1)
t.ok(indirectJourneys.length > 0)
t.end()
})
tape('@juliuste/ecolines.legDetails', async (t) => {
const berlin = '211'
const riga = '1'
const date = moment.tz('Europe/Riga').add(5, 'days').startOf('day').toDate()
const journeys = await ecolines.journeys(berlin, riga, { when: date })
t.ok(journeys.length > 0, 'precondition')
const leg = journeys[0].legs[0]
t.ok(leg, 'precondition')
t.ok(leg.id, 'precondition')
const detailedLeg = await ecolines.legDetails(leg.id)
t.ok(detailedLeg)
validate(detailedLeg)
t.ok(detailedLeg.origin.id === berlin)
t.ok(detailedLeg.stopovers[0].stop.id === berlin)
validate(detailedLeg.line)
t.ok(detailedLeg.line.operator === 'ecolines')
t.ok(detailedLeg.line.mode === 'bus')
t.ok(detailedLeg.line.public)
t.ok(detailedLeg.stopovers.length >= 2)
for (let stopover of detailedLeg.stopovers) {
validate(stopover)
t.ok(stopover.stop.location.address)
}
t.ok(detailedLeg.busPhone)
t.end()
})