From a35f0e9042eb1c825995afe2c6ad26e64a3430bc Mon Sep 17 00:00:00 2001 From: Anthony Shull Date: Wed, 12 Jun 2024 13:38:21 -0500 Subject: [PATCH] predictions working (#2099) * predictions working * fix linting * one big reject --- .../helpers/__tests__/departureInfoTest.tsx | 37 --------------- assets/ts/helpers/departureInfo.tsx | 47 +++++++------------ .../components/DepartureTimesTest.tsx | 4 +- 3 files changed, 19 insertions(+), 69 deletions(-) diff --git a/assets/ts/helpers/__tests__/departureInfoTest.tsx b/assets/ts/helpers/__tests__/departureInfoTest.tsx index 3ac0d0334d..125eb11d7e 100644 --- a/assets/ts/helpers/__tests__/departureInfoTest.tsx +++ b/assets/ts/helpers/__tests__/departureInfoTest.tsx @@ -163,42 +163,5 @@ describe("departureInfo", () => { ); expect(screen.queryAllByRole("listitem")).toHaveLength(customLength); }); - - it("internally, can remove schedules before last prediction for subway", () => { - const route = baseRoute("Purple", 3); - const stop = customStop({}); - const departures = Array.from({ length: 15 }, (x, i) => { - const trip = { id: `${i}`, direction_id: 0, headsign: `Way ${i}` }; - const prediction = [0, 1, 3, 5, 6, 8].includes(i) - ? { - route, - trip, - stop, - time: add(Date.now(), { minutes: 1 * i }) - } - : undefined; - return { - prediction, - schedule: { - route, - trip, - stop, - time: add(Date.now(), { minutes: 1 * i }) - }, - route, - trip, - isCancelled: false, - isDelayed: false, - routeMode: "subway" - } as DepartureInfo; - }); - - render(); - - // the schedule-only departures at index positions 2, 4, 7 should have been removed - expect(screen.queryAllByRole("listitem")).toHaveLength( - departures.length - 3 - ); - }); }); }); diff --git a/assets/ts/helpers/departureInfo.tsx b/assets/ts/helpers/departureInfo.tsx index 45825e122c..e4353eb003 100644 --- a/assets/ts/helpers/departureInfo.tsx +++ b/assets/ts/helpers/departureInfo.tsx @@ -158,17 +158,20 @@ const departuresListFromInfos = ( omitCancelledAndSkipped = false, WrapperEl: typeof DefaultWrapper = DefaultWrapper ): React.ReactElement[] => { - // optional cutoff time, before which we won't show schedules. - // just used with subway for now. - const predictionTimeCutoff = chain(departureInfos) - .filter(d => d.routeMode === SUBWAY) - .maxBy("prediction.time") - .value()?.prediction!.time; + const predictions = chain(departureInfos) + .reject( + departure => + (omitCancelledAndSkipped && + (!!departure.isCancelled || !!departure.isSkipped)) || + (isSubway && typeof departure.prediction === "undefined") + ) + .slice(0, listLength) + .value(); - const routeId = departureInfos[0]?.route?.id; - const tripId = departureInfos[0]?.trip?.id; + if (predictions.length === 0) { + const routeId = departureInfos[0]?.route?.id; + const tripId = departureInfos[0]?.trip?.id; - if (isSubway && !predictionTimeCutoff) { return [
No real-time data @@ -176,27 +179,11 @@ const departuresListFromInfos = ( ]; } - return chain(departureInfos) - .reject( - departure => - omitCancelledAndSkipped && - (!!departure.isCancelled || !!departure.isSkipped) - ) - .omitBy( - ({ prediction, schedule }) => - // omit schedule-only departures that are before latest prediction time - predictionTimeCutoff && - !prediction && - schedule && - schedule.time <= predictionTimeCutoff - ) - .map(d => ( - - - - )) - .slice(0, listLength) - .value(); + return predictions.map(d => ( + + + + )); }; const departureInfoInRoutePatterns = ( diff --git a/assets/ts/stop/__tests__/components/DepartureTimesTest.tsx b/assets/ts/stop/__tests__/components/DepartureTimesTest.tsx index 161bc987f0..079b4e10be 100644 --- a/assets/ts/stop/__tests__/components/DepartureTimesTest.tsx +++ b/assets/ts/stop/__tests__/components/DepartureTimesTest.tsx @@ -466,7 +466,7 @@ describe("DepartureTimes", () => { }); }); - it("renders 'No more trips' when no predictions or schedules", async () => { + it("renders 'No real-time data' when no predictions", async () => { renderWithRouter( { /> ); await waitFor(() => { - expect(screen.getByText("No more trips today")).toBeDefined(); + expect(screen.getByText("No real-time data")).toBeDefined(); expect(screen.getByText("Alewife")).toBeDefined(); }); });