Skip to content

AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary

12.2 KB
Raw Blame
import unittest

import pytest

from lib.reddit import (
    _extract_date,
    _extract_score,
    _extract_subreddit_name,
    _normalize_reddit_id,
    _total_engagement,
    enrich_with_comments,
)


class TestExtractSubredditName(unittest.TestCase):
    def test_from_string(self):
        self.assertEqual("openclaw", _extract_subreddit_name("openclaw"))

    def test_from_dict_with_name(self):
        self.assertEqual(
            "openclaw",
            _extract_subreddit_name({"id": "t5_ghydwa", "name": "openclaw"}),
        )

    def test_from_dict_with_display_name(self):
        self.assertEqual(
            "LocalLLM",
            _extract_subreddit_name({"display_name": "LocalLLM"}),
        )

    def test_from_dict_name_preferred_over_display_name(self):
        self.assertEqual(
            "name_wins",
            _extract_subreddit_name({"name": "name_wins", "display_name": "display"}),
        )

    def test_empty_string(self):
        self.assertEqual("", _extract_subreddit_name(""))

    def test_empty_dict(self):
        self.assertEqual("", _extract_subreddit_name({}))

    def test_strips_whitespace(self):
        self.assertEqual("test", _extract_subreddit_name("  test  "))


class TestExtractScore(unittest.TestCase):
    def test_ups(self):
        self.assertEqual(42, _extract_score({"ups": 42}))

    def test_score_field(self):
        self.assertEqual(77, _extract_score({"score": 77}))

    def test_votes(self):
        self.assertEqual(99, _extract_score({"votes": 99}))

    def test_ups_preferred_over_votes(self):
        self.assertEqual(10, _extract_score({"ups": 10, "votes": 99}))

    def test_missing(self):
        self.assertEqual(0, _extract_score({}))

    def test_zero_preserved(self):
        self.assertEqual(0, _extract_score({"ups": 0}))

    def test_zero_ups_does_not_fall_through(self):
        # ups=0 should be returned, not fall through to score
        self.assertEqual(0, _extract_score({"ups": 0, "score": 5}))


class TestExtractDate(unittest.TestCase):
    def test_unix_timestamp(self):
        self.assertEqual("2024-05-03", _extract_date({"created_utc": 1714694957}))

    def test_iso_string(self):
        result = _extract_date({"created_at": "2024-05-03T01:09:17.620000+0000"})
        self.assertEqual("2024-05-03", result)

    def test_iso_with_z_suffix(self):
        result = _extract_date({"created_at": "2024-05-03T01:09:17Z"})
        self.assertEqual("2024-05-03", result)

    def test_created_utc_preferred(self):
        result = _extract_date({"created_utc": 1714694957, "created_at": "2025-01-01T00:00:00Z"})
        self.assertEqual("2024-05-03", result)

    def test_missing(self):
        self.assertIsNone(_extract_date({}))


class TestNormalizeRedditId(unittest.TestCase):
    def test_strips_t3_prefix(self):
        self.assertEqual("abc123", _normalize_reddit_id("t3_abc123"))

    def test_no_prefix(self):
        self.assertEqual("abc123", _normalize_reddit_id("abc123"))

    def test_empty(self):
        self.assertEqual("", _normalize_reddit_id(""))

    def test_none(self):
        self.assertEqual("", _normalize_reddit_id(None))


class TestTotalEngagement(unittest.TestCase):
    def test_score_plus_comments(self):
        item = {"engagement": {"score": 100, "num_comments": 50}}
        self.assertEqual(150, _total_engagement(item))

    def test_high_comments_low_score(self):
        item = {"engagement": {"score": 1, "num_comments": 1387}}
        self.assertEqual(1388, _total_engagement(item))

    def test_missing_engagement(self):
        self.assertEqual(0, _total_engagement({}))

    def test_none_values(self):
        item = {"engagement": {"score": None, "num_comments": None}}
        self.assertEqual(0, _total_engagement(item))

    def test_score_only(self):
        item = {"engagement": {"score": 42}}
        self.assertEqual(42, _total_engagement(item))


