AgentnessBench / tests /agents /test_human.py
irregular6612's picture
refactor: restructure proteus into game/web subpackages
426093b
Raw
History Blame Contribute Delete
2.92 kB
from proteus.game.agents import HumanAgent
from proteus.game.agents.base import ActResult, ProbeResult
_ACTIONS = ["up", "down", "left", "right", "stay"]
def _scripted(seq):
"""Return an input_fn that yields the given strings in order."""
it = iter(seq)
return lambda prompt="": next(it)
def test_act_parses_plain_action():
out = []
agent = HumanAgent(input_fn=_scripted(["up"]), output_fn=out.append)
result = agent.act("OBSERVATION", _ACTIONS, "SYSTEM")
assert isinstance(result, ActResult)
assert result.action == "up"
assert result.reasoning == ""
assert result.raw_text == "up"
assert result.input_tokens == 0
assert result.output_tokens == 0
assert result.thinking_tokens == 0
# The observation was shown to the human.
assert any("OBSERVATION" in line for line in out)
def test_act_maps_wasd_shortcuts():
agent = HumanAgent(input_fn=_scripted(["w"]), output_fn=lambda s: None)
assert agent.act("o", _ACTIONS, "s").action == "up"
agent = HumanAgent(input_fn=_scripted(["a"]), output_fn=lambda s: None)
assert agent.act("o", _ACTIONS, "s").action == "left"
agent = HumanAgent(input_fn=_scripted(["s"]), output_fn=lambda s: None)
assert agent.act("o", _ACTIONS, "s").action == "down"
agent = HumanAgent(input_fn=_scripted(["d"]), output_fn=lambda s: None)
assert agent.act("o", _ACTIONS, "s").action == "right"
def test_act_is_case_and_whitespace_insensitive():
agent = HumanAgent(input_fn=_scripted([" UP "]), output_fn=lambda s: None)
result = agent.act("o", _ACTIONS, "s")
assert result.action == "up"
assert result.raw_text == "UP" # stripped, original casing preserved
def test_act_reprompts_on_invalid_then_accepts():
out = []
agent = HumanAgent(input_fn=_scripted(["nope", "diagonal", "right"]), output_fn=out.append)
result = agent.act("o", _ACTIONS, "s")
assert result.action == "right"
# Two invalid inputs produced two guidance messages.
assert sum("invalid" in line for line in out) == 2
def test_name_is_human():
assert HumanAgent().name == "human"
def test_reset_is_noop():
assert HumanAgent().reset() is None
def test_probe_returns_typed_answer():
out = []
agent = HumanAgent(
input_fn=_scripted(["the predator is east; go up"]),
output_fn=out.append,
)
result = agent.probe("OBSERVATION", "Where is the predator?", "SYSTEM")
assert isinstance(result, ProbeResult)
assert result.answer == "the predator is east; go up"
assert result.reasoning == ""
assert result.raw_text == "the predator is east; go up"
assert result.input_tokens == 0
assert result.output_tokens == 0
assert result.thinking_tokens == 0
# Both the observation and the question were shown.
assert any("OBSERVATION" in line for line in out)
assert any("Where is the predator?" in line for line in out)