|
| 1 | +"""Which remedy a folded flow-axis port gets, per flow axis. |
| 2 | +
|
| 3 | +A port declared on the edge opposite its connecting section makes the leg run |
| 4 | +the length of the trunk and double back through every station in between. |
| 5 | +``_reanchor_flow_axis_ports`` has two remedies: reverse the section's flow so |
| 6 | +the declared edge becomes the right one, or move the port to the edge its |
| 7 | +connecting station is actually on. |
| 8 | +
|
| 9 | +The choice is keyed to the flow axis, and deliberately so. A horizontal |
| 10 | +section is reversed; a vertical one has its port re-anchored, because reversing |
| 11 | +it re-seats the trailing exit on the far edge and the route out then wraps |
| 12 | +around the section and back through its target's interior. These tests pin that |
| 13 | +split, so the asymmetry is a stated contract. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import warnings |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +from nf_metro.parser.model import Edge, MetroGraph, PortSide, Section, Station |
| 23 | +from nf_metro.parser.resolve import ( |
| 24 | + _LEADING_SIDE, |
| 25 | + _TRAILING_SIDE, |
| 26 | + _flow_axis_is_x, |
| 27 | + _reanchor_flow_axis_ports, |
| 28 | +) |
| 29 | + |
| 30 | +_HIGH_SIDE = (PortSide.RIGHT, PortSide.BOTTOM) |
| 31 | +_REVERSED = {"LR": "RL", "RL": "LR", "TB": "BT", "BT": "TB"} |
| 32 | + |
| 33 | + |
| 34 | +def _folded_section_graph(direction: str) -> tuple[MetroGraph, list[Edge]]: |
| 35 | + """A 'mid' section flowing *direction*, whose exit folds. |
| 36 | +
|
| 37 | + 'mid' holds m1 -> m2. Its entry sits on the trailing edge and feeds m2, |
| 38 | + 'mid's own flow-sink, so the entry does not itself fold. Its exit sits on |
| 39 | + the leading edge and is fed by that same m2 rather than by the flow-source |
| 40 | + m1, so the exit folds. Neither port runs with the flow, which is the |
| 41 | + precondition a reversal needs. |
| 42 | + """ |
| 43 | + graph = MetroGraph() |
| 44 | + feed = Section(id="feed", name="Feed", direction="LR") |
| 45 | + mid = Section(id="mid", name="Mid", direction=direction) |
| 46 | + sink = Section(id="sink", name="Sink", direction="LR") |
| 47 | + graph.sections = {"feed": feed, "mid": mid, "sink": sink} |
| 48 | + |
| 49 | + graph.stations = { |
| 50 | + "f1": Station(id="f1", label="F1", section_id="feed"), |
| 51 | + "m1": Station(id="m1", label="M1", section_id="mid"), |
| 52 | + "m2": Station(id="m2", label="M2", section_id="mid"), |
| 53 | + "s1": Station(id="s1", label="S1", section_id="sink"), |
| 54 | + } |
| 55 | + feed.station_ids = ["f1"] |
| 56 | + mid.station_ids = ["m1", "m2"] |
| 57 | + sink.station_ids = ["s1"] |
| 58 | + mid.internal_edges = [Edge(source="m1", target="m2", line_id="a")] |
| 59 | + entry_side = _TRAILING_SIDE[direction] |
| 60 | + exit_side = _LEADING_SIDE[direction] |
| 61 | + mid.entry_hints = [(entry_side, ["a"])] |
| 62 | + mid.exit_hints = [(exit_side, ["a"])] |
| 63 | + |
| 64 | + # Each neighbour sits on the side of 'mid' its port faces, so the fold is |
| 65 | + # the section's own flow being backwards rather than a misplaced port. |
| 66 | + axis_is_x = _flow_axis_is_x(direction) |
| 67 | + feed_pos = 2 if entry_side in _HIGH_SIDE else 0 |
| 68 | + for section, pos in ((feed, feed_pos), (mid, 1), (sink, 2 - feed_pos)): |
| 69 | + section.grid_col, section.grid_row = (pos, 0) if axis_is_x else (0, pos) |
| 70 | + |
| 71 | + inter_section_edges = [ |
| 72 | + Edge(source="f1", target="m2", line_id="a"), |
| 73 | + Edge(source="m2", target="s1", line_id="a"), |
| 74 | + ] |
| 75 | + return graph, inter_section_edges |
| 76 | + |
| 77 | + |
| 78 | +def _resolve(direction: str) -> tuple[MetroGraph, list[str]]: |
| 79 | + graph, edges = _folded_section_graph(direction) |
| 80 | + with warnings.catch_warnings(record=True) as caught: |
| 81 | + warnings.simplefilter("always") |
| 82 | + _reanchor_flow_axis_ports(graph, edges) |
| 83 | + return graph, [str(w.message) for w in caught] |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize("direction", ["LR", "RL"]) |
| 87 | +def test_horizontal_fold_reverses_the_section(direction: str) -> None: |
| 88 | + """A horizontal section's flow is reversed, leaving its port hints alone.""" |
| 89 | + graph, messages = _resolve(direction) |
| 90 | + mid = graph.sections["mid"] |
| 91 | + |
| 92 | + assert mid.direction == _REVERSED[direction] |
| 93 | + assert "mid" in graph._fold_reoriented_sections |
| 94 | + assert mid.entry_hints == [(_TRAILING_SIDE[direction], ["a"])] |
| 95 | + assert mid.exit_hints == [(_LEADING_SIDE[direction], ["a"])] |
| 96 | + assert any("flow re-oriented" in m for m in messages), messages |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.parametrize("direction", ["TB", "BT"]) |
| 100 | +def test_vertical_fold_reanchors_the_port(direction: str) -> None: |
| 101 | + """A vertical section keeps its flow and has the folded port moved. |
| 102 | +
|
| 103 | + Reversing it instead would wrap the route out of the re-seated exit around |
| 104 | + the section and back through its target's interior, so the port-side |
| 105 | + remedy is the one that applies on this axis. |
| 106 | + """ |
| 107 | + graph, messages = _resolve(direction) |
| 108 | + mid = graph.sections["mid"] |
| 109 | + |
| 110 | + assert mid.direction == direction |
| 111 | + assert "mid" not in graph._fold_reoriented_sections |
| 112 | + assert mid.exit_hints == [(_TRAILING_SIDE[direction], ["a"])] |
| 113 | + assert any("re-anchored" in m for m in messages), messages |
| 114 | + |
| 115 | + |
| 116 | +@pytest.mark.parametrize( |
| 117 | + ("direction", "expected"), |
| 118 | + [("LR", True), ("RL", True), ("TB", False), ("BT", False)], |
| 119 | +) |
| 120 | +def test_flow_axis_is_x_covers_every_direction(direction: str, expected: bool) -> None: |
| 121 | + """The axis question is answered from the frame, for all four flows.""" |
| 122 | + assert _flow_axis_is_x(direction) is expected |
0 commit comments