mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-04-14 00:17:34 +08:00
feat: expand node artifact operations and retention
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@@ -432,7 +434,10 @@ func TestHandleWebUILogsLive(t *testing.T) {
|
||||
func TestHandleWebUINodesIncludesP2PSummary(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := NewServer("127.0.0.1", 0, "", nodes.NewManager())
|
||||
mgr := nodes.NewManager()
|
||||
mgr.Upsert(nodes.NodeInfo{ID: "edge-b", Name: "Edge B"})
|
||||
mgr.MarkOffline("edge-b")
|
||||
srv := NewServer("127.0.0.1", 0, "", mgr)
|
||||
workspace := t.TempDir()
|
||||
srv.SetWorkspacePath(workspace)
|
||||
if err := os.MkdirAll(filepath.Join(workspace, "memory"), 0755); err != nil {
|
||||
@@ -446,6 +451,9 @@ func TestHandleWebUINodesIncludesP2PSummary(t *testing.T) {
|
||||
"enabled": true,
|
||||
"transport": "webrtc",
|
||||
"active_sessions": 2,
|
||||
"nodes": []map[string]interface{}{
|
||||
{"node": "edge-b", "status": "connecting", "retry_count": 3, "last_error": "signal timeout"},
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
@@ -463,6 +471,10 @@ func TestHandleWebUINodesIncludesP2PSummary(t *testing.T) {
|
||||
if p2p == nil || p2p["transport"] != "webrtc" {
|
||||
t.Fatalf("expected p2p summary, got %+v", body)
|
||||
}
|
||||
alerts, _ := body["alerts"].([]interface{})
|
||||
if len(alerts) == 0 {
|
||||
t.Fatalf("expected node alerts, got %+v", body)
|
||||
}
|
||||
dispatches, _ := body["dispatches"].([]interface{})
|
||||
if len(dispatches) != 1 {
|
||||
t.Fatalf("expected dispatch audit rows, got %+v", body["dispatches"])
|
||||
@@ -473,3 +485,273 @@ func TestHandleWebUINodesIncludesP2PSummary(t *testing.T) {
|
||||
t.Fatalf("expected artifact previews in dispatch row, got %+v", first)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleWebUINodeDispatchReplay(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := NewServer("127.0.0.1", 0, "", nodes.NewManager())
|
||||
srv.SetNodeDispatchHandler(func(ctx context.Context, req nodes.Request, mode string) (nodes.Response, error) {
|
||||
if req.Node != "edge-a" || req.Action != "screen_snapshot" || mode != "auto" {
|
||||
t.Fatalf("unexpected replay request: %+v mode=%s", req, mode)
|
||||
}
|
||||
if fmt.Sprint(req.Args["quality"]) != "high" {
|
||||
t.Fatalf("unexpected args: %+v", req.Args)
|
||||
}
|
||||
return nodes.Response{
|
||||
OK: true,
|
||||
Node: req.Node,
|
||||
Action: req.Action,
|
||||
Payload: map[string]interface{}{
|
||||
"used_transport": "webrtc",
|
||||
},
|
||||
}, nil
|
||||
})
|
||||
|
||||
body := `{"node":"edge-a","action":"screen_snapshot","mode":"auto","args":{"quality":"high"}}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/webui/api/node_dispatches/replay", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
srv.handleWebUINodeDispatchReplay(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), `"used_transport":"webrtc"`) {
|
||||
t.Fatalf("expected replay result body, got: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleWebUINodeArtifactsListAndDelete(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := NewServer("127.0.0.1", 0, "", nodes.NewManager())
|
||||
workspace := t.TempDir()
|
||||
srv.SetWorkspacePath(workspace)
|
||||
if err := os.MkdirAll(filepath.Join(workspace, "memory"), 0o755); err != nil {
|
||||
t.Fatalf("mkdir memory: %v", err)
|
||||
}
|
||||
artifactPath := filepath.Join(workspace, "artifact.txt")
|
||||
if err := os.WriteFile(artifactPath, []byte("artifact-body"), 0o644); err != nil {
|
||||
t.Fatalf("write artifact: %v", err)
|
||||
}
|
||||
auditLine := fmt.Sprintf("{\"time\":\"2026-03-09T00:00:00Z\",\"node\":\"edge-a\",\"action\":\"run\",\"artifacts\":[{\"name\":\"artifact.txt\",\"kind\":\"text\",\"mime_type\":\"text/plain\",\"source_path\":\"%s\",\"size_bytes\":13}]}\n", artifactPath)
|
||||
if err := os.WriteFile(filepath.Join(workspace, "memory", "nodes-dispatch-audit.jsonl"), []byte(auditLine), 0o644); err != nil {
|
||||
t.Fatalf("write audit: %v", err)
|
||||
}
|
||||
|
||||
listReq := httptest.NewRequest(http.MethodGet, "/webui/api/node_artifacts", nil)
|
||||
listRec := httptest.NewRecorder()
|
||||
srv.handleWebUINodeArtifacts(listRec, listReq)
|
||||
if listRec.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", listRec.Code)
|
||||
}
|
||||
var listBody map[string]interface{}
|
||||
if err := json.Unmarshal(listRec.Body.Bytes(), &listBody); err != nil {
|
||||
t.Fatalf("decode list body: %v", err)
|
||||
}
|
||||
items, _ := listBody["items"].([]interface{})
|
||||
if len(items) != 1 {
|
||||
t.Fatalf("expected 1 artifact, got %+v", listBody)
|
||||
}
|
||||
item, _ := items[0].(map[string]interface{})
|
||||
artifactID := strings.TrimSpace(fmt.Sprint(item["id"]))
|
||||
if artifactID == "" {
|
||||
t.Fatalf("expected artifact id, got %+v", item)
|
||||
}
|
||||
|
||||
deleteReq := httptest.NewRequest(http.MethodPost, "/webui/api/node_artifacts/delete", strings.NewReader(fmt.Sprintf(`{"id":"%s"}`, artifactID)))
|
||||
deleteReq.Header.Set("Content-Type", "application/json")
|
||||
deleteRec := httptest.NewRecorder()
|
||||
srv.handleWebUINodeArtifactDelete(deleteRec, deleteReq)
|
||||
if deleteRec.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", deleteRec.Code, deleteRec.Body.String())
|
||||
}
|
||||
if _, err := os.Stat(artifactPath); !os.IsNotExist(err) {
|
||||
t.Fatalf("expected artifact file removed, stat err=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleWebUINodeArtifactsExport(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := NewServer("127.0.0.1", 0, "", nodes.NewManager())
|
||||
workspace := t.TempDir()
|
||||
srv.SetWorkspacePath(workspace)
|
||||
if err := os.MkdirAll(filepath.Join(workspace, "memory"), 0o755); err != nil {
|
||||
t.Fatalf("mkdir memory: %v", err)
|
||||
}
|
||||
auditLine := "{\"time\":\"2026-03-09T00:00:00Z\",\"node\":\"edge-a\",\"action\":\"screen_snapshot\",\"ok\":true,\"artifacts\":[{\"name\":\"shot.txt\",\"kind\":\"text\",\"mime_type\":\"text/plain\",\"content_base64\":\"Y2FwdHVyZQ==\",\"size_bytes\":7}]}\n"
|
||||
if err := os.WriteFile(filepath.Join(workspace, "memory", "nodes-dispatch-audit.jsonl"), []byte(auditLine), 0o644); err != nil {
|
||||
t.Fatalf("write audit: %v", err)
|
||||
}
|
||||
srv.mgr.Upsert(nodes.NodeInfo{ID: "edge-a", Name: "Edge A", Online: true})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/webui/api/node_artifacts/export?node=edge-a&action=screen_snapshot&kind=text", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
srv.handleWebUINodeArtifactsExport(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if got := rec.Header().Get("Content-Type"); !strings.Contains(got, "application/zip") {
|
||||
t.Fatalf("expected zip response, got %q", got)
|
||||
}
|
||||
zr, err := zip.NewReader(bytes.NewReader(rec.Body.Bytes()), int64(rec.Body.Len()))
|
||||
if err != nil {
|
||||
t.Fatalf("open zip: %v", err)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, file := range zr.File {
|
||||
seen[file.Name] = true
|
||||
}
|
||||
for _, required := range []string{"manifest.json", "dispatches.json", "alerts.json", "artifacts.json"} {
|
||||
if !seen[required] {
|
||||
t.Fatalf("missing zip entry %q in %+v", required, seen)
|
||||
}
|
||||
}
|
||||
foundFile := false
|
||||
for _, file := range zr.File {
|
||||
if !strings.HasPrefix(file.Name, "files/") {
|
||||
continue
|
||||
}
|
||||
foundFile = true
|
||||
rc, err := file.Open()
|
||||
if err != nil {
|
||||
t.Fatalf("open artifact file: %v", err)
|
||||
}
|
||||
body, _ := io.ReadAll(rc)
|
||||
_ = rc.Close()
|
||||
if string(body) != "capture" {
|
||||
t.Fatalf("unexpected artifact payload %q", string(body))
|
||||
}
|
||||
}
|
||||
if !foundFile {
|
||||
t.Fatalf("expected exported artifact file in zip")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleWebUINodeArtifactsPrune(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := NewServer("127.0.0.1", 0, "", nodes.NewManager())
|
||||
workspace := t.TempDir()
|
||||
srv.SetWorkspacePath(workspace)
|
||||
if err := os.MkdirAll(filepath.Join(workspace, "memory"), 0o755); err != nil {
|
||||
t.Fatalf("mkdir memory: %v", err)
|
||||
}
|
||||
auditLines := strings.Join([]string{
|
||||
"{\"time\":\"2026-03-09T00:00:00Z\",\"node\":\"edge-a\",\"action\":\"screen_snapshot\",\"ok\":true,\"artifacts\":[{\"name\":\"one.txt\",\"kind\":\"text\",\"mime_type\":\"text/plain\",\"content_base64\":\"b25l\"}]}",
|
||||
"{\"time\":\"2026-03-09T00:01:00Z\",\"node\":\"edge-a\",\"action\":\"screen_snapshot\",\"ok\":true,\"artifacts\":[{\"name\":\"two.txt\",\"kind\":\"text\",\"mime_type\":\"text/plain\",\"content_base64\":\"dHdv\"}]}",
|
||||
"{\"time\":\"2026-03-09T00:02:00Z\",\"node\":\"edge-a\",\"action\":\"screen_snapshot\",\"ok\":true,\"artifacts\":[{\"name\":\"three.txt\",\"kind\":\"text\",\"mime_type\":\"text/plain\",\"content_base64\":\"dGhyZWU=\"}]}",
|
||||
}, "\n") + "\n"
|
||||
if err := os.WriteFile(filepath.Join(workspace, "memory", "nodes-dispatch-audit.jsonl"), []byte(auditLines), 0o644); err != nil {
|
||||
t.Fatalf("write audit: %v", err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/webui/api/node_artifacts/prune", strings.NewReader(`{"node":"edge-a","action":"screen_snapshot","kind":"text","keep_latest":1}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
srv.handleWebUINodeArtifactPrune(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
items := srv.webUINodeArtifactsPayloadFiltered("edge-a", "screen_snapshot", "text", 10)
|
||||
if len(items) != 1 {
|
||||
t.Fatalf("expected 1 remaining artifact, got %d", len(items))
|
||||
}
|
||||
if got := fmt.Sprint(items[0]["name"]); got != "three.txt" {
|
||||
t.Fatalf("expected newest artifact to remain, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleWebUINodeArtifactsAppliesRetentionConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := NewServer("127.0.0.1", 0, "", nodes.NewManager())
|
||||
workspace := t.TempDir()
|
||||
srv.SetWorkspacePath(workspace)
|
||||
if err := os.MkdirAll(filepath.Join(workspace, "memory"), 0o755); err != nil {
|
||||
t.Fatalf("mkdir memory: %v", err)
|
||||
}
|
||||
cfg := cfgpkg.DefaultConfig()
|
||||
cfg.Gateway.Nodes.Artifacts.Enabled = true
|
||||
cfg.Gateway.Nodes.Artifacts.KeepLatest = 1
|
||||
cfg.Gateway.Nodes.Artifacts.PruneOnRead = true
|
||||
cfgPath := filepath.Join(workspace, "config.json")
|
||||
if err := cfgpkg.SaveConfig(cfgPath, cfg); err != nil {
|
||||
t.Fatalf("save config: %v", err)
|
||||
}
|
||||
srv.SetConfigPath(cfgPath)
|
||||
auditLines := strings.Join([]string{
|
||||
"{\"time\":\"2026-03-09T00:00:00Z\",\"node\":\"edge-a\",\"action\":\"screen_snapshot\",\"ok\":true,\"artifacts\":[{\"name\":\"one.txt\",\"kind\":\"text\",\"mime_type\":\"text/plain\",\"content_base64\":\"b25l\"}]}",
|
||||
"{\"time\":\"2026-03-09T00:01:00Z\",\"node\":\"edge-a\",\"action\":\"screen_snapshot\",\"ok\":true,\"artifacts\":[{\"name\":\"two.txt\",\"kind\":\"text\",\"mime_type\":\"text/plain\",\"content_base64\":\"dHdv\"}]}",
|
||||
}, "\n") + "\n"
|
||||
if err := os.WriteFile(filepath.Join(workspace, "memory", "nodes-dispatch-audit.jsonl"), []byte(auditLines), 0o644); err != nil {
|
||||
t.Fatalf("write audit: %v", err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/webui/api/node_artifacts", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
srv.handleWebUINodeArtifacts(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
items := srv.webUINodeArtifactsPayload(10)
|
||||
if len(items) != 1 {
|
||||
t.Fatalf("expected retention to keep 1 artifact, got %d", len(items))
|
||||
}
|
||||
if got := fmt.Sprint(items[0]["name"]); got != "two.txt" {
|
||||
t.Fatalf("expected newest artifact to remain, got %q", got)
|
||||
}
|
||||
stats := srv.artifactStatsSnapshot()
|
||||
if fmt.Sprint(stats["pruned"]) == "" || fmt.Sprint(stats["pruned"]) == "0" {
|
||||
t.Fatalf("expected retention stats to record pruned artifacts, got %+v", stats)
|
||||
}
|
||||
if fmt.Sprint(stats["keep_latest"]) != "1" {
|
||||
t.Fatalf("expected keep_latest in stats, got %+v", stats)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleWebUINodeArtifactsAppliesRetentionDays(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := NewServer("127.0.0.1", 0, "", nodes.NewManager())
|
||||
workspace := t.TempDir()
|
||||
srv.SetWorkspacePath(workspace)
|
||||
if err := os.MkdirAll(filepath.Join(workspace, "memory"), 0o755); err != nil {
|
||||
t.Fatalf("mkdir memory: %v", err)
|
||||
}
|
||||
cfg := cfgpkg.DefaultConfig()
|
||||
cfg.Gateway.Nodes.Artifacts.Enabled = true
|
||||
cfg.Gateway.Nodes.Artifacts.KeepLatest = 10
|
||||
cfg.Gateway.Nodes.Artifacts.RetainDays = 1
|
||||
cfg.Gateway.Nodes.Artifacts.PruneOnRead = true
|
||||
cfgPath := filepath.Join(workspace, "config.json")
|
||||
if err := cfgpkg.SaveConfig(cfgPath, cfg); err != nil {
|
||||
t.Fatalf("save config: %v", err)
|
||||
}
|
||||
srv.SetConfigPath(cfgPath)
|
||||
oldTime := time.Now().UTC().Add(-48 * time.Hour).Format(time.RFC3339)
|
||||
newTime := time.Now().UTC().Add(-2 * time.Hour).Format(time.RFC3339)
|
||||
auditLines := strings.Join([]string{
|
||||
fmt.Sprintf("{\"time\":%q,\"node\":\"edge-a\",\"action\":\"screen_snapshot\",\"ok\":true,\"artifacts\":[{\"name\":\"old.txt\",\"kind\":\"text\",\"mime_type\":\"text/plain\",\"content_base64\":\"b2xk\"}]}", oldTime),
|
||||
fmt.Sprintf("{\"time\":%q,\"node\":\"edge-a\",\"action\":\"screen_snapshot\",\"ok\":true,\"artifacts\":[{\"name\":\"fresh.txt\",\"kind\":\"text\",\"mime_type\":\"text/plain\",\"content_base64\":\"ZnJlc2g=\"}]}", newTime),
|
||||
}, "\n") + "\n"
|
||||
if err := os.WriteFile(filepath.Join(workspace, "memory", "nodes-dispatch-audit.jsonl"), []byte(auditLines), 0o644); err != nil {
|
||||
t.Fatalf("write audit: %v", err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/webui/api/node_artifacts", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
srv.handleWebUINodeArtifacts(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
items := srv.webUINodeArtifactsPayload(10)
|
||||
if len(items) != 1 {
|
||||
t.Fatalf("expected retention days to keep 1 artifact, got %d", len(items))
|
||||
}
|
||||
if got := fmt.Sprint(items[0]["name"]); got != "fresh.txt" {
|
||||
t.Fatalf("expected fresh artifact to remain, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user