@@ -440,18 +440,13 @@ def test_search_routes_with_ai_insights_success(client: TestClient, sample_itine
440440 with patch ("app.api.v1.endpoints.routes.ai_agents_service" ) as mock_ai_service :
441441 mock_routing_service .get_itinaries = AsyncMock (return_value = sample_itineraries )
442442
443- # Mock AI service to return insights for legs
444- async def mock_get_insight ( leg ):
445- if leg . mode == TransportMode . WALK :
446- return "Short walk to the bus stop."
447- return "Express bus with comfortable seats."
443+ # Mock AI service to enrich itinerary in place
444+ async def mock_get_itinerary_insight ( itinerary ):
445+ itinerary . ai_description = "This route offers a good balance of walking and public transport."
446+ itinerary . legs [ 0 ]. ai_insight = "Short walk to the bus stop."
447+ itinerary . legs [ 1 ]. ai_insight = "Express bus with comfortable seats."
448448
449- mock_ai_service .get_leg_insight = AsyncMock (side_effect = mock_get_insight )
450-
451- # Mock AI service to return itinerary description
452- mock_ai_service .get_itinerary_insight = AsyncMock (
453- return_value = "This route offers a good balance of walking and public transport."
454- )
449+ mock_ai_service .get_itinerary_insight = AsyncMock (side_effect = mock_get_itinerary_insight )
455450
456451 response = client .post (
457452 "/api/v1/routes/search" ,
@@ -475,9 +470,6 @@ async def mock_get_insight(leg):
475470 # Check second leg (BUS)
476471 assert itinerary ["legs" ][1 ]["ai_insight" ] == "Express bus with comfortable seats."
477472
478- # Verify AI service was called for each leg
479- assert mock_ai_service .get_leg_insight .call_count == 2
480-
481473 # Verify AI description for the itinerary
482474 assert (
483475 itinerary ["ai_description" ]
@@ -493,9 +485,12 @@ def test_search_routes_with_ai_service_unavailable(client: TestClient, sample_it
493485 with patch ("app.api.v1.endpoints.routes.ai_agents_service" ) as mock_ai_service :
494486 mock_routing_service .get_itinaries = AsyncMock (return_value = sample_itineraries )
495487
496- # Mock AI service to return None (service unavailable)
497- mock_ai_service .get_leg_insight = AsyncMock (return_value = None )
498- mock_ai_service .get_itinerary_insight = AsyncMock (return_value = None )
488+ # Mock AI service to do nothing (service unavailable)
489+ async def mock_get_itinerary_insight_unavailable (itinerary ):
490+ # Service unavailable - does not modify itinerary
491+ pass
492+
493+ mock_ai_service .get_itinerary_insight = AsyncMock (side_effect = mock_get_itinerary_insight_unavailable )
499494
500495 response = client .post (
501496 "/api/v1/routes/search" ,
@@ -522,22 +517,17 @@ def test_search_routes_with_ai_service_unavailable(client: TestClient, sample_it
522517
523518
524519def test_search_routes_with_ai_service_partial_failure (client : TestClient , sample_itineraries ):
525- """Test graceful degradation when AI service fails for some legs ."""
520+ """Test graceful degradation when AI service provides partial data ."""
526521 with patch ("app.api.v1.endpoints.routes.routing_service" ) as mock_routing_service :
527522 with patch ("app.api.v1.endpoints.routes.ai_agents_service" ) as mock_ai_service :
528523 mock_routing_service .get_itinaries = AsyncMock (return_value = sample_itineraries )
529524
530- # Mock AI service to succeed for first leg, fail for second
531- call_count = 0
532-
533- async def mock_get_insight_partial (leg ):
534- nonlocal call_count
535- call_count += 1
536- if call_count == 1 :
537- return "Short walk to the bus stop."
538- return None # Simulate failure for second leg
525+ # Mock AI service to provide partial data
526+ async def mock_get_itinerary_insight_partial (itinerary ):
527+ # Only set description, not leg insights
528+ itinerary .ai_description = "This is a good route."
539529
540- mock_ai_service .get_leg_insight = AsyncMock (side_effect = mock_get_insight_partial )
530+ mock_ai_service .get_itinerary_insight = AsyncMock (side_effect = mock_get_itinerary_insight_partial )
541531
542532 response = client .post (
543533 "/api/v1/routes/search" ,
@@ -550,10 +540,9 @@ async def mock_get_insight_partial(leg):
550540 assert response .status_code == 200
551541 data = response .json ()
552542
553- # Verify first leg has insight
554- assert data ["itineraries" ][0 ]["legs" ][0 ]["ai_insight" ] == "Short walk to the bus stop."
555-
556- # Verify second leg has no insight (graceful degradation)
543+ # Verify itinerary has description but legs don't have insights
544+ assert data ["itineraries" ][0 ]["ai_description" ] == "This is a good route."
545+ assert data ["itineraries" ][0 ]["legs" ][0 ]["ai_insight" ] is None
557546 assert data ["itineraries" ][0 ]["legs" ][1 ]["ai_insight" ] is None
558547
559548
@@ -564,7 +553,6 @@ def test_search_routes_with_ai_service_exception(client: TestClient, sample_itin
564553 mock_routing_service .get_itinaries = AsyncMock (return_value = sample_itineraries )
565554
566555 # Mock AI service to raise an exception
567- mock_ai_service .get_leg_insight = AsyncMock (side_effect = Exception ("AI service error" ))
568556 mock_ai_service .get_itinerary_insight = AsyncMock (
569557 side_effect = Exception ("AI service error" )
570558 )
0 commit comments