From 94b205200036ed982d7347afeac16ecbaf7d53a4 Mon Sep 17 00:00:00 2001 From: sloane <1699281+sloanelybutsurely@users.noreply.github.com> Date: Thu, 16 May 2024 13:36:21 -0400 Subject: [PATCH] handle cases where only one prediction exists use with and pattern matching to handle sequence of possible errors --- lib/screens/v2/widget_instance/line_map.ex | 21 +++++++++---------- .../v2/widget_instance/line_map_test.exs | 6 ++++++ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/screens/v2/widget_instance/line_map.ex b/lib/screens/v2/widget_instance/line_map.ex index 5bfba6a18..6d9a70ebe 100644 --- a/lib/screens/v2/widget_instance/line_map.ex +++ b/lib/screens/v2/widget_instance/line_map.ex @@ -265,9 +265,6 @@ defmodule Screens.V2.WidgetInstance.LineMap do def serialize_scheduled_departure(_departures, _direction_id, _stops, true = _is_terminal?), do: nil - def serialize_scheduled_departure([] = _departures, _direction_id, _stops, _is_terminal?), - do: nil - def serialize_scheduled_departure(departures, direction_id, stops, _is_terminal?) do # Number of departures with predictions (not just schedules) in this direction prediction_count = @@ -282,16 +279,18 @@ defmodule Screens.V2.WidgetInstance.LineMap do if prediction_count < 2 do %{name: origin_stop_name} = Enum.at(stops, 0) - {:ok, local_time} = - departures - |> Enum.filter(fn d -> is_nil(d.prediction) end) - |> Enum.at(0) - |> Departure.time() - |> DateTime.shift_zone("America/New_York") + if departure = Enum.find(departures, &is_nil(&1.prediction)) do + {:ok, local_time} = + departure + |> Departure.time() + |> DateTime.shift_zone("America/New_York") - {:ok, timestamp} = Timex.format(local_time, "{h12}:{m}") + {:ok, timestamp} = Timex.format(local_time, "{h12}:{m}") - %{timestamp: timestamp, station_name: origin_stop_name} + %{timestamp: timestamp, station_name: origin_stop_name} + else + nil + end else nil end diff --git a/test/screens/v2/widget_instance/line_map_test.exs b/test/screens/v2/widget_instance/line_map_test.exs index 8629751c4..81c83407b 100644 --- a/test/screens/v2/widget_instance/line_map_test.exs +++ b/test/screens/v2/widget_instance/line_map_test.exs @@ -308,6 +308,12 @@ defmodule Screens.V2.WidgetInstance.LineMapTest do assert nil == LineMap.serialize_scheduled_departure([], direction_id, stops, false) end + test "returns nil when there are only predictions", %{stops: stops} do + direction_id = 1 + d1 = %Departure{prediction: %Prediction{trip: %Trip{direction_id: direction_id}}} + assert nil == LineMap.serialize_scheduled_departure([d1], direction_id, stops, false) + end + test "returns the correct origin", %{stops: stops} do direction_id = 1