diff --git a/lib/dotcom/schedule_finder/upcoming_departures.ex b/lib/dotcom/schedule_finder/upcoming_departures.ex index 1402c4d33a..9330d5a08b 100644 --- a/lib/dotcom/schedule_finder/upcoming_departures.ex +++ b/lib/dotcom/schedule_finder/upcoming_departures.ex @@ -460,7 +460,7 @@ defmodule Dotcom.ScheduleFinder.UpcomingDepartures do defp arrival_substatus(%{ predicted_schedule: %PredictedSchedule{prediction: %Prediction{status: status}} }) - when status != nil, + when status != nil and status != "Delayed", do: {:status, status |> String.split(" ") |> Enum.map_join(" ", &String.capitalize/1)} defp arrival_substatus(%{ @@ -473,6 +473,7 @@ defmodule Dotcom.ScheduleFinder.UpcomingDepartures do }) do scheduled_time = schedule.departure_time predicted_time = prediction.departure_time + status = prediction.status cond do predicted_time == nil -> @@ -484,6 +485,9 @@ defmodule Dotcom.ScheduleFinder.UpcomingDepartures do DateTime.diff(predicted_time, scheduled_time, :second) >= 60 -> {:delayed_from, scheduled_time} + status != nil -> + {:status, status} + true -> :on_time end diff --git a/test/dotcom/schedule_finder/upcoming_departures_test.exs b/test/dotcom/schedule_finder/upcoming_departures_test.exs index 7167268900..5bd0014e78 100644 --- a/test/dotcom/schedule_finder/upcoming_departures_test.exs +++ b/test/dotcom/schedule_finder/upcoming_departures_test.exs @@ -1858,6 +1858,130 @@ defmodule Dotcom.ScheduleFinder.UpcomingDeparturesTest do assert departure.arrival_substatus == {:status, display_status} end + test "shows {:status, 'Delayed'} for commuter rail if the status is 'Delayed', but the predicted time is less than a minute late" do + # Setup + now = Dotcom.Utils.DateTime.now() + + route = Factories.Routes.Route.build(:commuter_rail_route) + route_id = route.id + stop_id = FactoryHelpers.build(:id) + + trip = Factories.Schedules.Trip.build(:trip) + direction_id = Faker.Util.pick([0, 1]) + + scheduled_departure_time = + Generators.DateTime.random_time_range_date_time( + {now, ServiceDateTime.end_of_service_day(now)} + ) + + predicted_departure_time = + scheduled_departure_time + |> DateTime.shift(second: Faker.random_between(0, 59)) + + expect(Predictions.Repo.Mock, :all, fn [ + route: ^route_id, + direction_id: ^direction_id, + include_terminals: true + ] -> + [ + Factories.Predictions.Prediction.build(:prediction, + departure_time: predicted_departure_time, + stop: Factories.Stops.Stop.build(:stop, id: stop_id), + status: "Delayed", + trip: trip + ) + ] + end) + + expect(Schedules.Repo.Mock, :by_route_ids, fn + _, _ -> + [ + Factories.Schedules.Schedule.build(:schedule, + departure_time: scheduled_departure_time, + time: scheduled_departure_time, + stop: Factories.Stops.Stop.build(:stop, id: stop_id), + trip: trip + ) + ] + end) + + # Exercise + departures = + UpcomingDepartures.upcoming_departures(%{ + direction_id: direction_id, + now: now, + route: route, + stop_id: stop_id + }) + + # Verify + assert [departure] = departures + assert departure.arrival_status == {:time, predicted_departure_time} + assert departure.arrival_substatus == {:status, "Delayed"} + end + + test "shows {:delayed_from, scheduled_time} for commuter rail if predicted time is more than a minute late even if the status is 'Delayed'" do + # Setup + now = Dotcom.Utils.DateTime.now() + + route = Factories.Routes.Route.build(:commuter_rail_route) + route_id = route.id + stop_id = FactoryHelpers.build(:id) + + trip = Factories.Schedules.Trip.build(:trip) + direction_id = Faker.Util.pick([0, 1]) + + scheduled_departure_time = + Generators.DateTime.random_time_range_date_time( + {now, ServiceDateTime.end_of_service_day(now)} + ) + + predicted_departure_time = + scheduled_departure_time + |> DateTime.shift(second: Faker.random_between(60, 3600)) + + expect(Predictions.Repo.Mock, :all, fn [ + route: ^route_id, + direction_id: ^direction_id, + include_terminals: true + ] -> + [ + Factories.Predictions.Prediction.build(:prediction, + departure_time: predicted_departure_time, + stop: Factories.Stops.Stop.build(:stop, id: stop_id), + status: "Delayed", + trip: trip + ) + ] + end) + + expect(Schedules.Repo.Mock, :by_route_ids, fn + _, _ -> + [ + Factories.Schedules.Schedule.build(:schedule, + departure_time: scheduled_departure_time, + time: scheduled_departure_time, + stop: Factories.Stops.Stop.build(:stop, id: stop_id), + trip: trip + ) + ] + end) + + # Exercise + departures = + UpcomingDepartures.upcoming_departures(%{ + direction_id: direction_id, + now: now, + route: route, + stop_id: stop_id + }) + + # Verify + assert [departure] = departures + assert departure.arrival_status == {:time, predicted_departure_time} + assert departure.arrival_substatus == {:delayed_from, scheduled_departure_time} + end + test "shows :cancelled for commuter rail or bus if the schedule_relationship is :cancelled or :skipped" do # Setup now = Dotcom.Utils.DateTime.now()