geoGo/qianwen_test.go

143 lines
3.6 KiB
Go
Raw Permalink 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 (
qianwenCfg, _ = config.LoadConfig()
qianwenManager = collect.NewCollectManager(context.Background(), qianwenCfg, log.DefaultLogger())
)
// TestQianwenCollector_WaitLogin 测试千问登录功能
func TestQianwenCollector_WaitLogin(t *testing.T) {
if testing.Short() {
t.Skip("跳过需要浏览器交互的测试")
}
params := &collect.CollectParams{
Platform: "qianwen",
Headless: false, // 登录测试需要显示浏览器
RequestID: "test_qianwen_login_001",
}
success, msg := qianwenManager.WaitLogin("qianwen", params)
if !success {
t.Errorf("千问登录失败: %s", msg)
} else {
t.Logf("千问登录成功: %s", msg)
}
}
// TestQianwenCollector_AskQuestion 测试千问单次提问功能
func TestQianwenCollector_AskQuestion(t *testing.T) {
if testing.Short() {
t.Skip("跳过需要浏览器交互的测试")
}
params := &collect.CollectParams{
Platform: "qianwen",
Headless: false, // 提问测试可以使用无头模式
RequestID: "test_qianwen_ask_001",
}
question := "为什么你会说你是扎着丸子头"
result, err := qianwenManager.AskQuestion("qianwen", 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)
}
}
// TestQianwenCollector_MultipleQuestions 测试千问多次提问功能
func TestQianwenCollector_MultipleQuestions(t *testing.T) {
if testing.Short() {
t.Skip("跳过需要浏览器交互的测试")
}
params := &collect.CollectParams{
Platform: "qianwen",
Headless: true,
RequestID: "test_qianwen_multi_001",
}
questions := []string{
"Python中如何定义一个函数",
"Go语言的特点是什么",
"解释一下机器学习的基本概念。",
}
for i, question := range questions {
t.Logf("第%d次提问: %s", i+1, question)
result, err := qianwenManager.AskQuestion("qianwen", 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)
}
}
// TestQianwenCollector_SpeedTest 测试千问响应速度
func TestQianwenCollector_SpeedTest(t *testing.T) {
if testing.Short() {
t.Skip("跳过需要浏览器交互的测试")
}
params := &collect.CollectParams{
Platform: "qianwen",
Headless: true,
RequestID: "test_qianwen_speed_001",
}
question := "1+1等于多少"
startTime := time.Now()
result, err := qianwenManager.AskQuestion("qianwen", 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])
}
}