test: 清理 backlog——chat_structured 超限检测 + client 上下文管理器

This commit is contained in:
lhl
2026-08-09 09:15:36 +08:00
parent a5eaec4de8
commit cb2deda08a
5 changed files with 56 additions and 5 deletions
+27 -5
View File
@@ -1,14 +1,10 @@
from __future__ import annotations
import json
import pytest
from genesis.inference.engine import InferenceEngine
from genesis.inference.exceptions import LLMError
from genesis.inference.prompt_registry import PromptRegistry
from genesis.inference.token import approximate_token_count
from genesis.inference.types import ChatMessage, Prompt
from genesis.inference.types import Prompt
from tests.inference_helpers import FakeLLMClient
@@ -161,6 +157,32 @@ def test_chat_structured_failed_on_network():
assert r.status == "failed"
def test_chat_structured_truncation_callback_triggered():
# chat_structured 超限时同样触发 truncate_cb(与 chat 流程一致)
seen = {}
def truncate_cb(prompt_text, variables):
seen["called"] = True
return {**variables, "chapter": "裁剪版"}
eng = InferenceEngine(
client=FakeLLMClient([("ok", '{"a": 1}')]),
models=Models(),
registry=PromptRegistry(),
estimator=approximate_token_count,
truncate_cb=truncate_cb,
)
eng._max_context_tokens = 2 # 强制超限(渲染约 3 token > 2
r = eng.chat_structured(
session_id="s1",
prompt=Prompt(name="p", version="v1", template="abcd{{ chapter }}"),
variables={"chapter": "很长很长的标题"},
schema={"type": "object", "properties": {"a": {"type": "number"}}},
)
assert seen["called"] is True
assert r.status == "ok"
# ---------- 补充分支覆盖(defensive / 缺失配置) ----------
def test_chat_fallback_all_failed_when_only_primary():