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
9 changes: 9 additions & 0 deletions pr_description.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
🎯 **What:** The `_memory_entry` helper function in `router/memory_mcp.py` was previously completely untested. This function is responsible for converting a raw dictionary representation of a memory entry from LiteLLM into a structured dictionary used by the MCP.

📊 **Coverage:** The new `test_memory_mcp.py` file covers several scenarios for the `_memory_entry` dictionary translation helper:
- **Happy Path:** Tests that valid and complete memory dictionaries are properly parsed, mapped, and typed.
- **Invalid Key:** Verifies that if a memory has a key that does not start with `"memory:"`, the function properly returns `None`.
- **Malformed/String Value:** Tests the graceful fallback when a memory value is a raw string instead of the expected JSON payload, ensuring it still parses the string into the `data` field and initializes an empty `tags` array without throwing a JSON decode error.
- **Missing Fields:** Tests dictionary inputs that are missing either `"key"` or `"value"` or both to ensure graceful behavior, fallback values, and early exits without `KeyError`s.

✨ **Result:** Increased code reliability and coverage for the core memory data transformation logic with pure, isolated dictionary tests ensuring zero runtime side effects.
75 changes: 75 additions & 0 deletions test_memory_mcp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env python3
import json
import pytest
from router.memory_mcp import _memory_entry

def test_memory_entry_happy_path():
"""Test correctly formatted and complete memory entry."""
valid_key = "memory:global:project_standards::1689201948123:a1b2c3d4e5f6"
valid_value = json.dumps({"data": "Use pytest for all tests", "tags": ["testing", "python"]})
lmem = {
"key": valid_key,
"value": valid_value,
"memory_id": "test_id_123"
}

result = _memory_entry(lmem)

assert result is not None
assert result["key"] == valid_key
assert result["category"] == "project_standards"
assert result["data"] == "Use pytest for all tests"
assert result["tags"] == ["testing", "python"]
assert result["scope"] == "global"
assert result["timestamp"] == "1689201948123"
assert result["memory_id"] == "test_id_123"

def test_memory_entry_invalid_key():
"""Test with a key that does not start with 'memory:'."""
lmem = {
"key": "notamemory:global:cat::123:hash",
"value": json.dumps({"data": "test", "tags": []})
}

result = _memory_entry(lmem)
assert result is None

def test_memory_entry_malformed_json_value():
"""Test with malformed/string value where JSON parsing fails."""
valid_key = "memory:local:notes::1689201948123:a1b2c3d4e5f6"
# value is just a raw string, not JSON
lmem = {
"key": valid_key,
"value": "This is just a raw string without tags"
}

result = _memory_entry(lmem)

assert result is not None
assert result["data"] == "This is just a raw string without tags"
assert result["tags"] == [] # Falls back to empty tags list
assert result["category"] == "notes"
assert result["scope"] == "local"

def test_memory_entry_missing_fields():
"""Test gracefully handling dictionaries with missing keys."""
# Missing 'value' and 'memory_id'
lmem1 = {
"key": "memory:global:ideas::123:hash"
}
result1 = _memory_entry(lmem1)
assert result1 is not None
assert result1["data"] == ""
assert result1["tags"] == []
assert result1["memory_id"] == ""

# Missing 'key'
lmem2 = {
"value": json.dumps({"data": "test", "tags": []})
}
result2 = _memory_entry(lmem2)
assert result2 is None

# Empty dict
result3 = _memory_entry({})
assert result3 is None
Comment on lines +54 to +75

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The test function test_memory_entry_missing_fields bundles multiple independent test scenarios (missing optional fields, missing required keys, and empty dictionaries) into a single test. This is a test smell known as Assertion Roulette, where a failure in an earlier assertion prevents subsequent independent scenarios from running, making debugging more difficult.

Since pytest is already imported in this file, we can leverage pytest.mark.parametrize to cleanly separate these test cases. This also makes use of the currently unused pytest import.

def test_memory_entry_missing_optional_fields():
    """Test gracefully handling dictionaries with missing optional fields."""
    lmem = {
        "key": "memory:global:ideas::123:hash"
    }
    result = _memory_entry(lmem)
    assert result is not None
    assert result["data"] == ""
    assert result["tags"] == []
    assert result["memory_id"] == ""

@pytest.mark.parametrize(
    "lmem",
    [
        {"value": json.dumps({"data": "test", "tags": []})},
        {},
    ]
)
def test_memory_entry_missing_required_key(lmem):
    """Test that missing the required 'key' field returns None."""
    assert _memory_entry(lmem) is None