From e145d57f9ea13f09d5556c53ba71ca1b4a950c6a Mon Sep 17 00:00:00 2001 From: dabund24 Date: Fri, 7 Feb 2025 23:17:31 +0100 Subject: [PATCH] employ enrichStations for board stop property --- parse/arrival-or-departure.js | 2 +- parse/location.js | 12 ++--- test/e2e/dbweb.js | 88 ++++++++++++++++++++++------------- 3 files changed, 61 insertions(+), 41 deletions(-) diff --git a/parse/arrival-or-departure.js b/parse/arrival-or-departure.js index 875e4042..b09fc4eb 100644 --- a/parse/arrival-or-departure.js +++ b/parse/arrival-or-departure.js @@ -11,7 +11,7 @@ const createParseArrOrDep = (prefix) => { const cancelled = profile.parseCancelled(d); const res = { tripId: d.journeyID || d.journeyId || d.train?.journeyId || d.zuglaufId, - stop: profile.parseLocation(ctx, d.station || d.abfrageOrt || d.bahnhofsId), + stop: profile.parseLocation(ctx, d.station || d.abfrageOrt || {bahnhofsId: d.bahnhofsId}), ...profile.parseWhen( ctx, null, diff --git a/parse/location.js b/parse/location.js index e3725426..8f3f72c5 100644 --- a/parse/location.js +++ b/parse/location.js @@ -13,14 +13,10 @@ const parseLocation = (ctx, l) => { return null; } - if (typeof l === 'string') { - return {type: 'station', id: l}; - } - const lid = parse(l.id || l.locationId, {delimiter: '@'}); const res = { type: 'location', - id: (l.extId || l.evaNr || lid.L || l.evaNumber || l.evaNo || '').replace(leadingZeros, '') || null, + id: (l.extId || l.evaNr || lid.L || l.evaNumber || l.evaNo || l.bahnhofsId || '').replace(leadingZeros, '') || null, }; const name = l.name || lid.O; @@ -33,12 +29,14 @@ const parseLocation = (ctx, l) => { } // addresses and pois might also have fake evaNr sometimes! - if (l.type === STATION || l.extId || l.evaNumber || l.evaNo || lid.A == '1') { + if (l.type === STATION || l.extId || l.evaNumber || l.evaNo || lid.A == '1' || l.bahnhofsId) { let stop = { type: 'station', id: res.id, - name: name, }; + if (name) { + stop.name = name; + } if ('number' === typeof res.latitude) { stop.location = res; // todo: remove `.id` } diff --git a/test/e2e/dbweb.js b/test/e2e/dbweb.js index f90747f9..70c81290 100644 --- a/test/e2e/dbweb.js +++ b/test/e2e/dbweb.js @@ -82,7 +82,7 @@ const assertValidTickets = (test, tickets) => { } }; -const client = createClient(dbProfile, 'public-transport/hafas-client:test', {enrichStations: false}); +const client = createClient(dbProfile, 'public-transport/hafas-client:test', {enrichStations: true}); const berlinHbf = '8011160'; const münchenHbf = '8000261'; @@ -395,47 +395,69 @@ tap.test('trip details', async (t) => { }); tap.test('departures at Berlin Schwedter Str.', async (t) => { - const res = await client.departures(blnSchwedterStr, { - duration: 5, when, - }); + const interval = setInterval(async () => { + const res = await client.departures(blnSchwedterStr, { + duration: 5, when, + }); - await testDepartures({ - test: t, - res, - validate, - id: blnSchwedterStr, - }); - t.end(); + if (res.departures[0].stop.name === undefined) { // ctx.common.locations have not loaded yet + return; + } + + clearInterval(interval); + await testDepartures({ + test: t, + res, + validate, + id: blnSchwedterStr, + }); + t.end(); + }, 4000); }); tap.test('departures with station object', async (t) => { - const res = await client.departures({ - type: 'station', - id: jungfernheide, - name: 'Berlin Jungfernheide', - location: { - type: 'location', - latitude: 1.23, - longitude: 2.34, - }, - }, {when}); + const interval = setInterval(async () => { + const res = await client.departures({ + type: 'station', + id: jungfernheide, + name: 'Berlin Jungfernheide', + location: { + type: 'location', + latitude: 1.23, + longitude: 2.34, + }, + }, {when}); - validate(t, res, 'departuresResponse', 'res'); - t.end(); + if (res.departures[0].stop.name === undefined) { // ctx.common.locations have not loaded yet + return; + } + + clearInterval(interval); + validate(t, res, 'departuresResponse', 'res'); + t.end(); + + }, 4000); }); tap.test('arrivals at Berlin Schwedter Str.', async (t) => { - const res = await client.arrivals(blnSchwedterStr, { - duration: 5, when, - }); + const interval = setInterval(async () => { + const res = await client.arrivals(blnSchwedterStr, { + duration: 5, when, + }); - await testArrivals({ - test: t, - res, - validate, - id: blnSchwedterStr, - }); - t.end(); + if (res.arrivals[0].stop.name === undefined) { // ctx.common.locations have not loaded yet + return; + } + + clearInterval(interval); + await testArrivals({ + test: t, + res, + validate, + id: blnSchwedterStr, + }); + t.end(); + }, 4000); }); tap.test('nearby Berlin Jungfernheide', async (t) => {