107 lines
3.2 KiB
Python
107 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
from types import SimpleNamespace
|
|
|
|
from fastapi import HTTPException
|
|
from routes.assistants import _validate_startup_actions
|
|
from schemas import AssistantUpsert
|
|
|
|
|
|
def startup_body(*, phase: str = "opening") -> AssistantUpsert:
|
|
return AssistantUpsert(
|
|
name="启动动作测试",
|
|
type="prompt",
|
|
runtimeMode="pipeline",
|
|
toolIds=["tool_message"],
|
|
startup={
|
|
"executionMode": "sequential",
|
|
"actions": [
|
|
{
|
|
"id": "opening_message",
|
|
"phase": phase,
|
|
"toolId": "tool_message",
|
|
"arguments": {
|
|
"title": "重要提示",
|
|
"message": "请确认已阅读。",
|
|
"actions": [
|
|
{
|
|
"id": "confirmed",
|
|
"label": "确认",
|
|
"style": "primary",
|
|
}
|
|
],
|
|
"dismissible": False,
|
|
},
|
|
"required": True,
|
|
}
|
|
],
|
|
},
|
|
)
|
|
|
|
|
|
class FakeSession:
|
|
def __init__(self, tool):
|
|
self.tool = tool
|
|
|
|
async def get(self, _model, tool_id):
|
|
return self.tool if tool_id == self.tool.id else None
|
|
|
|
|
|
class StartupActionValidationTests(unittest.IsolatedAsyncioTestCase):
|
|
def test_realtime_rejects_startup_actions(self):
|
|
with self.assertRaisesRegex(ValueError, "Realtime"):
|
|
AssistantUpsert(
|
|
name="Realtime 启动动作",
|
|
type="prompt",
|
|
runtimeMode="realtime",
|
|
toolIds=["tool_message"],
|
|
startup=startup_body().startup,
|
|
)
|
|
|
|
async def test_show_message_requires_session_wait(self):
|
|
tool = SimpleNamespace(
|
|
id="tool_message",
|
|
name="重要提示",
|
|
function_name="show_message",
|
|
type="client",
|
|
status="active",
|
|
definition={
|
|
"config": {
|
|
"wait_for_response": True,
|
|
"response_wait_mode": "timeout",
|
|
}
|
|
},
|
|
)
|
|
|
|
with self.assertRaisesRegex(HTTPException, "会话内等待"):
|
|
await _validate_startup_actions(FakeSession(tool), startup_body())
|
|
|
|
tool.definition["config"]["response_wait_mode"] = "session"
|
|
await _validate_startup_actions(FakeSession(tool), startup_body())
|
|
|
|
async def test_preflight_rejects_client_tools(self):
|
|
tool = SimpleNamespace(
|
|
id="tool_message",
|
|
name="重要提示",
|
|
function_name="show_message",
|
|
type="client",
|
|
status="active",
|
|
definition={
|
|
"config": {
|
|
"wait_for_response": True,
|
|
"response_wait_mode": "session",
|
|
}
|
|
},
|
|
)
|
|
|
|
with self.assertRaisesRegex(HTTPException, "preflight"):
|
|
await _validate_startup_actions(
|
|
FakeSession(tool),
|
|
startup_body(phase="preflight"),
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|