diff --git a/shared/src/commonMain/kotlin/com/mbta/tid/mbta_app/model/Alert.kt b/shared/src/commonMain/kotlin/com/mbta/tid/mbta_app/model/Alert.kt index 4530a2e9b..5311003d2 100644 --- a/shared/src/commonMain/kotlin/com/mbta/tid/mbta_app/model/Alert.kt +++ b/shared/src/commonMain/kotlin/com/mbta/tid/mbta_app/model/Alert.kt @@ -239,8 +239,9 @@ data class Alert( } /** - * Gets the alerts of the first stop that is downstream of the target stop which has alerts. - * Considers only alerts that have specified stops. + * Gets the alerts of the first stop that is downstream of the target stop which has any + * alerts that are different from the alerts at the target stop. Considers only alerts that + * have specified stops. * * @param alerts: The full list of alerts * @param trip: The trip used to calculate downstream stops @@ -255,6 +256,16 @@ data class Alert( val alerts = alerts.filter { it.hasStopsSpecified } + val targetStopAlertIds = + applicableAlerts( + alerts.toList(), + trip.directionId, + listOf(trip.routeId), + targetStopWithChildren + ) + .map { it.id } + .toSet() + val indexOfTargetStopInPattern = stopIds.indexOfFirst { targetStopWithChildren.contains(it) } if (indexOfTargetStopInPattern != -1 && indexOfTargetStopInPattern < stopIds.size - 1) { @@ -262,12 +273,15 @@ data class Alert( val firstStopAlerts = downstreamStops .map { stop -> - applicableAlerts( - alerts.toList() ?: listOf(), - trip.directionId, - listOf(trip.routeId), - setOf(stop) - ) + val alerts = + applicableAlerts( + alerts.toList(), + trip.directionId, + listOf(trip.routeId), + setOf(stop) + ) + .filter { !targetStopAlertIds.contains(it.id) } + alerts } .firstOrNull { alerts -> !alerts.isNullOrEmpty() } ?: listOf() diff --git a/shared/src/commonTest/kotlin/com/mbta/tid/mbta_app/model/AlertTest.kt b/shared/src/commonTest/kotlin/com/mbta/tid/mbta_app/model/AlertTest.kt index 127da1f8c..b83eef313 100644 --- a/shared/src/commonTest/kotlin/com/mbta/tid/mbta_app/model/AlertTest.kt +++ b/shared/src/commonTest/kotlin/com/mbta/tid/mbta_app/model/AlertTest.kt @@ -318,6 +318,70 @@ class AlertTest { } + @Test + fun `downstreamAlerts excludes alerts affecting the target stop`() { + val objects = ObjectCollectionBuilder() + val route = objects.route() + val targetStop = objects.stop() + val downstreamStop1 = objects.stop() + val downstreamStop2 = objects.stop() + + val alertAllStops = + objects.alert { + informedEntity( + listOf(Alert.InformedEntity.Activity.Board), + route = route.id, + stop = targetStop.id, + directionId = 0 + ) + informedEntity( + listOf(Alert.InformedEntity.Activity.Board), + route = route.id, + stop = downstreamStop1.id, + directionId = 0 + ) + informedEntity( + listOf(Alert.InformedEntity.Activity.Board), + route = route.id, + stop = downstreamStop2.id, + directionId = 0 + ) + } + + val alertDownstream2Only = + objects.alert { + informedEntity( + listOf(Alert.InformedEntity.Activity.Board), + route = route.id, + stop = downstreamStop2.id, + directionId = 0 + ) + } + + val trip = + objects.trip { + routeId = route.id + directionId = 0 + stopIds = + listOf( + targetStop.id, + downstreamStop1.id, + downstreamStop2.id, + ) + } + + val downstreamAlerts = + Alert.downstreamAlerts( + listOf(alertAllStops, alertDownstream2Only), + trip, + setOf(targetStop.id) + ) + + assertEquals(listOf(alertDownstream2Only), downstreamAlerts) + } + + + @Test fun `downstreamAlerts ignores alert without stops specified`() { val objects = ObjectCollectionBuilder() diff --git a/shared/src/commonTest/kotlin/com/mbta/tid/mbta_app/model/NearbyResponseTest.kt b/shared/src/commonTest/kotlin/com/mbta/tid/mbta_app/model/NearbyResponseTest.kt index f82d158f6..3d11e720a 100644 --- a/shared/src/commonTest/kotlin/com/mbta/tid/mbta_app/model/NearbyResponseTest.kt +++ b/shared/src/commonTest/kotlin/com/mbta/tid/mbta_app/model/NearbyResponseTest.kt @@ -3108,7 +3108,7 @@ class NearbyResponseTest { ) ), alertsHere = listOf(alert), - alertsDownstream = listOf(alert), + alertsDownstream = emptyList(), hasSchedulesToday = false ), )