import json import pytest from robotics.planner.task_planner import PlanError, parse_plan, plan SCENE = ["red cube", "blue bowl", "drawer"] def test_parse_valid_plan_in_code_fence(): text = '```json\n{"steps": [{"skill": "pick", "object": "red cube"},' \ ' {"skill": "place", "object": "red cube", "location": "in the blue bowl"}]}\n```' p = parse_plan(text, SCENE) assert [s.instruction() for s in p.steps] == ["pick up red cube", "place the red cube in the blue bowl"] @pytest.mark.parametrize("bad, msg", [ ('{"steps": [{"skill": "fly"}]}', "unknown skill"), ('{"steps": [{"skill": "pick", "object": "banana"}]}', "not in scene"), ('{"steps": [{"skill": "pick"}]}', "needs an object"), ("no json here", "no JSON"), ]) def test_parse_rejects_invalid(bad, msg): with pytest.raises(PlanError, match=msg): parse_plan(bad, SCENE) def test_plan_retries_with_error_feedback(): replies = iter(['{"steps": [{"skill": "teleport", "object": "red cube"}]}', '{"steps": [{"skill": "open", "object": "drawer"}]}']) seen = [] def fake_chat(messages, **kw): seen.append(messages[-1]["content"]) return next(replies), "fake" p = plan("open the drawer", SCENE, chat_fn=fake_chat) assert p.provider == "fake" and p.steps[0].skill == "open" assert "unknown skill" in seen[1] def test_infeasible_plan(): p = parse_plan(json.dumps({"steps": [], "infeasible_reason": "no knife"}), SCENE) assert p.steps == [] and p.infeasible_reason == "no knife"