feat: enrich summaries with contacts and Obsidian links
This commit is contained in:
56
tests/test_contacts.py
Normal file
56
tests/test_contacts.py
Normal file
@ -0,0 +1,56 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
|
||||
class FakeClient:
|
||||
def __init__(self):
|
||||
self.calls = []
|
||||
|
||||
def api(self, name, data):
|
||||
self.calls.append((name, data))
|
||||
if name == "chatters.PullChattersByIdsRequest":
|
||||
assert data["notSubscribeChatters"] is True
|
||||
people = {
|
||||
"u1": {"id": "u1", "name": "张三"},
|
||||
"u3": {"id": "u3", "name": "王五"},
|
||||
}
|
||||
return {
|
||||
"chatters": {
|
||||
user_id: people[user_id]
|
||||
for user_id in data["chatterIds"]
|
||||
if user_id in people
|
||||
}
|
||||
}
|
||||
assert name == "chats.PullChatChattersRequest"
|
||||
assert data["chatId"] == "group-a"
|
||||
assert data["needChatters"] is True
|
||||
if "cursor" not in data:
|
||||
return {
|
||||
"chatters": {"u1": {"id": "u1", "name": "张三"}},
|
||||
"nextCursor": 2,
|
||||
}
|
||||
assert data["cursor"] == 2
|
||||
return {"chatters": {"u2": {"id": "u2", "name": "李四"}}}
|
||||
|
||||
|
||||
def test_fetch_contacts_scopes_members_to_relevant_chats():
|
||||
from larkx.contacts import fetch_contacts
|
||||
|
||||
client = FakeClient()
|
||||
rows = [
|
||||
SimpleNamespace(chat_id="group-a", chat_type=2, sender_id="u1"),
|
||||
SimpleNamespace(chat_id="private-b", chat_type=1, sender_id="u3"),
|
||||
]
|
||||
|
||||
contacts = fetch_contacts(client, rows)
|
||||
|
||||
assert contacts == {
|
||||
"group-a": {"u1": "张三", "u2": "李四"},
|
||||
"private-b": {"u3": "王五"},
|
||||
}
|
||||
member_calls = [
|
||||
data
|
||||
for name, data in client.calls
|
||||
if name == "chats.PullChatChattersRequest"
|
||||
]
|
||||
assert len(member_calls) == 2
|
||||
assert all(data["chatId"] == "group-a" for data in member_calls)
|
||||
@ -207,6 +207,46 @@ def test_summary_is_per_conversation_before_daily_combination():
|
||||
assert "私聊原始内容" not in prompts[2]
|
||||
|
||||
|
||||
def test_summary_enriches_names_and_requests_obsidian_wikilinks():
|
||||
session = FakeSession(
|
||||
completions=[
|
||||
(DEFAULT_MODEL, "张三确认了事项"),
|
||||
(DEFAULT_MODEL, "# 最终总结\n张三确认了事项"),
|
||||
]
|
||||
)
|
||||
client = MagpieClient(DEFAULT_MODEL, session=session)
|
||||
rows = [
|
||||
synthetic_row(
|
||||
content="三确认一下",
|
||||
chat_id="group-a",
|
||||
chat_name="项目群",
|
||||
chat_type=2,
|
||||
)
|
||||
]
|
||||
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
|
||||
|
||||
result = summarize_messages(
|
||||
client,
|
||||
rows,
|
||||
date(2026, 11, 3),
|
||||
tz,
|
||||
contacts={"group-a": {"sender-test": "张三"}},
|
||||
)
|
||||
|
||||
assert result.startswith("# 最终总结")
|
||||
system_prompt = session.post_calls[0][1]["json"]["messages"][0]["content"]
|
||||
first_prompt = session.post_calls[0][1]["json"]["messages"][1]["content"]
|
||||
final_prompt = session.post_calls[1][1]["json"]["messages"][1]["content"]
|
||||
assert '"sender_name": "张三"' in first_prompt
|
||||
assert '"full_name": "张三"' in first_prompt
|
||||
assert "唯一匹配" in system_prompt
|
||||
assert "[[完整姓名]]" in system_prompt
|
||||
assert "#tag" in system_prompt
|
||||
assert "[[张三]]确认了事项" in final_prompt
|
||||
assert "[[张三]]确认了事项" in result
|
||||
assert "Wikilink" in final_prompt
|
||||
|
||||
|
||||
def test_storage_selects_only_received_messages_inside_day_and_chat():
|
||||
storage = Storage(url="sqlite:///:memory:")
|
||||
tz = ZoneInfo("Asia/Shanghai")
|
||||
@ -300,6 +340,8 @@ def test_cli_end_to_end_with_synthetic_db_and_fake_magpie(
|
||||
output=output,
|
||||
check=False,
|
||||
dry_run=False,
|
||||
no_contacts=True,
|
||||
no_fetch=True,
|
||||
)
|
||||
assert cmd_summarize(args) == 0
|
||||
assert output.read_text(encoding="utf-8") == "# 合成总结\n"
|
||||
@ -351,6 +393,7 @@ def test_cli_dry_run_exports_records_without_calling_model(
|
||||
config=config_file,
|
||||
check=False,
|
||||
dry_run=True,
|
||||
no_fetch=True,
|
||||
)
|
||||
|
||||
assert cmd_summarize(args) == 0
|
||||
@ -363,6 +406,61 @@ def test_cli_dry_run_exports_records_without_calling_model(
|
||||
assert "合成收到内容" not in terminal.out + terminal.err
|
||||
|
||||
|
||||
def test_cli_summarize_prefetches_before_reading(tmp_path, monkeypatch):
|
||||
from larkx.summary import resolve_day as resolve_summary_day
|
||||
|
||||
database_url = f"sqlite:///{tmp_path / 'synthetic.db'}"
|
||||
monkeypatch.setenv("LARKX_STORAGE_URL", database_url)
|
||||
session = FakeSession(
|
||||
completions=[(DEFAULT_MODEL, "单对话摘要"), (DEFAULT_MODEL, "# 合成总结")]
|
||||
)
|
||||
monkeypatch.setattr("larkx.summary.requests.Session", lambda: session)
|
||||
monkeypatch.setattr("larkx.cli.LarkClient", lambda auth: object())
|
||||
monkeypatch.setattr("larkx.cli.LarkAuth", lambda: object())
|
||||
sync_calls = []
|
||||
|
||||
def fake_sync(client, storage, target_day, tz, batch_size=50, chat_ids=None):
|
||||
assert len(session.get_calls) == 1 # EFundGPT preflight still comes first.
|
||||
_, start, _, _ = resolve_summary_day("2026-11-03", "Asia/Shanghai")
|
||||
storage.save_message(
|
||||
{
|
||||
"msg_id": "prefetched",
|
||||
"chat_id": "chat-synthetic",
|
||||
"chat_type": 2,
|
||||
"from_id": "sender-synthetic",
|
||||
"msg_type": 4,
|
||||
"msg_type_name": "TEXT",
|
||||
"create_time": int(start.timestamp()) + 60,
|
||||
"content": "合成预取消息",
|
||||
},
|
||||
direction="in",
|
||||
)
|
||||
sync_calls.append({"batch_size": batch_size, "chat_ids": chat_ids})
|
||||
return {"failed": 0, "matched": 1, "saved": 1, "chats": 1, "requests": 1}
|
||||
|
||||
monkeypatch.setattr("larkx.history.sync_all_chats_for_day", fake_sync)
|
||||
output = tmp_path / "summary.md"
|
||||
args = argparse.Namespace(
|
||||
date="2026-11-03",
|
||||
timezone="Asia/Shanghai",
|
||||
model=DEFAULT_MODEL,
|
||||
base_url="http://127.0.0.1:3425/v1",
|
||||
timeout=1,
|
||||
chat=["chat-synthetic"],
|
||||
max_chars=80000,
|
||||
output=output,
|
||||
check=False,
|
||||
dry_run=False,
|
||||
no_contacts=True,
|
||||
no_fetch=False,
|
||||
)
|
||||
|
||||
assert cmd_summarize(args) == 0
|
||||
assert sync_calls == [{"batch_size": 50, "chat_ids": ["chat-synthetic"]}]
|
||||
assert len(session.post_calls) == 2
|
||||
assert output.read_text(encoding="utf-8") == "# 合成总结\n"
|
||||
|
||||
|
||||
def test_cli_model_failure_happens_before_storage_is_opened(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
MagpieClient,
|
||||
@ -385,5 +483,7 @@ def test_cli_model_failure_happens_before_storage_is_opened(monkeypatch):
|
||||
output=None,
|
||||
check=False,
|
||||
dry_run=False,
|
||||
no_contacts=True,
|
||||
no_fetch=True,
|
||||
)
|
||||
assert cmd_summarize(args) == 1
|
||||
|
||||
Reference in New Issue
Block a user