import pytest from scripts.prepare_regional_burst_design import require_isolatable_pipe from scripts.screen_burst_benchmark_pipes import nearest_pressure_distances def test_region_without_valves_is_rejected(): with pytest.raises(ValueError, match="cannot be isolated"): require_isolatable_pipe(["supply:pipe:R:A", "target:pipe:A:B"], "target", ["R"], []) def test_nearby_valve_does_not_qualify_an_uncontrolled_source_path(): links = ["v:valve:R:A", "bypass:pipe:R:B", "target:pipe:A:B"] with pytest.raises(ValueError, match="cannot be isolated"): require_isolatable_pipe(links, "target", ["R"], []) def test_single_boundary_valve_can_qualify_a_branch(): links = ["v:valve:R:A", "target:pipe:A:B"] result = require_isolatable_pipe(links, "target", ["R"], []) assert result["must_close_valves"] == ["v"] assert set(result["affected_nodes"]) == {"A", "B"} assert result["source_check_performed"] def test_source_metadata_is_mandatory(): with pytest.raises(ValueError, match="cannot be isolated"): require_isolatable_pipe(["v:valve:R:A", "target:pipe:A:B"], "target", [], []) def test_distance_is_to_the_nearest_of_all_meters(): graph = {"A": [("P", 1500)], "B": [("P", 100)], "P": [("A", 1500), ("B", 100)]} devices = [{"device_id": "far", "node_id": "A"}, {"device_id": "near", "node_id": "B"}] assert nearest_pressure_distances(graph, devices, 3000)["P"] == (100, "near") def test_network_path_length_and_cutoff_are_used(): graph = {"A": [("B", 600)], "B": [("A", 600), ("C", 700)], "C": [("B", 700)]} devices = [{"device_id": "meter", "node_id": "A"}] assert "C" not in nearest_pressure_distances(graph, devices, 1200) assert nearest_pressure_distances(graph, devices, 1500)["C"] == (1300, "meter") def test_unreachable_pipe_is_not_a_zero_distance_candidate(): graph = {"A": [], "P": []} assert "P" not in nearest_pressure_distances(graph, [{"device_id": "meter", "node_id": "A"}], 3000)