Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/data-layer/src/monitor_data/tools/mongodb_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1266,13 +1266,13 @@ def mongodb_create_combat(params: CombatCreate) -> CombatResponse:
raise ValueError(f"Scene {params.scene_id} not found")

# Validate story exists (via Neo4j)
neo4j = get_neo4j_client()
with neo4j.session() as session: # type: ignore[attr-defined]
result = session.run(
"MATCH (s:Story {id: $story_id}) RETURN s", story_id=str(params.story_id)
)
if not result.single():
raise ValueError(f"Story {params.story_id} not found")
neo4j_client = get_neo4j_client()
story_exists = neo4j_client.execute_read(
"MATCH (s:Story {id: $story_id}) RETURN s.id AS story_id",
{"story_id": str(params.story_id)},
)
if not story_exists:
raise ValueError(f"Story {params.story_id} not found")

now = datetime.now(timezone.utc)
encounter_id = uuid4()
Expand Down
40 changes: 33 additions & 7 deletions packages/data-layer/tests/test_tools/test_combat_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,8 @@ def test_create_combat_success(

# Mock Neo4j
mock_neo4j = MagicMock()
mock_session = MagicMock()
mock_result = MagicMock()
mock_single = MagicMock()

mock_get_neo4j.return_value = mock_neo4j
mock_neo4j.session.return_value.__enter__.return_value = mock_session
mock_session.run.return_value = mock_result
mock_result.single.return_value = mock_single
mock_neo4j.execute_read.return_value = [{"story_id": str(story_id)}]

# Test data
participants = [
Expand Down Expand Up @@ -146,6 +140,38 @@ def test_create_combat_scene_not_found(
mongodb_create_combat(params)


@patch("monitor_data.tools.mongodb_tools.get_neo4j_client")
@patch("monitor_data.tools.mongodb_tools.get_mongodb_client")
def test_create_combat_story_not_found(
mock_get_mongodb: Mock,
mock_get_neo4j: Mock,
):
"""Test creating combat with invalid story_id."""
scene_id = uuid4()
story_id = uuid4()

mock_mongodb = MagicMock()
mock_combats = MagicMock()
mock_scenes = MagicMock()

mock_get_mongodb.return_value = mock_mongodb
mock_mongodb.get_collection.side_effect = lambda name: (
mock_combats if name == "combat_encounters" else mock_scenes
)

# Scene exists but story does not
mock_scenes.find_one.return_value = {"scene_id": str(scene_id)}
mock_get_neo4j.return_value = MagicMock(execute_read=MagicMock(return_value=[]))

Copilot AI Jan 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Neo4j mock setup should be consistent with the pattern used in test_create_combat_success. Instead of creating a nested MagicMock inline, create separate mock objects for better clarity and consistency within the test file. The current approach with MagicMock(execute_read=MagicMock(return_value=[])) is harder to debug and less explicit than the pattern used elsewhere in the file.

Suggested change
mock_get_neo4j.return_value = MagicMock(execute_read=MagicMock(return_value=[]))
mock_neo4j_client = MagicMock()
mock_neo4j_client.execute_read = MagicMock(return_value=[])
mock_get_neo4j.return_value = mock_neo4j_client

Copilot uses AI. Check for mistakes.

params = CombatCreate(
scene_id=scene_id,
story_id=story_id,
)

with pytest.raises(ValueError, match=f"Story {story_id} not found"):
mongodb_create_combat(params)


# =============================================================================
# TEST: mongodb_get_combat
# =============================================================================
Expand Down