feat: US-015 - Add EditTool tests

Added comprehensive test coverage for EditTool (EditFileTool, AppendFileTool) with 10 test cases:
- TestEditTool_EditFile_Success: Verifies successful file editing
- TestEditTool_EditFile_NotFound: Verifies error handling for non-existent files
- TestEditTool_EditFile_OldTextNotFound: Verifies error when old_text not found
- TestEditTool_EditFile_MultipleMatches: Verifies error for multiple occurrences
- TestEditTool_EditFile_OutsideAllowedDir: Verifies directory restriction
- TestEditTool_EditFile_MissingPath: Verifies missing path parameter
- TestEditTool_EditFile_MissingOldText: Verifies missing old_text parameter
- TestEditTool_EditFile_MissingNewText: Verifies missing new_text parameter
- TestEditTool_AppendFile_Success: Verifies successful file appending
- TestEditTool_AppendFile_MissingPath: Verifies missing path for append
- TestEditTool_AppendFile_MissingContent: Verifies missing content for append

EditTool implementation already conforms to ToolResult specification:
- EditFile returns SilentResult('File edited: ...')
- AppendFile returns SilentResult('Appended to ...')
- Errors return ErrorResult with IsError=true

EditFileTool includes security feature: optional directory restriction to prevent editing files outside allowed paths.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
yinwm
2026-02-12 19:55:57 +08:00
parent 0ac93d4429
commit 35fa64cde8
3 changed files with 316 additions and 2 deletions

View File

@@ -6,7 +6,7 @@ Tool 返回值结构化重构 - 将 Tool 接口返回值从 (string, error) 改
## Progress
### Completed (14/21)
### Completed (15/21)
- US-001: Add ToolResult struct and helper functions
- US-002: Modify Tool interface to return *ToolResult
@@ -21,6 +21,7 @@ Tool 返回值结构化重构 - 将 Tool 接口返回值从 (string, error) 改
- US-012: Refactor ShellTool to use ToolResult
- US-013: Refactor FilesystemTool to use ToolResult
- US-014: Refactor WebTool to use ToolResult
- US-015: Refactor EditTool to use ToolResult
- US-012: Refactor ShellTool to use ToolResult
- US-013: Refactor FilesystemTool to use ToolResult
@@ -44,6 +45,7 @@ Tool 返回值结构化重构 - 将 Tool 接口返回值从 (string, error) 改
| US-012 | Refactor ShellTool to use ToolResult | Completed | |
| US-013 | Refactor FilesystemTool to use ToolResult | Completed | Added test file filesystem_test.go |
| US-014 | Refactor WebTool to use ToolResult | Completed | Added test file web_test.go |
| US-015 | Refactor EditTool to use ToolResult | Completed | Added test file edit_test.go |
| US-015 | Refactor EditTool to use ToolResult | Completed | |
| US-016 | Refactor CronTool to use ToolResult | Pending | |
| US-017 | Refactor SpawnTool to use AsyncTool and callbacks | Pending | |
@@ -295,4 +297,27 @@ Tool 返回值结构化重构 - 将 Tool 接口返回值从 (string, error) 改
- **Gotchas encountered:** 无
- **Useful context:** WebSearchTool 需要配置 BRAVE_API_KEY 环境变量才能正常工作。WebFetchTool 支持多种内容类型JSON格式化、HTML文本提取、纯文本。
---
## [2026-02-12] - US-015
- What was implemented:
- EditTool 已经完全使用 ToolResult 返回值(无需修改实现)
- 添加了完整的测试文件 `pkg/tools/edit_test.go`,包含 10 个测试用例
- 测试覆盖了所有验收标准:
- EditFileTool 成功返回 SilentResultSilent=trueForUser=空)
- AppendFileTool 成功返回 SilentResult
- 文件不存在错误处理
- old_text 不存在错误处理
- 多次匹配错误处理
- 目录限制测试
- 缺少参数错误处理
- Files changed:
- `pkg/tools/edit_test.go` (新增)
- **Learnings for future iterations:**
- **Patterns discovered:** EditTool 包含两个子工具EditFileTool替换文本和 AppendFileTool追加内容。都返回 SilentResult 避免重复发送内容给用户。
- **Gotchas encountered:** 无
- **Useful context:** EditFileTool 支持可选的目录限制,用于安全控制,防止编辑允许目录外的文件。
---