|
| 1 | +"""Tests for the MowerMapTrace accumulator and SVG renderer.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from deebot_client.mower_trace import MowerMapTrace |
| 8 | + |
| 9 | + |
| 10 | +def test_empty_trace_renders_to_none() -> None: |
| 11 | + trace = MowerMapTrace() |
| 12 | + assert not trace.has_points |
| 13 | + assert trace.to_svg() is None |
| 14 | + |
| 15 | + |
| 16 | +def test_add_data_parses_tokens_and_returns_count() -> None: |
| 17 | + trace = MowerMapTrace() |
| 18 | + added = trace.add_data("0,0;100,200;300,400") |
| 19 | + assert added == 3 |
| 20 | + assert trace.has_points |
| 21 | + |
| 22 | + |
| 23 | +def test_add_data_skips_malformed_tokens() -> None: |
| 24 | + trace = MowerMapTrace() |
| 25 | + added = trace.add_data("0,0;not-a-point;100,abc;200,300;;") |
| 26 | + # Only "0,0" and "200,300" parse cleanly. |
| 27 | + assert added == 2 |
| 28 | + |
| 29 | + |
| 30 | +def test_add_data_returns_zero_on_all_invalid() -> None: |
| 31 | + trace = MowerMapTrace() |
| 32 | + assert trace.add_data(";;not-valid;") == 0 |
| 33 | + assert not trace.has_points |
| 34 | + |
| 35 | + |
| 36 | +def test_clear_drops_all_points() -> None: |
| 37 | + trace = MowerMapTrace() |
| 38 | + trace.add_data("1,2;3,4") |
| 39 | + trace.clear() |
| 40 | + assert not trace.has_points |
| 41 | + assert trace.to_svg() is None |
| 42 | + |
| 43 | + |
| 44 | +def test_max_points_fifo_drop() -> None: |
| 45 | + trace = MowerMapTrace() |
| 46 | + over = MowerMapTrace.MAX_POINTS + 100 |
| 47 | + payload = ";".join(f"{i},{i}" for i in range(over)) |
| 48 | + added = trace.add_data(payload) |
| 49 | + assert added == over |
| 50 | + |
| 51 | + svg = trace.to_svg() |
| 52 | + assert svg is not None |
| 53 | + |
| 54 | + # Polyline holds exactly MAX_POINTS coordinate pairs after FIFO drop. |
| 55 | + points_attr = svg.split('points="', 1)[1].split('"', 1)[0] |
| 56 | + assert len(points_attr.split(" ")) == MowerMapTrace.MAX_POINTS |
| 57 | + |
| 58 | + # The MAX_POINTS most recent points are kept; first kept point has |
| 59 | + # x = (over - MAX_POINTS), here 100. |
| 60 | + first_kept_x = over - MowerMapTrace.MAX_POINTS |
| 61 | + assert points_attr.startswith(f"{first_kept_x},") |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + ("raw", "expected_substrings", "absent_substrings"), |
| 66 | + [ |
| 67 | + # Single-point trace: padding gives a non-zero viewBox. |
| 68 | + ("500,500", ["polyline", "viewBox=", "stroke="], []), |
| 69 | + # Two-point trace: SVG contains the flipped polyline points. |
| 70 | + ("0,0;100,100", ["polyline points=", "viewBox="], []), |
| 71 | + ], |
| 72 | +) |
| 73 | +def test_to_svg_structure( |
| 74 | + raw: str, expected_substrings: list[str], absent_substrings: list[str] |
| 75 | +) -> None: |
| 76 | + trace = MowerMapTrace() |
| 77 | + trace.add_data(raw) |
| 78 | + svg = trace.to_svg() |
| 79 | + assert svg is not None |
| 80 | + assert svg.startswith("<svg") |
| 81 | + assert svg.endswith("</svg>") |
| 82 | + for needle in expected_substrings: |
| 83 | + assert needle in svg |
| 84 | + for needle in absent_substrings: |
| 85 | + assert needle not in svg |
| 86 | + |
| 87 | + |
| 88 | +def test_to_svg_y_axis_is_flipped() -> None: |
| 89 | + """SVG renders top-down; mower coords are bottom-up. Verify the flip.""" |
| 90 | + trace = MowerMapTrace() |
| 91 | + trace.add_data("0,0;0,1000") |
| 92 | + svg = trace.to_svg() |
| 93 | + assert svg is not None |
| 94 | + # Extract the polyline points attribute. |
| 95 | + points = svg.split('points="', 1)[1].split('"', 1)[0].split(" ") |
| 96 | + # Two points, both with x=0; y values must be swapped versus input |
| 97 | + # (input 0 -> output max_y+min_y-0 = larger value; |
| 98 | + # input 1000 -> output max_y+min_y-1000 = smaller value). |
| 99 | + y0 = int(points[0].split(",")[1]) |
| 100 | + y1 = int(points[1].split(",")[1]) |
| 101 | + assert y0 > y1 |
0 commit comments