test_variable_collection_info.py•7.75 kB
"""
Test enhanced variable information (length, type info for collections).
"""
import sys
import pytest
from pathlib import Path
from mcp_debug_tool.schemas import BreakpointRequest, StartSessionRequest
from mcp_debug_tool.sessions import SessionManager
@pytest.fixture
def workspace_root(tmp_path):
"""Create a temporary workspace."""
return tmp_path
@pytest.fixture
def test_script_with_collections(workspace_root):
"""Create a test script with various collection types."""
script_path = workspace_root / "test_collections.py"
script_path.write_text("""def process_collections():
# Various collection types
my_list = [1, 2, 3, 4, 5]
my_tuple = (10, 20, 30)
my_dict = {"name": "Alice", "age": 30, "city": "Tokyo"}
my_set = {100, 200, 300}
my_string = "Hello, World!"
empty_list = []
large_list = list(range(1000))
# Breakpoint here to inspect collections
result = len(my_list) + len(my_tuple)
return result
if __name__ == "__main__":
process_collections()
""")
return script_path
@pytest.fixture
def session_manager(workspace_root):
"""Create a session manager."""
return SessionManager(workspace_root)
def test_list_variable_has_length_info(session_manager, test_script_with_collections):
"""Test that list variables include length information."""
# Create session
create_request = StartSessionRequest(
entry=test_script_with_collections.relative_to(session_manager.workspace_root).as_posix(),
pythonPath=sys.executable,
)
create_response = session_manager.create_session(create_request)
session_id = create_response.sessionId
# Run to breakpoint (line 12 - result = len(my_list) + len(my_tuple))
bp_request = BreakpointRequest(
file=test_script_with_collections.relative_to(session_manager.workspace_root).as_posix(),
line=12,
)
response = session_manager.run_to_breakpoint(session_id, bp_request)
# Verify breakpoint was hit
assert response.hit is True
assert response.locals is not None
# Check my_list
assert "my_list" in response.locals
my_list_var = response.locals["my_list"]
assert my_list_var["type"] == "list"
assert "length" in my_list_var
assert my_list_var["length"] == 5
assert my_list_var.get("isIndexed") is True
assert my_list_var.get("isExpandable") is True
# Clean up
session_manager.end_session(session_id)
def test_dict_variable_has_named_properties_info(session_manager, test_script_with_collections):
"""Test that dict variables include named properties information."""
create_request = StartSessionRequest(
entry=test_script_with_collections.relative_to(session_manager.workspace_root).as_posix(),
pythonPath=sys.executable,
)
create_response = session_manager.create_session(create_request)
session_id = create_response.sessionId
bp_request = BreakpointRequest(
file=test_script_with_collections.relative_to(session_manager.workspace_root).as_posix(),
line=12,
)
response = session_manager.run_to_breakpoint(session_id, bp_request)
assert response.hit is True
assert "my_dict" in response.locals
my_dict_var = response.locals["my_dict"]
assert my_dict_var["type"] == "dict"
assert "length" in my_dict_var or "namedCount" in my_dict_var
# Either field name is acceptable
count = my_dict_var.get("length") or my_dict_var.get("namedCount")
assert count == 3
assert my_dict_var.get("hasNamedProperties") is True
assert my_dict_var.get("isExpandable") is True
session_manager.end_session(session_id)
def test_empty_collection_has_length_zero(session_manager, test_script_with_collections):
"""Test that empty collections have length 0 and are not expandable."""
create_request = StartSessionRequest(
entry=test_script_with_collections.relative_to(session_manager.workspace_root).as_posix(),
pythonPath=sys.executable,
)
create_response = session_manager.create_session(create_request)
session_id = create_response.sessionId
bp_request = BreakpointRequest(
file=test_script_with_collections.relative_to(session_manager.workspace_root).as_posix(),
line=12,
)
response = session_manager.run_to_breakpoint(session_id, bp_request)
assert response.hit is True
assert "empty_list" in response.locals
empty_list_var = response.locals["empty_list"]
assert empty_list_var["type"] == "list"
assert empty_list_var.get("length") == 0
# Empty collections should not be marked as expandable
assert empty_list_var.get("isExpandable") is not True
session_manager.end_session(session_id)
def test_large_collection_marked_as_truncated(session_manager, test_script_with_collections):
"""Test that large collections are marked as truncated."""
create_request = StartSessionRequest(
entry=test_script_with_collections.relative_to(session_manager.workspace_root).as_posix(),
pythonPath=sys.executable,
)
create_response = session_manager.create_session(create_request)
session_id = create_response.sessionId
bp_request = BreakpointRequest(
file=test_script_with_collections.relative_to(session_manager.workspace_root).as_posix(),
line=12,
)
response = session_manager.run_to_breakpoint(session_id, bp_request)
assert response.hit is True
assert "large_list" in response.locals
large_list_var = response.locals["large_list"]
assert large_list_var["type"] == "list"
assert large_list_var.get("length") == 1000
# Large collections should be marked as truncated
assert large_list_var.get("isTruncated") is True
session_manager.end_session(session_id)
def test_tuple_variable_info(session_manager, test_script_with_collections):
"""Test that tuple variables include proper type information."""
create_request = StartSessionRequest(
entry=test_script_with_collections.relative_to(session_manager.workspace_root).as_posix(),
pythonPath=sys.executable,
)
create_response = session_manager.create_session(create_request)
session_id = create_response.sessionId
bp_request = BreakpointRequest(
file=test_script_with_collections.relative_to(session_manager.workspace_root).as_posix(),
line=12,
)
response = session_manager.run_to_breakpoint(session_id, bp_request)
assert response.hit is True
assert "my_tuple" in response.locals
my_tuple_var = response.locals["my_tuple"]
assert my_tuple_var["type"] == "tuple"
assert my_tuple_var.get("length") == 3
assert my_tuple_var.get("isIndexed") is True
session_manager.end_session(session_id)
def test_string_variable_has_length(session_manager, test_script_with_collections):
"""Test that string variables include length information."""
create_request = StartSessionRequest(
entry=test_script_with_collections.relative_to(session_manager.workspace_root).as_posix(),
pythonPath=sys.executable,
)
create_response = session_manager.create_session(create_request)
session_id = create_response.sessionId
bp_request = BreakpointRequest(
file=test_script_with_collections.relative_to(session_manager.workspace_root).as_posix(),
line=12,
)
response = session_manager.run_to_breakpoint(session_id, bp_request)
assert response.hit is True
assert "my_string" in response.locals
my_string_var = response.locals["my_string"]
assert my_string_var["type"] == "str"
assert my_string_var.get("length") == 13 # "Hello, World!" is 13 chars
session_manager.end_session(session_id)