"""Self-check of the dashboard switches and the arm's state: with Greeter only Lumi works in place (head, lift, arm, gripper, wave) and refuses the base and the waist with one line; Patrol on allows them; "stop" works either way; an arm action with the arm switched off asks to enable it, nothing else asks. Fake state, no robot: python tests/test_switches.py""" import types from assistant import lumi from tasks import fetch, follow M = ["home", "p3", "p8"] moved, said = [], [] def forbid(*a, **k): raise AssertionError("a test reached the robot") for mod in (lumi.agv, lumi.head, lumi.arm, lumi.gripper, lumi.motion, lumi.skills): # nothing may reach the robot for name in dir(mod): if not name.startswith("_") and callable(getattr(mod, name)) and not isinstance(getattr(mod, name), type): setattr(mod, name, forbid) lumi.wave_mod.wave = forbid lumi.log = fetch.log = lambda *a: None lumi.mouth = types.SimpleNamespace(say=lambda text, **k: said.append(text)) arm_state = {"power": 1, "enable": 1} lumi.arm.cmd = lambda name, **a: dict(arm_state) if name == "get_robot_state" else forbid() L = lumi.Lumi.__new__(lumi.Lumi) L.lang, L.markers, L.current, L.patrol, L.vision, L.voice_angle = "en", M, None, None, None, 0.0 L.turn_head = lambda *a: moved.append("head") L.brain = types.SimpleNamespace(history=[]) L.keep_listening = lambda: None L.mission_running = lambda: False L.fetch_status = lambda: None L.skill_busy = lambda: False FAKED = ("start_trip", "move_by", "come_to", "follow_me", "fetch_request", "object_task", "arm_pose", "lift", "look", "come_back", "receive_from_hand") for name in FAKED: setattr(L, name, lambda *a, _n=name, **k: moved.append(_n) or _n != "look") # look: True would be "past range" L.wave = lambda *a, **k: None # quick_command starts it in a thread BASE = {"go to p3": "go_to", "p3로 가": "go_to", "follow me": "follow", "따라와": "follow", "come here": "come", "이리 와": "come", "move forward 1 meter": "move", "1미터 앞으로 가": "move", "turn left": "turn", "come back": "come_back"} IN_PLACE = {"look left": "look", "고개 들어": "look", "look behind you": "look", "lift up": "lift", "키 좀 낮춰": "lift", "pick up the cup": "pick", "open your hand": "arm", "reach out your hand": "arm", "wave": "wave", "bring me the cup": "fetch"} for text, action in {**BASE, **IN_PLACE}.items(): got = lumi.brain_mod.intent(text, M) assert got and got[0] == action, (text, got) DOES = {"go_to": "start_trip", "follow": "follow_me", "come": "come_to", "move": "move_by", "turn": "move_by", "arm": "arm_pose", "pick": "object_task", "fetch": "fetch_request", "come_back": "come_back", "look": "look", "lift": "lift"} # ---------- Greeter only: the base refused aloud (no "Okay" first), everything in place done ---------- L.modes = types.SimpleNamespace(greeter=True, patrol=False, drive=True, wave=True) for text in BASE: moved.clear() said.clear() assert L.quick_command(text) and moved == [] and said == [lumi.BASE_OFF_SAY[0]], (text, moved, said) L.lang = "ko" said.clear() assert L.quick_command("p3로 가") and said == [lumi.BASE_OFF_SAY[1]] L.lang = "en" for text, action in IN_PLACE.items(): moved.clear() said.clear() L.quick_command(text) assert lumi.BASE_OFF_SAY[0] not in said, (text, said) assert action == "wave" or moved == [DOES[action]], (text, moved) for action, target in (("go_to", "p3"), ("follow", ""), ("move", "forward 1"), ("turn", "left 90"), ("come", "")): moved.clear() L.act(action, target) # the LLM's action: answer() has said the line assert moved == [] and L.base_off(action, target, say=False), action for action, target in (("look", "left"), ("lift", "up"), ("arm", "reach")): moved.clear() L.act(action, target) assert moved == [DOES[action]], (action, moved) said.clear() assert L.mission_request("go to p3, wait 10 seconds and come back") and said == [lumi.BASE_OFF_SAY[0]] said.clear() L.heard = "그럼 가 보세요" L.go_offered("p3") assert said == [lumi.BASE_OFF_SAY[0]] # a fetch from or to a place drives: refused; one here runs in place (fetch.stay_put) started = [] real_start = fetch.start fetch.start = lambda task, say=None: started.append((task, fetch.stay_put)) lumi.agv.pose = lambda: (1.0, 2.0, 0.0) said.clear() L.fetch_start({"object": "cup", "source": {"marker": "p3"}, "dest": "here", "finish": "hold"}) assert started == [] and said == [lumi.BASE_OFF_SAY[0]] L.fetch_start({"object": "cup", "source": "here", "dest": "here", "finish": "hold"}) assert started and started[-1][1] is True # in place: head and lift only # fetch.stay_put: no base, no waist; the search is the head and the lift fetch.stay_put = True fetch.head.status = lambda: [50.0, 30.0, 0.0, 0.0] # the waist turned 30 deg (jogged before) fetch._waist_turn = lambda: 30.0 fetch.head.moveto = forbid fetch.agv.turn_by = fetch.agv.go_point = fetch.agv.go = fetch.agv.request = forbid assert fetch._waist_to(0.0) is False and fetch._turn(90.0) is False for fn, args in ((fetch._waist_front, ()), (fetch._straight, (0.5,)), (fetch._approach, ({"x": 0, "y": 900}, 0.0, set())), (fetch._drive_to, ({"marker": "p3"}, None))): try: fn(*args) raise AssertionError(f"{fn.__name__} moved with Patrol off") except fetch.FetchError as e: assert e.args == fetch.NEED_PATROL, (fn.__name__, e.args) fetch.stay_put = False fetch.start = real_start # ---------- the arm switched off: arm actions ask to enable it, the rest runs ---------- arm_state.update(enable=0) for text in ("pick up the cup", "reach out your hand", "wave", "bring me the cup"): moved.clear() said.clear() assert L.quick_command(text) and moved == [] and said == [lumi.ARM_OFF_SAY[0]], (text, moved, said) for text in ("look left", "lift up", "open your hand"): # the gripper alone does not ask moved.clear() said.clear() L.quick_command(text) assert lumi.ARM_OFF_SAY[0] not in said and moved, (text, said) said.clear() L.lang = "ko" lumi.Lumi.receive_from_hand(L, "cup") assert said == [lumi.ARM_OFF_SAY[1]] L.lang = "en" assert lumi.wave_mod.OFF == follow.ARM_OFF == lumi.ARM_OFF_SAY # follow: an enabled arm never refuses; a disabled one only when it must move to look follow.fetch._charging = lambda: False at_look = [True] follow.fetch._at_pose = lambda name: at_look[0] follow.fetch._look = lambda: "look" follow.motion._ready = lambda: None if arm_state["enable"] else (_ for _ in ()).throw(follow.motion.MotionError("off")) arm_state.update(enable=1) assert follow.ready() is None at_look[0] = False assert follow.ready() is None # enabled: it may go to look arm_state.update(enable=0) assert follow.ready() == follow.ARM_OFF # off and not at look: ask at_look[0] = True assert follow.ready() is None # off but already at look: follows # ---------- stop works either way ---------- stopped = [] real_follow_stop, real_fetch_stop = follow.stop, fetch.stop follow.stop = lambda: stopped.append("follow") fetch.stop = lambda: stopped.append("fetch") lumi.agv.cancel = lambda: stopped.append("base") lumi.skills.cancel = lambda: stopped.append("skill") lumi.motion.stop = lambda: stopped.append("arm") said.clear() L.stop_all() assert stopped == ["fetch", "follow", "base", "skill", "arm"] and said == ["Okay, I stopped."], (stopped, said) assert lumi.stop_order("stop", M) and lumi.stop_order("멈춰", M) # ---------- Patrol on: the base moves too ---------- arm_state.update(enable=1) L.modes.patrol = True for text, action in {**BASE, **IN_PLACE}.items(): moved.clear() said.clear() L.quick_command(text) assert lumi.BASE_OFF_SAY[0] not in said, (text, said) assert action == "wave" or moved == [DOES[action]], (text, moved) moved.clear() L.act("go_to", "p3") assert moved == ["start_trip"] follow.stop, fetch.stop = real_follow_stop, real_fetch_stop print(f"switches ok: {len(BASE)} base phrases refused with Greeter only, {len(IN_PLACE)} in place done; " "arm off asks; Patrol allows all; stop always")