Skip to content

Commit

Permalink
fix(TimetableController): update via-Fairmount trains (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecristen authored Oct 10, 2023
1 parent a8c3682 commit 09f99fc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,45 @@ defmodule SiteWeb.ScheduleController.TimetableController do
"""
@spec trip_messages(Routes.Route.t(), 0 | 1) :: %{{String.t(), String.t()} => String.t()}
def trip_messages(%Routes.Route{id: "CR-Franklin"}, 0) do
train = "731"
["741", "757", "759", "735"]
|> Enum.flat_map(&franklin_via_fairmount(&1, 0))
|> Enum.into(%{})
end

def trip_messages(%Routes.Route{id: "CR-Franklin"}, 1) do
["740", "728", "758", "732", "760"]
|> Enum.flat_map(&franklin_via_fairmount(&1, 1))
|> Enum.into(%{})
end

def trip_messages(_, _) do
%{}
end

defp franklin_via_fairmount(train, direction) do
stops = stops_for_fairmount(direction)

[
List.duplicate(train, 4),
["place-bbsta", "place-rugg", "place-NEC-2203", "place-DB-0095"],
["Via", "Fair-", "mount", "Line"]
List.duplicate(train, length(stops)),
stops,
["Via", "Fair-", "mount", "Line", "-"]
]
|> make_via_list()
|> Map.put({train}, "Via Fairmount Line")
|> Enum.concat([{{train}, "Via Fairmount Line"}])
end

def trip_messages(_, _) do
%{}
defp stops_for_fairmount(1) do
["place-DB-0095", "place-forhl", "place-rugg", "place-bbsta"]
end

defp stops_for_fairmount(0) do
["place-bbsta", "place-rugg", "place-forhl", "place-NEC-2203", "place-DB-0095"]
end

def make_via_list(list) do
list
|> List.zip()
|> Map.new(fn {train, stop, value} -> {{train, stop}, value} end)
|> Enum.map(fn {train, stop, value} -> {{train, stop}, value} end)
end

defp all_stops(conn, _) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,20 @@ defmodule SiteWeb.ScheduleController.TimetableControllerTest do
end

describe "trip_messages/2" do
test "returns proper messages for CR Franklin" do
assert trip_messages(%Routes.Route{id: "CR-Franklin"}, 1) == %{}

assert [
"731"
] ==
test "returns proper messages for a CR Franklin train running via Fairmount" do
assert Enum.member?(
%Routes.Route{id: "CR-Franklin"}
|> trip_messages(0)
|> Map.keys()
|> Enum.map(&elem(&1, 0))
|> Enum.uniq()
|> Enum.sort()
|> Enum.sort(),
"735"
)
end

test "returns proper messages for CR Fairmount" do
assert trip_messages(%Routes.Route{id: "CR-Fairmount"}, 1) == %{}
test "returns proper messages for others" do
assert trip_messages(%Routes.Route{id: "CR-Worcester"}, 1) == %{}
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ defmodule SiteWeb.ScheduleControllerTest do
assert conn.assigns.trip_messages
end

@doc """
FIXME: The map_size will change whenever the schedule changes the number of trains needing these messages.
"""
test "assigns trip messages for a few route/directions", %{conn: conn} do
for {route_id, direction_id, expected_size} <- [
{"CR-Franklin", 0, 5}
{"CR-Franklin", 0, 24},
{"CR-Franklin", 1, 25}
] do
path =
timetable_path(conn, :show, route_id, schedule_direction: %{direction_id: direction_id})
Expand Down

0 comments on commit 09f99fc

Please sign in to comment.