| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- package main
-
- import (
- "bufio"
- "bytes"
- "encoding/json"
- "fmt"
- "io"
- "net/http"
- "testing"
- "time"
- )
-
- // TestConversationFlow 测试完整的对话流程(模拟前端行为)
- func TestConversationFlow(t *testing.T) {
- // 获取svc-code服务地址
- svcCodeURL := "http://localhost:8020"
- configureURL := "http://localhost:8080"
-
- // 检查服务是否运行
- if !isServiceRunning2(t, svcCodeURL) {
- t.Skipf("svc-code服务未运行在 %s,跳过测试", svcCodeURL)
- }
- if !isServiceRunning2(t, configureURL) {
- t.Skipf("配置中心服务未运行在 %s,跳过测试", configureURL)
- }
-
- // 1. 用户登录获取token
- token, err := loginAndGetToken(t, svcCodeURL, "admin", "admin123")
- if err != nil {
- t.Fatalf("登录失败: %v", err)
- }
- t.Logf("获取到Token: %s... (长度: %d)", token[:20], len(token))
-
- // 2. 创建会话
- sessionID, err := createSession(t, svcCodeURL, token)
- if err != nil {
- t.Fatalf("创建会话失败: %v", err)
- }
- t.Logf("创建会话成功: %s", sessionID)
-
- // 3. 测试流式对话(模拟前端fetch请求)
- t.Run("StreamConversation", func(t *testing.T) {
- testStreamConversation(t, svcCodeURL, token, sessionID)
- })
-
- // 4. 测试同步对话
- t.Run("SyncConversation", func(t *testing.T) {
- testSyncConversation(t, svcCodeURL, token, sessionID)
- })
- }
-
- // loginAndGetToken 登录并获取token
- func loginAndGetToken(t *testing.T, svcCodeURL, username, password string) (string, error) {
- loginURL := svcCodeURL + "/api/auth/login"
- loginData := map[string]string{
- "user_id": username,
- "password": password,
- }
-
- jsonData, err := json.Marshal(loginData)
- if err != nil {
- return "", fmt.Errorf("编码登录数据失败: %w", err)
- }
-
- resp, err := http.Post(loginURL, "application/json", bytes.NewBuffer(jsonData))
- if err != nil {
- return "", fmt.Errorf("登录请求失败: %w", err)
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != http.StatusOK {
- body, _ := io.ReadAll(resp.Body)
- return "", fmt.Errorf("登录失败 (状态码 %d): %s", resp.StatusCode, body)
- }
-
- var result map[string]interface{}
- if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
- return "", fmt.Errorf("解析登录响应失败: %w", err)
- }
-
- if success, ok := result["success"].(bool); !ok || !success {
- return "", fmt.Errorf("登录失败: %v", result)
- }
-
- token, ok := result["data"].(string)
- if !ok || token == "" {
- return "", fmt.Errorf("token无效: %v", result)
- }
-
- return token, nil
- }
-
- // createSession 创建新会话
- func createSession(t *testing.T, svcCodeURL, token string) (string, error) {
- sessionURL := svcCodeURL + "/api/session/create"
- sessionData := map[string]interface{}{
- "name": "测试会话",
- "description": "对话流程测试",
- }
-
- jsonData, err := json.Marshal(sessionData)
- if err != nil {
- return "", fmt.Errorf("编码会话数据失败: %w", err)
- }
-
- req, err := http.NewRequest("POST", sessionURL, bytes.NewBuffer(jsonData))
- if err != nil {
- return "", fmt.Errorf("创建请求失败: %w", err)
- }
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("Authorization", "Bearer "+token)
-
- client := &http.Client{Timeout: 10 * time.Second}
- resp, err := client.Do(req)
- if err != nil {
- return "", fmt.Errorf("发送请求失败: %w", err)
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != http.StatusOK {
- body, _ := io.ReadAll(resp.Body)
- return "", fmt.Errorf("创建会话失败 (状态码 %d): %s", resp.StatusCode, body)
- }
-
- var result map[string]interface{}
- if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
- return "", fmt.Errorf("解析响应失败: %w", err)
- }
-
- if success, ok := result["success"].(bool); !ok || !success {
- return "", fmt.Errorf("创建会话失败: %v", result)
- }
-
- data, ok := result["data"].(map[string]interface{})
- if !ok {
- return "", fmt.Errorf("响应数据格式错误: %v", result)
- }
-
- sessionID, ok := data["id"].(string)
- if !ok {
- return "", fmt.Errorf("会话ID无效: %v", data)
- }
-
- return sessionID, nil
- }
-
- // testStreamConversation 测试流式对话
- func testStreamConversation(t *testing.T, svcCodeURL, token, sessionID string) {
- streamURL := svcCodeURL + "/api/prompt/stream"
-
- requestData := map[string]interface{}{
- "sessionID": sessionID,
- "parts": []map[string]interface{}{
- {
- "type": "text",
- "text": "你好,请简单介绍你自己",
- },
- },
- }
-
- jsonData, err := json.Marshal(requestData)
- if err != nil {
- t.Fatalf("编码请求数据失败: %v", err)
- }
-
- req, err := http.NewRequest("POST", streamURL, bytes.NewBuffer(jsonData))
- if err != nil {
- t.Fatalf("创建请求失败: %v", err)
- }
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("Authorization", "Bearer "+token)
- req.Header.Set("Accept", "text/event-stream")
-
- client := &http.Client{Timeout: 30 * time.Second}
- resp, err := client.Do(req)
- if err != nil {
- t.Fatalf("发送请求失败: %v", err)
- }
- defer resp.Body.Close()
-
- t.Logf("流式响应状态: %d %s", resp.StatusCode, resp.Status)
- t.Logf("Content-Type: %s", resp.Header.Get("content-type"))
-
- if resp.StatusCode != http.StatusOK {
- body, _ := io.ReadAll(resp.Body)
- t.Fatalf("请求失败 (状态码 %d): %s", resp.StatusCode, body)
- }
-
- // 解析SSE响应
- reader := bufio.NewReader(resp.Body)
- eventCount := 0
-
- for {
- line, err := reader.ReadString('\n')
- if err != nil {
- if err == io.EOF {
- t.Log("SSE流结束")
- break
- }
- t.Fatalf("读取响应失败: %v", err)
- }
-
- line = line[:len(line)-1] // 移除换行符
-
- if line == "" {
- // 空行表示事件结束
- continue
- }
-
- if len(line) > 6 && line[:6] == "data: " {
- data := line[6:]
- t.Logf("收到SSE数据: %s", data)
- eventCount++
-
- if data == "[DONE]" {
- t.Log("收到DONE标记")
- break
- }
- }
- }
-
- if eventCount == 0 {
- t.Error("未收到任何SSE事件")
- } else {
- t.Logf("总共收到 %d 个SSE事件", eventCount)
- }
- }
-
- // testSyncConversation 测试同步对话
- func testSyncConversation(t *testing.T, svcCodeURL, token, sessionID string) {
- syncURL := svcCodeURL + "/api/prompt/sync"
-
- requestData := map[string]interface{}{
- "sessionID": sessionID,
- "parts": []map[string]interface{}{
- {
- "type": "text",
- "text": "这是一个测试消息",
- },
- },
- }
-
- jsonData, err := json.Marshal(requestData)
- if err != nil {
- t.Fatalf("编码请求数据失败: %v", err)
- }
-
- req, err := http.NewRequest("POST", syncURL, bytes.NewBuffer(jsonData))
- if err != nil {
- t.Fatalf("创建请求失败: %v", err)
- }
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("Authorization", "Bearer "+token)
-
- client := &http.Client{Timeout: 30 * time.Second}
- resp, err := client.Do(req)
- if err != nil {
- t.Fatalf("发送请求失败: %v", err)
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != http.StatusOK {
- body, _ := io.ReadAll(resp.Body)
- t.Fatalf("请求失败 (状态码 %d): %s", resp.StatusCode, body)
- }
-
- var result map[string]interface{}
- if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
- t.Fatalf("解析响应失败: %v", err)
- }
-
- t.Logf("同步响应: %v", result)
-
- if success, ok := result["success"].(bool); !ok || !success {
- t.Errorf("同步对话失败: %v", result)
- }
- }
-
- // isServiceRunning2 检查服务是否运行
- func isServiceRunning2(t *testing.T, url string) bool {
- client := &http.Client{Timeout: 2 * time.Second}
- resp, err := client.Get(url + "/api/health")
- if err != nil {
- return false
- }
- resp.Body.Close()
- return resp.StatusCode == http.StatusOK
- }
|