Files
unibots/tools/file/write_test.go
T
agent fc644ecd6e feat: import agents_and_robots platform as unibots (Matrix-out, unibus transport)
Reemplaza el scaffold del echobot por la plataforma completa de bots traida
desde ~/DataProyects/Github/agents_and_robots tras la operacion Matrix-out:
los bots ya no hablan por Matrix sino por el bus unibus (modelo todo-rooms +
E2E via shell/transportunibus sobre github.com/enmanuel/unibus/pkg/client).

- go.mod: replace de unibus -> ../unibus y de fn-registry -> ../../../.. (paths
  relativos reajustados a la nueva ubicacion dentro de fn_registry).
- app.md: bump a 0.2.0, descripcion + arquitectura + comandos + gotchas reales.
- modulo Go conservado como github.com/enmanuel/agents (sin reescribir imports).

agents_and_robots queda archivado como museo de la era Matrix.
2026-06-07 11:50:13 +02:00

203 lines
5.2 KiB
Go

package file
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/enmanuel/agents/internal/config"
)
func TestWriteFile_CreatesNewFile(t *testing.T) {
tmp := t.TempDir()
cfg := config.FileOpsCfg{AllowedPaths: []string{tmp}, ReadOnly: false}
tool := NewWriteFile(cfg)
target := filepath.Join(tmp, "new.txt")
result := tool.Exec(context.Background(), map[string]any{
"path": target,
"content": "hello world",
})
if result.Err != nil {
t.Fatalf("expected success, got: %v", result.Err)
}
data, err := os.ReadFile(target)
if err != nil {
t.Fatalf("file not created: %v", err)
}
if string(data) != "hello world" {
t.Fatalf("expected 'hello world', got %q", string(data))
}
if !strings.Contains(result.Output, "11 bytes") {
t.Fatalf("expected output mentioning bytes, got %q", result.Output)
}
}
func TestWriteFile_RejectsReadOnly(t *testing.T) {
tmp := t.TempDir()
cfg := config.FileOpsCfg{AllowedPaths: []string{tmp}, ReadOnly: true}
tool := NewWriteFile(cfg)
result := tool.Exec(context.Background(), map[string]any{
"path": filepath.Join(tmp, "test.txt"),
"content": "data",
})
if result.Err == nil {
t.Fatal("expected error when ReadOnly is true")
}
if !strings.Contains(result.Err.Error(), "read_only") {
t.Fatalf("expected read_only error, got: %v", result.Err)
}
}
func TestWriteFile_RejectsPathOutsideAllowed(t *testing.T) {
tmp := t.TempDir()
cfg := config.FileOpsCfg{AllowedPaths: []string{tmp}, ReadOnly: false}
tool := NewWriteFile(cfg)
result := tool.Exec(context.Background(), map[string]any{
"path": "/etc/test.txt",
"content": "data",
})
if result.Err == nil {
t.Fatal("expected error for path outside allowed")
}
}
func TestWriteFile_RejectsContentOver1MB(t *testing.T) {
tmp := t.TempDir()
cfg := config.FileOpsCfg{AllowedPaths: []string{tmp}, ReadOnly: false}
tool := NewWriteFile(cfg)
bigContent := strings.Repeat("x", maxWriteSize+1)
result := tool.Exec(context.Background(), map[string]any{
"path": filepath.Join(tmp, "big.txt"),
"content": bigContent,
})
if result.Err == nil {
t.Fatal("expected error for content exceeding 1 MB")
}
if !strings.Contains(result.Err.Error(), "1 MB") {
t.Fatalf("expected 1 MB error, got: %v", result.Err)
}
}
func TestWriteFile_CreatesParentDirectories(t *testing.T) {
tmp := t.TempDir()
cfg := config.FileOpsCfg{AllowedPaths: []string{tmp}, ReadOnly: false}
tool := NewWriteFile(cfg)
target := filepath.Join(tmp, "sub", "dir", "file.txt")
result := tool.Exec(context.Background(), map[string]any{
"path": target,
"content": "nested",
})
if result.Err != nil {
t.Fatalf("expected success, got: %v", result.Err)
}
data, err := os.ReadFile(target)
if err != nil {
t.Fatalf("file not created: %v", err)
}
if string(data) != "nested" {
t.Fatalf("expected 'nested', got %q", string(data))
}
}
func TestWriteFile_OverwritesExistingFile(t *testing.T) {
tmp := t.TempDir()
target := filepath.Join(tmp, "exists.txt")
os.WriteFile(target, []byte("old"), 0644)
cfg := config.FileOpsCfg{AllowedPaths: []string{tmp}, ReadOnly: false}
tool := NewWriteFile(cfg)
result := tool.Exec(context.Background(), map[string]any{
"path": target,
"content": "new content",
})
if result.Err != nil {
t.Fatalf("expected success, got: %v", result.Err)
}
data, _ := os.ReadFile(target)
if string(data) != "new content" {
t.Fatalf("expected 'new content', got %q", string(data))
}
}
func TestWriteFile_PathTraversal(t *testing.T) {
tmp := t.TempDir()
cfg := config.FileOpsCfg{AllowedPaths: []string{tmp}, ReadOnly: false}
tool := NewWriteFile(cfg)
result := tool.Exec(context.Background(), map[string]any{
"path": filepath.Join(tmp, "..", "..", "etc", "evil.txt"),
"content": "data",
})
if result.Err == nil {
t.Fatal("expected error for path traversal")
}
}
func TestWriteFile_SymlinkEscape(t *testing.T) {
tmp := t.TempDir()
link := filepath.Join(tmp, "escape")
os.Symlink("/tmp", link)
cfg := config.FileOpsCfg{AllowedPaths: []string{tmp}, ReadOnly: false}
tool := NewWriteFile(cfg)
result := tool.Exec(context.Background(), map[string]any{
"path": filepath.Join(link, "evil.txt"),
"content": "data",
})
if result.Err == nil {
t.Fatal("expected error for symlink escape")
}
}
func TestWriteFile_EmptyPath(t *testing.T) {
cfg := config.FileOpsCfg{AllowedPaths: []string{"/tmp"}, ReadOnly: false}
tool := NewWriteFile(cfg)
result := tool.Exec(context.Background(), map[string]any{
"path": "",
"content": "data",
})
if result.Err == nil {
t.Fatal("expected error for empty path")
}
}
func TestWriteFile_EmptyContent(t *testing.T) {
cfg := config.FileOpsCfg{AllowedPaths: []string{"/tmp"}, ReadOnly: false}
tool := NewWriteFile(cfg)
result := tool.Exec(context.Background(), map[string]any{
"path": "/tmp/test.txt",
"content": "",
})
if result.Err == nil {
t.Fatal("expected error for empty content")
}
}
func TestWriteFile_DenyByDefault(t *testing.T) {
cfg := config.FileOpsCfg{AllowedPaths: []string{}, ReadOnly: false}
tool := NewWriteFile(cfg)
result := tool.Exec(context.Background(), map[string]any{
"path": "/tmp/test.txt",
"content": "data",
})
if result.Err == nil {
t.Fatal("expected error when AllowedPaths is empty")
}
}