Skip to content

Commit ad700d9

Browse files
committed
feat(SF2.0/UpcomingDepartures): Cross out other_stops on cancelled trips
1 parent bb8f487 commit ad700d9

File tree

3 files changed

+112
-7
lines changed

3 files changed

+112
-7
lines changed

lib/dotcom/schedule_finder/upcoming_departures.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ defmodule Dotcom.ScheduleFinder.UpcomingDepartures do
5252
"""
5353

5454
defstruct [
55+
:cancelled?,
5556
:stop_id,
5657
:stop_name,
5758
:time
@@ -204,6 +205,7 @@ defmodule Dotcom.ScheduleFinder.UpcomingDepartures do
204205
stop = ps |> PredictedSchedule.stop()
205206

206207
%OtherStop{
208+
cancelled?: cancelled?(ps),
207209
stop_id: stop.id,
208210
stop_name: stop.name,
209211
time: prediction_time(ps)
@@ -227,6 +229,16 @@ defmodule Dotcom.ScheduleFinder.UpcomingDepartures do
227229
defp prediction_time(%PredictedSchedule{schedule: schedule}) when schedule != nil,
228230
do: prediction_time(schedule)
229231

232+
defp cancelled?(%PredictedSchedule{schedule: schedule, prediction: prediction})
233+
when prediction != nil and schedule != nil do
234+
schedule_time = prediction_time(schedule)
235+
prediction_time = prediction_time(prediction)
236+
237+
schedule_time != nil && prediction_time == nil
238+
end
239+
240+
defp cancelled?(_), do: false
241+
230242
defp arrival_status(%{
231243
predicted_schedule: %PredictedSchedule{prediction: nil},
232244
route_type: :subway

lib/dotcom_web/live/schedule_finder_live.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ defmodule DotcomWeb.ScheduleFinderLive do
552552
<div class={["grow", @stop_id == @other_stop.stop_id && "font-bold"]}>
553553
{@other_stop.stop_name}
554554
</div>
555-
<div class={["ml-auto", @stop_id == @other_stop.stop_id && "font-bold"]}>
555+
<div class={["ml-auto", @stop_id == @other_stop.stop_id && "font-bold", @other_stop.cancelled? && "line-through"]}>
556556
{format!(@other_stop.time, :hour_12_minutes)}
557557
</div>
558558
</.lined_list_item>

test/dotcom/schedule_finder/upcoming_departures_test.exs

Lines changed: 99 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,16 +1416,31 @@ defmodule Dotcom.ScheduleFinder.UpcomingDeparturesTest do
14161416
trip_details = departure.trip_details
14171417

14181418
assert trip_details.stops_before
1419-
|> Enum.map(&(&1 |> Map.take([:stop_id, :stop_name, :time]))) == [
1420-
%{stop_id: stop_before.id, stop_name: stop_before.name, time: arrival_time_before}
1419+
|> Enum.map(&(&1 |> Map.take([:cancelled?, :stop_id, :stop_name, :time]))) == [
1420+
%{
1421+
cancelled?: false,
1422+
stop_id: stop_before.id,
1423+
stop_name: stop_before.name,
1424+
time: arrival_time_before
1425+
}
14211426
]
14221427

1423-
assert trip_details.stop |> Map.take([:stop_id, :stop_name, :time]) ==
1424-
%{stop_id: stop.id, stop_name: stop.name, time: arrival_time}
1428+
assert trip_details.stop |> Map.take([:cancelled?, :stop_id, :stop_name, :time]) ==
1429+
%{
1430+
cancelled?: false,
1431+
stop_id: stop.id,
1432+
stop_name: stop.name,
1433+
time: arrival_time
1434+
}
14251435

14261436
assert trip_details.stops_after
1427-
|> Enum.map(&(&1 |> Map.take([:stop_id, :stop_name, :time]))) == [
1428-
%{stop_id: stop_after.id, stop_name: stop_after.name, time: arrival_time_after}
1437+
|> Enum.map(&(&1 |> Map.take([:cancelled?, :stop_id, :stop_name, :time]))) == [
1438+
%{
1439+
cancelled?: false,
1440+
stop_id: stop_after.id,
1441+
stop_name: stop_after.name,
1442+
time: arrival_time_after
1443+
}
14291444
]
14301445
end
14311446

@@ -1648,6 +1663,84 @@ defmodule Dotcom.ScheduleFinder.UpcomingDeparturesTest do
16481663
]
16491664
end
16501665

1666+
test "shows an `other_stop` as cancelled if the time on its prediction is nil and the time on its schedule is non-nil" do
1667+
# Setup
1668+
now = Dotcom.Utils.DateTime.now()
1669+
1670+
route = Factories.Routes.Route.build(:route)
1671+
route_id = route.id
1672+
1673+
stop_ids =
1674+
Faker.Util.sample_uniq(2, fn -> FactoryHelpers.build(:id) end)
1675+
1676+
[stop, stop_after] =
1677+
stop_ids |> Enum.map(&Factories.Stops.Stop.build(:stop, id: &1))
1678+
1679+
trip_id = FactoryHelpers.build(:id)
1680+
trip = Factories.Schedules.Trip.build(:trip, id: trip_id)
1681+
direction_id = Faker.Util.pick([0, 1])
1682+
1683+
arrival_time_offsets =
1684+
Faker.Util.sample_uniq(2, fn -> Faker.random_between(2, 59) end) |> Enum.sort()
1685+
1686+
[arrival_time, arrival_time_after] =
1687+
arrival_time_offsets |> Enum.map(&(now |> DateTime.shift(minute: &1)))
1688+
1689+
expect(Predictions.Repo.Mock, :all, fn [
1690+
route: ^route_id,
1691+
direction_id: ^direction_id,
1692+
include_terminals: true
1693+
] ->
1694+
[
1695+
Factories.Predictions.Prediction.build(:prediction,
1696+
arrival_time: arrival_time,
1697+
stop: stop,
1698+
trip: trip
1699+
),
1700+
Factories.Predictions.Prediction.build(:prediction,
1701+
arrival_time: nil,
1702+
departure_time: nil,
1703+
stop: stop_after,
1704+
trip: trip
1705+
)
1706+
]
1707+
end)
1708+
1709+
expect(Schedules.Repo.Mock, :by_route_ids, fn
1710+
[^route_id], direction_id: ^direction_id, date: _date ->
1711+
[
1712+
Factories.Schedules.Schedule.build(:schedule,
1713+
stop: stop,
1714+
trip: trip
1715+
),
1716+
Factories.Schedules.Schedule.build(:schedule,
1717+
arrival_time: arrival_time_after,
1718+
departure_time: arrival_time_after |> DateTime.shift(second: 30),
1719+
time: arrival_time_after,
1720+
stop: stop_after,
1721+
trip: trip
1722+
)
1723+
]
1724+
end)
1725+
1726+
# Exercise
1727+
departures =
1728+
UpcomingDepartures.upcoming_departures(%{
1729+
direction_id: direction_id,
1730+
now: now,
1731+
route: route,
1732+
stop_id: stop.id
1733+
})
1734+
1735+
# Verify
1736+
assert [departure] = departures
1737+
trip_details = departure.trip_details
1738+
1739+
assert [stop_after] = trip_details.stops_after
1740+
assert stop_after.time == arrival_time_after
1741+
assert stop_after.cancelled?
1742+
end
1743+
16511744
test "does not include upcoming departures for other stops" do
16521745
# Setup
16531746
now = Dotcom.Utils.DateTime.now()

0 commit comments

Comments
 (0)