agenda/tests/test_busy.py
Edward Betts 6286699b52 Fix location tracking for all remaining weekend test failures
Expand return-home heuristic to cover more countries and reorganize logic
to prioritize trip-based location detection over individual travel data.

Key changes:
1. Add Balkan countries (GR, AL, XK, HR, etc.) to European heuristic
2. Add major international countries (US, CA, IN, JP, etc.) to heuristic
3. Change condition from >1 day to >=1 day for faster return detection
4. Move trip heuristic check before individual flight/accommodation lookup

This fixes cases where stopovers or connections (like Kosovo→Albania on
July 1) were overriding the trip-based "return home" logic. Now correctly
detects return home from all types of trips including Balkan and
international destinations.

All weekend location tests now pass - ensures locations show "Bristol"
when free (no events) or show trip locations when traveling.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-16 06:44:08 +02:00

77 lines
2.6 KiB
Python

from datetime import date, datetime, timedelta
import agenda.busy
import agenda.travel as travel
import agenda.trip
from web_view import app
def test_get_location_for_date() -> None:
app.config["SERVER_NAME"] = "test"
with app.app_context():
today = datetime.now().date()
start = date(2023, 1, 1)
trips = agenda.trip.build_trip_list()
data_dir = app.config["PERSONAL_DATA"]
busy_events = agenda.busy.get_busy_events(start, app.config, trips)
weekends = agenda.busy.weekends(start, busy_events, trips, data_dir)
# Parse YAML files once for the test
bookings = travel.parse_yaml("flights", data_dir)
accommodations = travel.parse_yaml("accommodation", data_dir)
airports = travel.parse_yaml("airports", data_dir)
for weekend in weekends:
for day in "saturday", "sunday":
assert weekend[day + "_location"][0] == "Bristol" or bool(weekend[day])
# Debug the April 29 issue
april_29_location = agenda.busy.get_location_for_date(
date(2023, 4, 29), trips, bookings, accommodations, airports
)
print(f"\nDirect call for April 29: {april_29_location}")
# Check what the foss-north trip looks like
foss_north_trip = None
for trip in trips:
if trip.title == "foss-north" and trip.start == date(2023, 4, 22):
foss_north_trip = trip
print(f"foss-north trip: {trip.start} to {trip.end}")
print(f"foss-north locations: {trip.locations()}")
break
l1 = agenda.busy.get_location_for_date(
date(2025, 2, 15), trips, bookings, accommodations, airports
)
assert l1[0] == "Hackettstown"
l2 = agenda.busy.get_location_for_date(
date(2025, 7, 1), trips, bookings, accommodations, airports
)
assert l2[0] == "Bristol"
l2 = agenda.busy.get_location_for_date(
date(2023, 12, 2), trips, bookings, accommodations, airports
)
assert l2[0] == "Bristol"
l2 = agenda.busy.get_location_for_date(
date(2023, 10, 7), trips, bookings, accommodations, airports
)
assert l2[0] == "Bristol"
l2 = agenda.busy.get_location_for_date(
date(2023, 2, 18), trips, bookings, accommodations, airports
)
assert l2[0] == "Bristol"
l2 = agenda.busy.get_location_for_date(
date(2025, 8, 2), trips, bookings, accommodations, airports
)
assert l2[0] == "Bristol"
# Fix the April 29 case
assert april_29_location[0] == "Bristol"