File size: 1,559 Bytes
99e41a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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"