Skip to content

Commit

Permalink
fix: missing CR route abbreviation for Foxboro
Browse files Browse the repository at this point in the history
This also changes the logic to omit the abbreviation and log a warning
instead of crashing when it encounters a Commuter Rail route we haven't
heard of, since the fallback of showing a CR icon is reasonable and this
issue should not prevent the whole screen/section from working.
  • Loading branch information
digitalcora committed Jun 28, 2024
1 parent 13b4be0 commit d743e01
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
27 changes: 18 additions & 9 deletions lib/screens/v2/widget_instance/serializer/route_pill.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Screens.V2.WidgetInstance.Serializer.RoutePill do
@moduledoc false

require Logger

alias Screens.Routes.Route
alias Screens.RouteType

Expand Down Expand Up @@ -37,18 +39,19 @@ defmodule Screens.V2.WidgetInstance.Serializer.RoutePill do
@type icon :: :bus | :light_rail | :rail | :boat

@cr_line_abbreviations %{
"Fairmount" => "FMT",
"Fitchburg" => "FBG",
"Foxboro" => "FOX",
"Franklin" => "FRK",
"Greenbush" => "GRB",
"Haverhill" => "HVL",
"Newburyport" => "NBP",
"Kingston" => "KNG",
"Lowell" => "LWL",
"Fitchburg" => "FBG",
"Worcester" => "WOR",
"Middleborough" => "MID",
"Needham" => "NDM",
"Franklin" => "FRK",
"Newburyport" => "NBP",
"Providence" => "PVD",
"Fairmount" => "FMT",
"Middleborough" => "MID",
"Kingston" => "KNG",
"Greenbush" => "GRB"
"Worcester" => "WOR"
}

@special_bus_route_names %{
Expand Down Expand Up @@ -201,7 +204,13 @@ defmodule Screens.V2.WidgetInstance.Serializer.RoutePill do
defp do_serialize("Blue", _), do: %{type: :text, text: "BL"}

defp do_serialize("CR-" <> line, opts) do
base = %{route_abbrev: Map.fetch!(@cr_line_abbreviations, line)}
abbreviation =
Map.get_lazy(@cr_line_abbreviations, line, fn ->
Logger.warning("missing route pill abbreviation for CR-" <> line)
nil
end)

base = %{route_abbrev: abbreviation}

if track_number = opts[:track_number],
do: Map.merge(base, %{type: :text, text: "TR#{track_number}"}),
Expand Down
11 changes: 11 additions & 0 deletions test/screens/v2/widget_instance/serializer/route_pill_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Screens.V2.WidgetInstance.Serializer.RoutePillTest do
use ExUnit.Case, async: true

import ExUnit.CaptureLog
import Screens.V2.WidgetInstance.Serializer.RoutePill

describe "serialize_for_departure/4" do
Expand All @@ -14,6 +15,16 @@ defmodule Screens.V2.WidgetInstance.Serializer.RoutePillTest do
serialize_for_departure("CR-Fairmount", "", :rail, 3)
end

test "Returns no abbreviation and logs a warning for an unknown CR route" do
logs =
capture_log([level: :warning], fn ->
assert %{type: :icon, icon: :rail, color: :purple, route_abbrev: nil} ==
serialize_for_departure("CR-Foobar", "", :rail, nil)
end)

assert logs =~ "missing route pill abbreviation for CR-Foobar"
end

test "Returns boat icon if route type is :ferry" do
assert %{type: :icon, icon: :boat, color: :teal} ==
serialize_for_departure("Boat-F1", "", :ferry, nil)
Expand Down

0 comments on commit d743e01

Please sign in to comment.