"""Regression tests for trip page route wiring and rendering.""" from datetime import date import typing import web_view from agenda.types import Trip def test_trip_page_passes_data_dir_to_unbooked_flight_helper() -> None: """Trip page should call helper with routes, coordinates and data_dir.""" trip = Trip(start=date(2025, 1, 28)) captured: dict[str, str] = {} with web_view.app.app_context(): original_get_trip_list = web_view.get_trip_list original_add_schengen = ( web_view.agenda.trip_schengen.add_schengen_compliance_to_trip ) original_collect_trip_coordinates = ( web_view.agenda.trip.collect_trip_coordinates ) original_get_trip_routes = web_view.agenda.trip.get_trip_routes original_add_coordinates = ( web_view.agenda.trip.add_coordinates_for_unbooked_flights ) original_get_trip_weather = web_view.agenda.weather.get_trip_weather original_render_template = web_view.flask.render_template try: web_view.get_trip_list = lambda: [trip] web_view.agenda.trip_schengen.add_schengen_compliance_to_trip = lambda t: t web_view.agenda.trip.collect_trip_coordinates = lambda _trip: [] web_view.agenda.trip.get_trip_routes = lambda _trip, _data_dir: [] def fake_add_coordinates( _routes: list[typing.Any], _coordinates: list[typing.Any], data_dir: str, ) -> None: captured["data_dir"] = data_dir web_view.agenda.trip.add_coordinates_for_unbooked_flights = ( fake_add_coordinates ) web_view.agenda.weather.get_trip_weather = lambda *_args, **_kwargs: [] web_view.flask.render_template = lambda *_args, **_kwargs: "ok" with web_view.app.test_request_context("/trip/2025-01-28"): result = web_view.trip_page("2025-01-28") assert result == "ok" assert captured["data_dir"] == web_view.app.config["PERSONAL_DATA"] finally: web_view.get_trip_list = original_get_trip_list web_view.agenda.trip_schengen.add_schengen_compliance_to_trip = ( original_add_schengen ) web_view.agenda.trip.collect_trip_coordinates = ( original_collect_trip_coordinates ) web_view.agenda.trip.get_trip_routes = original_get_trip_routes web_view.agenda.trip.add_coordinates_for_unbooked_flights = ( original_add_coordinates ) web_view.agenda.weather.get_trip_weather = original_get_trip_weather web_view.flask.render_template = original_render_template def test_trip_page_renders_with_date_only_train_leg() -> None: """Trip page should render when train legs use date values (no time).""" trip = Trip( start=date(2025, 1, 28), travel=[ { "type": "train", "depart": date(2025, 1, 28), "from": "A", "to": "B", "from_station": { "name": "A", "country": "gb", "latitude": 51.5, "longitude": -0.1, }, "to_station": { "name": "B", "country": "gb", "latitude": 51.6, "longitude": -0.2, }, "legs": [ { "from": "A", "to": "B", "depart": date(2025, 1, 28), "arrive": date(2025, 1, 28), "from_station": { "name": "A", "country": "gb", "latitude": 51.5, "longitude": -0.1, }, "to_station": { "name": "B", "country": "gb", "latitude": 51.6, "longitude": -0.2, }, "operator": "Test Rail", } ], } ], ) with web_view.app.app_context(): original_get_trip_list = web_view.get_trip_list original_get_trip_weather = web_view.agenda.weather.get_trip_weather try: web_view.get_trip_list = lambda: [trip] web_view.agenda.weather.get_trip_weather = lambda *_args, **_kwargs: [] web_view.app.config["TESTING"] = True with web_view.app.test_client() as client: response = client.get("/trip/2025-01-28") assert response.status_code == 200 assert b"Test Rail" in response.data finally: web_view.get_trip_list = original_get_trip_list web_view.agenda.weather.get_trip_weather = original_get_trip_weather