feat: US-013 - Add FilesystemTool tests

Added comprehensive test coverage for FilesystemTool (ReadFileTool, WriteFileTool, ListDirTool) with 10 test cases:
- TestFilesystemTool_ReadFile_Success: Verifies file content goes to ForLLM
- TestFilesystemTool_ReadFile_NotFound: Verifies error handling for missing files
- TestFilesystemTool_ReadFile_MissingPath: Verifies missing parameter handling
- TestFilesystemTool_WriteFile_Success: Verifies SilentResult behavior
- TestFilesystemTool_WriteFile_CreateDir: Verifies automatic directory creation
- TestFilesystemTool_WriteFile_MissingPath: Verifies missing path parameter handling
- TestFilesystemTool_WriteFile_MissingContent: Verifies missing content parameter handling
- TestFilesystemTool_ListDir_Success: Verifies directory listing functionality
- TestFilesystemTool_ListDir_NotFound: Verifies error handling for invalid paths
- TestFilesystemTool_ListDir_DefaultPath: Verifies default to current directory

FilesystemTool implementation already conforms to ToolResult specification:
- ReadFile/ListDir return NewToolResult (ForLLM only, ForUser empty)
- WriteFile returns SilentResult (Silent=true, ForUser empty)
- Errors return ErrorResult with IsError=true

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
yinwm
2026-02-12 19:53:00 +08:00
parent e7e3f95ebe
commit 88014ecaff
3 changed files with 275 additions and 2 deletions

View File

@@ -19,6 +19,7 @@ Tool 返回值结构化重构 - 将 Tool 接口返回值从 (string, error) 改
- US-010: Update RecordLastChannel to use atomic save
- US-011: Refactor MessageTool to use ToolResult
- US-012: Refactor ShellTool to use ToolResult
- US-013: Refactor FilesystemTool to use ToolResult
### In Progress
@@ -38,7 +39,7 @@ Tool 返回值结构化重构 - 将 Tool 接口返回值从 (string, error) 改
| US-010 | Update RecordLastChannel to use atomic save | Completed | |
| US-011 | Refactor MessageTool to use ToolResult | Completed | Added test file message_test.go |
| US-012 | Refactor ShellTool to use ToolResult | Completed | |
| US-013 | Refactor FilesystemTool 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 | |
| US-015 | Refactor EditTool to use ToolResult | Completed | |
| US-016 | Refactor CronTool to use ToolResult | Pending | |
@@ -245,4 +246,27 @@ Tool 返回值结构化重构 - 将 Tool 接口返回值从 (string, error) 改
- **Gotchas encountered:** 无
- **Useful context:** ShellTool 的安全防护机制包括:危险命令检测(如 `rm -rf`)、工作空间路径遍历检测、命令超时控制。
---
## [2026-02-12] - US-013
- What was implemented:
- FilesystemTool 已经完全使用 ToolResult 返回值(无需修改实现)
- 添加了完整的测试文件 `pkg/tools/filesystem_test.go`,包含 10 个测试用例
- 测试覆盖了所有验收标准:
- ReadFileTool 成功返回 NewToolResultForLLM=内容ForUser=空)
- WriteFileTool 成功返回 SilentResultSilent=trueForUser=空)
- ListDirTool 成功返回 NewToolResult列出文件和目录
- 错误场景返回 ErrorResultIsError=true
- 缺少参数的错误处理
- 目录自动创建功能测试
- 默认路径处理测试
- Files changed:
- `pkg/tools/filesystem_test.go` (新增)
- **Learnings for future iterations:**
- **Patterns discovered:** FilesystemTool 中不同操作使用不同的返回值模式ReadFile/ListDir 使用 NewToolResult仅设置 ForLLMWriteFile 使用 SilentResult设置 Silent=true
- **Gotchas encountered:** 最初误解了 NewToolResult 的行为,以为它会设置 ForUser但实际上它只设置 ForLLM。
- **Useful context:** WriteFileTool 会自动创建不存在的目录(使用 os.MkdirAll这是文件写入工具的重要功能。
---