feat(testing): add fixed tool simulations

This commit is contained in:
Xin Wang
2026-08-26 09:36:37 +08:00
parent 26582bfb87
commit 239b9027ca
16 changed files with 1525 additions and 237 deletions
+69 -1
View File
@@ -7,7 +7,11 @@ from fastapi import HTTPException
from routes.assistants import _validate_startup_actions
from models import RuntimeToolBinding
from schemas import AssistantUpsert
from services.config_resolver import _runtime_tool_ids
from services.config_resolver import (
_implicit_startup_tool_names,
_runtime_tool_ids,
_tools_for,
)
def startup_body(
@@ -57,6 +61,25 @@ class FakeSession:
return self.tool if self.tool is not None and tool_id == self.tool.id else None
class FakeToolQueryResult:
def __init__(self, tools):
self.tools = tools
def scalars(self):
return self
def all(self):
return self.tools
class FakeToolCatalogSession:
def __init__(self, tools):
self.tools = tools
async def execute(self, _statement):
return FakeToolQueryResult(self.tools)
class StartupActionValidationTests(unittest.IsolatedAsyncioTestCase):
def test_runtime_pool_includes_bound_and_startup_only_tools(self):
self.assertEqual(
@@ -75,6 +98,51 @@ class StartupActionValidationTests(unittest.IsolatedAsyncioTestCase):
["conversation_tool", "shared_tool", "opening_tool"],
)
def test_confirmation_opening_declares_implicit_show_message_tool(self):
self.assertEqual(
_implicit_startup_tool_names(
{
"opening_mode": "confirmation",
"opening_message": {
"title": "重要提示",
"message": "请确认已阅读。",
},
}
),
["show_message"],
)
self.assertEqual(
_implicit_startup_tool_names(
{
"openingMode": "playback",
"openingMessage": {"message": "无需确认"},
}
),
[],
)
async def test_implicit_show_message_enters_runtime_pool_without_binding(self):
tool = SimpleNamespace(
id="tool_show_message_default",
name="显示确认消息",
function_name="show_message",
type="client",
description="显示确认弹窗",
definition={"config": {}},
secrets={},
mcp_server_id=None,
)
runtime_tools = await _tools_for(
FakeToolCatalogSession([tool]),
SimpleNamespace(type="prompt"),
[],
["show_message"],
)
self.assertEqual([item.id for item in runtime_tools], [tool.id])
self.assertEqual(runtime_tools[0].function_name, "show_message")
def test_realtime_rejects_startup_actions(self):
with self.assertRaisesRegex(ValueError, "Realtime"):
AssistantUpsert(