-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
110 lines (100 loc) · 2.44 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
'use strict'
const a = require('assert')
const createFetchAlternatives = require('.')
const second = 1000
const minute = 60 * second
const fromIso = iso => +new Date(iso)
const toIso = t => new Date(t).toISOString()
const journey = leg => ({legs: [{...leg}]})
const line1 = {
type: 'line',
id: '1',
name: 'one',
public: true,
mode: 'train',
product: 'suburban'
}
const line2 = {
type: 'line',
id: '2',
name: 'two',
public: true,
mode: 'bus',
product: 'bus'
}
const dep = '2019-02-21T13:32:00.000+01:00'
const leg = {
id: '12345',
origin: {
type: 'stop',
id: 'A',
name: 'station A',
location: {type: 'location', latitude: 1.23, longitude: 3.21}
},
destination: {
type: 'stop',
id: 'B',
name: 'station B',
location: {type: 'location', latitude: 2.34, longitude: 4.32}
},
departure: dep,
arrival: '2019-02-21T13:35:00.000+01:00',
line: line1,
direction: 'foo',
cycle: {min: 120, max: 300, nr: 36}
}
const alt1Dep = toIso(fromIso(dep) - 5 * minute)
const alt2Dep = toIso(fromIso(dep) + 1 * minute)
const hafasMock = {
journeys: (from, to, opt) => {
a.strictEqual(from, leg.origin)
a.strictEqual(to, leg.destination)
a.ok(opt)
a.strictEqual(fromIso(opt.departure), fromIso(dep) - 10 * minute)
a.strictEqual(opt.transfers, 0)
const legs = [{
...leg,
id: leg.id + '-1',
departure: alt1Dep,
arrival: toIso(fromIso(dep) - 2 * minute)
}, {
...leg,
id: leg.id + '-2',
departure: alt2Dep,
arrival: toIso(fromIso(dep) + 4 * minute + 30 * second), // 3m30s
line: line2
}, { // cancelled, to be filtered out
...leg,
id: leg.id + '-3',
cancelled: true,
departure: toIso(fromIso(dep) + 4 * minute),
arrival: toIso(fromIso(dep) + 7 * minute)
}, { // too slow, to be filtered out
...leg,
id: leg.id + '-4',
departure: toIso(fromIso(dep) + 6 * minute),
arrival: toIso(fromIso(dep) + 11 * minute) // 5m
}]
return Promise.resolve(legs.map(journey))
}
}
const fetchAlternatives = createFetchAlternatives(hafasMock)
fetchAlternatives(journey(leg))
.then(({legs}) => {
const alts = legs[0].alternatives
a.ok(Array.isArray(alts))
a.strictEqual(alts.length, 2)
a.ok(alts[0])
a.strictEqual(alts[0].tripId, '12345-1')
a.strictEqual(alts[0].line, line1)
a.strictEqual(alts[0].when, alt1Dep)
a.ok(alts[1])
a.strictEqual(alts[1].tripId, '12345-2')
a.strictEqual(alts[1].line, line2)
a.strictEqual(alts[1].when, alt2Dep)
// todo
})
.catch((err) => {
console.error(err)
process.exitCode = 1
})