46 lines
2.2 KiB
Python
46 lines
2.2 KiB
Python
"""ws_builtin_tools_patch 单测:补丁后 LocalWorkspace 内建工具为空、幂等、六工具白名单路不受影响。
|
|
跑:cheap-worker/.venv/bin/python -m pytest cheap-worker/tests/test_ws_builtin_tools_patch.py -v
|
|
"""
|
|
import asyncio
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1])) # → cheap-worker/
|
|
import _bootstrap # noqa: E402,F401 仅加 sys.path(import 时不取 key、不触网)
|
|
from ws_builtin_tools_patch import apply_ws_builtin_tools_patch # noqa: E402
|
|
|
|
|
|
def test_patch_empties_builtin_tools(tmp_path):
|
|
# 补丁前:2.0.2 硬编码六内建(Bash/Edit/Glob/Grep/Read/Write);补丁后:空表。
|
|
from agentscope.workspace import LocalWorkspace
|
|
|
|
apply_ws_builtin_tools_patch()
|
|
ws = LocalWorkspace(workspace_id="t", workdir=str(tmp_path))
|
|
assert asyncio.run(ws.list_tools()) == [], "补丁后内建工具面必须为空(写白名单不被旁路)"
|
|
|
|
|
|
def test_patch_idempotent():
|
|
# 重复 apply 不再包层(标记位短路),且行为不变。
|
|
from agentscope.workspace import LocalWorkspace
|
|
|
|
apply_ws_builtin_tools_patch()
|
|
fn1 = LocalWorkspace.list_tools
|
|
apply_ws_builtin_tools_patch()
|
|
assert LocalWorkspace.list_tools is fn1, "幂等:第二次 apply 不应替换函数对象"
|
|
|
|
|
|
def test_cheap_six_tools_unaffected(tmp_path):
|
|
# 六工具经 build_toolkit(extra_agent_tools 工厂路)产出,不走 workspace.list_tools —— 补丁不动它。
|
|
# 提取方式与生产同源(service.app._extract_function_tools,从 Toolkit.tool_groups 收)。
|
|
apply_ws_builtin_tools_patch()
|
|
from cheap_toolkit import CheapSession, build_toolkit
|
|
from service.app import _extract_function_tools
|
|
|
|
session = CheapSession(game_id="99999")
|
|
tools = _extract_function_tools(build_toolkit(session))
|
|
names = {getattr(t, "name", getattr(getattr(t, "func", None), "__name__", "")) for t in tools}
|
|
# 至少核心读写与收尾工具在席(名字集按 cheap_toolkit 现约定,松断言防脆)。
|
|
assert any("read" in n for n in names), f"六工具应含读工具,实际:{names}"
|
|
assert any("write" in n for n in names), f"六工具应含写工具,实际:{names}"
|
|
assert any("finish" in n for n in names), f"六工具应含 finish,实际:{names}"
|