magpie/tests/services/platforms/test_reddit_comment_strategy.py
pyr0ball e37be0935d feat: implement RedditCommentStrategy and register in platform registry
Adds RedditCommentStrategy to app/services/platforms/reddit_comment.py,
resolving thread_id via thread_url_override or _find_sticky title search,
falling back to reconstructed URL when client.comment() returns empty string.
Registers the strategy under "reddit_comment" in the platform _REGISTRY.
7 new tests confirm all execution paths: url override, title pattern lookup,
not-found error, missing-extra error, empty-URL reconstruction, dupe guard,
and registry presence. Full suite: 34/34 passing.
2026-04-27 12:00:08 -07:00

90 lines
4.3 KiB
Python

from unittest.mock import patch, MagicMock
import pytest
from app.services.platforms.reddit_comment import RedditCommentStrategy
from app.services.platforms.base import PostResult
@pytest.fixture
def strategy():
return RedditCommentStrategy()
def test_execute_with_url_override(strategy, monkeypatch):
"""Uses thread_url_override to get thread_id, calls client.comment()"""
mock_client = MagicMock()
mock_client.comment.return_value = "https://www.reddit.com/r/flipping/comments/abc123/_/xyz789/"
with patch("app.services.platforms.reddit_comment.RedditClient", return_value=mock_client):
with patch("app.services.platforms.reddit_comment.get_settings") as mock_settings:
mock_settings.return_value.reddit_session_file = "/fake/session.json"
result = strategy.execute(
target="flipping",
title="ignored",
body="Hello thread!",
extra={"thread_url_override": "https://www.reddit.com/r/flipping/comments/abc123/weekly/"},
)
assert result.url == "https://www.reddit.com/r/flipping/comments/abc123/_/xyz789/"
assert result.metadata["thread_id"] == "abc123"
mock_client.comment.assert_called_once_with(thread_id="abc123", body="Hello thread!")
def test_execute_with_title_pattern_found(strategy):
"""Uses _find_sticky to locate thread, posts comment"""
with patch("app.services.platforms.reddit_comment._find_sticky", return_value="def456"):
with patch("app.services.platforms.reddit_comment.RedditClient") as MockClient:
with patch("app.services.platforms.reddit_comment.get_settings") as mock_settings:
mock_settings.return_value.reddit_session_file = "/fake/session.json"
MockClient.return_value.comment.return_value = ""
result = strategy.execute(
target="cscareerquestions",
title="ignored",
body="Job search tool",
extra={"thread_title_pattern": "Monthly Resume"},
)
assert "def456" in result.url
assert result.metadata["thread_id"] == "def456"
def test_execute_thread_not_found(strategy):
"""Raises ValueError when _find_sticky returns None"""
with patch("app.services.platforms.reddit_comment._find_sticky", return_value=None):
with patch("app.services.platforms.reddit_comment.get_settings") as mock_settings:
mock_settings.return_value.reddit_session_file = "/fake/session.json"
with pytest.raises(ValueError, match="No thread matching"):
strategy.execute(
target="cscareerquestions",
title="ignored",
body="body",
extra={"thread_title_pattern": "Monthly Resume"},
)
def test_execute_no_extra_raises(strategy):
"""Raises ValueError when neither thread_url_override nor thread_title_pattern provided"""
with patch("app.services.platforms.reddit_comment.get_settings") as mock_settings:
mock_settings.return_value.reddit_session_file = "/fake/session.json"
with pytest.raises(ValueError, match="requires thread_url_override or thread_title_pattern"):
strategy.execute(target="flipping", title="t", body="b", extra={})
def test_reconstructed_url_on_empty_comment_url(strategy):
"""When client.comment() returns empty string, reconstructs URL from thread_id"""
with patch("app.services.platforms.reddit_comment.RedditClient") as MockClient:
with patch("app.services.platforms.reddit_comment.get_settings") as mock_settings:
mock_settings.return_value.reddit_session_file = "/fake/session.json"
MockClient.return_value.comment.return_value = ""
result = strategy.execute(
target="flipping",
title="t",
body="b",
extra={"thread_url_override": "https://www.reddit.com/r/flipping/comments/abc123/weekly/"},
)
assert result.url == "https://www.reddit.com/r/flipping/comments/abc123/"
def test_supports_dupe_guard_false(strategy):
assert strategy.supports_dupe_guard() is False
def test_registry_contains_reddit_comment():
from app.services.platforms import _REGISTRY
assert "reddit_comment" in _REGISTRY