From dc2d8bee0ab513bfce06386973570cac0bd07d8a Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Thu, 12 Dec 2024 14:25:32 -0500 Subject: [PATCH] fix: De-duplicate start and end locations when they're next to transit segments (#2264) --- lib/dotcom/trip_plan/leg_to_segment_helper.ex | 8 +++++ .../trip_plan/leg_to_segment_helper_test.exs | 33 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/dotcom/trip_plan/leg_to_segment_helper.ex b/lib/dotcom/trip_plan/leg_to_segment_helper.ex index 0b6c6bbc0c..a3c17f31e5 100644 --- a/lib/dotcom/trip_plan/leg_to_segment_helper.ex +++ b/lib/dotcom/trip_plan/leg_to_segment_helper.ex @@ -25,6 +25,10 @@ defmodule Dotcom.TripPlan.LegToSegmentHelper do {:transit_segment, leg} end + defp prepend_start_location([{:transit_segment, _} | _] = segments) do + segments + end + defp prepend_start_location([{_, leg} | _] = segments) do [ {:location_segment, %{time: leg.start, place: leg.from}} @@ -32,6 +36,10 @@ defmodule Dotcom.TripPlan.LegToSegmentHelper do ] end + defp append_end_location([{:transit_segment, _} = last_segment]) do + [last_segment] + end + defp append_end_location([{_, leg} = last_segment]) do [last_segment, {:location_segment, %{time: leg.stop, place: leg.to}}] end diff --git a/test/dotcom/trip_plan/leg_to_segment_helper_test.exs b/test/dotcom/trip_plan/leg_to_segment_helper_test.exs index bef92dfd1f..55e7ae01f4 100644 --- a/test/dotcom/trip_plan/leg_to_segment_helper_test.exs +++ b/test/dotcom/trip_plan/leg_to_segment_helper_test.exs @@ -20,4 +20,37 @@ defmodule Dotcom.TripPlan.LegToSegmentHelperTest do %Leg{mode: %PersonalDetail{}} ]) end + + test "does not prepend location if the first segment is transit" do + assert [ + {:transit_segment, _}, + {:walking_segment, _}, + {:location_segment, _} + ] = + LegToSegmentHelper.legs_to_segments([ + %Leg{mode: %TransitDetail{}}, + %Leg{mode: %PersonalDetail{}} + ]) + end + + test "does not append location if the last segment is transit" do + assert [ + {:location_segment, _}, + {:walking_segment, _}, + {:transit_segment, _} + ] = + LegToSegmentHelper.legs_to_segments([ + %Leg{mode: %PersonalDetail{}}, + %Leg{mode: %TransitDetail{}} + ]) + end + + test "works if there is just one transit leg" do + assert [ + {:transit_segment, _} + ] = + LegToSegmentHelper.legs_to_segments([ + %Leg{mode: %TransitDetail{}} + ]) + end end