Files

65 lines
3.6 KiB
Python
Raw Permalink Normal View History

"""Self-check: a marker added on the base after the API started is in /map/info, /agv/markers and the assistant's
2026-11-17 13:22:40 +09:00
list at once, and the no-go lines of this floor (api/server.py, robot/agv.py), with a fake base, no robot:
python tests/test_map_info.py"""
import json
import time
import no_robot # noqa: F401 any connection to the robot fails the test
from api import server
2026-11-17 18:33:14 +09:00
import tempfile, pathlib
server.MARKER_TIMES = pathlib.Path(tempfile.mkdtemp()) / "marker_times.json" # not the real file
server._map_dates = lambda: {}
from robot import agv
MARKS = {"home_dock": {"key": 11, "num": 1, "floor": 1, "pose": {"position": {"x": 0, "y": 0},
"orientation": {"z": 0, "w": 1}}}}
def fake(cmd, timeout=3.0):
path = cmd.split("?")[0]
if path == "/api/map/get_current_map":
return {"results": {"map_name": "office", "floor": 1, "info": {"resolution": 0.05}}}
if path in ("/api/markers/query_list", "/api/markers/query_brief"):
return {"results": {n: dict(m) for n, m in MARKS.items()}}
if path == "/api/map/list":
return {"results": {"office": [1]}}
raise AssertionError(cmd)
2026-11-17 16:41:06 +09:00
LINES = {"door": {"floor": 1, "points": [[0, 1], [2, 1]], "type": "forbidden", "date": "2026-10-08 16:35:09"},
"new": {"floor": 1, "points": [[5, 5], [6, 5]], "date": "2026-11-17 15:00:00"},
2026-11-17 13:22:40 +09:00
"up": {"floor": 2, "points": [[0, 0], [1, 0]]}, "dot": {"floor": 1, "points": [[3, 3]]}}
def fake_service(service, args, timeout=3.0):
assert service == "/forbidden_manager/action_service" and args["cmd"] == "list_lines", (service, args)
return {"success": True, "message": json.dumps(LINES)}
agv.request = server.agv.request = fake
2026-11-17 13:22:40 +09:00
server.rosbridge.call_service = fake_service
names = lambda d: [m["name"] for m in d["markers"]]
assert names(server.map_info()) == ["home_dock"]
2026-11-17 16:41:06 +09:00
assert server.map_info()["lines"] == [{"name": "new", "points": [[5, 5], [6, 5]]}, # newest first
{"name": "door", "points": [[0, 1], [2, 1]]}] # this floor, 2 points or more
assert server.markers()["markers"] == ["home"] and agv.known_markers() == ["home"]
MARKS["p13"] = {"key": 0, "floor": 1, "pose": {"position": {"x": 1.0, "y": 2.0}, "orientation": {"z": 0, "w": 1}}}
assert names(server.map_info()) == ["home_dock", "p13"] # the next page load sees it
assert server.markers()["markers"] == ["home", "p13"]
assert agv.known_markers(fresh=True) == ["home", "p13"] # a name not on the list: read again
agv._known["t"] = time.time() - agv.MARKERS_FRESH_S - 1
del MARKS["p13"]
assert agv.known_markers() == ["home"] # and within MARKERS_FRESH_S anyway
2026-11-17 18:33:14 +09:00
# newest first: markers by when this API saved them (undated ones after, in the base's order), maps by folder date
server.MARKER_TIMES.write_text(json.dumps({"office/1/p13": 200, "office/1/z9": 100}))
MARKS.update({n: {"key": 0, "floor": 1, "pose": {"position": {"x": 0, "y": 0}, "orientation": {"z": 0, "w": 1}}}
for n in ("p13", "z9", "a1")})
assert names(server.map_info()) == ["p13", "z9", "home_dock", "a1"], names(server.map_info())
server._map_dates = lambda: {"old": "2026-01-01 00:00", "office": "2026-11-17 09:00"}
real_request = agv.request
agv.request = server.agv.request = lambda cmd, timeout=3.0: ({"results": {"office": [1], "old": [1], "nodate": [1]}}
if cmd == "/api/map/list" else real_request(cmd, timeout))
assert [m["name"] for m in server.map_info()["maps"]] == ["office", "old", "nodate"], server.map_info()["maps"]
print("map info ok")