diff --git a/lib/realtime/ghost.ex b/lib/realtime/ghost.ex index 4817d34cb..2d095c599 100644 --- a/lib/realtime/ghost.ex +++ b/lib/realtime/ghost.ex @@ -9,7 +9,7 @@ defmodule Realtime.Ghost do trip_id: Trip.id(), headsign: String.t(), block_id: Block.id(), - run_id: Run.id(), + run_id: Run.id() | nil, via_variant: RoutePattern.via_variant() | nil, layover_departure_time: Util.Time.timestamp() | nil, scheduled_timepoint_status: TimepointStatus.timepoint_status(), diff --git a/lib/realtime/vehicle_or_ghost.ex b/lib/realtime/vehicle_or_ghost.ex index 473c10d4e..ac78afc4a 100644 --- a/lib/realtime/vehicle_or_ghost.ex +++ b/lib/realtime/vehicle_or_ghost.ex @@ -52,7 +52,9 @@ defmodule Realtime.VehicleOrGhost do |> matches?(text) end - @spec matches?(String.t(), String.t()) :: boolean() + @spec matches?(String.t() | nil, String.t()) :: boolean() + defp matches?(nil, _text), do: false + defp matches?("999-0" <> shuttle_run, text) do # special case to match shuttle runs even without the leading 0 matches?("999" <> shuttle_run, text) || matches?("9990" <> shuttle_run, text) diff --git a/test/realtime/vehicle_or_ghost_test.exs b/test/realtime/vehicle_or_ghost_test.exs index 2894918bd..17cfc3d3a 100644 --- a/test/realtime/vehicle_or_ghost_test.exs +++ b/test/realtime/vehicle_or_ghost_test.exs @@ -122,5 +122,11 @@ defmodule Realtime.VehicleOrGhostTest do test "short circuits to an empty result if trying to match on the empty string" do assert VehicleOrGhost.find_by(@vehicles, %{text: "", property: :all}) == [] end + + test "handles nil values" do + ghost_with_nil_run_id = %{@ghost | run_id: nil} + + assert VehicleOrGhost.find_by([ghost_with_nil_run_id], %{text: "710", property: :all}) == [] + end end end