test_core_units.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. """核心单元测试:跟踪器、计量器、来源、提示词、工具、并发、计费、存储。
  2. 这些用例全部离线可跑(不起服务、不连模型)。端到端的 HTTP 验证见
  3. ``scripts/http_e2e.py``(需要一个真实可用的模型密钥)。
  4. """
  5. from __future__ import annotations
  6. import json
  7. import pytest
  8. # ============================================================
  9. # 子智能体跟踪
  10. # ============================================================
  11. class TestSubagentTracker:
  12. def test_namespace_prefix_parsing(self):
  13. from api.subagent_tracker import parse_namespace
  14. assert parse_namespace(("tools:abc123",)) == "abc123"
  15. assert parse_namespace(["tools:x"]) == "x"
  16. assert parse_namespace("tools:y") == "y"
  17. def test_empty_namespace_is_main_agent(self):
  18. from api.subagent_tracker import parse_namespace
  19. assert parse_namespace(()) is None
  20. assert parse_namespace(None) is None
  21. assert parse_namespace([]) is None
  22. def test_control_segments_not_mistaken_for_ids(self):
  23. """带冒号/斜杠的控制段不能被当成 tool_call id。"""
  24. from api.subagent_tracker import parse_namespace
  25. assert parse_namespace(("model_request:1",)) is None
  26. assert parse_namespace(("a/b",)) is None
  27. def test_lifecycle_started_to_done(self):
  28. from api.subagent_tracker import SubagentTracker
  29. tracker = SubagentTracker()
  30. calls = [{"name": "task", "id": "c1", "args": {"subagent_type": "demo-searcher", "description": "查资料"}}]
  31. started = tracker.note_task_calls(calls)
  32. assert len(started) == 1
  33. assert started[0].subagent_type == "demo-searcher"
  34. assert started[0].status == "started"
  35. run = tracker.note_namespace(("tools:c1",))
  36. assert run is not None and run.status == "running" and run.tool_calls == 1
  37. done = tracker.note_result("c1", ok=True)
  38. assert done is not None and done.status == "done"
  39. assert done.duration_ms is not None
  40. def test_non_task_calls_ignored(self):
  41. from api.subagent_tracker import SubagentTracker
  42. tracker = SubagentTracker()
  43. assert tracker.note_task_calls([{"name": "calculate", "id": "c1", "args": {}}]) == []
  44. assert len(tracker) == 0
  45. def test_call_without_id_skipped(self):
  46. """没有 id 就无法关联后续子图事件,跳过而不是编一个假 id。"""
  47. from api.subagent_tracker import SubagentTracker
  48. tracker = SubagentTracker()
  49. assert tracker.note_task_calls([{"name": "task", "args": {"subagent_type": "x"}}]) == []
  50. assert len(tracker) == 0
  51. def test_unknown_namespace_does_not_invent_run(self):
  52. """表里没有的 id 不能凭空造一条 subagent_type=unknown 的记录。"""
  53. from api.subagent_tracker import SubagentTracker
  54. tracker = SubagentTracker()
  55. assert tracker.note_namespace(("tools:never-seen",)) is None
  56. assert len(tracker) == 0
  57. def test_finish_all_flushes_active(self):
  58. """流异常退出时不能让子智能体永远挂在 started,前端卡片会一直转。"""
  59. from api.subagent_tracker import SubagentTracker
  60. tracker = SubagentTracker()
  61. tracker.note_task_calls([
  62. {"name": "task", "id": "c1", "args": {"subagent_type": "a"}},
  63. {"name": "task", "id": "c2", "args": {"subagent_type": "b"}},
  64. ])
  65. tracker.note_result("c1", ok=True)
  66. flushed = tracker.finish_all()
  67. assert len(flushed) == 1 and flushed[0].call_id == "c2"
  68. assert not tracker.active()
  69. def test_duplicate_task_call_not_counted_twice(self):
  70. from api.subagent_tracker import SubagentTracker
  71. tracker = SubagentTracker()
  72. call = {"name": "task", "id": "c1", "args": {"subagent_type": "a"}}
  73. tracker.note_task_calls([call])
  74. tracker.note_task_calls([call])
  75. assert len(tracker) == 1
  76. # ============================================================
  77. # 上下文计量
  78. # ============================================================
  79. class _Msg:
  80. """最小的消息替身。"""
  81. def __init__(self, content="", usage_metadata=None, response_metadata=None,
  82. additional_kwargs=None, name=None) -> None:
  83. self.content = content
  84. self.usage_metadata = usage_metadata
  85. self.response_metadata = response_metadata or {}
  86. self.additional_kwargs = additional_kwargs or {}
  87. self.name = name
  88. class TestContextMeter:
  89. def test_extract_usage_from_langchain_metadata(self):
  90. from api.context_meter import extract_usage
  91. usage = extract_usage(_Msg(usage_metadata={
  92. "input_tokens": 100, "output_tokens": 20, "total_tokens": 120,
  93. "input_token_details": {"cache_read": 30},
  94. }))
  95. assert usage == {"input_tokens": 100, "output_tokens": 20, "cache_read_tokens": 30}
  96. def test_extract_usage_from_provider_raw(self):
  97. from api.context_meter import extract_usage
  98. usage = extract_usage(_Msg(response_metadata={
  99. "token_usage": {"prompt_tokens": 50, "completion_tokens": 5,
  100. "prompt_tokens_details": {"cached_tokens": 8}}
  101. }))
  102. assert usage == {"input_tokens": 50, "output_tokens": 5, "cache_read_tokens": 8}
  103. def test_missing_usage_returns_none_not_zero(self):
  104. """「用量为 0」与「没有用量信息」必须能区分,否则估算兜底会失效。"""
  105. from api.context_meter import extract_usage
  106. assert extract_usage(_Msg()) is None
  107. assert extract_usage(_Msg(usage_metadata={})) is None
  108. def test_accumulates_across_model_calls(self):
  109. from api.context_meter import ContextMeter
  110. meter = ContextMeter(window=1000)
  111. meter.note_message(_Msg(usage_metadata={"input_tokens": 100, "output_tokens": 10}))
  112. meter.note_message(_Msg(usage_metadata={"input_tokens": 150, "output_tokens": 20}))
  113. assert meter.input_tokens == 250
  114. assert meter.output_tokens == 30
  115. assert meter.model_calls == 2
  116. def test_context_tokens_is_last_input(self):
  117. """上下文占用取最近一次调用的 input,而不是累加值。"""
  118. from api.context_meter import ContextMeter
  119. meter = ContextMeter(window=1000)
  120. meter.note_message(_Msg(usage_metadata={"input_tokens": 100, "output_tokens": 10}))
  121. meter.note_message(_Msg(usage_metadata={"input_tokens": 180, "output_tokens": 10}))
  122. assert meter.context_tokens == 180
  123. assert meter.input_tokens == 280
  124. def test_source_is_provider_when_usage_present(self):
  125. from api.context_meter import ContextMeter
  126. meter = ContextMeter(window=1000)
  127. meter.note_message(_Msg(usage_metadata={"input_tokens": 5, "output_tokens": 5}))
  128. assert meter.usage_source == "provider"
  129. def test_estimate_marks_source_estimate(self):
  130. from api.context_meter import ContextMeter
  131. meter = ContextMeter(window=1000)
  132. meter.note_estimated(300)
  133. assert meter.usage_source == "estimate"
  134. assert meter.input_tokens == 300
  135. def test_estimate_ignored_when_real_usage_seen(self):
  136. """已有真实用量时估算值只会污染口径,丢弃。"""
  137. from api.context_meter import ContextMeter
  138. meter = ContextMeter(window=1000)
  139. meter.note_message(_Msg(usage_metadata={"input_tokens": 100, "output_tokens": 10}))
  140. meter.note_estimated(9999)
  141. assert meter.input_tokens == 100
  142. assert meter.usage_source == "provider"
  143. def test_compaction_detected_by_lc_source(self):
  144. from api.context_meter import ContextMeter
  145. meter = ContextMeter(window=1000)
  146. meter.note_message(_Msg(content="摘要", response_metadata={"lc_source": "summarization"}))
  147. assert meter.compacted and meter.compact_count == 1
  148. def test_compaction_detected_by_context_drop(self):
  149. """占用断崖式下跌也视为压缩。"""
  150. from api.context_meter import ContextMeter
  151. meter = ContextMeter(window=10000)
  152. meter.note_message(_Msg(usage_metadata={"input_tokens": 9000, "output_tokens": 10}))
  153. assert not meter.compacted
  154. meter.note_message(_Msg(usage_metadata={"input_tokens": 2000, "output_tokens": 10}))
  155. assert meter.compacted and meter.compact_count == 1
  156. def test_compaction_counted_once(self):
  157. """两个信号同时命中不能重复计数。"""
  158. from api.context_meter import ContextMeter
  159. meter = ContextMeter(window=10000)
  160. meter.note_message(_Msg(usage_metadata={"input_tokens": 9000, "output_tokens": 1}))
  161. meter.note_message(_Msg(usage_metadata={"input_tokens": 100, "output_tokens": 1}))
  162. meter.note_message(_Msg(content="x", response_metadata={"lc_source": "summarization"}))
  163. assert meter.compact_count == 1
  164. def test_archive_text_captured_on_compaction(self):
  165. from api.context_meter import ContextMeter
  166. meter = ContextMeter(window=1000)
  167. meter.note_text("被压缩掉的早期对话")
  168. meter.note_message(_Msg(content="摘要", response_metadata={"lc_source": "summarization"}))
  169. snapshot = meter.snapshot()
  170. assert snapshot.compacted and snapshot.archive_text == "被压缩掉的早期对话"
  171. def test_fill_ratio(self):
  172. from api.context_meter import ContextMeter
  173. meter = ContextMeter(window=1000)
  174. meter.note_message(_Msg(usage_metadata={"input_tokens": 850, "output_tokens": 0}))
  175. assert meter.fill_ratio() == pytest.approx(0.85)
  176. def test_estimate_tokens_positive_for_cjk(self):
  177. from api.context_meter import estimate_tokens
  178. assert estimate_tokens("矿井通风") > 0
  179. assert estimate_tokens("") == 0
  180. # ============================================================
  181. # 来源标注
  182. # ============================================================
  183. class TestSourceTracker:
  184. def test_extract_from_sourced_payload(self):
  185. from api.source_tracker import SourceTracker
  186. from tools.registry import source, sourced
  187. tracker = SourceTracker()
  188. payload = json.dumps(sourced(data=[1], sources=[source("T", "https://a.invalid/x", "摘要", 0.5)]))
  189. added = tracker.note_tool_result("search_demo_knowledge", payload)
  190. assert len(added) == 1 and added[0]["title"] == "T"
  191. def test_extract_from_dict_payload(self):
  192. from api.source_tracker import SourceTracker
  193. from tools.registry import source, sourced
  194. tracker = SourceTracker()
  195. tracker.note_tool_result("t", sourced(data=[], sources=[source("T", "https://a.invalid")]))
  196. assert tracker.count == 1
  197. def test_dedup_by_url(self):
  198. from api.source_tracker import SourceTracker
  199. from tools.registry import source, sourced
  200. tracker = SourceTracker()
  201. payload = sourced(data=[], sources=[source("T", "https://same.invalid"), source("T2", "https://same.invalid")])
  202. tracker.note_tool_result("t", payload)
  203. assert tracker.count == 1
  204. def test_records_without_title_or_url_dropped(self):
  205. """既无标题也无链接的来源无法核实,不展示空壳。"""
  206. from api.source_tracker import SourceTracker
  207. from tools.registry import sourced
  208. tracker = SourceTracker()
  209. tracker.note_tool_result("t", sourced(data=[], sources=[{"snippet": "只有摘要"}]))
  210. assert tracker.count == 0
  211. def test_plain_text_yields_nothing(self):
  212. from api.source_tracker import SourceTracker
  213. tracker = SourceTracker()
  214. tracker.note_tool_result("t", "这是一段普通文本,没有 sources")
  215. assert tracker.count == 0
  216. def test_classification_web_vs_kb(self):
  217. from api.source_tracker import SourceTracker
  218. from tools.registry import source, sourced
  219. tracker = SourceTracker()
  220. tracker.note_tool_result("t", sourced(data=[], sources=[
  221. source("网页", "https://news.example.com/a"),
  222. source("知识库", "https://kb.example.com/b"),
  223. ]))
  224. types = {i["title"]: i["type"] for i in tracker.build_citations()}
  225. assert types["网页"] == "web"
  226. assert types["知识库"] == "knowledge_base"
  227. def test_nested_payload_lookup(self):
  228. from api.source_tracker import SourceTracker
  229. from tools.registry import source
  230. tracker = SourceTracker()
  231. tracker.note_tool_result("t", {"data": {"sources": [source("深层", "https://d.invalid")]}})
  232. assert tracker.count == 1 and tracker.build_citations()[0]["title"] == "深层"
  233. def test_citations_sorted_by_score(self):
  234. from api.source_tracker import SourceTracker
  235. from tools.registry import source, sourced
  236. tracker = SourceTracker()
  237. tracker.note_tool_result("t", sourced(data=[], sources=[
  238. source("低", "https://a.invalid", score=0.1),
  239. source("高", "https://b.invalid", score=0.9),
  240. ]))
  241. assert [i["title"] for i in tracker.build_citations()] == ["高", "低"]
  242. def test_label_attached(self):
  243. from api.source_tracker import SourceTracker
  244. from tools.registry import source, sourced
  245. tracker = SourceTracker()
  246. tracker.note_tool_result("search_demo_knowledge",
  247. sourced(data=[], sources=[source("T", "https://a.invalid")]))
  248. item = tracker.build_citations()[0]
  249. assert item["tool"] == "search_demo_knowledge"
  250. assert item["tool_cn"] == "检索演示知识库"
  251. # ============================================================
  252. # 提示词与消息前缀
  253. # ============================================================
  254. class TestPrompts:
  255. def test_skill_prefix_extracted(self):
  256. from agents.prompts import extract_skill_prefix
  257. text, skill = extract_skill_prefix("#skill:demo-skill 帮我写一份简报")
  258. assert skill == "demo-skill" and text == "帮我写一份简报"
  259. def test_skill_prefix_only_at_start(self):
  260. """正文中间的 #skill: 是普通文本,不能当指令。"""
  261. from agents.prompts import extract_skill_prefix
  262. text, skill = extract_skill_prefix("请看这个 #skill:demo-skill 的用法")
  263. assert skill is None and text.startswith("请看这个")
  264. def test_skill_prefix_without_message(self):
  265. from agents.prompts import extract_skill_prefix
  266. text, skill = extract_skill_prefix("#skill:demo-skill")
  267. assert skill == "demo-skill" and text == ""
  268. def test_path_traversal_rejected(self):
  269. """技能名带路径分隔符不能当技能解析——否则能读任意文件。"""
  270. from agents.prompts import extract_skill_prefix
  271. for bad in ("#skill:../../etc/passwd x", "#skill:.hidden x", "#skill:a/b x"):
  272. _, skill = extract_skill_prefix(bad)
  273. assert skill is None, bad
  274. def test_session_ref_extracted(self):
  275. from agents.prompts import extract_session_ref
  276. text, ref = extract_session_ref("#session:abc123 接着上次说")
  277. assert ref == "abc123" and text == "接着上次说"
  278. def test_both_prefixes_stacked(self):
  279. from agents.prompts import extract_session_ref, extract_skill_prefix
  280. text, skill = extract_skill_prefix("#skill:demo-skill #session:abc 继续")
  281. text, ref = extract_session_ref(text)
  282. assert skill == "demo-skill" and ref == "abc" and text == "继续"
  283. def test_mode_instruction_differs(self):
  284. from agents.prompts import build_mode_instruction
  285. plan = build_mode_instruction("plan")
  286. full = build_mode_instruction("full")
  287. assert "plan" in plan and "计划" in plan
  288. assert "full" in full and plan != full
  289. def test_preference_instruction_empty_when_no_prefs(self):
  290. from agents.prompts import build_preference_instruction
  291. assert build_preference_instruction("") == ""
  292. def test_time_block_format(self):
  293. from agents.prompts import build_time_block
  294. block = build_time_block()
  295. assert "## 系统时间" in block and "当前时间" in block
  296. def test_system_prompt_includes_subagents(self):
  297. from agents.prompts import build_system_prompt
  298. prompt = build_system_prompt([
  299. {"name": "demo-searcher", "description": "检索专员"}
  300. ])
  301. assert "demo-searcher" in prompt
  302. assert "检索专员" in prompt
  303. def test_system_prompt_without_subagents(self):
  304. from agents.prompts import build_system_prompt
  305. prompt = build_system_prompt([])
  306. assert "子智能体" in prompt # 通用说明里提到,但不该有具体名单
  307. assert "可委派的子智能体" not in prompt
  308. # ============================================================
  309. # 工具
  310. # ============================================================
  311. class TestTools:
  312. def test_builtin_tools_registered(self):
  313. from tools import registry
  314. names = set(registry.get_tool_names())
  315. assert {"get_current_time", "search_demo_knowledge", "calculate",
  316. "request_plan_approval"} <= names
  317. def test_tools_are_basetool_instances(self):
  318. """子智能体的 tools 必须是 BaseTool(ToolNode 要读 .name)。"""
  319. from tools import registry
  320. for tool in registry.get_all_tools():
  321. assert hasattr(tool, "name") and hasattr(tool, "description")
  322. def test_get_tools_by_name_skips_unknown(self):
  323. from tools import registry
  324. tools = registry.get_tools_by_name(["calculate", "does-not-exist"])
  325. assert [t.name for t in tools] == ["calculate"]
  326. def test_duplicate_registration_raises(self):
  327. from tools.registry import register, reset_for_tests
  328. reset_for_tests()
  329. @register(group="X")
  330. def dup_tool() -> str:
  331. """d"""
  332. return ""
  333. with pytest.raises(ValueError, match="工具名冲突"):
  334. @register(group="Y")
  335. def dup_tool() -> str: # noqa: F811
  336. """d"""
  337. return ""
  338. reset_for_tests()
  339. from tools import registry as r
  340. r.load_builtin_tools()
  341. def test_calculate_basic(self):
  342. from tools.demo_tools import calculate
  343. assert calculate("(128000 * 0.85)")["result"] == 108800
  344. def test_calculate_integer_result_is_int(self):
  345. from tools.demo_tools import calculate
  346. assert isinstance(calculate("1+1")["result"], int)
  347. def test_calculate_rejects_code_execution(self):
  348. """表达式求值绝不能变成代码执行入口。"""
  349. from tools.demo_tools import calculate
  350. for evil in [
  351. "__import__('os').system('echo x')",
  352. "open('/etc/passwd').read()",
  353. "(lambda: 1)()",
  354. "1 if True else 2",
  355. "[x for x in range(3)]",
  356. ]:
  357. result = calculate(evil)
  358. assert "error" in result, evil
  359. def test_calculate_rejects_huge_power(self):
  360. from tools.demo_tools import calculate
  361. assert "error" in calculate("9 ** 9 ** 9")
  362. def test_calculate_zero_division(self):
  363. from tools.demo_tools import calculate
  364. assert "零" in calculate("1/0")["error"]
  365. def test_calculate_empty(self):
  366. from tools.demo_tools import calculate
  367. assert "error" in calculate("")
  368. def test_calculate_returns_error_not_raises(self):
  369. """工具失败要返回可读原因,抛异常会打断整轮工具调用。"""
  370. from tools.demo_tools import calculate
  371. result = calculate("1 +")
  372. assert isinstance(result, dict) and "error" in result
  373. def test_search_returns_sources_contract(self):
  374. from tools.demo_tools import search_demo_knowledge
  375. result = search_demo_knowledge("需风量")
  376. assert result["data"] and result["sources"]
  377. assert result["sources"][0]["url"]
  378. def test_search_empty_query_returns_error(self):
  379. from tools.demo_tools import search_demo_knowledge
  380. result = search_demo_knowledge(" ")
  381. assert result["error"] and result["sources"] == []
  382. def test_search_no_match(self):
  383. from tools.demo_tools import search_demo_knowledge
  384. result = search_demo_knowledge("不存在的关键词zzz")
  385. assert result["data"] == [] and result["sources"] == []
  386. def test_registry_describe_shape(self):
  387. from tools import registry
  388. described = registry.describe()
  389. assert described["count"] >= 4
  390. assert all(isinstance(v, list) for v in described["groups"].values())
  391. # ============================================================
  392. # 并发闸
  393. # ============================================================
  394. class TestConcurrency:
  395. def setup_method(self):
  396. from api import concurrency
  397. concurrency.reset_for_tests()
  398. def teardown_method(self):
  399. from api import concurrency
  400. concurrency.reset_for_tests()
  401. def test_acquire_and_release(self):
  402. from api import concurrency
  403. slot = concurrency.acquire_task_slot("u1")
  404. assert concurrency.stats()["global_active"] == 1
  405. assert slot.release() is True
  406. assert concurrency.stats()["global_active"] == 0
  407. def test_release_is_idempotent(self):
  408. """SSE 有多条退出路径,重复 release 不能把计数减成负数。"""
  409. from api import concurrency
  410. slot = concurrency.acquire_task_slot("u1")
  411. slot.release()
  412. slot.release()
  413. assert concurrency.stats()["global_active"] == 0
  414. def test_per_user_limit(self, monkeypatch):
  415. from api import concurrency
  416. monkeypatch.setattr(concurrency.config, "MAX_USER_TASKS", 1)
  417. concurrency.acquire_task_slot("u1")
  418. with pytest.raises(concurrency.TooManyTasksError) as exc:
  419. concurrency.acquire_task_slot("u1")
  420. assert exc.value.scope == "user"
  421. def test_global_limit(self, monkeypatch):
  422. from api import concurrency
  423. monkeypatch.setattr(concurrency.config, "MAX_CONCURRENT_TASKS", 1)
  424. monkeypatch.setattr(concurrency.config, "MAX_USER_TASKS", 10)
  425. concurrency.acquire_task_slot("u1")
  426. with pytest.raises(concurrency.TooManyTasksError) as exc:
  427. concurrency.acquire_task_slot("u2")
  428. assert exc.value.scope == "global"
  429. def test_different_users_independent(self, monkeypatch):
  430. from api import concurrency
  431. monkeypatch.setattr(concurrency.config, "MAX_USER_TASKS", 1)
  432. concurrency.acquire_task_slot("u1")
  433. concurrency.acquire_task_slot("u2")
  434. assert concurrency.stats()["global_active"] == 2
  435. # ============================================================
  436. # 计费
  437. # ============================================================
  438. class TestBilling:
  439. def test_cost_computation(self, pricing_ready):
  440. from billing import pricing
  441. from db import billing_store
  442. billing_store.set_pricing("m1", 0.001, 0.002)
  443. cost, currency = pricing.compute_cost("m1", 1000, 500)
  444. assert cost == pytest.approx(0.001 + 0.001)
  445. assert currency == "CNY"
  446. def test_unpriced_model_fails_closed(self, pricing_ready, monkeypatch):
  447. """静默按 0 计费等于白送额度,所以默认必须报错。"""
  448. from billing import pricing
  449. monkeypatch.setattr(pricing.config, "get_bool", lambda *a, **k: False)
  450. with pytest.raises(pricing.UnpricedModelError):
  451. pricing.compute_cost("unknown-model", 100, 100)
  452. def test_unpriced_model_allowed_when_configured(self, pricing_ready, monkeypatch):
  453. from billing import pricing
  454. monkeypatch.setattr(pricing.config, "get_bool", lambda *a, **k: True)
  455. cost, _ = pricing.compute_cost("unknown-model", 100, 100)
  456. assert cost == 0.0
  457. def test_quota_blocks_when_exhausted(self, pricing_ready, unique_user):
  458. from billing import guard
  459. from db import billing_store, db_conn
  460. day = db_conn.day_key()
  461. billing_store.set_quota_limit(unique_user, day, 100)
  462. billing_store.record_usage(unique_user, "s", "chat", "m", 100, 0, 0.0, day)
  463. with pytest.raises(guard.QuotaExceededError):
  464. guard.precheck(unique_user, day)
  465. def test_quota_zero_means_unlimited(self, pricing_ready, unique_user):
  466. from billing import guard
  467. from db import billing_store, db_conn
  468. day = db_conn.day_key()
  469. billing_store.set_quota_limit(unique_user, day, 0)
  470. billing_store.record_usage(unique_user, "s", "chat", "m", 10_000_000, 0, 0.0, day)
  471. guard.precheck(unique_user, day) # 不抛即通过
  472. def test_meter_settle_idempotent(self, pricing_ready, unique_user):
  473. from billing import guard
  474. from billing.meter import UsageMeter
  475. meter = UsageMeter(unique_user, "s1", "chat", "deepseek-flash")
  476. meter.add(1000, 100)
  477. first = meter.settle()
  478. second = meter.settle()
  479. assert first["settled"] is True
  480. assert second.get("duplicated") is True
  481. assert guard.snapshot(unique_user)["requests"] == 1
  482. def test_meter_no_usage_skips(self, pricing_ready, unique_user):
  483. from billing import guard
  484. from billing.meter import UsageMeter
  485. meter = UsageMeter(unique_user, "s1", "chat", "deepseek-flash")
  486. result = meter.settle()
  487. assert result["settled"] is False and result["reason"] == "no_usage"
  488. assert guard.snapshot(unique_user)["requests"] == 0
  489. def test_meter_source_downgrades_to_estimate(self, pricing_ready, unique_user):
  490. from billing.meter import UsageMeter
  491. meter = UsageMeter(unique_user, "s1", "chat", "deepseek-flash")
  492. meter.add(100, 10, "provider")
  493. meter.add(50, 5, "estimate")
  494. assert meter.usage_source == "estimate"
  495. def test_snapshot_has_total_tokens(self, pricing_ready, unique_user):
  496. """字段名与 billing_store.usage_for_day 对齐,消费方不用记两套名字。"""
  497. from billing import guard
  498. snapshot = guard.snapshot(unique_user)
  499. assert "total_tokens" in snapshot and "used_tokens" in snapshot
  500. def test_usage_day_bound_to_request_start(self, pricing_ready, unique_user):
  501. """跨零点的长请求不能被算到第二天。"""
  502. from billing import guard
  503. from billing.meter import UsageMeter
  504. from db import db_conn
  505. yesterday = "20200101"
  506. meter = UsageMeter(unique_user, "s1", "chat", "deepseek-flash", usage_day=yesterday)
  507. meter.add(100, 10)
  508. meter.settle()
  509. assert guard.snapshot(unique_user, yesterday)["requests"] == 1
  510. assert guard.snapshot(unique_user, db_conn.day_key())["requests"] == 0
  511. # ============================================================
  512. # 存储
  513. # ============================================================
  514. class TestStores:
  515. def test_message_ordering_stable_within_same_second(self):
  516. """同秒写入的多条消息顺序必须稳定——排序错乱会让补种的上下文角色错位。"""
  517. from db import chat_store
  518. sid = chat_store.create_session("u", "full")["id"]
  519. for i in range(6):
  520. chat_store.save_message(sid, "user", f"Q{i}")
  521. chat_store.save_message(sid, "assistant", f"A{i}")
  522. rows = chat_store.get_messages(sid)
  523. assert [r["msg_role"] for r in rows] == ["user", "assistant"] * 6
  524. assert [r["content"] for r in rows] == [
  525. c for i in range(6) for c in (f"Q{i}", f"A{i}")
  526. ]
  527. def test_get_messages_limit_returns_most_recent_in_order(self):
  528. from db import chat_store
  529. sid = chat_store.create_session("u", "full")["id"]
  530. for i in range(5):
  531. chat_store.save_message(sid, "user", f"M{i}")
  532. recent = chat_store.get_messages(sid, limit=3)
  533. assert [r["content"] for r in recent] == ["M2", "M3", "M4"]
  534. def test_message_json_fields_roundtrip(self):
  535. from db import chat_store
  536. sid = chat_store.create_session("u", "full")["id"]
  537. chat_store.save_message(
  538. sid, "assistant", "答",
  539. tool_calls=[{"name": "calculate", "args": {"expression": "1+1"}}],
  540. citations=[{"title": "T", "url": "u"}],
  541. )
  542. row = chat_store.get_messages(sid)[0]
  543. assert row["tool_calls"][0]["name"] == "calculate"
  544. assert row["citations"][0]["title"] == "T"
  545. def test_session_ownership_check(self):
  546. from db import chat_store
  547. sid = chat_store.create_session("alice", "full")["id"]
  548. assert chat_store.get_session_for_user(sid, "alice") is not None
  549. assert chat_store.get_session_for_user(sid, "bob") is None
  550. def test_delete_session_cascades(self):
  551. from db import chat_store
  552. sid = chat_store.create_session("u", "full")["id"]
  553. chat_store.save_message(sid, "user", "x")
  554. chat_store.save_context_usage(sid, 1, 10, 5)
  555. chat_store.delete_session(sid)
  556. assert chat_store.get_session(sid) is None
  557. assert chat_store.get_messages(sid) == []
  558. assert chat_store.get_context_usage(sid)["history"] == []
  559. def test_preference_upsert(self):
  560. from db import chat_store
  561. chat_store.save_preference("u", "单位", "米")
  562. chat_store.save_preference("u", "单位", "千米")
  563. prefs = {p["pref_key"]: p["pref_value"] for p in chat_store.list_preferences("u")}
  564. assert prefs == {"单位": "千米"}
  565. def test_preferences_as_text_empty(self):
  566. from db import chat_store
  567. assert chat_store.preferences_as_text("nobody") == ""
  568. def test_search_sessions_by_title(self):
  569. from db import chat_store
  570. chat_store.create_session("u", "full", "通风计算讨论")
  571. chat_store.create_session("u", "full", "其他话题")
  572. found = chat_store.search_sessions("u", "通风")
  573. assert len(found) == 1 and "通风" in found[0]["title"]
  574. def test_pinned_sessions_first(self):
  575. from db import chat_store
  576. a = chat_store.create_session("u", "full", "A")["id"]
  577. chat_store.create_session("u", "full", "B")
  578. chat_store.update_session_pin(a, True)
  579. assert chat_store.list_sessions("u")[0]["id"] == a
  580. def test_context_usage_aggregates(self):
  581. from db import chat_store
  582. sid = chat_store.create_session("u", "full")["id"]
  583. chat_store.save_context_usage(sid, 1, 100, 10, usage_source="provider")
  584. chat_store.save_context_usage(sid, 2, 200, 20, compacted=True, compact_count=1,
  585. usage_source="estimate")
  586. usage = chat_store.get_context_usage(sid)
  587. assert usage["compact_count"] == 1
  588. assert usage["latest"]["total_tokens"] == 220
  589. assert usage["latest"]["usage_source"] == "estimate"
  590. def test_schedule_crud_and_runs(self):
  591. from db import schedule_store
  592. schedule = schedule_store.create_schedule("u", "每日", "0 9 * * *", "汇报")
  593. sid = schedule["id"]
  594. assert schedule["enabled"] is True
  595. run_id = schedule_store.start_run(sid, "thread-1")
  596. schedule_store.finish_run(run_id, "success", output="ok")
  597. runs = schedule_store.list_runs(sid)
  598. assert len(runs) == 1 and runs[0]["status"] == "success"
  599. updated = schedule_store.update_schedule(sid, enabled=False, title="改名")
  600. assert updated["enabled"] is False and updated["title"] == "改名"
  601. assert schedule_store.delete_schedule(sid) is True
  602. assert schedule_store.get_schedule(sid) is None
  603. assert schedule_store.list_runs(sid) == []
  604. def test_schedule_update_ignores_unknown_fields(self):
  605. from db import schedule_store
  606. sid = schedule_store.create_schedule("u", "T", "0 9 * * *", "P")["id"]
  607. schedule_store.update_schedule(sid, title="新", 恶意字段="x")
  608. assert schedule_store.get_schedule(sid)["title"] == "新"
  609. def test_init_schema_idempotent(self):
  610. from db import db_conn
  611. db_conn.init_schema()
  612. db_conn.init_schema(force=True)
  613. assert db_conn.health()["status"] == "ok"
  614. # ============================================================
  615. # 模型工厂
  616. # ============================================================
  617. class TestModelFactory:
  618. def test_context_window_known(self):
  619. from agents import model_factory
  620. assert model_factory.context_window("deepseek-flash") == 128_000
  621. def test_context_limit_override_wins(self, monkeypatch):
  622. from agents import model_factory
  623. monkeypatch.setattr(model_factory.config, "CONTEXT_LIMIT", 5000)
  624. assert model_factory.context_window("deepseek-flash") == 5000
  625. def test_unknown_model_uses_fallback(self):
  626. from agents import model_factory
  627. assert model_factory.context_window("some-unknown-model-xyz") == \
  628. model_factory.FALLBACK_CONTEXT_WINDOW
  629. def test_thinking_levels_normalized(self):
  630. from agents import model_factory
  631. assert model_factory.normalize_thinking("HIGH") == "high"
  632. assert model_factory.normalize_thinking("bogus") == "off"
  633. assert model_factory.normalize_thinking(None) in model_factory.VALID_THINKING_LEVELS
  634. def test_off_sends_no_extra_body(self):
  635. """off 时什么都不发:不同 provider 关闭思考的字段名不一致,发错会被拒。"""
  636. from agents import model_factory
  637. assert model_factory._thinking_payload("off") is None
  638. assert model_factory._thinking_payload("high") is not None
  639. def test_build_model_declares_profile(self):
  640. """必须显式声明上下文窗口,否则摘要中间件首次触发时会报错。"""
  641. from agents import model_factory
  642. model = model_factory.build_model()
  643. assert model.profile and model.profile.get("max_input_tokens")
  644. # ============================================================
  645. # 中断恢复翻译
  646. # ============================================================
  647. class TestResumeTranslation:
  648. def _payload(self, **kw):
  649. from api.models import ResumeRequest
  650. base = {"session_id": "s", "status": "answered"}
  651. base.update(kw)
  652. return ResumeRequest(**base)
  653. def test_approve(self):
  654. from api.chat_routes import _to_official_resume
  655. assert _to_official_resume(self._payload(approved=True), 1) == \
  656. {"decisions": [{"type": "approve"}]}
  657. def test_reject(self):
  658. from api.chat_routes import _to_official_resume
  659. decisions = _to_official_resume(self._payload(approved=False), 1)["decisions"]
  660. assert decisions[0]["type"] == "reject" and decisions[0]["message"]
  661. def test_cancelled_becomes_reject(self):
  662. from api.chat_routes import _to_official_resume
  663. decisions = _to_official_resume(self._payload(status="cancelled"), 1)["decisions"]
  664. assert decisions[0]["type"] == "reject"
  665. def test_answers_become_respond(self):
  666. from api.chat_routes import _to_official_resume
  667. decisions = _to_official_resume(self._payload(answers=["甲", "乙"]), 1)["decisions"]
  668. assert decisions[0]["type"] == "respond" and "甲" in decisions[0]["message"]
  669. def test_multiple_pending_actions_get_matching_decisions(self):
  670. """官方要求 decisions 与 action_requests 数量严格一致。"""
  671. from api.chat_routes import _to_official_resume
  672. decisions = _to_official_resume(self._payload(approved=True), 3)["decisions"]
  673. assert len(decisions) == 3
  674. assert all(d["type"] == "approve" for d in decisions)
  675. def test_unknown_payload_defaults_to_safe_reject(self):
  676. """没有明确答复时宁可拒绝执行。"""
  677. from api.chat_routes import _to_official_resume
  678. decisions = _to_official_resume(self._payload(), 1)["decisions"]
  679. assert decisions[0]["type"] == "reject"