2024-02-06 22:58:49 +01: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 = {
|
2020-02-15 19:13:43 +00:00
|
|
|
parseLocation: parse,
|
2019-10-20 00:19:11 +02:00
|
|
|
parseStationName: (_, name) => name.toLowerCase(),
|
2024-02-06 22:58:49 +01:00
|
|
|
parseProductsBitmask: (_, bitmask) => [bitmask],
|
|
|
|
};
|
2019-10-20 00:19:11 +02:00
|
|
|
|
|
|
|
const ctx = {
|
|
|
|
data: {},
|
|
|
|
opt: {
|
2020-02-15 19:13:43 +00:00
|
|
|
linesOfStops: false,
|
|
|
|
subStops: true,
|
|
|
|
entrances: true,
|
2019-10-20 00:19:11 +02:00
|
|
|
},
|
2024-02-06 22:58:49 +01:00
|
|
|
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',
|
2024-02-06 22:58:49 +01:00
|
|
|
crd: {x: 13418027, y: 52515503},
|
|
|
|
};
|
2019-09-03 15:35:12 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
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,
|
2024-02-06 22:58:49 +01:00
|
|
|
longitude: 13.418027,
|
|
|
|
});
|
2019-09-03 15:35:12 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
t.end();
|
|
|
|
});
|
2019-09-03 15:35:12 +02:00
|
|
|
|
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',
|
2024-02-06 22:58:49 +01:00
|
|
|
crd: {x: 13418027, y: 52515503},
|
|
|
|
};
|
2019-09-03 15:35:12 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
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,
|
2024-02-06 22:58:49 +01:00
|
|
|
longitude: 13.418027,
|
|
|
|
});
|
2019-09-03 15:35:12 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const withExtId = parse(ctx, {...input, extId: 'some ext id'});
|
|
|
|
t.equal(withExtId.id, 'some ext id');
|
2019-09-03 15:35:12 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const withLeadingZero = parse(ctx, {...input, extId: '00some ext id'});
|
|
|
|
t.equal(withLeadingZero.id, 'some ext id');
|
2019-09-03 15:35:12 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
t.end();
|
|
|
|
});
|
2019-09-03 15:35:12 +02:00
|
|
|
|
2020-02-15 19:13:43 +00:00
|
|
|
const fooBusStop = {
|
|
|
|
type: 'S',
|
|
|
|
name: 'Foo bus stop',
|
|
|
|
lid: 'a=b@L=foo%20stop',
|
|
|
|
crd: {x: 13418027, y: 52515503},
|
2024-02-06 22:58:49 +01:00
|
|
|
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) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
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,
|
2024-02-06 22:58:49 +01:00
|
|
|
longitude: 13.418027,
|
2019-09-03 15:35:12 +02:00
|
|
|
},
|
2024-02-06 22:58:49 +01:00
|
|
|
products: [123],
|
|
|
|
});
|
2019-09-03 15:35:12 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const withoutLoc = parse(ctx, omit(fooBusStop, ['crd']));
|
|
|
|
t.equal(withoutLoc.location, null);
|
2019-09-03 15:35:12 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const mainMast = parse(ctx, {...fooBusStop, isMainMast: true});
|
|
|
|
t.equal(mainMast.type, 'station');
|
2019-09-03 15:35:12 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const meta = parse(ctx, {...fooBusStop, meta: 1});
|
|
|
|
t.equal(meta.isMeta, true);
|
2019-09-03 15:35:12 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const lineA = {id: 'a'};
|
2019-10-20 00:19:11 +02:00
|
|
|
const withLines = parse({
|
|
|
|
...ctx,
|
2024-02-06 22:58:49 +01:00
|
|
|
opt: {...ctx.opt, linesOfStops: true},
|
2019-10-20 00:19:11 +02:00
|
|
|
}, {
|
2024-02-06 22:58:49 +01:00
|
|
|
...fooBusStop, lines: [lineA],
|
|
|
|
});
|
|
|
|
t.same(withLines.lines, [lineA]);
|
2019-09-03 15:35:12 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
t.end();
|
|
|
|
});
|
2020-03-18 21:37:31 +01:00
|
|
|
|
2021-05-20 16:42:43 +01:00
|
|
|
tap.test('falls back to coordinates from `lid`', (t) => {
|
2020-03-18 21:37:31 +01:00
|
|
|
const {location} = parse(ctx, {
|
|
|
|
type: 'S',
|
|
|
|
name: 'foo',
|
2024-02-06 22:58:49 +01:00
|
|
|
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();
|
|
|
|
});
|
2020-02-15 19:13:43 +00:00
|
|
|
|
2021-05-20 16:42:43 +01:00
|
|
|
tap.test('handles recursive references properly', (t) => {
|
2020-02-15 19:13:43 +00:00
|
|
|
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.
|
2024-02-06 22:58:49 +01:00
|
|
|
stopLocL: [1],
|
|
|
|
};
|
2020-02-15 19:13:43 +00:00
|
|
|
const northernInput = {
|
|
|
|
type: 'S',
|
|
|
|
name: 'Northern Platform',
|
|
|
|
lid: 'a=b@L=northern-platform',
|
2020-02-15 19:14:50 +00:00
|
|
|
crd: {x: 44444444, y: 33333333},
|
|
|
|
// This doesn't make sense semantically, but we test if
|
|
|
|
// `parseLocation` falls into an endless recursive loop.
|
2024-02-06 22:58:49 +01:00
|
|
|
entryLocL: [0],
|
|
|
|
};
|
|
|
|
const common = {locL: [southernInput, northernInput]};
|
|
|
|
const _ctx = {...ctx, res: {common}};
|
2020-02-15 19:13:43 +00:00
|
|
|
|
|
|
|
const northernExpected = {
|
|
|
|
type: 'stop',
|
|
|
|
id: 'northern-platform',
|
|
|
|
name: 'northern platform', // lower-cased!
|
|
|
|
location: {
|
|
|
|
type: 'location',
|
|
|
|
id: 'northern-platform',
|
2024-02-06 22:58:49 +01:00
|
|
|
latitude: 33.333333, longitude: 44.444444,
|
|
|
|
},
|
|
|
|
};
|
2020-02-15 19:13:43 +00:00
|
|
|
const southernExpected = {
|
2020-02-15 19:10:41 +00:00
|
|
|
type: 'station',
|
2020-02-15 19:13:43 +00:00
|
|
|
id: 'southern-platform',
|
|
|
|
name: 'southern platform', // lower-cased!
|
|
|
|
location: {
|
|
|
|
type: 'location',
|
|
|
|
id: 'southern-platform',
|
2024-02-06 22:58:49 +01:00
|
|
|
latitude: 11.111111, longitude: 22.222222,
|
2020-02-15 19:13:43 +00:00
|
|
|
},
|
2024-02-06 22:58:49 +01:00
|
|
|
stops: [northernExpected],
|
|
|
|
};
|
2020-02-15 19:13:43 +00:00
|
|
|
|
|
|
|
const {entrances} = parse(_ctx, {
|
|
|
|
...fooBusStop,
|
2024-02-06 22:58:49 +01:00
|
|
|
entryLocL: [0],
|
|
|
|
});
|
|
|
|
t.same(entrances, [southernExpected.location]);
|
2020-02-15 19:13:43 +00:00
|
|
|
|
|
|
|
const {type, stops} = parse(_ctx, {
|
|
|
|
...fooBusStop,
|
2024-02-06 22:58:49 +01:00
|
|
|
stopLocL: [0],
|
|
|
|
});
|
|
|
|
t.equal(type, 'station');
|
|
|
|
t.same(stops, [southernExpected]);
|
2020-02-15 19:13:43 +00:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
t.end();
|
|
|
|
});
|