db-vendo-client/test/parse/location.js

183 lines
3.8 KiB
JavaScript
Raw Normal View History

2022-05-07 16:17:37 +02:00
import tap from 'tap'
import omit from 'lodash/omit.js'
import {parseLocation as parse} from '../../parse/location.js'
2019-09-03 15:35:12 +02:00
const profile = {
parseLocation: parse,
parseStationName: (_, name) => name.toLowerCase(),
parseProductsBitmask: (_, bitmask) => [bitmask]
2019-09-03 15:35:12 +02:00
}
const ctx = {
data: {},
opt: {
linesOfStops: false,
subStops: true,
entrances: true,
},
profile
2019-09-03 15:35:12 +02:00
}
2021-05-20 16:42:43 +01:00
tap.test('parses an address correctly', (t) => {
2019-09-03 15:35:12 +02:00
const input = {
type: 'A',
name: 'Foo street 3',
lid: 'a=b@L=some%20id',
crd: {x: 13418027, y: 52515503}
}
const address = parse(ctx, input)
2021-05-20 16:42:43 +01:00
t.same(address, {
2019-09-03 15:35:12 +02:00
type: 'location',
id: 'some id',
address: 'Foo street 3',
latitude: 52.515503,
longitude: 13.418027
})
t.end()
})
2021-05-20 16:42:43 +01:00
tap.test('parses a POI correctly', (t) => {
2019-09-03 15:35:12 +02:00
const input = {
type: 'P',
name: 'some POI',
lid: 'a=b@L=some%20id',
crd: {x: 13418027, y: 52515503}
}
const poi = parse(ctx, input)
2021-05-20 16:42:43 +01:00
t.same(poi, {
2019-09-03 15:35:12 +02:00
type: 'location',
poi: true,
id: 'some id',
name: 'some POI',
latitude: 52.515503,
longitude: 13.418027
})
const withExtId = parse(ctx, {...input, extId: 'some ext id'})
2019-09-03 15:35:12 +02:00
t.equal(withExtId.id, 'some ext id')
const withLeadingZero = parse(ctx, {...input, extId: '00some ext id'})
2019-09-03 15:35:12 +02:00
t.equal(withLeadingZero.id, 'some ext id')
t.end()
})
const fooBusStop = {
type: 'S',
name: 'Foo bus stop',
lid: 'a=b@L=foo%20stop',
crd: {x: 13418027, y: 52515503},
pCls: 123
}
2019-09-03 15:35:12 +02:00
2021-05-20 16:42:43 +01:00
tap.test('parses a stop correctly', (t) => {
const stop = parse(ctx, fooBusStop)
2021-05-20 16:42:43 +01:00
t.same(stop, {
2019-09-03 15:35:12 +02:00
type: 'stop',
id: 'foo stop',
name: 'foo bus stop', // lower-cased!
location: {
type: 'location',
id: 'foo stop',
latitude: 52.515503,
longitude: 13.418027
},
products: [123]
})
const withoutLoc = parse(ctx, omit(fooBusStop, ['crd']))
2019-09-03 15:35:12 +02:00
t.equal(withoutLoc.location, null)
const mainMast = parse(ctx, {...fooBusStop, isMainMast: true})
2019-09-03 15:35:12 +02:00
t.equal(mainMast.type, 'station')
const meta = parse(ctx, {...fooBusStop, meta: 1})
2019-09-03 15:35:12 +02:00
t.equal(meta.isMeta, true)
const lineA = {id: 'a'}
const withLines = parse({
...ctx,
opt: {...ctx.opt, linesOfStops: true}
}, {
...fooBusStop, lines: [lineA]
2019-09-03 15:35:12 +02:00
})
2021-05-20 16:42:43 +01:00
t.same(withLines.lines, [lineA])
2019-09-03 15:35:12 +02:00
t.end()
})
2021-05-20 16:42:43 +01:00
tap.test('falls back to coordinates from `lid`', (t) => {
const {location} = parse(ctx, {
type: 'S',
name: 'foo',
lid: 'a=b@L=foo@X=12345678@Y=23456789'
})
t.ok(location)
t.equal(location.latitude, 23.456789)
t.equal(location.longitude, 12.345678)
t.end()
})
2021-05-20 16:42:43 +01:00
tap.test('handles recursive references properly', (t) => {
const southernInput = {
type: 'S',
name: 'Southern Platform',
lid: 'a=b@L=southern-platform',
crd: {x: 22222222, y: 11111111},
// This doesn't make sense semantically, but we test if
// `parseLocation` falls into an endless recursive loop.
stopLocL: [1]
}
const northernInput = {
type: 'S',
name: 'Northern Platform',
lid: 'a=b@L=northern-platform',
crd: {x: 44444444, y: 33333333},
// This doesn't make sense semantically, but we test if
// `parseLocation` falls into an endless recursive loop.
entryLocL: [0]
}
const common = {locL: [southernInput, northernInput]}
const _ctx = {...ctx, res: {common}}
const northernExpected = {
type: 'stop',
id: 'northern-platform',
name: 'northern platform', // lower-cased!
location: {
type: 'location',
id: 'northern-platform',
latitude: 33.333333, longitude: 44.444444
}
}
const southernExpected = {
type: 'station',
id: 'southern-platform',
name: 'southern platform', // lower-cased!
location: {
type: 'location',
id: 'southern-platform',
latitude: 11.111111, longitude: 22.222222
},
stops: [northernExpected]
}
const {entrances} = parse(_ctx, {
...fooBusStop,
entryLocL: [0]
})
2021-05-20 16:42:43 +01:00
t.same(entrances, [southernExpected.location])
const {type, stops} = parse(_ctx, {
...fooBusStop,
stopLocL: [0]
})
t.equal(type, 'station')
2021-05-20 16:42:43 +01:00
t.same(stops, [southernExpected])
t.end()
})