|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from src.comptext_v7.graph import ( |
| 6 | + ReplayGraphDiff, |
| 7 | + adjacency_map, |
| 8 | + compare_edges, |
| 9 | + find_order_violations, |
| 10 | + has_path, |
| 11 | + nodes_from_edges, |
| 12 | + normalize_edges, |
| 13 | + reachable_nodes, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +def test_normalize_edges_removes_duplicates_and_sorts() -> None: |
| 18 | + edges = [("b", "c"), ("a", "b"), ("b", "c")] |
| 19 | + assert normalize_edges(edges) == (("a", "b"), ("b", "c")) |
| 20 | + |
| 21 | + |
| 22 | +def test_normalize_edges_rejects_self_loop() -> None: |
| 23 | + with pytest.raises(ValueError): |
| 24 | + normalize_edges([("n1", "n1")]) |
| 25 | + |
| 26 | + |
| 27 | +def test_nodes_from_edges_returns_sorted_nodes() -> None: |
| 28 | + edges = [("b", "c"), ("a", "b")] |
| 29 | + assert nodes_from_edges(edges) == ("a", "b", "c") |
| 30 | + |
| 31 | + |
| 32 | +def test_adjacency_map_is_deterministic() -> None: |
| 33 | + edges = [("b", "c"), ("a", "b"), ("a", "c")] |
| 34 | + assert adjacency_map(edges) == { |
| 35 | + "a": ("b", "c"), |
| 36 | + "b": ("c",), |
| 37 | + "c": (), |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +def test_find_order_violations_detects_reversed_and_sorts() -> None: |
| 42 | + sequence = ["c", "b", "a"] |
| 43 | + required = [("a", "b"), ("b", "c"), ("x", "a")] |
| 44 | + assert find_order_violations(sequence, required) == (("a", "b"), ("b", "c")) |
| 45 | + |
| 46 | + |
| 47 | +def test_find_order_violations_ignores_missing_nodes() -> None: |
| 48 | + sequence = ["a", "b"] |
| 49 | + required = [("x", "b"), ("a", "y")] |
| 50 | + assert find_order_violations(sequence, required) == () |
| 51 | + |
| 52 | + |
| 53 | +def test_reachable_nodes_and_path_on_connected_graph() -> None: |
| 54 | + edges = [("a", "b"), ("b", "d"), ("a", "c")] |
| 55 | + assert reachable_nodes(edges, "a") == ("b", "c", "d") |
| 56 | + assert has_path(edges, "a", "d") is True |
| 57 | + assert has_path(edges, "c", "d") is False |
| 58 | + |
| 59 | + |
| 60 | +def test_reachable_nodes_handles_disconnected_graph() -> None: |
| 61 | + edges = [("a", "b"), ("x", "y")] |
| 62 | + assert reachable_nodes(edges, "a") == ("b",) |
| 63 | + assert reachable_nodes(edges, "z") == () |
| 64 | + |
| 65 | + |
| 66 | +def test_reachable_nodes_includes_start_when_cycle_exists() -> None: |
| 67 | + edges = [("a", "b"), ("b", "a")] |
| 68 | + assert reachable_nodes(edges, "a") == ("a", "b") |
| 69 | + assert has_path(edges, "a", "a") is True |
| 70 | + |
| 71 | + |
| 72 | +def test_compare_edges_detects_edge_and_node_diffs_deterministically() -> None: |
| 73 | + original = [("a", "b"), ("b", "c"), ("d", "e")] |
| 74 | + replay = [("a", "b"), ("b", "d"), ("x", "y")] |
| 75 | + |
| 76 | + diff = compare_edges(original, replay) |
| 77 | + |
| 78 | + assert diff == ReplayGraphDiff( |
| 79 | + missing_edges=(("b", "c"), ("d", "e")), |
| 80 | + added_edges=(("b", "d"), ("x", "y")), |
| 81 | + missing_nodes=("c", "e"), |
| 82 | + added_nodes=("x", "y"), |
| 83 | + ) |
| 84 | + assert isinstance(diff.missing_edges, tuple) |
| 85 | + assert isinstance(diff.added_nodes, tuple) |
0 commit comments