66 lines
2.4 KiB
Python
66 lines
2.4 KiB
Python
# tests/test_board_history.py
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import os
|
||
import tempfile
|
||
import unittest
|
||
from pathlib import Path
|
||
from unittest.mock import patch
|
||
|
||
from daily.board_history import extract_shown_keys, load_recent_shown_keys, merge_wecom_shown_into_data
|
||
from daily.config import board_dedup_days, news_backfill_enabled
|
||
|
||
|
||
class ConfigDiversityTests(unittest.TestCase):
|
||
def test_board_dedup_days_default(self):
|
||
with patch.dict(os.environ, {}, clear=True):
|
||
self.assertEqual(board_dedup_days(), 7)
|
||
|
||
def test_news_backfill_default_off(self):
|
||
with patch.dict(os.environ, {}, clear=True):
|
||
self.assertFalse(news_backfill_enabled())
|
||
|
||
|
||
class ShownKeysTests(unittest.TestCase):
|
||
def test_extract_github_repo_keys(self):
|
||
items = [{"repo": "a/b"}, {"repo": "c/d"}]
|
||
self.assertEqual(extract_shown_keys("github_trending", items), ["a/b", "c/d"])
|
||
|
||
def test_load_recent_reads_wecom_shown_not_baseline(self):
|
||
with tempfile.TemporaryDirectory() as tmp:
|
||
out = Path(tmp)
|
||
# 前日:shown 只有 x/y;baseline raw 含 a/b —— 周去重只能看到 x/y
|
||
payload = {
|
||
"data": {
|
||
"date": "2026-07-13",
|
||
"movement_baseline": {
|
||
"github_trending": [{"repo": "a/b"}, {"repo": "x/y"}],
|
||
},
|
||
"wecom_shown_keys": {"github_trending": ["x/y"]},
|
||
}
|
||
}
|
||
(out / "2026-07-13.data.json").write_text(
|
||
json.dumps(payload, ensure_ascii=False), encoding="utf-8"
|
||
)
|
||
with patch("daily.board_history.OUTPUT_DIR", out):
|
||
keys = load_recent_shown_keys("2026-07-14", lookback_days=7)
|
||
self.assertEqual(keys["github_trending"], {"x/y"})
|
||
self.assertNotIn("a/b", keys["github_trending"])
|
||
|
||
def test_merge_shown_does_not_touch_baseline(self):
|
||
data = {
|
||
"movement_baseline": {"github_trending": [{"repo": "raw/one"}]},
|
||
}
|
||
merged = merge_wecom_shown_into_data(
|
||
data, {"github_trending": ["shown/one"]}
|
||
)
|
||
self.assertEqual(
|
||
merged["movement_baseline"]["github_trending"][0]["repo"], "raw/one"
|
||
)
|
||
self.assertEqual(merged["wecom_shown_keys"]["github_trending"], ["shown/one"])
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|