-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
98 lines (80 loc) · 2.34 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
'use strict'
const test = require('tape')
const isRoughlyEqual = require('is-roughly-equal')
const portal = require('.')
const minute = 60 * 1000
const isValidDate = (d) => {
return 'string' === typeof d && !Number.isNaN(+new Date(d))
}
test('status', (t) => {
t.plan(8)
portal.status()
.then((data) => {
t.equal(data.ok, true)
t.equal(typeof data.speed, 'number')
t.ok(data.speed >= 0)
t.ok(data.speed < 500)
t.equal(data.gpsOk, true)
t.ok(isRoughlyEqual(10, data.latitude, 49))
t.ok(isRoughlyEqual(10, data.longitude, 10))
t.ok(isRoughlyEqual(10 * minute, data.serverTime, Date.now()))
})
.catch(t.ifError)
})
test('journey', (t) => {
portal.journey()
.then((leg) => {
t.equal(leg.public, true)
t.equal(leg.mode, 'train')
t.ok(leg.line)
t.equal(leg.line.type, 'line')
t.equal(typeof leg.line.id, 'string')
t.ok(leg.line.id)
t.equal(typeof leg.line.name, 'string')
t.ok(leg.line.name)
if (leg.next) t.equal(leg.next.type, 'station')
if (leg.previous) t.equal(leg.previous.type, 'station')
t.ok(leg.last)
t.equal(leg.last.type, 'station')
t.ok(Array.isArray(leg.passed))
for (let i = 0; i < leg.passed.length; i++) {
const p = leg.passed[i]
const msg = 'passed ' + i
t.ok(p, msg)
t.ok(p.station, msg)
t.equal(typeof p.station.id, 'string', msg)
t.ok(p.station.id, msg)
t.equal(typeof p.station.name, 'string', msg)
t.ok(p.station.name, msg)
const l = p.station.location
t.ok(l, msg)
t.equal(l.type, 'location', msg)
t.ok(isRoughlyEqual(10, l.latitude, 49), msg)
t.ok(isRoughlyEqual(10, l.longitude, 10), msg)
if (p.arrival) t.ok(isValidDate(p.arrival), msg)
if (p.departure) t.ok(isValidDate(p.departure), msg)
if (p.arrivalDelay !== null) {
t.equal(typeof p.arrivalDelay, 'number', msg)
}
if (p.departureDelay !== null) {
t.equal(typeof p.departureDelay, 'number', msg)
}
if (p.arrivalPlatform) {
t.equal(typeof p.arrivalPlatform, 'string', msg)
t.ok(p.arrivalPlatform, msg)
}
if (p.departurePlatform) {
t.equal(typeof p.departurePlatform, 'string', msg)
t.ok(p.departurePlatform, msg)
}
t.equal(typeof p.passed, 'boolean', msg)
t.equal(typeof p.kmFromStart, 'number', msg)
t.ok(p.kmFromStart >= 0, msg)
if (p.delayReasons) {
t.ok(p.delayReasons instanceof Array, msg)
}
}
t.end()
})
.catch(t.ifError)
})