Files
ai-agent-book/chapter10/book-translation/tests/test_glossary_robustness.py
T
liqiang b119135836
Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
ai-agent-book 精选快照(<2MB 代码与文档,来自 github.com/bojieli/ai-agent-book)
2026-08-20 13:12:50 +00:00

62 lines
2.6 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.
"""回归测试:Glossary Agent 返回不合规 JSON 时,run_orchestration 不应崩溃。
覆盖两类模型失误(此前会让整轮管理者模式直接 KeyError/AttributeError):
1) glossary 条目缺 en/zh 键、或值为显式 null / 空串 -> 条目被丢弃;
2) 顶层 JSON 是数组而非对象 -> glossary_agent 返回空表。
不依赖真实 APIllm_chat / get_client 被打桩。
"""
import json
import agents
# 混合各种坏条目的 glossary:错键名 / null / 空串 都应被丢弃,只有合规条目保留。
GLOSSARY_JSON = json.dumps({
"glossary": [
{"term": "token", "translation": "词元"}, # 错键名
{"en": None, "zh": "提示词"}, # 显式 null
{"en": "", "zh": "时延"}, # 空串
{"en": "attention", "zh": "注意力", "pos": "名词"}, # 合规
]
}, ensure_ascii=False)
CHAPTERS = {"Chapter 1: Intro": "# Chapter 1\nSome text about attention."}
def _install_fake_llm(glossary_payload=GLOSSARY_JSON):
def fake_llm_chat(client, tracker, agent, messages, json_mode=False, note=""):
tracker.record(agent, 10, 5, note)
if agent == "Glossary":
return glossary_payload
return "译文"
agents.get_client = lambda: object()
agents.llm_chat = fake_llm_chat
def test_orchestration_skips_malformed_glossary_entries(tmp_path):
_install_fake_llm()
result = agents.run_orchestration(
CHAPTERS, str(tmp_path), enable_glossary=True, enable_proofreading=False)
glossary = result["glossary"]
# 所有存活条目必须是非空 en/zh 字符串(下游 g["en"]/g["zh"] 索引的前提)
for g in glossary:
assert isinstance(g["en"], str) and g["en"].strip()
assert isinstance(g["zh"], str) and g["zh"].strip()
ens = {g["en"] for g in glossary}
assert "attention" in ens # 合规条目保留
assert "term" not in ens # 错键名条目已丢弃
for en in agents.EDITORIAL_MANDATE: # 编辑部指定术语仍会补齐
assert en in ens
assert (tmp_path / "glossary.json").exists() # 产物正常落盘
assert (tmp_path / "chapter1_zh.md").read_text(encoding="utf-8") == "译文"
def test_glossary_agent_tolerates_json_array():
_install_fake_llm(glossary_payload='["not", "an", "object"]')
assert agents.glossary_agent(None, agents.TokenTracker(), "book text") == []
def test_glossary_agent_tolerates_missing_glossary_key():
_install_fake_llm(glossary_payload='{"terms": []}')
assert agents.glossary_agent(None, agents.TokenTracker(), "book text") == []