package file import ( "context" "strings" "testing" "git.yeij.top/AskaEth/Cyrene_v2/internal/plugin" ) func setup(t *testing.T) *Plugin { t.Helper() p := New() if err := p.Init(context.Background(), plugin.PluginConfig{DataDir: t.TempDir()}); err != nil { t.Fatalf("Init: %v", err) } return p } func TestWriteReadList(t *testing.T) { p := setup(t) ctx := context.Background() out, err := p.Execute(ctx, "file_write", map[string]any{"path": "notes/todo.txt", "content": "买牛奶"}) if err != nil { t.Fatalf("write: %v", err) } if !strings.Contains(out, "已写入") { t.Errorf("unexpected write output: %q", out) } out, err = p.Execute(ctx, "file_read", map[string]any{"path": "notes/todo.txt"}) if err != nil || out != "买牛奶" { t.Errorf("read = %q, err = %v", out, err) } out, err = p.Execute(ctx, "file_list", map[string]any{"path": "notes"}) if err != nil || !strings.Contains(out, "todo.txt") { t.Errorf("list = %q, err = %v", out, err) } } func TestPathEscapeRejected(t *testing.T) { p := setup(t) for _, path := range []string{"../secret", "../../etc/passwd", "/etc/passwd"} { if _, err := p.Execute(context.Background(), "file_read", map[string]any{"path": path}); err == nil { t.Errorf("path %q should be rejected", path) } } } func TestReadMissingFile(t *testing.T) { p := setup(t) if _, err := p.Execute(context.Background(), "file_read", map[string]any{"path": "nope.txt"}); err == nil { t.Error("expected error for missing file") } }