86 lines
2.4 KiB
Python
86 lines
2.4 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
|
|
def test_analyze_trends_parses_json(monkeypatch, isolated_output):
|
|
import daily.agent_workflow as agent
|
|
import daily.config as config
|
|
|
|
monkeypatch.setattr(config, "OUTPUT_DIR", isolated_output)
|
|
monkeypatch.setattr(agent, "OUTPUT_DIR", isolated_output)
|
|
|
|
monkeypatch.setattr(
|
|
agent,
|
|
"llm_chat",
|
|
lambda system, user: json.dumps(
|
|
{
|
|
"headline": "Skills 视频工具升温",
|
|
"opening": "今日 remotion 相关技能继续走强。",
|
|
"themes": [{"name": "视频", "summary": "Remotion 生态活跃"}],
|
|
"top_picks": [],
|
|
"signals": [],
|
|
},
|
|
ensure_ascii=False,
|
|
),
|
|
)
|
|
|
|
llm_input = {"date": "2026-07-03", "skills_trending": []}
|
|
trends = agent.analyze_trends(llm_input, date_str="2026-07-03")
|
|
|
|
assert trends is not None
|
|
assert trends["headline"] == "Skills 视频工具升温"
|
|
assert (isolated_output / "2026-07-03.trends.json").exists()
|
|
|
|
|
|
def test_write_wecom_report_extracts_markdown_block(monkeypatch):
|
|
import daily.agent_workflow as agent
|
|
|
|
monkeypatch.setattr(
|
|
agent,
|
|
"llm_chat",
|
|
lambda system, user: "```markdown\n📰 **早报 · 2026-07-03**\n\n正文\n```",
|
|
)
|
|
|
|
md = agent.write_wecom_report(
|
|
{"date": "2026-07-03"},
|
|
{"headline": "test", "opening": "open"},
|
|
date_str="2026-07-03",
|
|
time_str="09:30 (UTC+8)",
|
|
updated="2026-07-02",
|
|
)
|
|
|
|
assert md is not None
|
|
assert md.startswith("📰")
|
|
assert "正文" in md
|
|
|
|
|
|
def test_run_agent_workflow_retries_step2(monkeypatch):
|
|
import daily.agent_workflow as agent
|
|
|
|
calls = {"n": 0}
|
|
|
|
def fake_llm(system, user):
|
|
if '"trends"' in user:
|
|
calls["n"] += 1
|
|
if calls["n"] == 1:
|
|
raise RuntimeError("Bridge request timed out")
|
|
return "📰 **早报 · 2026-07-03**\n\n重试成功"
|
|
return json.dumps(
|
|
{"headline": "h", "opening": "o", "themes": [], "top_picks": [], "signals": []},
|
|
ensure_ascii=False,
|
|
)
|
|
|
|
monkeypatch.setattr(agent, "llm_chat", fake_llm)
|
|
|
|
md = agent.run_agent_workflow(
|
|
{"date": "2026-07-03"},
|
|
date_str="2026-07-03",
|
|
time_str="09:30 (UTC+8)",
|
|
updated="2026-07-02",
|
|
)
|
|
|
|
assert md is not None
|
|
assert "重试成功" in md
|
|
assert calls["n"] == 2
|