"""Location parsing tests.""" from coastwise.locations import location_context_from_model, location_candidates, parse_location def test_parse_location_from_question_or_optional_beach_field(): pacifica = parse_location("can I eat mussels from Pacifica today?", "") monterey = parse_location("what's the lingcod size?", "Monterey") blank = parse_location("what's the min size of lingcod?", "") unsupported = parse_location("what are trout rules?", "Lake Tahoe") assert pacifica.matched_place == "Pacifica" assert pacifica.region_key == "san_francisco" assert monterey.matched_place == "Monterey" assert monterey.region_key == "central" assert blank.confidence == "absent" assert "local" in (blank.warning or "").lower() assert unsupported.confidence == "unsupported" assert "california ocean" in (unsupported.warning or "").lower() def test_parse_location_recognizes_half_moon_bay_for_public_queries(): context = parse_location("is shellfish quarantine in half moon bay?", "") assert context.matched_place == "Half Moon Bay" assert context.region_key == "san_francisco" assert context.confidence == "recognized" def test_location_candidates_include_official_regions_without_typo_alias_growth(): candidates = location_candidates() assert candidates["Mendocino"] == "mendocino" assert candidates["Mendocino Ocean Region"] == "mendocino" assert "Mandocino" not in candidates def test_location_context_from_validated_model_output(): context = location_context_from_model( question="im in mandocino. is it legal to harvest dungenese crabs?", location_text="", location_name="Mendocino", region_key="mendocino", ) assert context.matched_place == "Mendocino" assert context.region_key == "mendocino" assert context.confidence == "recognized" assert context.warning is None def test_location_context_from_model_preserves_existing_alias_scope(): context = location_context_from_model( question="can I eat mussels from pacifica today?", location_text="", location_name="Pacifica", region_key="san_francisco", ) assert context.matched_place == "Pacifica" assert context.region_key == "san_francisco" assert context.area_or_scope == "San Francisco coast" def test_location_context_from_missing_model_output_falls_back_to_parse_location(): context = location_context_from_model( question="can I eat mussels from Pacifica today?", location_text="", location_name=None, region_key=None, ) assert context.matched_place == "Pacifica" assert context.region_key == "san_francisco" assert context.area_or_scope == "San Francisco coast" def test_location_context_from_model_name_region_mismatch_falls_back_to_parse_location(): context = location_context_from_model( question="what's the lingcod size?", location_text="Monterey", location_name="Pacifica", region_key="central", ) assert context.matched_place == "Monterey" assert context.region_key == "central" assert context.area_or_scope == "Central California ocean region" def test_parse_location_fuzzy_matches_misspelled_mendocino(): context = parse_location("im in mandocino. is it legal to harvest dungenese crabs?", "") assert context.matched_place == "Mendocino" assert context.region_key == "mendocino" def test_parse_location_fuzzy_does_not_false_match_plain_species_question(): context = parse_location("what's the min size of lingcod?", "") assert context.matched_place is None def test_parse_location_recognizes_sf_abbreviation(): context = parse_location("is it legal to catch lingcod in sf", "") assert context.matched_place == "San Francisco" assert context.region_key == "san_francisco"