class TestEnrichSelectsTopEngagement(unittest.TestCase):
    """Verify enrich_with_comments picks threads by total engagement, not list order."""

    def test_high_comment_thread_enriched_over_low_engagement(self):
        """A thread with 1387 comments but score:1 should be enriched before
        a thread with score:5 and 0 comments."""
        from unittest.mock import patch

        items = [
            # Low engagement thread (first in list)
            {
                "id": "R1",
                "url": "https://www.reddit.com/r/test/comments/low",
                "engagement": {"score": 5, "num_comments": 0},
            },
            # High engagement thread (second in list)
            {
                "id": "R2",
                "url": "https://www.reddit.com/r/test/comments/high",
                "engagement": {"score": 1, "num_comments": 1387},
            },
            # Medium engagement
            {
                "id": "R3",
                "url": "https://www.reddit.com/r/test/comments/med",
                "engagement": {"score": 50, "num_comments": 10},
            },
        ]

        enriched_urls = []

        def mock_fetch_comments(url, token, **kwargs):
            enriched_urls.append(url)
            return [{"body": "Great thread!", "ups": 10, "author": "testuser"}]

        # Only allow 1 enrichment to prove selection order matters
        with patch("lib.reddit.fetch_post_comments", side_effect=mock_fetch_comments):
            result = enrich_with_comments(items, token="fake", depth="quick")

        # With quick depth (3 enrichments), all 3 should be enriched.
        # But the key assertion: the high-comment thread (R2) must be included.
        self.assertIn(
            "https://www.reddit.com/r/test/comments/high",
            enriched_urls,
            "High-comment thread should always be selected for enrichment",
        )

    def test_enrichment_order_by_engagement(self):
        """With a budget of 1, only the highest-engagement thread gets enriched."""
        from unittest.mock import patch

        items = [
            {
                "id": "R1",
                "url": "https://www.reddit.com/r/test/comments/a",
                "engagement": {"score": 200, "num_comments": 5},
            },
            {
                "id": "R2",
                "url": "https://www.reddit.com/r/test/comments/b",
                "engagement": {"score": 1, "num_comments": 1500},
            },
        ]

        enriched_urls = []

        def mock_fetch_comments(url, token, **kwargs):
            enriched_urls.append(url)
            return [{"body": "Comment", "ups": 5, "author": "user"}]

        # Override DEPTH_CONFIG to allow only 1 enrichment
        custom_config = {"comment_enrichments": 1}
        with patch("lib.reddit.DEPTH_CONFIG", {"test": custom_config, "default": custom_config}), \
             patch("lib.reddit.fetch_post_comments", side_effect=mock_fetch_comments):
            enrich_with_comments(items, token="fake", depth="test")

        # R2 has 1501 total engagement vs R1's 205 -- R2 should be picked
        self.assertEqual(len(enriched_urls), 1)
        self.assertEqual(
            enriched_urls[0],
            "https://www.reddit.com/r/test/comments/b",
        )


class TestEnrichmentBudget(unittest.TestCase):
    """Tests for the enrichment time budget in enrich_with_comments()."""

    def _make_items(self, n):
        return [
            {"url": f"https://reddit.com/r/test/comments/{i}/post", "score": 100 - i, "num_comments": 50,
             "engagement": {"score": 100 - i, "num_comments": 50}}
            for i in range(n)
        ]

    def test_all_complete_within_budget(self):
        """When enrichment is fast, all items get comments."""
        from unittest.mock import patch
        items = self._make_items(3)
        fast_comments = [{"body": "Great post!", "score": 42, "author": "user1"}]

        with patch("lib.reddit.fetch_post_comments", return_value=fast_comments):
            result = enrich_with_comments(items, "fake-token", depth="quick", budget_seconds=60)

        enriched = [i for i in result if i.get("top_comments")]
        self.assertEqual(len(enriched), 3)

    def test_budget_zero_returns_items_unenriched(self):
        """With budget=0, items are returned without enrichment (not discarded)."""
        from unittest.mock import patch

        items = self._make_items(3)

        with patch("lib.reddit.fetch_post_comments") as fetch:
            result = enrich_with_comments(items, "fake-token", depth="quick", budget_seconds=0)
        self.assertIs(result, items)
        self.assertEqual(len(result), 3)
        self.assertTrue(all("top_comments" not in item for item in result))
        fetch.assert_not_called()

    def test_empty_items_returns_immediately(self):
        result = enrich_with_comments([], "fake-token", depth="default", budget_seconds=60)
        self.assertEqual(result, [])

    def test_exceptions_dont_crash(self):
        """If enrichment raises, items are returned without comments."""
        from unittest.mock import patch
        items = self._make_items(3)

        with patch("lib.reddit.fetch_post_comments", side_effect=ConnectionError("boom")):
            result = enrich_with_comments(items, "fake-token", depth="quick", budget_seconds=60)

        self.assertEqual(len(result), 3)
        enriched = [i for i in result if i.get("top_comments")]
        self.assertEqual(len(enriched), 0)

