Skip to content

Commit 5907f6a

Browse files
committed
feat(SF2.0/UpcomingDepartures): Cross out other_stops on cancelled trips
1 parent 535ff48 commit 5907f6a

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
@@ -206,6 +207,7 @@ defmodule Dotcom.ScheduleFinder.UpcomingDepartures do
206207
stop = ps |> PredictedSchedule.stop()
207208

208209
%OtherStop{
210+
cancelled?: cancelled?(ps),
209211
stop_id: stop.id,
210212
stop_name: stop.name,
211213
time: prediction_time(ps)
@@ -229,6 +231,16 @@ defmodule Dotcom.ScheduleFinder.UpcomingDepartures do
229231
defp prediction_time(%PredictedSchedule{schedule: schedule}) when schedule != nil,
230232
do: prediction_time(schedule)
231233

234+
defp cancelled?(%PredictedSchedule{schedule: schedule, prediction: prediction})
235+
when prediction != nil and schedule != nil do
236+
schedule_time = prediction_time(schedule)
237+
prediction_time = prediction_time(prediction)
238+
239+
schedule_time != nil && prediction_time == nil
240+
end
241+
242+
defp cancelled?(_), do: false
243+
232244
defp arrival_status(%{
233245
predicted_schedule: %PredictedSchedule{prediction: nil},
234246
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

@@ -1730,6 +1745,84 @@ defmodule Dotcom.ScheduleFinder.UpcomingDeparturesTest do
17301745
]
17311746
end
17321747

1748+
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
1749+
# Setup
1750+
now = Dotcom.Utils.DateTime.now()
1751+
1752+
route = Factories.Routes.Route.build(:route)
1753+
route_id = route.id
1754+
1755+
stop_ids =
1756+
Faker.Util.sample_uniq(2, fn -> FactoryHelpers.build(:id) end)
1757+
1758+
[stop, stop_after] =
1759+
stop_ids |> Enum.map(&Factories.Stops.Stop.build(:stop, id: &1))
1760+
1761+
trip_id = FactoryHelpers.build(:id)
1762+
trip = Factories.Schedules.Trip.build(:trip, id: trip_id)
1763+
direction_id = Faker.Util.pick([0, 1])
1764+
1765+
arrival_time_offsets =
1766+
Faker.Util.sample_uniq(2, fn -> Faker.random_between(2, 59) end) |> Enum.sort()
1767+
1768+
[arrival_time, arrival_time_after] =
1769+
arrival_time_offsets |> Enum.map(&(now |> DateTime.shift(minute: &1)))
1770+
1771+
expect(Predictions.Repo.Mock, :all, fn [
1772+
route: ^route_id,
1773+
direction_id: ^direction_id,
1774+
include_terminals: true
1775+
] ->
1776+
[
1777+
Factories.Predictions.Prediction.build(:prediction,
1778+
arrival_time: arrival_time,
1779+
stop: stop,
1780+
trip: trip
1781+
),
1782+
Factories.Predictions.Prediction.build(:prediction,
1783+
arrival_time: nil,
1784+
departure_time: nil,
1785+
stop: stop_after,
1786+
trip: trip
1787+
)
1788+
]
1789+
end)
1790+
1791+
expect(Schedules.Repo.Mock, :by_route_ids, fn
1792+
[^route_id], direction_id: ^direction_id, date: _date ->
1793+
[
1794+
Factories.Schedules.Schedule.build(:schedule,
1795+
stop: stop,
1796+
trip: trip
1797+
),
1798+
Factories.Schedules.Schedule.build(:schedule,
1799+
arrival_time: arrival_time_after,
1800+
departure_time: arrival_time_after |> DateTime.shift(second: 30),
1801+
time: arrival_time_after,
1802+
stop: stop_after,
1803+
trip: trip
1804+
)
1805+
]
1806+
end)
1807+
1808+
# Exercise
1809+
departures =
1810+
UpcomingDepartures.upcoming_departures(%{
1811+
direction_id: direction_id,
1812+
now: now,
1813+
route: route,
1814+
stop_id: stop.id
1815+
})
1816+
1817+
# Verify
1818+
assert [departure] = departures
1819+
trip_details = departure.trip_details
1820+
1821+
assert [stop_after] = trip_details.stops_after
1822+
assert stop_after.time == arrival_time_after
1823+
assert stop_after.cancelled?
1824+
end
1825+
17331826
test "does not include upcoming departures for other stops" do
17341827
# Setup
17351828
now = Dotcom.Utils.DateTime.now()

0 commit comments

Comments
 (0)