You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

142 lines
3.4 KiB

package agents_test
import (
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
)
const (
baseURL = "https://voitrans.ideapsound.com"
appID = "app_346bbeb6dc3bd37c"
appSecret = "sk_RJHRhzpost64qus5SCBlzECbPdMaiG_fcv7eJ-3u"
)
// Test_GetAgents 测试获取智能体列表
func Test_GetAgents(t *testing.T) {
url := fmt.Sprintf("%s/open/v1/agents", baseURL)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Fatalf("创建请求失败: %v", err)
}
req.Header.Set("X-App-Id", appID)
req.Header.Set("X-App-Secret", appSecret)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("请求失败: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("读取响应失败: %v", err)
}
t.Logf("状态码: %d", resp.StatusCode)
if resp.StatusCode != http.StatusOK {
t.Fatalf("状态码异常: %d, body: %s", resp.StatusCode, string(body))
}
var result map[string]interface{}
if err = json.Unmarshal(body, &result); err != nil {
t.Fatalf("解析JSON失败: %v, body: %s", err, string(body))
}
pretty, _ := json.MarshalIndent(result, "", " ")
t.Logf("响应数据:\n%s", string(pretty))
}
// Test_GetAgentUrl 测试获取智能体连接信息
func Test_GetAgentUrl(t *testing.T) {
// 先获取列表拿到一个 agent_id
agentID := getFirstAgentID(t)
if agentID == "" {
t.Fatal("未获取到可用的 agent_id")
}
t.Logf("使用 agent_id: %s", agentID)
url := fmt.Sprintf("%s/open/v1/agents/%s/connect", baseURL, agentID)
req, err := http.NewRequest("POST", url, nil)
if err != nil {
t.Fatalf("创建请求失败: %v", err)
}
req.Header.Set("X-App-Id", appID)
req.Header.Set("X-App-Secret", appSecret)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("请求失败: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("读取响应失败: %v", err)
}
t.Logf("状态码: %d", resp.StatusCode)
if resp.StatusCode != http.StatusOK {
t.Fatalf("状态码异常: %d, body: %s", resp.StatusCode, string(body))
}
var result map[string]interface{}
if err = json.Unmarshal(body, &result); err != nil {
t.Fatalf("解析JSON失败: %v, body: %s", err, string(body))
}
pretty, _ := json.MarshalIndent(result, "", " ")
t.Logf("响应数据:\n%s", string(pretty))
// 校验关键字段
if _, ok := result["connect_url"]; !ok {
t.Error("响应缺少 connect_url 字段")
}
if _, ok := result["token"]; !ok {
t.Error("响应缺少 token 字段")
}
}
// getFirstAgentID 从列表接口获取第一个 agent_id
func getFirstAgentID(t *testing.T) string {
t.Helper()
url := fmt.Sprintf("%s/open/v1/agents", baseURL)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Fatalf("创建请求失败: %v", err)
}
req.Header.Set("X-App-Id", appID)
req.Header.Set("X-App-Secret", appSecret)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("请求失败: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("读取响应失败: %v", err)
}
var wrapped map[string]interface{}
if err = json.Unmarshal(body, &wrapped); err != nil {
t.Fatalf("解析agents列表失败: %v", err)
}
if agents, ok := wrapped["agents"]; ok {
if arr, ok := agents.([]interface{}); ok && len(arr) > 0 {
if item, ok := arr[0].(map[string]interface{}); ok {
if id, ok := item["agent_id"].(string); ok {
return id
}
}
}
}
return ""
}