package collect import ( "context" "geo/internal/collect" "geo/internal/config" "testing" "time" "github.com/gofiber/fiber/v2/log" ) var ( doubaoCfg, _ = config.LoadConfig() doubaoManager = collect.NewCollectManager(context.Background(), doubaoCfg, log.DefaultLogger()) ) // TestDoubaoCollector_WaitLogin 测试豆包登录功能 func TestDoubaoCollector_WaitLogin(t *testing.T) { if testing.Short() { t.Skip("跳过需要浏览器交互的测试") } params := &collect.CollectParams{ Headless: false, // 显示浏览器窗口以便扫码登录 RequestID: "test_doubao_login_001", Platform: "doubao", } t.Log("开始测试豆包登录...") t.Log("请在打开的浏览器窗口中完成豆包账号登录(扫码或输入账号密码)") success, msg := doubaoManager.WaitLogin("doubao", params) if !success { t.Errorf("豆包登录失败: %s", msg) return } t.Logf("豆包登录成功: %s", msg) t.Log("Cookie已保存,后续测试可以使用已登录状态") } // TestDoubaoCollector_AskQuestion 测试豆包提问功能 // 注意:此测试需要有效的登录状态 func TestDoubaoCollector_AskQuestion(t *testing.T) { if testing.Short() { t.Skip("跳过需要浏览器交互的测试") } // 设置收集参数 params := &collect.CollectParams{ Headless: false, // 显示浏览器以便调试 RequestID: "test_doubao_001", Platform: "doubao", } // 定义提问内容 question := "今天天气怎么样" t.Logf("向豆包提问: %s", question) // 调用管理器提问并获取答案 result, err := doubaoManager.AskQuestion("doubao", params, question) if err != nil { t.Errorf("提问失败: %v", err) return } t.Logf("获取到答案:\n%s", result.Answer) // 验证答案非空 if len(result.Answer) == 0 { t.Error("答案为空") } } // TestDoubaoCollector_MultipleQuestions 测试豆包多次提问 func TestDoubaoCollector_MultipleQuestions(t *testing.T) { if testing.Short() { t.Skip("跳过需要浏览器交互的测试") } params := &collect.CollectParams{ Headless: false, RequestID: "test_doubao_multi_001", Platform: "doubao", } questions := []string{ "什么是人工智能?", "Python和Go的区别是什么?", "如何学习编程?", "中国有哪些著名的旅游景点?", "健康饮食的基本原则是什么?", "区块链技术的主要应用场景有哪些?", "如何提高英语口语能力?", "云计算的优势和劣势分别是什么?", "环境保护的重要性体现在哪些方面?", "未来十年最有前景的行业有哪些?", } for i, question := range questions { t.Logf("========== 第 %d/%d 个问题 ==========", i+1, len(questions)) t.Logf("问题: %s", question) result, err := doubaoManager.AskQuestion("doubao", params, question) if err != nil { t.Errorf("第 %d 个问题提问失败: %v", i+1, err) continue } t.Logf("✓ 第 %d 个问题的答案长度: %d 字符", i+1, len(result.Answer)) t.Logf("答案预览: %s...", result.Answer[:min(100, len(result.Answer))]) // 每个问题之间等待一下 if i < len(questions)-1 { t.Log("等待3秒后继续下一个问题...") time.Sleep(3 * time.Second) } } t.Logf("========== 测试完成 ==========") t.Logf("成功回答了 %d 个问题", len(questions)) } // TestDoubaoCollector_SpeedTest 测试优化后的速度 func TestDoubaoCollector_SpeedTest(t *testing.T) { if testing.Short() { t.Skip("跳过需要浏览器交互的测试") } params := &collect.CollectParams{ Headless: false, RequestID: "test_doubao_speed", Platform: "doubao", } question := "1+1等于几?" startTime := time.Now() t.Logf("开始提问: %s", question) result, err := doubaoManager.AskQuestion("doubao", params, question) if err != nil { t.Fatalf("提问失败: %v", err) } elapsed := time.Since(startTime) t.Logf("✓ 完成时间: %v", elapsed) t.Logf("答案长度: %d 字符", len(result.Answer)) t.Logf("分享链接: %s", result.ShareLink) t.Logf("答案预览: %s...", result.Answer[:min(100, len(result.Answer))]) // 如果超过60秒,说明还有优化空间 if elapsed > 60*time.Second { t.Logf("⚠️ 警告: 耗时较长 (%v),可能需要进一步优化", elapsed) } else { t.Logf("✅ 速度正常") } } // min 辅助函数 func min(a, b int) int { if a < b { return a } return b }