From 3f0fdb328969a5e55a017698113064c4f93aa07e Mon Sep 17 00:00:00 2001 From: John Randall Date: Thu, 30 Apr 2026 21:45:01 -0400 Subject: [PATCH] Fix get_post_content silently dropping comment_depth MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit get_post_content accepts a comment_depth parameter, but inside the function the call to get_post_comments dropped it. get_post_comments then called _build_comment_tree(node) with no depth argument, defaulting to 3 every time — so the user-supplied comment_depth had no effect and threads silently truncated at depth 3. Symptom: callers pass comment_depth=10 expecting deep threads; tree truncates at 3 regardless. Found while sweeping a Reddit thread where the answer to a user's question lived at depth 4. Fix: thread depth through get_post_comments and into the recursion. Adds three regression tests covering the default, the propagation through get_post_comments, and the bug-site propagation through get_post_content. Claude-Session-Id: $CLAUDE_SESSION_ID Resume: claude --resume $CLAUDE_SESSION_ID --- src/mcp_server_reddit/server.py | 8 ++-- tests/test_comment_depth.py | 85 +++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 tests/test_comment_depth.py diff --git a/src/mcp_server_reddit/server.py b/src/mcp_server_reddit/server.py index 4345863..911425d 100644 --- a/src/mcp_server_reddit/server.py +++ b/src/mcp_server_reddit/server.py @@ -176,16 +176,16 @@ def get_post_content(self, post_id: str, comment_limit: int = 10, comment_depth: post = self._build_post(submission) # Fetch comments - comments = self.get_post_comments(post_id, comment_limit) - + comments = self.get_post_comments(post_id, comment_limit, comment_depth) + return PostDetail(post=post, comments=comments) - def get_post_comments(self, post_id: str, limit: int = 10) -> list[Comment]: + def get_post_comments(self, post_id: str, limit: int = 10, depth: int = 3) -> list[Comment]: """Get comments from a post""" comments = [] tree_node = self.client.p.comment_tree.fetch(post_id, sort='top', limit=limit) for node in tree_node.children: - comment = self._build_comment_tree(node) + comment = self._build_comment_tree(node, depth) if comment: comments.append(comment) return comments diff --git a/tests/test_comment_depth.py b/tests/test_comment_depth.py new file mode 100644 index 0000000..5338048 --- /dev/null +++ b/tests/test_comment_depth.py @@ -0,0 +1,85 @@ +"""Regression tests for get_post_content propagating comment_depth. + +The bug: get_post_content accepted a comment_depth parameter and the +dispatcher passed it through, but inside get_post_content the call to +get_post_comments dropped the argument. get_post_comments then called +_build_comment_tree(node) with no depth argument, defaulting to 3 every +time. The user-supplied comment_depth had no effect, silently +truncating threads deeper than 3 levels. +""" + +from unittest.mock import MagicMock, patch + +from mcp_server_reddit.server import RedditServer, Comment + + +def _make_comment_node(comment_id, body, children=None): + """Build a mock comment-tree node with the shape redditwarp returns.""" + node = MagicMock() + node.value.id36 = comment_id + node.value.author_display_name = 'tester' + node.value.body = body + node.value.score = 1 + node.children = children or [] + return node + + +def _make_server(): + with patch.object(RedditServer, '__init__', lambda self: None): + server = RedditServer() + server.client = MagicMock() + return server + + +def _build_5_level_chain(): + """Return the root of a 5-level-deep linear comment chain.""" + leaf = _make_comment_node('c5', 'level 5') + c4 = _make_comment_node('c4', 'level 4', [leaf]) + c3 = _make_comment_node('c3', 'level 3', [c4]) + c2 = _make_comment_node('c2', 'level 2', [c3]) + c1 = _make_comment_node('c1', 'level 1', [c2]) + return c1 + + +def _depth_of(comment: Comment) -> int: + depth = 1 + node = comment + while node.replies: + depth += 1 + node = node.replies[0] + return depth + + +def test_get_post_comments_default_depth_is_3(): + server = _make_server() + tree = MagicMock() + tree.children = [_build_5_level_chain()] + server.client.p.comment_tree.fetch.return_value = tree + + result = server.get_post_comments('postid') + + assert len(result) == 1 + assert _depth_of(result[0]) == 3 + + +def test_get_post_comments_respects_depth_arg(): + server = _make_server() + tree = MagicMock() + tree.children = [_build_5_level_chain()] + server.client.p.comment_tree.fetch.return_value = tree + + result = server.get_post_comments('postid', limit=10, depth=5) + + assert len(result) == 1 + assert _depth_of(result[0]) == 5 + + +def test_get_post_content_propagates_comment_depth(): + """The bug site: depth was dropped on the way to get_post_comments.""" + server = _make_server() + with patch.object(server, '_build_post'), \ + patch.object(server, 'get_post_comments', return_value=[]) as mock_get_comments, \ + patch('mcp_server_reddit.server.PostDetail'): + server.get_post_content('postid', comment_limit=7, comment_depth=5) + + mock_get_comments.assert_called_once_with('postid', 7, 5)