oclaw/tests/test_web_fetch_clean_tool.py
oliver ae44cbcad5 完善 MCP/会话上下文治理并补齐搜索与诊断能力。
补充历史压缩与会话诊断链路,强化 tool pairing 与空响应兜底观测;完善 Admin MCP 导入/绑定与相关前端展示;新增 web_search_fast/web_fetch_clean 及多项测试与文档更新,并同步技能安装与角色绑定策略改进。

Made-with: Cursor
2026-05-01 17:46:50 +08:00

43 lines
1.5 KiB
Python

from __future__ import annotations
from oclaw.runtime.tools.public.web_fetch_clean_tool import web_fetch_clean_tool
class _Resp:
def __init__(self, body: bytes, *, status: int = 200, headers: dict[str, str] | None = None):
self._body = body
self.status = status
self.headers = headers or {"content-type": "text/html; charset=utf-8"}
def read(self) -> bytes:
return self._body
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
return False
def test_web_fetch_clean_html_strip(monkeypatch) -> None:
html = b"""
<html><head><title> Hello </title><style>.x{}</style></head>
<body><h1>Title</h1><p>Para <b>one</b>.</p><script>alert(1)</script></body></html>
"""
monkeypatch.setattr("urllib.request.urlopen", lambda req, timeout=0: _Resp(html)) # noqa: ARG005
out = web_fetch_clean_tool().handler({"url": "https://example.com"})
assert out.get("ok") is True, out
assert out.get("title") == "Hello"
text = str(out.get("text") or "")
assert "Para" in text
assert "one" in text
assert "alert" not in str(out.get("text") or "")
def test_web_fetch_clean_http_error(monkeypatch) -> None:
monkeypatch.setattr("urllib.request.urlopen", lambda req, timeout=0: _Resp(b"no", status=403)) # noqa: ARG005
out = web_fetch_clean_tool().handler({"url": "https://example.com"})
# Our stub doesn't raise HTTPError; status is checked and returns http_error.
assert out.get("ok") is False
assert out.get("error_code") == "http_error"