diff --git a/rust/bambam/src/model/frontier/multimodal/constraint.rs b/rust/bambam/src/model/frontier/multimodal/constraint.rs index fe6594b9..e6b93718 100644 --- a/rust/bambam/src/model/frontier/multimodal/constraint.rs +++ b/rust/bambam/src/model/frontier/multimodal/constraint.rs @@ -89,7 +89,7 @@ impl MultimodalFrontierConstraint { })?; let n_legs = match active_mode { Some(active_mode) if active_mode != edge_mode => n_existing_legs + 1, - _ => 0, + _ => n_existing_legs, }; let is_valid = n_legs <= *max_legs; Ok(is_valid) diff --git a/rust/bambam/src/model/frontier/multimodal/model.rs b/rust/bambam/src/model/frontier/multimodal/model.rs index e1b4aab2..eddf1f1f 100644 --- a/rust/bambam/src/model/frontier/multimodal/model.rs +++ b/rust/bambam/src/model/frontier/multimodal/model.rs @@ -871,4 +871,42 @@ mod test { .expect("test failed"); assert!(!is_valid); // Should be invalid as this would create a second leg } + + #[test] + fn test_max_trip_legs_same_mode_continuation_at_limit() { + // Test that continuing with the same mode when at the limit is still invalid + // This tests the bug fix where same-mode continuation was always returning 0 legs + + // max_trip_legs is the state buffer size, constraint is the actual limit + let max_trip_legs = 2; // State buffer can hold 2 legs + let constraint_limit = 1; // But we only allow 1 leg + + let (bike_mtm, bike_mfm, state_model, mut state) = test_setup( + vec![MultimodalFrontierConstraint::MaxTripLegs(constraint_limit)], + "bike", // FrontierModel for bike edges + &["walk", "bike"], + &[], + max_trip_legs, + ); + + // Set up state with 2 legs: walk then bike (exceeds constraint_limit of 1) + inject_trip_legs( + &["walk", "bike"], + &mut state, + &state_model, + &bike_mtm.mode_to_state, + max_trip_legs, + ); + + // Test continuing with bike-mode edge (same as active mode) + // edge.edge_list_id doesn't matter since we're just checking constraints, not traversal + // The important thing is that bike_mfm has mode="bike" which matches active_mode="bike" + // Before the fix, this would incorrectly return n_legs=0 and be valid + // After the fix, this should correctly use n_existing_legs=2 and be invalid + let bike_edge = Edge::new(0, 0, 0, 1, Length::new::(1000.0)); + let is_valid = bike_mfm + .valid_frontier(&bike_edge, None, &state, &state_model) + .expect("test failed"); + assert!(!is_valid); // Should be invalid as we already have 2 legs, which exceeds constraint_limit of 1 + } }