geoGo/yuanbao_test.go

143 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package collect
import (
"context"
"testing"
"time"
"geo/internal/collect"
"geo/internal/config"
"github.com/gofiber/fiber/v2/log"
)
var (
yuanbaoCfg, _ = config.LoadConfig()
yuanbaoManager = collect.NewCollectManager(context.Background(), yuanbaoCfg, log.DefaultLogger())
)
// TestYuanbaoCollector_WaitLogin 测试元宝登录功能
func TestYuanbaoCollector_WaitLogin(t *testing.T) {
if testing.Short() {
t.Skip("跳过需要浏览器交互的测试")
}
params := &collect.CollectParams{
Platform: "yuanbao",
Headless: false, // 登录测试需要显示浏览器
RequestID: "test_yuanbao_login_001",
}
success, msg := yuanbaoManager.WaitLogin("yuanbao", params)
if !success {
t.Errorf("元宝登录失败: %s", msg)
} else {
t.Logf("元宝登录成功: %s", msg)
}
}
// TestYuanbaoCollector_AskQuestion 测试元宝单次提问功能
func TestYuanbaoCollector_AskQuestion(t *testing.T) {
if testing.Short() {
t.Skip("跳过需要浏览器交互的测试")
}
params := &collect.CollectParams{
Platform: "yuanbao",
Headless: false, // 提问测试可以使用无头模式
RequestID: "test_yuanbao_ask_001",
}
question := "你好,请介绍一下你自己。"
result, err := yuanbaoManager.AskQuestion("yuanbao", params, question)
if err != nil {
t.Fatalf("元宝提问失败: %v", err)
}
if result.Answer == "" {
t.Error("元宝返回的答案为空")
} else {
t.Logf("元宝回答长度: %d 字符", len(result.Answer))
// 输出前200个字符作为预览
previewLen := min(len(result.Answer), 200)
t.Logf("元宝回答预览: %s...", result.Answer[:previewLen])
}
if result.ShareLink != "" {
t.Logf("元宝分享链接: %s", result.ShareLink)
}
}
// TestYuanbaoCollector_MultipleQuestions 测试元宝多次提问功能
func TestYuanbaoCollector_MultipleQuestions(t *testing.T) {
if testing.Short() {
t.Skip("跳过需要浏览器交互的测试")
}
params := &collect.CollectParams{
Platform: "yuanbao",
Headless: true,
RequestID: "test_yuanbao_multi_001",
}
questions := []string{
"Python中如何定义一个函数",
"Go语言的特点是什么",
"解释一下机器学习的基本概念。",
}
for i, question := range questions {
t.Logf("第%d次提问: %s", i+1, question)
result, err := yuanbaoManager.AskQuestion("yuanbao", params, question)
if err != nil {
t.Errorf("第%d次提问失败: %v", i+1, err)
continue
}
if result.Answer == "" {
t.Errorf("第%d次提问返回的答案为空", i+1)
} else {
t.Logf("第%d次回答长度: %d 字符", i+1, len(result.Answer))
// 输出前150个字符作为预览
previewLen := min(len(result.Answer), 150)
t.Logf("第%d次回答预览: %s...", i+1, result.Answer[:previewLen])
}
// 每次提问之间间隔2秒避免过于频繁
time.Sleep(2 * time.Second)
}
}
// TestYuanbaoCollector_SpeedTest 测试元宝响应速度
func TestYuanbaoCollector_SpeedTest(t *testing.T) {
if testing.Short() {
t.Skip("跳过需要浏览器交互的测试")
}
params := &collect.CollectParams{
Platform: "yuanbao",
Headless: true,
RequestID: "test_yuanbao_speed_001",
}
question := "1+1等于多少"
startTime := time.Now()
result, err := yuanbaoManager.AskQuestion("yuanbao", params, question)
duration := time.Since(startTime)
if err != nil {
t.Fatalf("元宝速度测试失败: %v", err)
}
if result.Answer == "" {
t.Error("元宝速度测试返回的答案为空")
} else {
t.Logf("元宝响应时间: %v, 回答长度: %d 字符", duration, len(result.Answer))
previewLen := min(len(result.Answer), 100)
t.Logf("回答预览: %s...", result.Answer[:previewLen])
}
}