-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
252 lines (229 loc) · 9.62 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// todo: use import assertions once they're supported by Node.js & ESLint
// https://github.com/tc39/proposal-import-assertions
import {createRequire} from 'module';
const require = createRequire(import.meta.url);
import test from 'node:test'
import {ok, deepStrictEqual} from 'node:assert'
const {dataVersion: v} = require('./package.json')
import {unique as hash} from 'shorthash'
import {createGetStableOperatorIds as operatorIds} from './operator.js'
import {createGetStableStopIds as stopIds} from './stop.js'
import {createGetStableLineIds as lineIds} from './line.js'
import {createGetStableArrivalDepartureIds as arrivalDepartureIds} from './arrival-departure.js'
import {createGetStableTripIds as tripIds} from './trip.js'
import {versionedId} from './lib/versioned-id.js'
const normalize = (name, thing) => {
return [
name.toLowerCase().trim(),
thing.id ? thing.id[0].toUpperCase() : '-'
].join('')
}
test('operator OneStop ID', (t) => {
const op = {
type: 'operator',
id: 'foo',
wikidataId: 'Q99633',
name: 'Foo Transit',
serviceArea: {
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [[
[-81, 41], [-88, 36], [-84, 31],
[-80, 33], [-77, 39], [-81, 41]
]]
}
}
}
const ids = operatorIds('sauce', normalize)(op)
const beginsWith = str => ([str2]) => str2.slice(0, str.length) === str
const onestopId = ids.find(beginsWith(`${v}:custom:o:`))
ok(onestopId)
deepStrictEqual(ids, [
[[v, op.wikidataId].join(':'), 10],
[[v, 'sauce', op.id].join(':'), 20],
[`${v}:custom:o:dn:foo~transitF`, 30],
[[v, normalize(op.name, op), '36.1340', '-82.3110'].join(':'), 31],
])
})
test('stop IDs', (t) => {
const ids = stopIds('sauce', normalize)({
id: '123',
name: 'Foo',
station: {
id: '12',
name: 'Bar',
location: {latitude: 54.321, longitude: 65.432},
wikidataId: 'Q1097', // Berlin central station's ID
},
location: {latitude: 12.345, longitude: 23.456}
})
deepStrictEqual(ids, [
// stop IDs
[[v, 'sauce', '123'].join(':'), 20], // data src, stop ID
// normalized stop name, normalized coords
[[v, 'foo1', 12.345.toFixed(4), 23.456.toFixed(4)].join(':'), 30],
// normalized stop name, normalized & shifted coords
[[v, 'foo1', 12.345.toFixed(4), (23.456 + .001).toFixed(4)].join(':'), 31],
[[v, 'foo1', 12.345.toFixed(4), (23.456 - .001).toFixed(4)].join(':'), 31],
[[v, 'foo1', (12.345 + .001).toFixed(4), 23.456.toFixed(4)].join(':'), 31],
[[v, 'foo1', (12.345 - .001).toFixed(4), 23.456.toFixed(4)].join(':'), 31],
[[v, 'foo1', (12.345 + .001).toFixed(4), (23.456 + .001).toFixed(4)].join(':'), 32],
[[v, 'foo1', (12.345 + .001).toFixed(4), (23.456 - .001).toFixed(4)].join(':'), 32],
[[v, 'foo1', (12.345 - .001).toFixed(4), (23.456 + .001).toFixed(4)].join(':'), 32],
[[v, 'foo1', (12.345 - .001).toFixed(4), (23.456 - .001).toFixed(4)].join(':'), 32],
// station IDs as stop IDs
[[v, 'Q1097'].join(':'), 50 + 10],
[[v, 'sauce', '12'].join(':'), 50 + 20],
[[v, 'bar1', 54.321.toFixed(4), 65.432.toFixed(4)].join(':'), 50 + 30],
[[v, 'bar1', 54.321.toFixed(4), (65.432 + .001).toFixed(4)].join(':'), 50 + 31],
[[v, 'bar1', 54.321.toFixed(4), (65.432 - .001).toFixed(4)].join(':'), 50 + 31],
[[v, 'bar1', (54.321 + .001).toFixed(4), 65.432.toFixed(4)].join(':'), 50 + 31],
[[v, 'bar1', (54.321 - .001).toFixed(4), 65.432.toFixed(4)].join(':'), 50 + 31],
[[v, 'bar1', (54.321 + .001).toFixed(4), (65.432 + .001).toFixed(4)].join(':'), 50 + 32],
[[v, 'bar1', (54.321 + .001).toFixed(4), (65.432 - .001).toFixed(4)].join(':'), 50 + 32],
[[v, 'bar1', (54.321 - .001).toFixed(4), (65.432 + .001).toFixed(4)].join(':'), 50 + 32],
[[v, 'bar1', (54.321 - .001).toFixed(4), (65.432 - .001).toFixed(4)].join(':'), 50 + 32],
// parent station IDs
[[v, 'station', 'Q1097'].join(':'), 50 + 20 + 10],
[[v, 'station', 'sauce', '12'].join(':'), 50 + 20 + 20],
[[v, 'station', 'bar1', 54.321.toFixed(4), 65.432.toFixed(4)].join(':'), 50 + 20 + 30],
[[v, 'station', 'bar1', 54.321.toFixed(4), (65.432 + .001).toFixed(4)].join(':'), 50 + 20 + 31],
[[v, 'station', 'bar1', 54.321.toFixed(4), (65.432 - .001).toFixed(4)].join(':'), 50 + 20 + 31],
[[v, 'station', 'bar1', (54.321 + .001).toFixed(4), 65.432.toFixed(4)].join(':'), 50 + 20 + 31],
[[v, 'station', 'bar1', (54.321 - .001).toFixed(4), 65.432.toFixed(4)].join(':'), 50 + 20 + 31],
[[v, 'station', 'bar1', (54.321 + .001).toFixed(4), (65.432 + .001).toFixed(4)].join(':'), 50 + 20 + 32],
[[v, 'station', 'bar1', (54.321 + .001).toFixed(4), (65.432 - .001).toFixed(4)].join(':'), 50 + 20 + 32],
[[v, 'station', 'bar1', (54.321 - .001).toFixed(4), (65.432 + .001).toFixed(4)].join(':'), 50 + 20 + 32],
[[v, 'station', 'bar1', (54.321 - .001).toFixed(4), (65.432 - .001).toFixed(4)].join(':'), 50 + 20 + 32],
])
const normalizeEmpty = () => ''
const ids2 = stopIds('sauce', normalizeEmpty)({
id: '123',
name: 'Foo',
location: {latitude: 12.345, longitude: 23.456},
})
deepStrictEqual(ids2, [
[[v, 'sauce', '123'].join(':'), 50 + 20], // data src, stop ID
])
})
test(`stop's parent-station-based and the station's IDs overlap`, (t) => {
const foo = {
type: 'stop',
id: '123',
name: 'Foo',
station: {
type: 'station',
id: '12',
name: 'Bar',
location: {latitude: 54.321, longitude: 65.432},
wikidataId: 'Q1097', // Berlin central station's ID
},
location: {latitude: 12.345, longitude: 23.456},
}
const fooIds = stopIds('sauce', normalize)(foo)
const barIds = stopIds('sauce', normalize)({
...foo.station,
type: 'stop',
})
ok(fooIds.some(([fooId, fooSpecificity]) => {
return barIds.some(([barId, barSpecificity]) => (
fooId === barId
&& Math.round(fooSpecificity / 10) === Math.round(barSpecificity / 10)
))
}), 'fooIds & barIds should have an overlap')
})
test('line IDs', (t) => {
const ids = lineIds('sauce', normalize)({
id: '1a',
name: 'Some Line',
product: 'suburban',
mode: 'train',
operator: {id: 'foo-bar baz'}
})
deepStrictEqual(ids, [
[[v, 'sauce', '1a'].join(':'), 20], // data src, line ID
[[v, 'foo-bar baz', 'some line1'].join(':'), 30], // operator ID, normalized name
[[v, 'suburban', 'some line1'].join(':'), 31], // product, normalized name
[[v, 'train', 'some line1'].join(':'), 32], // mode, normalized name
])
})
test('arrival/departure IDs', (t) => {
const stopIds = [['some-stop', 20], ['another:stop', 31]]
const routeIds = [['some-route', 10], ['another:route', 20]]
const tripIds = [['some-trip', 30], ['another:trip', 30]]
const lineIds = [['some-line', 10], ['another:line', 11]]
const normalizePlatform = (platform, arrDep) => {
return platform.toLowerCase().trim() + arrDep.when[0]
}
const getIds = arrivalDepartureIds(stopIds, tripIds, routeIds, lineIds, normalizePlatform)
const ids = getIds('arrival', {
when: '2019-01-01T10:11+01:00',
plannedWhen: '2019-01-01T10:10+01:00', // todo: what if null?
platform: '3',
plannedPlatform: '2A/B '
})
deepStrictEqual(ids, [
// stop ID + trip ID
[[v, 'arr', 'some-stop', 'some-trip'].join(':'), 20 + 30 + 20],
[[v, 'arr', 'another:stop', 'some-trip'].join(':'), 31 + 30 + 20],
[[v, 'arr', 'some-stop', 'another:trip'].join(':'), 20 + 30 + 20],
[[v, 'arr', 'another:stop', 'another:trip'].join(':'), 31 + 30 + 20],
// stop ID + route ID + plannedWhen
[[v, 'arr', 'some-stop', 'some-route', 1546333800].join(':'), 20 + 10 + 30],
[[v, 'arr', 'another:stop', 'some-route', 1546333800].join(':'), 31 + 10 + 30],
[[v, 'arr', 'some-stop', 'another:route', 1546333800].join(':'), 20 + 20 + 30],
[[v, 'arr', 'another:stop', 'another:route', 1546333800].join(':'), 31 + 20 + 30],
// stop ID + line ID + plannedWhen + plannedPlatform
[[v, 'arr', 'some-stop', 'some-line', 1546333800, '2a/b2'].join(':'), 20 + 10 + 30],
[[v, 'arr', 'another:stop', 'some-line', 1546333800, '2a/b2'].join(':'), 31 + 10 + 30],
[[v, 'arr', 'some-stop', 'another:line', 1546333800, '2a/b2'].join(':'), 20 + 11 + 30],
[[v, 'arr', 'another:stop', 'another:line', 1546333800, '2a/b2'].join(':'), 31 + 11 + 30],
])
})
test('trip IDs', (t) => {
const lineIds = [['some-line', 10], ['another:line', 11]]
const depsIds = [
[[versionedId('dep0a'), 40], ['dep0b', 50], ['dep0c', 51]],
[['dep1a', 40], [versionedId('dep1b'), 41], ['dep1c', 50]],
[['dep2a', 30], ['dep2b', 50]], // note: just 2 IDs
]
const arrsIds = [
[['arr0', 42]],
[[versionedId('arr1a'), 40], ['arr1b', 60]], // note: 2 IDs
[['arr2', 50]],
]
const ids = tripIds('sauce', lineIds, depsIds, arrsIds)({
id: 'trip-12345',
direction: 'S Spandau',
line: {
id: '1a',
name: 'Some Line',
product: 'suburban',
mode: 'train',
fahrtNr: '12345',
},
})
deepStrictEqual(ids, [
[[v, 'sauce', 'trip-12345'].join(':'), 20], // data src + trip ID
// line IDs + fahrt nr
[[v, 'some-line', '12345'].join(':'), 10 + 30],
[[v, 'another:line', '12345'].join(':'), 11 + 30],
// line ID + first departure ID
[[v, 'dep0', 'some-line', 'dep0a'].join(':'), 40 + 10 + 21],
[[v, 'dep0', 'another:line', 'dep0a'].join(':'), 40 + 11 + 21],
[[v, 'dep0', 'some-line', 'dep0b'].join(':'), 50 + 10 + 21],
[[v, 'dep0', 'another:line', 'dep0b'].join(':'), 50 + 11 + 21],
[[v, 'dep0', 'some-line', 'dep0c'].join(':'), 51 + 10 + 21],
[[v, 'dep0', 'another:line', 'dep0c'].join(':'), 51 + 11 + 21],
// all departures' IDs
[v + ':some-dep:' + hash(['dep0a', 'dep1a', 'dep2a'].join(':')), 40 + 20],
[v + ':some-dep:' + hash(['dep0b', 'dep1b', 'dep2b'].join(':')), 50 + 20],
// line ID + first arrival ID
[[v, 'arr0', 'some-line', 'arr0'].join(':'), 42 + 10 + 21],
[[v, 'arr0', 'another:line', 'arr0'].join(':'), 42 + 11 + 21],
// all arrivals' IDs
[v + ':some-arr:' + hash(['arr0', 'arr1a', 'arr2'].join(':')), 50 + 20],
])
})