@@ -217,6 +217,34 @@ def volunteer_with_mixed_availability(db_session, another_volunteer):
217217 return another_volunteer
218218
219219
220+ @pytest .fixture
221+ def volunteer_with_alt_availability (db_session ):
222+ """Create a different volunteer with distinct availability."""
223+ now = datetime .now (timezone .utc )
224+ tomorrow = now + timedelta (days = 2 )
225+
226+ volunteer = User (
227+ first_name = "Alt" ,
228+ last_name = "Volunteer" ,
229+ 230+ role_id = 2 ,
231+ auth_id = "volunteer_alt_auth_id" ,
232+ )
233+ db_session .add (volunteer )
234+ db_session .flush ()
235+
236+ slots = [
237+ tomorrow .replace (hour = 9 , minute = 0 , second = 0 , microsecond = 0 ),
238+ tomorrow .replace (hour = 9 , minute = 30 , second = 0 , microsecond = 0 ),
239+ ]
240+ for slot in slots :
241+ volunteer .availability .append (TimeBlock (start_time = slot ))
242+
243+ db_session .commit ()
244+ db_session .refresh (volunteer )
245+ return volunteer
246+
247+
220248@pytest .fixture
221249def sample_match (db_session , participant_user , volunteer_user ):
222250 """Create a pending match with suggested times."""
@@ -1444,3 +1472,49 @@ async def test_update_match_invalid_match_404(self, db_session):
14441472
14451473 assert exc_info .value .status_code == 404
14461474 assert "Match" in exc_info .value .detail
1475+
1476+ @pytest .mark .asyncio
1477+ async def test_update_match_reassigns_volunteer_resets_suggested_times (
1478+ self ,
1479+ db_session ,
1480+ participant_user ,
1481+ volunteer_with_availability ,
1482+ volunteer_with_alt_availability ,
1483+ ):
1484+ """Changing the volunteer regenerates suggested times and clears chosen slot"""
1485+ try :
1486+ match_service = MatchService (db_session )
1487+
1488+ # Create match with first volunteer and schedule it
1489+ create_request = MatchCreateRequest (
1490+ participant_id = participant_user .id ,
1491+ volunteer_ids = [volunteer_with_availability .id ],
1492+ )
1493+ response = await match_service .create_matches (create_request )
1494+ match_id = response .matches [0 ].id
1495+
1496+ match = db_session .get (Match , match_id )
1497+ time_block_id = match .suggested_time_blocks [0 ].id
1498+ await match_service .schedule_match (match_id , time_block_id , acting_participant_id = participant_user .id )
1499+
1500+ db_session .refresh (match )
1501+ assert match .match_status .name == "confirmed"
1502+ assert match .chosen_time_block_id is not None
1503+
1504+ # Reassign to alternate volunteer
1505+ update_request = MatchUpdateRequest (volunteer_id = volunteer_with_alt_availability .id )
1506+ await match_service .update_match (match_id , update_request )
1507+
1508+ db_session .refresh (match )
1509+ assert match .volunteer_id == volunteer_with_alt_availability .id
1510+ assert match .chosen_time_block_id is None
1511+ assert match .match_status .name == "pending"
1512+
1513+ starts = sorted (block .start_time for block in match .suggested_time_blocks )
1514+ expected = [slot .start_time for slot in volunteer_with_alt_availability .availability ]
1515+ assert starts == expected
1516+
1517+ db_session .commit ()
1518+ except Exception :
1519+ db_session .rollback ()
1520+ raise
0 commit comments