Files
ai-agent-book/chapter4/execution-tools/test_edit_reject_empty_search.py
liqiang b119135836
Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
ai-agent-book 精选快照(<2MB 代码与文档,来自 github.com/bojieli/ai-agent-book)
2026-08-20 13:12:50 +00:00

40 lines
1.3 KiB
Python

"""Regression: edit_file must reject empty search text (not insert at start)."""
import tempfile
from pathlib import Path
import pytest
from llm_helper import LLMHelper
from file_tools import FileTools
@pytest.mark.asyncio
async def test_empty_search_rejected():
with tempfile.TemporaryDirectory() as td:
root = Path(td).resolve()
tools = FileTools(LLMHelper())
tools.workspace_dir = root
target = root / "note.txt"
target.write_text("hello world\n", encoding="utf-8")
result = await tools.edit_file(path="note.txt", search="", replace="INJECT")
assert result["success"] is False
assert "empty" in result["error"].lower()
assert target.read_text(encoding="utf-8") == "hello world\n"
@pytest.mark.asyncio
async def test_normal_edit_still_works():
with tempfile.TemporaryDirectory() as td:
root = Path(td).resolve()
tools = FileTools(LLMHelper())
tools.workspace_dir = root
target = root / "note.txt"
target.write_text("hello world\n", encoding="utf-8")
result = await tools.edit_file(
path="note.txt", search="hello", replace="hi"
)
assert result["success"] is True
assert target.read_text(encoding="utf-8") == "hi world\n"