108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
package collect
|
||
|
||
import (
|
||
"context"
|
||
"geo/internal/collect"
|
||
"geo/internal/config"
|
||
"github.com/gofiber/fiber/v2/log"
|
||
"testing"
|
||
)
|
||
|
||
var (
|
||
cfg, _ = config.LoadConfig()
|
||
|
||
manager = collect.NewCollectManager(context.Background(), cfg, log.DefaultLogger())
|
||
)
|
||
|
||
// TestCollectManager_Basic 测试收集管理器的基本功能
|
||
func TestCollectManager_Basic(t *testing.T) {
|
||
|
||
// 测试列出平台
|
||
platforms := manager.ListPlatforms()
|
||
t.Logf("支持的平台: %v", platforms)
|
||
|
||
if len(platforms) != 4 {
|
||
t.Errorf("期望4个平台,实际: %d", len(platforms))
|
||
}
|
||
// 测试获取收集器
|
||
for _, platform := range platforms {
|
||
params := &collect.CollectParams{
|
||
Headless: true,
|
||
RequestID: "test_req",
|
||
Platform: platform,
|
||
}
|
||
|
||
collector, err := manager.GetCollector(platform, params)
|
||
if err != nil {
|
||
t.Errorf("获取%s收集器失败: %v", platform, err)
|
||
continue
|
||
}
|
||
|
||
if collector == nil {
|
||
t.Errorf("%s收集器为nil", platform)
|
||
}
|
||
|
||
t.Logf("成功创建%s收集器", platform)
|
||
}
|
||
}
|
||
|
||
// TestWenxinCollector_WaitLogin 测试文心一言登录功能
|
||
func TestWenxinCollector_WaitLogin(t *testing.T) {
|
||
if testing.Short() {
|
||
t.Skip("跳过需要浏览器交互的测试")
|
||
}
|
||
|
||
params := &collect.CollectParams{
|
||
Headless: false, // 显示浏览器窗口以便扫码登录
|
||
RequestID: "test_wenxin_login_001",
|
||
Platform: "wenxin",
|
||
}
|
||
|
||
t.Log("开始测试文心一言登录...")
|
||
t.Log("请在打开的浏览器窗口中完成百度账号登录(扫码或输入账号密码)")
|
||
|
||
success, msg := manager.WaitLogin("wenxin", params)
|
||
|
||
if !success {
|
||
t.Errorf("文心一言登录失败: %s", msg)
|
||
return
|
||
}
|
||
|
||
t.Logf("文心一言登录成功: %s", msg)
|
||
t.Log("Cookie已保存,后续测试可以使用已登录状态")
|
||
}
|
||
|
||
// TestWenxinCollector_AskQuestion 测试文心一言提问功能
|
||
// 注意:此测试需要有效的登录状态
|
||
func TestWenxinCollector_AskQuestion(t *testing.T) {
|
||
if testing.Short() {
|
||
t.Skip("跳过需要浏览器交互的测试")
|
||
}
|
||
|
||
// 设置收集参数
|
||
params := &collect.CollectParams{
|
||
Headless: true, // 显示浏览器以便调试
|
||
RequestID: "test_wenxin_001",
|
||
Platform: "wenxin",
|
||
}
|
||
|
||
// 定义提问内容
|
||
question := "四川房地产软件排名"
|
||
t.Logf("向文心一言提问: %s", question)
|
||
|
||
// 调用管理器提问并获取答案
|
||
result, err := manager.AskQuestion("wenxin", params, question)
|
||
if err != nil {
|
||
t.Errorf("提问失败: %v", err)
|
||
return
|
||
}
|
||
|
||
t.Logf("获取到答案:\n%s", result.Answer)
|
||
t.Logf("分享链接: %s", result.ShareLink)
|
||
|
||
// 验证答案非空
|
||
if len(result.Answer) == 0 {
|
||
t.Error("答案为空")
|
||
}
|
||
}
|