From 0f7382e3b8a916d2587e4e1177a6de9413512650 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sat, 15 Oct 2022 14:02:09 +0200 Subject: [PATCH] =?UTF-8?q?parse{Arrival,Departure}:=20properly=20parse=20?= =?UTF-8?q?.origin=20&=20.destination=20=F0=9F=90=9B=E2=9C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes #273 Co-Authored-By: Jürgen Bergmann --- parse/arrival-or-departure.js | 9 +- test/db-deps-with-destination.js | 33 + test/fixtures/db-deps-with-destination.json | 630 ++++++++++++++++++++ 3 files changed, 668 insertions(+), 4 deletions(-) create mode 100644 test/db-deps-with-destination.js create mode 100644 test/fixtures/db-deps-with-destination.json diff --git a/parse/arrival-or-departure.js b/parse/arrival-or-departure.js index 995fdec6..37c1eed9 100644 --- a/parse/arrival-or-departure.js +++ b/parse/arrival-or-departure.js @@ -14,6 +14,7 @@ const createParseArrOrDep = (prefix) => { const parseArrOrDep = (ctx, d) => { // d = raw arrival/departure const {profile, opt} = ctx + const {locL} = ctx.res.common const tPlanned = d.stbStop[prefix + 'TimeS'] const tPrognosed = d.stbStop[prefix + 'TimeR'] @@ -37,12 +38,12 @@ const createParseArrOrDep = (prefix) => { destination: null } - if (prefix === DEPARTURE && Array.isArray(d.prodL) && d.prodL[0].tLocX) { - res.destination = profile.parseLocation(ctx, ctx.res.common.locL[d.prodL[0].tLocX]) + if (prefix === DEPARTURE && Array.isArray(d.prodL) && d.prodL[0] && locL[d.prodL[0].tLocX]) { + res.destination = profile.parseLocation(ctx, locL[d.prodL[0].tLocX]) } - if (prefix === ARRIVAL && Array.isArray(d.prodL) && d.prodL[0].fLocX) { - res.origin = profile.parseLocation(ctx, ctx.res.common.locL[d.prodL[0].fLocX]) + if (prefix === ARRIVAL && Array.isArray(d.prodL) && d.prodL[0] && locL[d.prodL[0].fLocX]) { + res.origin = profile.parseLocation(ctx, locL[d.prodL[0].fLocX]) } if (d.pos) { diff --git a/test/db-deps-with-destination.js b/test/db-deps-with-destination.js new file mode 100644 index 00000000..e0728cfd --- /dev/null +++ b/test/db-deps-with-destination.js @@ -0,0 +1,33 @@ +'use strict' + +const tap = require('tap') + +const createClient = require('..') +const rawProfile = require('../p/db') +const res = require('./fixtures/db-deps-with-destination.json') +// const expected = require('./fixtures/db-deps-with-destination.js') + +const client = createClient(rawProfile, 'public-transport/hafas-client:test') +const {profile} = client + +const opt = { + direction: null, + duration: 10, + linesOfStops: true, + remarks: true, + stopovers: true, + includeRelatedStations: true, + when: '2022-10-15T15:45:00+02:00', + products: {}, +} + +tap.test('parses departure.destination correctly (DB)', (t) => { + const common = profile.parseCommon({profile, opt, res}) + const ctx = {profile, opt, common, res} + const departure = profile.parseDeparture(ctx, res.jnyL[0]) + + t.ok(departure.destination, 'missing departure.destination') + t.equal(departure.destination.type, 'stop', 'invalid departure.destination.type') + t.equal(departure.destination.id, '930200', 'invalid departure.destination.id') + t.end() +}) diff --git a/test/fixtures/db-deps-with-destination.json b/test/fixtures/db-deps-with-destination.json new file mode 100644 index 00000000..8ec2743d --- /dev/null +++ b/test/fixtures/db-deps-with-destination.json @@ -0,0 +1,630 @@ +{ + "common": { + "locL": [ + { + "lid": "A=1@O=Hauptbahnhof Bushst.1-3, Bielefeld@X=8532669@Y=52028585@U=80@L=930200@", + "type": "S", + "name": "Hauptbahnhof Bushst.1-3, Bielefeld", + "icoX": 0, + "extId": "930200", + "state": "F", + "crd": { + "x": 8532669, + "y": 52028585, + "floor": 0 + }, + "pCls": 303, + "pRefL": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8 + ], + "mMastLocX": 1 + }, + { + "lid": "A=1@O=Bielefeld Hbf@X=8532723@Y=52029259@U=80@L=8000036@", + "type": "S", + "name": "Bielefeld Hbf", + "icoX": 2, + "extId": "8000036", + "state": "F", + "crd": { + "x": 8532777, + "y": 52029421, + "z": 0, + "floor": 0 + }, + "pCls": 303, + "pRefL": [ + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 1, + 2, + 24, + 4, + 5, + 25, + 6, + 26, + 27, + 28, + 29, + 7, + 8, + 30, + 31, + 32, + 33 + ] + } + ], + "prodL": [ + { + "name": "Bus 87", + "nameS": "87", + "number": "87", + "icoX": 0, + "cls": 32, + "oprX": 0, + "prodCtx": { + "name": "Bus 87", + "num": "73374", + "line": "87", + "lineId": "5_owl032_87", + "matchId": "87", + "catOut": "Bus", + "catOutS": "Bus", + "catOutL": "Bus", + "catIn": "Bus", + "catCode": "5", + "admin": "owl032" + } + }, + { + "name": "Bus 52", + "nameS": "52", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus 52", + "line": "52", + "lineId": "5_owl032_52", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "Bus 54", + "nameS": "54", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus 54", + "line": "54", + "lineId": "5_owl032_54", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "Bus 61", + "nameS": "61", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus 61", + "line": "61", + "lineId": "5_owl051_61", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "Bus 62", + "nameS": "62", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus 62", + "line": "62", + "lineId": "5_owl051_62", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "Bus 87", + "nameS": "87", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus 87", + "line": "87", + "lineId": "5_owl032_87", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "Bus 95", + "nameS": "95", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus 95", + "line": "95", + "lineId": "5_owl032_95", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "Bus 369", + "nameS": "369", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus 369", + "line": "369", + "lineId": "5_owl032_369", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "Bus 80.2", + "nameS": "80.2", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus 80.2", + "line": "80.2", + "lineId": "5_owl050_80.2", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "ICE", + "icoX": 2, + "cls": 1, + "prodCtx": { + "name": "ICE ", + "line": "", + "catOut": "ICE ", + "catOutS": "ICE", + "catOutL": "Intercity-Express" + } + }, + { + "name": "EC", + "icoX": 3, + "cls": 2, + "prodCtx": { + "name": "EC ", + "line": "", + "catOut": "EC ", + "catOutS": "EC", + "catOutL": "Eurocity" + } + }, + { + "name": "IC", + "icoX": 4, + "cls": 2, + "prodCtx": { + "name": "IC ", + "line": "", + "catOut": "IC ", + "catOutS": "IC", + "catOutL": "Intercity" + } + }, + { + "name": "FLX", + "icoX": 5, + "cls": 4, + "prodCtx": { + "name": "FLX", + "line": "", + "catOut": "DPF ", + "catOutS": "DPF", + "catOutL": "Fernreisezug externer EU" + } + }, + { + "name": "Bus 61", + "nameS": "61", + "icoX": 0, + "cls": 8, + "prodCtx": { + "name": "Bus 61", + "line": "61", + "lineId": "3_R2_____61!!949218!!5787966", + "catOut": "Bus ", + "catOutS": "Bsv", + "catOutL": "SEV-Bus" + } + }, + { + "name": "Bus 69", + "nameS": "69", + "icoX": 0, + "cls": 8, + "prodCtx": { + "name": "Bus 69", + "line": "69", + "lineId": "3_R2_____69!!949218!!5787966", + "catOut": "Bus ", + "catOutS": "Bsv", + "catOutL": "SEV-Bus" + } + }, + { + "name": "Bus 78", + "nameS": "78", + "icoX": 0, + "cls": 8, + "prodCtx": { + "name": "Bus 78", + "line": "78", + "lineId": "3_R2_____78!!949218!!5787966", + "catOut": "Bus ", + "catOutS": "Bsv", + "catOutL": "SEV-Bus" + } + }, + { + "name": "Bus 82", + "nameS": "82", + "icoX": 0, + "cls": 8, + "prodCtx": { + "name": "Bus 82", + "line": "82", + "lineId": "3_R2_____82", + "catOut": "Bus ", + "catOutS": "Bsv", + "catOutL": "SEV-Bus" + } + }, + { + "name": "Bus SEV", + "nameS": "SEV", + "icoX": 0, + "cls": 8, + "prodCtx": { + "name": "Bus SEV", + "line": "SEV", + "lineId": "3_NWBus__SEV!!896828!!5815064", + "catOut": "Bus ", + "catOutS": "Bsv", + "catOutL": "SEV-Bus" + } + }, + { + "name": "NWB", + "icoX": 6, + "cls": 8, + "prodCtx": { + "name": "NWB", + "line": "", + "catOut": "DPN ", + "catOutS": "DPN", + "catOutL": "Nahreisezug" + } + }, + { + "name": "RB", + "icoX": 6, + "cls": 8, + "prodCtx": { + "name": "RB ", + "line": "", + "catOut": "DPN ", + "catOutS": "DPN", + "catOutL": "Nahreisezug" + } + }, + { + "name": "RE", + "icoX": 6, + "cls": 8, + "prodCtx": { + "name": "RE ", + "line": "", + "catOut": "DPN ", + "catOutS": "DPN", + "catOutL": "Nahreisezug" + } + }, + { + "name": "TRI", + "icoX": 6, + "cls": 8, + "prodCtx": { + "name": "TRI", + "line": "", + "catOut": "DPN ", + "catOutS": "DPN", + "catOutL": "Nahreisezug" + } + }, + { + "name": "WFB", + "icoX": 6, + "cls": 8, + "prodCtx": { + "name": "WFB", + "line": "", + "catOut": "DPN ", + "catOutS": "DPN", + "catOutL": "Nahreisezug" + } + }, + { + "name": "Bus 48", + "nameS": "48", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus 48", + "line": "48", + "lineId": "5_owl051_48", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "Bus 59", + "nameS": "59", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus 59", + "line": "59", + "lineId": "5_owl051_59", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "Bus 88", + "nameS": "88", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus 88", + "line": "88", + "lineId": "5_owl051_88", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "Bus N8", + "nameS": "N8", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus N8", + "line": "N8", + "lineId": "5_owl032_N8", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "Bus 196", + "nameS": "196", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus 196", + "line": "196", + "lineId": "5_owl032_196", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "Bus 350", + "nameS": "350", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus 350", + "line": "350", + "lineId": "5_owl021_350", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "Bus 351", + "nameS": "351", + "icoX": 0, + "cls": 32, + "prodCtx": { + "name": "Bus 351", + "line": "351", + "lineId": "5_owl021_351", + "catOut": "Bus ", + "catOutS": "Bus", + "catOutL": "Bus" + } + }, + { + "name": "STB 1", + "nameS": "1", + "icoX": 7, + "cls": 256, + "prodCtx": { + "name": "STB 1", + "line": "1", + "lineId": "8_owl031_1", + "catOut": "STB ", + "catOutS": "stb", + "catOutL": "Stadtbahn" + } + }, + { + "name": "STB 2", + "nameS": "2", + "icoX": 7, + "cls": 256, + "prodCtx": { + "name": "STB 2", + "line": "2", + "lineId": "8_owl031_2", + "catOut": "STB ", + "catOutS": "stb", + "catOutL": "Stadtbahn" + } + }, + { + "name": "STB 3", + "nameS": "3", + "icoX": 7, + "cls": 256, + "prodCtx": { + "name": "STB 3", + "line": "3", + "lineId": "8_owl031_3", + "catOut": "STB ", + "catOutS": "stb", + "catOutL": "Stadtbahn" + } + }, + { + "name": "STB 4", + "nameS": "4", + "icoX": 7, + "cls": 256, + "prodCtx": { + "name": "STB 4", + "line": "4", + "lineId": "8_owl031_4", + "catOut": "STB ", + "catOutS": "stb", + "catOutL": "Stadtbahn" + } + } + ], + "opL": [ + { + "name": "Nahreisezug", + "icoX": 1 + } + ], + "icoL": [ + { + "res": "Bus" + }, + { + "res": "DPN", + "txt": "Nahreisezug" + }, + { + "res": "ICE" + }, + { + "res": "EC" + }, + { + "res": "IC" + }, + { + "res": "DPF" + }, + { + "res": "DPN" + }, + { + "res": "STB" + } + ], + "lDrawStyleL": [ + { + "sIcoX": 0, + "type": "SOLID" + }, + { + "type": "SOLID" + } + ] + }, + "type": "DEP", + "jnyL": [ + { + "jid": "1|1616073|0|80|15102022", + "date": "20221015", + "prodX": 0, + "dirTxt": "Hauptbahnhof Bushst.1-3, Bielefeld", + "dirFlg": "2", + "status": "P", + "isRchbl": true, + "stbStop": { + "locX": 0, + "idx": 0, + "dProdX": 0, + "dTimeS": "154600", + "dTZOffset": 120, + "type": "N" + }, + "subscr": "F", + "prodL": [ + { + "prodX": 0, + "fLocX": 0, + "tLocX": 0, + "fIdx": 0, + "tIdx": 37 + } + ], + "sumLDrawStyleX": 0, + "resLDrawStyleX": 1, + "trainStartDate": "20221015" + } + ], + "fpB": "20211212", + "fpE": "20231209", + "planrtTS": "1665819982", + "sD": "20221015", + "sT": "094752", + "locRefL": [ + 0 + ] +}