package server import ( "context" "encoding/json" "strings" "testing" ) func TestToolSpawnTaskUnmarshalsInput(t *testing.T) { s := &Server{m: &Manager{tasks: map[string]*Task{}}} tool := s.toolSpawnTask() t.Run("missing goal rejected", func(t *testing.T) { res, err := tool.Call(context.Background(), json.RawMessage(`{"description":"端口探测"}`), nil) if err != nil { t.Fatalf("unexpected error: %v", err) } if !res.IsError || !strings.Contains(res.Flatten(), "goal 为必填") { t.Fatalf("expected goal required error, got isError=%v text=%q", res.IsError, res.Flatten()) } }) t.Run("invalid json rejected", func(t *testing.T) { res, err := tool.Call(context.Background(), json.RawMessage(`{invalid`), nil) if err != nil { t.Fatalf("unexpected error: %v", err) } if !res.IsError || !strings.Contains(res.Flatten(), "参数无效") { t.Fatalf("expected invalid json error, got isError=%v text=%q", res.IsError, res.Flatten()) } }) t.Run("valid goal parsed past required check", func(t *testing.T) { res, err := tool.Call(context.Background(), json.RawMessage(`{ "description": "47.116.200.74 端口探测", "goal": "仅对 47.116.200.74 做端口探测", "source_task_ids": ["99999"] }`), nil) if err != nil { t.Fatalf("unexpected error: %v", err) } if !res.IsError || !strings.Contains(res.Flatten(), "关联任务 #99999 不存在") { t.Fatalf("expected source_task_ids check after parsing goal, got isError=%v text=%q", res.IsError, res.Flatten()) } }) }