Skip to content

Commit

Permalink
predictions working (#2099)
Browse files Browse the repository at this point in the history
* predictions working

* fix linting

* one big reject
  • Loading branch information
anthonyshull authored Jun 12, 2024
1 parent 6df9331 commit a35f0e9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 69 deletions.
37 changes: 0 additions & 37 deletions assets/ts/helpers/__tests__/departureInfoTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<ul>{departuresListFromInfos(departures, false, false)}</ul>);

// the schedule-only departures at index positions 2, 4, 7 should have been removed
expect(screen.queryAllByRole("listitem")).toHaveLength(
departures.length - 3
);
});
});
});
47 changes: 17 additions & 30 deletions assets/ts/helpers/departureInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,45 +158,32 @@ 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 [
<div className="no-real-time-data" key={`${routeId}-${tripId}`}>
No real-time data
</div>
];
}

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 => (
<WrapperEl key={getInfoKey(d)}>
<DisplayTime departure={d} isCR={isCR} targetDate={targetDate} />
</WrapperEl>
))
.slice(0, listLength)
.value();
return predictions.map(d => (
<WrapperEl key={getInfoKey(d)}>
<DisplayTime departure={d} isCR={isCR} targetDate={targetDate} />
</WrapperEl>
));
};

const departureInfoInRoutePatterns = (
Expand Down
4 changes: 2 additions & 2 deletions assets/ts/stop/__tests__/components/DepartureTimesTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<DepartureTimes
headsign="Alewife"
Expand All @@ -479,7 +479,7 @@ describe("DepartureTimes", () => {
/>
);
await waitFor(() => {
expect(screen.getByText("No more trips today")).toBeDefined();
expect(screen.getByText("No real-time data")).toBeDefined();
expect(screen.getByText("Alewife")).toBeDefined();
});
});
Expand Down

0 comments on commit a35f0e9

Please sign in to comment.