feat: complete file manager core operations

This commit is contained in:
OpenClaw Agent
2026-09-22 03:08:35 +08:00
parent c29ba6eb40
commit 8155fd259e
2 changed files with 61 additions and 10 deletions
@@ -6,8 +6,10 @@ import (
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"chfs-ng/internal/auth"
@@ -149,3 +151,44 @@ func TestHTTPPermissionMatrix(t *testing.T) {
})
}
}
func TestHTTPBatchDeleteAndTransferConflict(t *testing.T) {
s, root := permissionTestServer(t, auth.Read|auth.Write|auth.Delete)
h := s.Handler()
if err := os.WriteFile(filepath.Join(root, "second.txt"), []byte("second"), 0o644); err != nil {
t.Fatal(err)
}
var b bytes.Buffer
mw := multipart.NewWriter(&b)
_ = mw.WriteField("filepath", "file.txt")
_ = mw.WriteField("filepath", "second.txt")
_ = mw.Close()
rr := doRequest(h, http.MethodDelete, "/chfs/rmfiles", &b, mw.FormDataContentType())
if rr.Code != http.StatusNoContent {
t.Fatalf("batch delete: got %d", rr.Code)
}
if _, err := os.Stat(filepath.Join(root, "file.txt")); !os.IsNotExist(err) {
t.Fatalf("file.txt still exists: %v", err)
}
if _, err := os.Stat(filepath.Join(root, "second.txt")); !os.IsNotExist(err) {
t.Fatalf("second.txt still exists: %v", err)
}
if err := os.WriteFile(filepath.Join(root, "source.txt"), []byte("source"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "target.txt"), []byte("target"), 0o644); err != nil {
t.Fatal(err)
}
form := url.Values{"source": {"source.txt"}, "destination": {"target.txt"}}
copyReq := doRequest(h, http.MethodPost, "/chfs/copy", strings.NewReader(form.Encode()), "application/x-www-form-urlencoded")
if copyReq.Code != http.StatusBadRequest {
t.Fatalf("copy conflict: got %d", copyReq.Code)
}
data, err := os.ReadFile(filepath.Join(root, "target.txt"))
if err != nil {
t.Fatal(err)
}
if string(data) != "target" {
t.Fatalf("target overwritten: %q", data)
}
}