merge origin main updates

This commit is contained in:
LPF
2026-05-10 17:43:06 +08:00
58 changed files with 7574 additions and 1192 deletions

View File

@@ -2,8 +2,11 @@ package tools
import (
"context"
"encoding/json"
"runtime"
"strings"
"testing"
"time"
"github.com/YspCoder/clawgo/pkg/bus"
)
@@ -66,3 +69,77 @@ func TestProcessToolParsesStringIntegers(t *testing.T) {
t.Fatalf("expected json list output, got %s", out)
}
}
func TestProcessToolWatchPatternsMatchesLog(t *testing.T) {
t.Parallel()
pm := NewProcessManager(t.TempDir())
id, err := pm.Start(context.Background(), readyCommand(), "")
if err != nil {
t.Fatalf("start failed: %v", err)
}
tool := NewProcessTool(pm)
out, err := tool.Execute(context.Background(), map[string]interface{}{
"action": "watch_patterns",
"session_id": id,
"patterns": []interface{}{"ready"},
"timeout_ms": 2000,
"interval_ms": 50,
})
if err != nil {
t.Fatalf("execute failed: %v", err)
}
var payload map[string]interface{}
if err := json.Unmarshal([]byte(out), &payload); err != nil {
t.Fatalf("invalid json output: %v (%s)", err, out)
}
if matched, _ := payload["matched"].(bool); !matched {
t.Fatalf("expected matched response, got %v", payload)
}
}
func TestProcessToolWatchPatternsTimesOut(t *testing.T) {
t.Parallel()
pm := NewProcessManager(t.TempDir())
id, err := pm.Start(context.Background(), sleepCommand(), "")
if err != nil {
t.Fatalf("start failed: %v", err)
}
tool := NewProcessTool(pm)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
out, err := tool.Execute(ctx, map[string]interface{}{
"action": "watch_patterns",
"session_id": id,
"patterns": "nomatch",
"timeout_ms": "120",
"interval_ms": "30",
})
if err != nil {
t.Fatalf("execute failed: %v", err)
}
var payload map[string]interface{}
if err := json.Unmarshal([]byte(out), &payload); err != nil {
t.Fatalf("invalid json output: %v (%s)", err, out)
}
if timedOut, _ := payload["timed_out"].(bool); !timedOut {
t.Fatalf("expected timed_out=true, got %v", payload)
}
}
func readyCommand() string {
if runtime.GOOS == "windows" {
return "echo READY & ping -n 2 127.0.0.1 > nul"
}
return "printf 'READY\\n'; sleep 0.05"
}
func sleepCommand() string {
if runtime.GOOS == "windows" {
return "ping -n 2 127.0.0.1 > nul"
}
return "sleep 0.3"
}