From cb389d9f9fbd661384bdbfc07eb91edb8fa44da6 Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Mon, 26 Jan 2026 15:32:38 -0500 Subject: [PATCH] fix: Have `endpoint_stops/2` not crash when given an empty set of routes --- lib/dotcom/alerts/endpoint_stops.ex | 12 +++++++++--- test/dotcom/alerts/endpoint_stops_test.exs | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/dotcom/alerts/endpoint_stops.ex b/lib/dotcom/alerts/endpoint_stops.ex index 0e6cb2ddb6..3d3d07b308 100644 --- a/lib/dotcom/alerts/endpoint_stops.ex +++ b/lib/dotcom/alerts/endpoint_stops.ex @@ -23,9 +23,15 @@ defmodule Dotcom.Alerts.EndpointStops do # stop-range per alert, and returns the non-nil ones. @impl Behaviour def endpoint_stops(alerts, route_ids) do - alerts - |> Enum.map(&endpoint_stops_for_alert(&1, route_ids)) - |> Enum.reject(&Kernel.is_nil/1) + route_ids = route_ids |> Enum.reject(&Kernel.is_nil/1) + + if route_ids |> Enum.empty?() do + [] + else + alerts + |> Enum.map(&endpoint_stops_for_alert(&1, route_ids)) + |> Enum.reject(&Kernel.is_nil/1) + end end # Returns a single endpoint stop-range for the alert given, or nil diff --git a/test/dotcom/alerts/endpoint_stops_test.exs b/test/dotcom/alerts/endpoint_stops_test.exs index f6a6aa8145..6aded9b00b 100644 --- a/test/dotcom/alerts/endpoint_stops_test.exs +++ b/test/dotcom/alerts/endpoint_stops_test.exs @@ -14,7 +14,9 @@ defmodule Dotcom.Alerts.EndpointStopsTest do } setup do - Routes.Repo.Mock |> stub(:get, fn _ -> Route.build(:route) end) + Routes.Repo.Mock + |> stub(:get, fn route_id when is_binary(route_id) -> Route.build(:route) end) + verify_on_exit!() :ok end @@ -27,6 +29,17 @@ defmodule Dotcom.Alerts.EndpointStopsTest do assert EndpointStops.endpoint_stops([alert], [route_id]) == [] end + # [nil] is what you get if you pull the route-informed-entities + # out of an alert that isn't associated with any route. Sometimes + # those route-informed-entities are what get passed back into the + # endpoint_stops/2, which is why [nil] is a relevant input to test + # here. + test "returns empty list if the route ID's provided are [nil]" do + alert = Alert.new(informed_entity: [%InformedEntity{stop: nil}]) + + assert EndpointStops.endpoint_stops([alert], [nil]) == [] + end + test "returns the same stop twice if an alert's informed entities includes precisely one stop" do # Setup route_id = FactoryHelpers.build(:id)