Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/dotcom/schedule_finder/upcoming_departures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,14 @@ defmodule Dotcom.ScheduleFinder.UpcomingDepartures do
predicted_time == nil ->
prediction.schedule_relationship

DateTime.diff(scheduled_time, predicted_time, :second) |> abs() < 60 ->
:on_time
DateTime.diff(predicted_time, scheduled_time, :second) < -60 ->
{:early_from, scheduled_time}

DateTime.diff(predicted_time, scheduled_time, :second) > 60 ->
{:delayed_from, scheduled_time}

true ->
{:scheduled_at, scheduled_time}
:on_time
end
end
end
24 changes: 21 additions & 3 deletions lib/dotcom_web/live/schedule_finder_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -846,11 +846,29 @@ defmodule DotcomWeb.ScheduleFinderLive do

defp prediction_substatus_display(%{arrival_substatus: nil} = assigns), do: ~H""

defp prediction_substatus_display(%{arrival_substatus: {:scheduled_at, time}} = assigns) do
assigns = assigns |> assign(:time, time)
defp prediction_substatus_display(%{arrival_substatus: {:delayed_from, time}} = assigns) do
assigns =
assigns
|> assign(:time, time)
|> assign(:readout_time, time |> format!(:hour_12_minutes))

~H"""
<span class="text-xs line-through" aria-label={"Delayed from #{@readout_time}"}>
<.formatted_time time={@time} />
</span>
"""
end

defp prediction_substatus_display(%{arrival_substatus: {:early_from, time}} = assigns) do
assigns =
assigns
|> assign(:time, time)
|> assign(:readout_time, time |> format!(:hour_12_minutes))

~H"""
<span class="text-xs line-through">{format!(@time, :hour_12_minutes)}</span>
<span class="text-xs line-through" aria-label={"Early; Originally scheduled at #{@readout_time}"}>
<.formatted_time time={@time} />
</span>
Comment thread
thecristen marked this conversation as resolved.
"""
end

Expand Down
129 changes: 126 additions & 3 deletions test/dotcom/schedule_finder/upcoming_departures_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,66 @@ defmodule Dotcom.ScheduleFinder.UpcomingDeparturesTest do
]
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 == :on_time
end

test "shows :on_time for commuter rail if there is no schedule" 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(-59, 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),
trip: trip
)
]
end)

expect(Schedules.Repo.Mock, :by_route_ids, fn
_, _ ->
[]
Expand All @@ -1256,7 +1316,70 @@ defmodule Dotcom.ScheduleFinder.UpcomingDeparturesTest do
assert departure.arrival_substatus == :on_time
end

test "shows {:scheduled_at, scheduled_time} for commuter rail if predicted and scheduled times differ by more than a minute" do
test "shows {:early_from, scheduled_time} for commuter rail if predicted and scheduled times differ by more than a minute" 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),
trip: trip
)
]
end)

expect(Schedules.Repo.Mock, :by_route_ids, fn
[^route_id], direction_id: ^direction_id, date: date ->
assert date == ServiceDateTime.service_date(now)

[
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 == {:early_from, scheduled_departure_time}
end

test "shows {:delayed_from, scheduled_time} for commuter rail if predicted time is more than a minute late" do
# Setup
now = Dotcom.Utils.DateTime.now()

Expand All @@ -1274,7 +1397,7 @@ defmodule Dotcom.ScheduleFinder.UpcomingDeparturesTest do

predicted_departure_time =
scheduled_departure_time
|> DateTime.shift(second: Faker.Util.pick([1, -1]) * Faker.random_between(60, 3600))
|> DateTime.shift(second: Faker.random_between(60, 3600))

expect(Predictions.Repo.Mock, :all, fn [
route: ^route_id,
Expand Down Expand Up @@ -1316,7 +1439,7 @@ defmodule Dotcom.ScheduleFinder.UpcomingDeparturesTest do
# Verify
assert [departure] = departures
assert departure.arrival_status == {:time, predicted_departure_time}
assert departure.arrival_substatus == {:scheduled_at, scheduled_departure_time}
assert departure.arrival_substatus == {:delayed_from, scheduled_departure_time}
end

test "shows :scheduled for commuter rail if there is no prediction" do
Expand Down
Loading