mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-09 07:20:44 +08:00
Refuse openpyxl/pandas-to_excel via run_command, auto-write content-only files under tmp/, and limit WhatsApp ops short intents to 8 tool rounds by default. Co-authored-by: Cursor <cursoragent@cursor.com>
52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from runtime.tools.public.write_file_tool import write_file_tool
|
|
from runtime.tools.tool_validation import filter_arguments_to_schema, validate_tool_arguments
|
|
|
|
|
|
def test_write_file_accepts_filename_alias(tmp_path, monkeypatch) -> None:
|
|
target = tmp_path / "workspace" / "note.txt"
|
|
monkeypatch.setattr(
|
|
"runtime.tools.public.write_file_tool.resolve_workspace_path",
|
|
lambda raw: target,
|
|
)
|
|
spec = write_file_tool()
|
|
out = spec.handler({"filename": "note.txt", "text": "hello"})
|
|
assert out.get("ok") is True
|
|
assert target.read_text(encoding="utf-8") == "hello"
|
|
|
|
|
|
def test_write_file_schema_allows_aliases() -> None:
|
|
spec = write_file_tool()
|
|
args = {"file": "a.txt", "content": "x"}
|
|
filtered = filter_arguments_to_schema(spec.parameters, args)
|
|
ok, err = validate_tool_arguments(spec.parameters, filtered)
|
|
assert ok, err
|
|
assert "file" in filtered
|
|
|
|
|
|
def test_write_file_path_required_message() -> None:
|
|
spec = write_file_tool()
|
|
out = spec.handler({})
|
|
assert out.get("ok") is False
|
|
assert out.get("error") == "path_required"
|
|
assert "example" in out
|
|
|
|
|
|
def test_write_file_defaults_path_when_content_only(tmp_path, monkeypatch) -> None:
|
|
def _resolve(raw: str):
|
|
return tmp_path / "workspace" / str(raw).replace("\\", "/").lstrip("/")
|
|
|
|
monkeypatch.setattr(
|
|
"runtime.tools.public.write_file_tool.resolve_workspace_path",
|
|
_resolve,
|
|
)
|
|
spec = write_file_tool()
|
|
out = spec.handler({"content": "hello-auto"})
|
|
assert out.get("ok") is True
|
|
assert out.get("auto_path") is True
|
|
path = Path(str(out.get("path") or ""))
|
|
assert path.name.startswith("write_")
|
|
assert path.read_text(encoding="utf-8") == "hello-auto"
|