diff --git a/lib/dotcom_web/channels/predictions_channel.ex b/lib/dotcom_web/channels/predictions_channel.ex index 77e04e4322..6595d5bf37 100644 --- a/lib/dotcom_web/channels/predictions_channel.ex +++ b/lib/dotcom_web/channels/predictions_channel.ex @@ -49,6 +49,7 @@ defmodule DotcomWeb.PredictionsChannel do no_departure_time?(prediction) || skipped_or_cancelled?(prediction) end) + |> Enum.filter(&in_future_seconds?/1) end defp no_trip?(prediction), do: is_nil(prediction.trip) @@ -61,4 +62,8 @@ defmodule DotcomWeb.PredictionsChannel do Route.subway?(prediction.route.type, prediction.route.id) && prediction.schedule_relationship in [:cancelled, :skipped] end + + defp in_future_seconds?(prediction) do + Timex.compare(prediction.departure_time, Util.now(), :seconds) >= 0 + end end diff --git a/test/dotcom_web/channels/predictions_channel_test.exs b/test/dotcom_web/channels/predictions_channel_test.exs index d3d7c54e89..9e16c5b664 100644 --- a/test/dotcom_web/channels/predictions_channel_test.exs +++ b/test/dotcom_web/channels/predictions_channel_test.exs @@ -86,6 +86,31 @@ defmodule DotcomWeb.PredictionsChannelTest do # Verify assert_push("data", %{predictions: ^predictions}) end + + test "filters out past predictions", context do + # Setup + now = Timex.now() |> Timex.shift(seconds: 1) + past = Timex.shift(now, seconds: -15) + future = Timex.shift(now, seconds: 15) + + predictions = + [past, now, future] + |> Enum.map(&Prediction.build(:canonical_prediction, %{departure_time: &1})) + + expect(@predictions_pub_sub, :subscribe, fn _ -> + predictions + end) + + {:ok, _, socket} = subscribe_and_join(context.socket, PredictionsChannel, context.channel) + + # Exercise + PredictionsChannel.handle_info({:new_predictions, predictions}, socket) + + [_past_prediction | expected_predictions] = predictions + + # Verify + assert_push("data", %{predictions: ^expected_predictions}) + end end describe "terminate/2" do