from datetime import date from types import SimpleNamespace from zoneinfo import ZoneInfo import pytest from snooper_summary import summarize_messages from snooper_weekly import ( WeeklyError, generate_weekly, 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) ) def test_previous_weekly_background_uses_previous_monday(tmp_path): output = { "summary": str(tmp_path / "Daily" / "Msg{date}.md"), "weekly": str(tmp_path / "Weekly" / "Week{date}.md"), } weekly = tmp_path / "Weekly" / "Week2024-07-01.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 / "Week2024-07-01.md").write_text("上周合成周记", encoding="utf-8") config = tmp_path / "config.yaml" config.write_text( f"output:\n summary: '{daily}/Msg{{date}}.md'\n" f" weekly: '{weekly}/Week{{date}}.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" 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]