tests: centralize script path setup in conftest.py

Add a pytest-discovered tests/conftest.py for the last30days scripts path and
remove duplicate per-file sys.path.insert boilerplate from tests.

Normalize affected imports to rely on the shared scripts path and remove the
now-unneeded E402 suppressions.
This commit is contained in:
Yong-yuan-X
2026-05-20 23:16:07 +08:00
parent 850c7e0185
commit e74b0e1e93
84 changed files with 194 additions and 578 deletions
+11 -16
View File
@@ -5,21 +5,17 @@ from __future__ import annotations
import json
import os
import shutil
import sys
from datetime import datetime, timedelta, timezone
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
sys.path.insert(0, str(Path(__file__).parent.parent / "skills" / "last30days" / "scripts"))
from lib import digg # noqa: E402
from lib import subproc # noqa: E402
from lib import digg
from lib import subproc
# === Helpers ===
def _cluster(
cluster_url_id: str = "abc123xy",
title: str = "Sample cluster",
@@ -66,9 +62,9 @@ def _post(
def _stdout_for(payload: dict) -> subproc.SubprocResult:
return subproc.SubprocResult(returncode=0, stdout=json.dumps(payload), stderr="")
# === _parse_first_post_age ===
def test_parse_first_post_age_days():
today = datetime(2026, 5, 9, tzinfo=timezone.utc)
assert digg._parse_first_post_age("5d", today=today) == "2026-05-04"
@@ -104,9 +100,9 @@ def test_parse_first_post_age_invalid():
assert digg._parse_first_post_age("d") is None
assert digg._parse_first_post_age("-3d") is None
# === parse_digg_response ===
def test_parse_response_happy_path():
response = {
"results": [
@@ -193,9 +189,9 @@ def test_parse_response_engagement_rank_score():
assert by_id["top"]["engagement"]["rank_score"] == 50.0
assert by_id["off-leaderboard"]["engagement"]["rank_score"] == 0.0
# === _parse_post ===
def test_parse_post_happy():
out = digg._parse_post(_post(username="adam", body="Hello world"))
assert out is not None
@@ -210,9 +206,9 @@ def test_parse_post_drops_missing_body_or_handle_or_url():
assert digg._parse_post({"author": {"username": "x"}, "body": "txt", "xUrl": ""}) is None
assert digg._parse_post(None) is None # type: ignore[arg-type]
# === _run_cli / search_digg with stubbed subprocess ===
def test_search_digg_binary_missing_returns_empty(monkeypatch):
monkeypatch.setattr(digg.shutil, "which", lambda _: None)
out = digg.search_digg("anything", "2026-04-09", "2026-05-09")
@@ -280,9 +276,9 @@ def test_search_digg_empty_query_short_circuits(monkeypatch):
assert out["results"] == []
called.assert_not_called()
# === enrich_with_top_posts ===
def test_enrich_with_top_posts_attaches_posts(monkeypatch):
monkeypatch.setattr(digg.shutil, "which", lambda _: "/fake/path")
@@ -357,9 +353,9 @@ def test_enrich_top_k_zero_skips_all(monkeypatch):
digg.enrich_with_top_posts(items, top_k=0)
fake.assert_not_called()
# === enrich_source_items (post-dedupe path) ===
class _FakeSourceItem:
def __init__(self, source, item_id, engagement, metadata):
self.source = source
@@ -410,14 +406,14 @@ def test_enrich_source_items_falls_back_to_item_id(monkeypatch):
digg.enrich_source_items(items, top_k=1)
assert captured["cluster_id"] == "fallbackid"
# === Live tests (opt-in) ===
LIVE = os.environ.get("LAST30DAYS_DIGG_LIVE", "").lower() in ("1", "true", "yes")
HAVE_BIN = shutil.which(digg.CLI_BIN) is not None
@pytest.mark.skipif(not (LIVE and HAVE_BIN), reason="LAST30DAYS_DIGG_LIVE not set or digg-pp-cli missing")
class TestLiveDigg:
def test_search_returns_clusters(self):
out = digg.search_digg("claude code", "2026-04-09", "2026-05-09", depth="quick")
@@ -451,6 +447,5 @@ class TestLiveDigg:
posts = digg.fetch_top_posts("notarealclusterid", posts_per=2)
assert posts == []
if __name__ == "__main__":
pytest.main([__file__, "-v"])