if __name__ == "__main__":
    unittest.main()


def test_window_to_time_filter_rounds_up_for_rolling_buckets(monkeypatch):
    """Recent calendar windows need the next Reddit rolling bucket (Greptile on #860)."""
    from datetime import date
    from lib import reddit

    class _FrozenDateTime:
        @staticmethod
        def now(tz=None):
            class _D:
                @staticmethod
                def date():
                    return date(2026, 7, 24)
            return _D()

    monkeypatch.setattr(reddit, "datetime", _FrozenDateTime)
    assert reddit._window_to_time_filter("2026-07-23", "2026-07-24") == "week"
    assert reddit._window_to_time_filter("2026-07-17", "2026-07-24") == "month"
    assert reddit._window_to_time_filter("2026-06-24", "2026-07-24") == "month"
    # Just past the month bucket (+1 slack) needs year.
    assert reddit._window_to_time_filter("2026-06-23", "2026-07-24") == "year"


def test_window_to_time_filter_covers_historical_from_date(monkeypatch):
    """Short historical windows must still reach from_date (Greptile follow-up on #860)."""
    from datetime import date
    from lib import reddit

    class _FrozenDateTime:
        @staticmethod
        def now(tz=None):
            class _D:
                @staticmethod
                def date():
                    return date(2026, 7, 24)
            return _D()

    monkeypatch.setattr(reddit, "datetime", _FrozenDateTime)
    # One-day request ending two weeks ago: span alone would pick "week" and
    # miss the entire range; age of from_date requires "month".
    assert reddit._window_to_time_filter("2026-07-09", "2026-07-10") == "month"


@pytest.mark.parametrize(
    "depth,from_date,to_date,expected",
    [
        ("default", "2026-04-25", "2026-07-24", "year"),
        ("quick", "2026-06-24", "2026-07-24", "month"),
        ("deep", "2026-04-25", "2026-07-24", "year"),
        ("quick", "2026-07-09", "2026-07-10", "month"),
        ("default", "2026-07-23", "2026-07-24", "week"),
        ("deep", "2026-07-23", "2026-07-24", "week"),
    ],
)
def test_search_reddit_requests_complete_date_window(monkeypatch, depth, from_date, to_date, expected):
    from datetime import datetime, timezone
    from unittest.mock import Mock

    from lib import reddit

    clock = Mock()
    clock.now.return_value = datetime(2026, 7, 24, tzinfo=timezone.utc)
    monkeypatch.setattr(reddit, "datetime", clock)
    global_search = Mock(return_value=[])
    subreddit_search = Mock(return_value=[])
    monkeypatch.setattr(reddit, "_global_search", global_search)
    monkeypatch.setattr(reddit, "_subreddit_search", subreddit_search)

    reddit.search_reddit(
        "Python", from_date, to_date, depth=depth,
        token="dummy-scrapecreators-key", subreddits=["Python"],
    )

    assert global_search.call_count > 0
    assert subreddit_search.call_count > 0
    assert {call.args[-1] for call in global_search.call_args_list} == {expected}
    assert {call.args[-1] for call in subreddit_search.call_args_list} == {expected}