Files
Lark-Snooper/tests/test_weekly.py
2026-11-23 22:27:00 +08:00

125 lines
3.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from datetime import date
from types import SimpleNamespace
from zoneinfo import ZoneInfo
import pytest
from snooper_summary import summarize_messages
from snooper_weekly import (
WEEKLY_FINAL_PROMPT,
WeeklyError,
generate_weekly,
iso_week_label,
load_previous_weekly_background,
week_bounds,
)
def test_week_bounds_use_monday_and_sunday():
assert week_bounds(date(2024, 7, 4)) == (
date(2024, 7, 1), date(2024, 7, 7)
)
assert iso_week_label(date(2024, 7, 1)) == "2024-W27"
assert "# 周记({week_start} 至 {week_end})" in WEEKLY_FINAL_PROMPT
assert "# {weekly_date} 周记" not in WEEKLY_FINAL_PROMPT
def test_previous_weekly_background_uses_previous_monday(tmp_path):
output = {
"summary": str(tmp_path / "Daily" / "MsgYYYY-MM-DD.md"),
"weekly": str(tmp_path / "Weekly" / "MsgWYYYY-WW.md"),
}
weekly = tmp_path / "Weekly" / "MsgW2024-27.md"
weekly.parent.mkdir(parents=True)
weekly.write_text("---\ntype: weekly\n---\n\n上周合成周记", encoding="utf-8")
content, missing = load_previous_weekly_background(output, date(2024, 7, 8))
assert content == "上周合成周记"
assert missing is False
def test_generate_weekly_reads_previous_week_and_sanitizes_links(
monkeypatch, tmp_path
):
daily = tmp_path / "Daily"
weekly = tmp_path / "Weekly"
daily.mkdir()
weekly.mkdir()
for day in ("2024-07-08", "2024-07-09"):
(daily / f"Msg{day}.md").write_text(
f"---\nmodel: synthetic\n---\n\n# {day}\n合成进展", encoding="utf-8"
)
(weekly / "MsgW2024-27.md").write_text("上周合成周记", encoding="utf-8")
config = tmp_path / "config.yaml"
config.write_text(
f"output:\n summary: '{daily}/MsgYYYY-MM-DD.md'\n"
f" weekly: '{weekly}/MsgWYYYY-WW.md'\n",
encoding="utf-8",
)
class FakeClient:
def __init__(self, **kwargs):
self.prompts = []
self.responses = ["分日报提取", "周记[[审核群|指数内容部落]]"]
def assert_model_available(self):
return None
def complete(self, prompt):
self.prompts.append(prompt)
return self.responses.pop(0)
clients = []
monkeypatch.setattr(
"snooper_weekly.MagpieClient",
lambda **kwargs: clients.append(FakeClient(**kwargs)) or clients[-1],
)
output = generate_weekly("2024-07-08", config_path=str(config))
text = output.read_text(encoding="utf-8")
assert "[[审核群|指数内容部落]]" in text
assert "type: weekly" not in text
assert "date:" not in text
assert "上周合成周记" in clients[0].prompts[-1]
def test_existing_weekly_does_not_call_model(tmp_path):
config = tmp_path / "config.yaml"
output = tmp_path / "Week2024-07-08.md"
output.write_text("已有周记", encoding="utf-8")
config.write_text(
f"output:\n weekly: '{output}'\n", encoding="utf-8"
)
with pytest.raises(WeeklyError, match="已存在"):
generate_weekly("2024-07-08", config_path=str(config))
def test_daily_final_prompt_receives_previous_weekly():
class FakeAI:
def __init__(self):
self.prompts = []
def complete(self, prompt):
self.prompts.append(prompt)
return "单对话摘要" if len(self.prompts) == 1 else "最终日报"
row = SimpleNamespace(
msg_id="m1", create_time=1_720_000_000, chat_id="group-a",
chat_name="合成群", chat_type=2, sender_id="sender-a",
sender_name="同事甲", msg_type=4, msg_type_name="TEXT",
direction="in", content="合成消息", scope="chat", anchor="",
root_id="", parent_id="", is_read=False,
)
ai = FakeAI()
result = summarize_messages(
ai, [row], date(2024, 7, 8), ZoneInfo("Asia/Shanghai"),
previous_weekly_summary="上周合成周记",
)
assert result == "最终日报"
assert "上周合成周记" in ai.prompts[-1]