|
1 | 1 | # tests/test_llm_agent.py |
2 | 2 |
|
| 3 | +import json |
3 | 4 | import re |
4 | 5 |
|
5 | 6 | import pytest |
@@ -591,3 +592,94 @@ def __init__(self): |
591 | 592 | obs = agent.generate_obs() |
592 | 593 |
|
593 | 594 | assert len(obs.local_state) == 0 |
| 595 | + |
| 596 | + |
| 597 | +# --------------------------------------------------------------------------- |
| 598 | +# send_message / asend_message - store unique_ids, not Agent objects (#156) |
| 599 | +# --------------------------------------------------------------------------- |
| 600 | + |
| 601 | + |
| 602 | +def _make_send_message_model(monkeypatch): |
| 603 | + """Shared setup: two-agent MultiGrid model with ShortTermMemory.""" |
| 604 | + monkeypatch.setenv("GEMINI_API_KEY", "dummy") |
| 605 | + |
| 606 | + class DummyModel(Model): |
| 607 | + def __init__(self): |
| 608 | + super().__init__(seed=45) |
| 609 | + self.grid = MultiGrid(3, 3, torus=False) |
| 610 | + |
| 611 | + def add_agent(self, pos): |
| 612 | + agents = LLMAgent.create_agents( |
| 613 | + self, |
| 614 | + n=1, |
| 615 | + reasoning=lambda agent: None, |
| 616 | + system_prompt="Test", |
| 617 | + vision=-1, |
| 618 | + internal_state=[], |
| 619 | + ) |
| 620 | + agent = agents.to_list()[0] |
| 621 | + self.grid.place_agent(agent, pos) |
| 622 | + return agent |
| 623 | + |
| 624 | + model = DummyModel() |
| 625 | + |
| 626 | + sender = model.add_agent((0, 0)) |
| 627 | + sender.memory = ShortTermMemory(agent=sender, n=5, display=True) |
| 628 | + sender.unique_id = 10 |
| 629 | + |
| 630 | + recipient = model.add_agent((1, 1)) |
| 631 | + recipient.memory = ShortTermMemory(agent=recipient, n=5, display=True) |
| 632 | + recipient.unique_id = 20 |
| 633 | + |
| 634 | + return sender, recipient |
| 635 | + |
| 636 | + |
| 637 | +def test_send_message_stores_serializable_ids(monkeypatch): |
| 638 | + """send_message stores sender/recipients as unique_ids, not Agent objects.""" |
| 639 | + sender, recipient = _make_send_message_model(monkeypatch) |
| 640 | + |
| 641 | + captured = {} |
| 642 | + |
| 643 | + def capture_content(type, content): |
| 644 | + captured.update(content) |
| 645 | + |
| 646 | + monkeypatch.setattr(recipient.memory, "add_to_memory", capture_content) |
| 647 | + monkeypatch.setattr(sender.memory, "add_to_memory", lambda *a, **kw: None) |
| 648 | + |
| 649 | + sender.send_message("hello", recipients=[recipient]) |
| 650 | + |
| 651 | + assert captured["sender"] == 10 |
| 652 | + assert captured["recipients"] == [20] |
| 653 | + assert captured["message"] == "hello" |
| 654 | + |
| 655 | + # Must not raise TypeError when serializing |
| 656 | + data = json.loads(json.dumps(captured)) |
| 657 | + assert data["sender"] == 10 |
| 658 | + assert data["recipients"] == [20] |
| 659 | + |
| 660 | + |
| 661 | +@pytest.mark.asyncio |
| 662 | +async def test_asend_message_stores_serializable_ids(monkeypatch): |
| 663 | + """asend_message stores sender/recipients as unique_ids, not Agent objects.""" |
| 664 | + sender, recipient = _make_send_message_model(monkeypatch) |
| 665 | + |
| 666 | + captured = {} |
| 667 | + |
| 668 | + async def capture_content(type, content): |
| 669 | + captured.update(content) |
| 670 | + |
| 671 | + async def noop(*a, **kw): |
| 672 | + pass |
| 673 | + |
| 674 | + monkeypatch.setattr(recipient.memory, "aadd_to_memory", capture_content) |
| 675 | + monkeypatch.setattr(sender.memory, "aadd_to_memory", noop) |
| 676 | + |
| 677 | + await sender.asend_message("hello", recipients=[recipient]) |
| 678 | + |
| 679 | + assert captured["sender"] == 10 |
| 680 | + assert captured["recipients"] == [20] |
| 681 | + assert captured["message"] == "hello" |
| 682 | + |
| 683 | + data = json.loads(json.dumps(captured)) |
| 684 | + assert data["sender"] == 10 |
| 685 | + assert data["recipients"] == [20] |
0 commit comments