"""Cache de routes : origine normalisée, rattachement au favori, séparation statique / trafic.""" from datetime import datetime, timedelta, timezone import pytest import routing import storage def meters_between(origin, other): return storage.distance_km(origin[0], origin[1], other[0], other[1]) * 1000 def test_gps_jitter_of_a_few_dozens_of_meters_shares_one_cache_key(store): target = (44.795280, -0.604509) first = (44.78060, -0.57060) second = (44.78063, -0.57064) # ~5 m plus loin : bruit normal d'une mesure téléphone third = (44.78110, -0.57110) # ~70 m plus loin assert meters_between(first, second) < 10 keys = {storage.route_key(point, target) for point in (first, second, third)} assert len(keys) == 1, f"positions quasi identiques, clés distinctes : {keys}" def test_two_positions_hundreds_of_meters_apart_use_distinct_keys(store): target = (44.795280, -0.604509) near = storage.route_key((44.78060, -0.57060), target) far = storage.route_key((44.81000, -0.61000), target) assert near != far def test_distinct_stations_never_share_a_key(store): origin = (44.7806, -0.5706) key_a = storage.route_key(origin, (44.795280, -0.604509)) key_b = storage.route_key(origin, (44.777000, -0.610000)) assert key_a != key_b def test_favourite_tag_replaces_raw_coordinates(store): target = (44.795280, -0.604509) key = storage.route_key((44.78055, -0.57051), target, origin_tag="fav:3") assert key.startswith("fav:3|") assert storage.route_key((44.79999, -0.57051), target, origin_tag="fav:3") == key def test_match_favorite_absorbs_gps_jitter(store): storage.save_entity("favorites", dict(name="Villenave", lat=44.78060, lon=-0.57060, radius_km=10)) favorite = storage.match_favorite(44.78068, -0.57070) assert favorite and favorite["name"] == "Villenave" assert favorite["distance_km"] <= 0.15 assert storage.match_favorite(44.90000, -0.57070) is None def test_osrm_duration_is_static_and_never_an_eta(store, monkeypatch): monkeypatch.setitem( routing.PROVIDERS, "osrm", lambda origin, targets: { t[0]: dict(distance_m=5000.0, duration_s=600.0, duration_kind=routing.STATIC) for t in targets }, ) monkeypatch.setenv("FUEL_ROUTING_PROVIDERS", "osrm") routes, info = routing.routes((44.78, -0.57), [("A", 44.79, -0.60)]) assert routes["A"]["duration_kind"] == routing.STATIC assert routes["A"]["duration_label"] == "hors trafic" assert routes["A"]["eta_min"] is None assert info["eta_count"] == 0 assert "hors trafic" in info["eta_note"] assert store.eta_lookup([storage.route_key((44.78, -0.57), (44.79, -0.60))]) == {} def test_google_traffic_duration_goes_to_short_cache_only(store, monkeypatch): monkeypatch.setattr(routing, "GOOGLE_KEY", "clé-de-test") monkeypatch.setitem( routing.PROVIDERS, "google", lambda origin, targets: { t[0]: dict(distance_m=8000.0, duration_s=900.0, duration_kind=routing.TRAFFIC) for t in targets }, ) monkeypatch.setenv("FUEL_ROUTING_PROVIDERS", "google") routes, info = routing.routes((44.78, -0.57), [("A", 44.79, -0.60)]) assert routes["A"]["distance_km"] == pytest.approx(8.0) assert routes["A"]["duration_min"] is None, "une durée de trafic n’est pas une durée statique" assert routes["A"]["eta_min"] == pytest.approx(15.0) assert routes["A"]["eta_age_seconds"] is not None assert info["eta_count"] == 1 and info["eta_provider"] == "google" cached = store.route_lookup([storage.route_key((44.78, -0.57), (44.79, -0.60))]) assert list(cached.values())[0]["duration_s"] is None, "pas de durée trafic dans le cache long" def test_stale_eta_is_ignored_and_static_duration_is_used(store, monkeypatch): monkeypatch.setattr(routing, "GOOGLE_KEY", "clé-de-test") monkeypatch.setitem( routing.PROVIDERS, "google", lambda origin, targets: { t[0]: dict(distance_m=8000.0, duration_s=900.0, duration_kind=routing.TRAFFIC) for t in targets }, ) monkeypatch.setenv("FUEL_ROUTING_PROVIDERS", "google") routing.routes((44.78, -0.57), [("A", 44.79, -0.60)]) key = storage.route_key((44.78, -0.57), (44.79, -0.60)) stale = (datetime.now(timezone.utc) - timedelta(hours=3)).isoformat() store.eta_save([dict(key=key, duration_s=1800.0, provider="google")]) with store.connect() as db: db.execute("UPDATE route_eta SET computed_at=? WHERE key=?", (stale, key)) routes, info = routing.routes((44.78, -0.57), [("A", 44.79, -0.60)], eta_ttl=900) assert routes["A"]["eta_min"] is None, "une ETA de 3 heures n’est pas une ETA actuelle" assert info["eta_count"] == 0 assert "Aucune ETA de trafic" in info["eta_note"] def test_eta_key_is_separate_for_tagged_origin(store, monkeypatch): monkeypatch.setattr(routing, "GOOGLE_KEY", "clé-de-test") monkeypatch.setitem( routing.PROVIDERS, "google", lambda origin, targets: { t[0]: dict(distance_m=4000.0, duration_s=600.0, duration_kind=routing.TRAFFIC) for t in targets }, ) monkeypatch.setenv("FUEL_ROUTING_PROVIDERS", "google") _, info = routing.routes((44.78, -0.57), [("A", 44.79, -0.60)], origin_tag="fav:7") assert info["eta_count"] == 1 assert store.eta_lookup(["fav:7|44.7900|-0.6000"]) != {}