This repository was archived by the owner on Apr 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.js
More file actions
153 lines (121 loc) · 4.94 KB
/
test.js
File metadata and controls
153 lines (121 loc) · 4.94 KB
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
'use strict'
const tapeWithoutPromise = require('tape')
const addPromiseSupport = require('tape-promise').default
const tape = addPromiseSupport(tapeWithoutPromise)
const validate = require('validate-fptf')()
const moment = require('moment-timezone')
const isString = require('lodash/isString')
const isURL = require('is-url-superb')
const bilkom = require('.')
tape('bilkom.stations (all)', async (t) => {
let results = await bilkom.stations()
t.ok(Array.isArray(results))
t.ok(results.length > 1000, 'stations length')
for (let s of results) {
validate(s)
t.ok(s.location.longitude > 10 && s.location.longitude < 25, 'longitude')
t.ok(s.location.latitude > 40 && s.location.latitude < 60, 'latitude')
}
t.end()
})
tape('bilkom.stations (query)', async (t) => {
let results = await bilkom.stations({ query: 'a' })
t.ok(Array.isArray(results))
t.ok(results.length === 0, 'stations length')
results = await bilkom.stations({ query: 'Krak' })
t.ok(Array.isArray(results))
t.ok(results.length >= 5 && results.length < 100, 'stations length')
for (let s of results) validate(s)
const krakowGl = results.find(x => x.name.indexOf('Kraków Gł') >= 0)
t.ok(krakowGl.location.longitude > 10 && krakowGl.location.longitude < 20, 'krakow longitude')
t.ok(krakowGl.location.latitude > 45 && krakowGl.location.latitude < 55, 'krakow latitude')
results = await bilkom.stations({ query: 'Gda' })
t.ok(Array.isArray(results))
t.ok(results.length >= 5 && results.length < 100, 'stations length')
for (let s of results) validate(s)
const gdanskGl = results.find(x => x.name.indexOf('Gdańsk Gł') >= 0)
t.ok(gdanskGl.location.longitude > 10 && gdanskGl.location.longitude < 20, 'gdansk longitude')
t.ok(gdanskGl.location.latitude > 45 && gdanskGl.location.latitude < 55, 'gdansk latitude')
t.end()
})
const szczecin = '005100057'
const przemysl = '005100234'
const gdansk = '005100009'
// const gdynia = '005100010'
tape('bilkom.journeys', async (t) => {
const journeys = await bilkom.journeys(szczecin, przemysl, moment.tz('Europe/Warsaw').add(5, 'days').startOf('day').add(7, 'hours').toDate(), { duration: 12 * 60 * 60 * 1000, prices: true })
t.ok(journeys.length > 3, 'journeys length')
for (let j of journeys) {
validate(j)
for (let l of j.legs) {
for (let s of l.stopovers) validate(s)
validate(l.line)
}
}
t.ok(journeys[0].legs[0].origin.id === szczecin, 'origin id')
t.ok(journeys[0].legs[journeys[0].legs.length - 1].destination.id === przemysl, 'destination id')
t.ok(journeys[0].legs.every(l => l.operator.id === 'PKP'), 'leg operator')
t.ok(journeys.some(j => j.price), 'price')
for (let journey of journeys) {
if (journey.price) {
t.ok(journey.price.amount > 0, 'price amount')
t.ok(journey.price.currency === 'PLN', 'price currency')
t.ok(isURL(journey.price.url), 'price url')
}
}
t.end()
})
tape('bilkom.departures', async (t) => {
const momentDate = moment.tz('Europe/Warsaw').add(5, 'days').startOf('day')
const departures = await bilkom.departures(gdansk, momentDate.toDate())
t.ok(Array.isArray(departures))
t.ok(departures.length > 50, 'departures length')
const day = momentDate.format('DDMMYYYY')
for (let d of departures) {
t.ok(d.journeyId && isString(d.journeyId), 'departure journeyId')
t.ok(d.station && isString(d.station), 'departure station')
validate(d.line)
t.ok(d.line.product && isString(d.line.product), 'departure line.product')
t.ok(d.when && isString(d.when), 'departure when')
const tDay = moment(d.when).format('DDMMYYYY')
t.ok(day === tDay)
t.ok(d.direction && isString(d.direction), 'departure direction')
}
t.end()
})
tape('bilkom.journeyLeg', async (t) => {
const date = moment.tz('Europe/Warsaw').add(5, 'days').startOf('day').toDate()
const departures = await bilkom.departures(przemysl, date)
t.ok(Array.isArray(departures), 'precondition')
t.ok(departures.length >= 5, 'precondition')
let leg = departures.find(x => x.line.product === 'IC')
t.ok(leg, 'precondition a')
t.ok(leg.line, 'precondition a')
t.ok(leg.line.product, 'precondition a')
t.ok(leg.line.id, 'precondition a')
let details = await bilkom.journeyLeg(leg.journeyId, leg.line.product, leg.line.id)
t.ok(details)
t.ok(details.journeyId === leg.journeyId)
t.ok(details.line.type === leg.line.type)
t.ok(details.line.id === leg.line.id)
t.ok(details.line.product === leg.line.product)
for (let stopover of details.stopovers) {
validate(stopover)
t.ok(stopover.stop.location)
}
leg = departures.find(x => x.line.product === 'D')
t.ok(leg, 'precondition b')
t.ok(leg.line, 'precondition b')
t.ok(leg.line.product, 'precondition b')
t.ok(leg.line.id, 'precondition b')
details = await bilkom.journeyLeg(leg.journeyId, leg.line.product, leg.line.id)
t.ok(details)
t.ok(details.journeyId === leg.journeyId)
t.ok(details.line.type === leg.line.type)
t.ok(details.line.id === leg.line.id)
t.ok(details.line.product === leg.line.product)
for (let stopover of details.stopovers) {
validate(stopover)
}
t.end()
})