可靠性与数据一致性:为 SQLite、MySQL、PostgreSQL 增加 00017 至 00021 迁移,引入账号暂停时间、默认回复和批量铺货租约、自动化运行恢复、延迟任务、动作检查点与通知 outbox。补强订单、卡密发货、Cookie、批量铺货和自动化任务的原子认领、幂等更新、并发控制、失败分类、重试与进程重启恢复,并扩展跨方言覆盖。 登录与风控恢复:完善扫码、密码登录、Cookie 续期、MTOP token、UA 与 WebSocket 生命周期;加入账号级持久化浏览器目录、Playwright 主引擎和直接 Chromium/CDP 备用引擎、严格的新 x5sec 成功判定、失败控件恢复及真实浏览器集成测试。增加可配置远程滑块服务,在明确失败、链接过期和服务不可用场景中保持既定的本地回退语义。 安全与配置:通过 XIANYU_DATA_KEY 为 Cookie、登录密码、设备与访问令牌、AI/SMTP 密钥及通知配置提供 AES-256-GCM 落库加密和历史明文升级。新增出站网络目标分级、防 DNS rebinding、登录限流、HTTP 客户端超时与 SMTP 目标校验;管理员配置的 AI 服务允许可信内网地址,非可信 Webhook 和远程资源继续限制为公网目标。 自动化与通知:为发货、评价赠送、邀评和回复动作增加断点、租约、延迟执行及人工继续/重试/取消入口;通知改为 outbox 驱动的可恢复投递。同步完善账号运行态、暂停状态、续期冷却、告警审计和异常查询接口。 前端体验:新增风控验证面板、方形二维码、账号运行态合并、暂停状态保护、自动化异常处置、批量铺货状态选择、订单导入结果和最新请求门禁。完善订单搜索与导入、规则编辑、批次恢复、通知与设置页面,并为请求取消、超时、陈旧响应和主要状态转换补充 Vitest 覆盖。 测试与部署:扩展三库功能编排、SQLite 一致性备份、数据卷持久化、浏览器集成镜像、前端类型检查和构建资产分包;功能测试覆盖健康检查、订单、铺货、自动化异常、账号暂停、关键词和系统设置。更新 README 中数据密钥、可信 AI 地址和测试端口说明。 验证结果:go vet ./...、go build ./cmd/server、gofmt 检查、npm run typecheck、npm test(17 个文件、95 个用例)及 npm run build 通过。go test ./... 未完全通过:internal/adapter 仍有 2 个密码续期用例失败,internal/renewal 仍有 4 个浏览器续期调度用例失败;其余 Go 包通过,失败现状随本次既有工作区一并提交。
147 lines
4.9 KiB
Go
147 lines
4.9 KiB
Go
package browser
|
||
|
||
import (
|
||
"math"
|
||
"testing"
|
||
"time"
|
||
)
|
||
|
||
func TestGenerateTrajectoryShape(t *testing.T) {
|
||
pts := generateTrajectory(200)
|
||
if len(pts) != 6 {
|
||
t.Fatalf("参考三阶段轨迹应生成 6 个高层点,got %d", len(pts))
|
||
}
|
||
// 参考实现明确禁止超调,最后一点必须精确落在目标位置。
|
||
last := pts[len(pts)-1]
|
||
if math.Abs(last.x-200) > 1e-9 {
|
||
t.Fatalf("末端 x 必须精确等于 distance,got %.6f", last.x)
|
||
}
|
||
// 三阶段轨迹不超调,且保持向目标方向移动。
|
||
for i := 1; i < len(pts); i++ {
|
||
if pts[i].x < pts[i-1].x {
|
||
t.Fatalf("轨迹 x 不单调: pts[%d]=%.1f < pts[%d]=%.1f", i, pts[i].x, i-1, pts[i-1].x)
|
||
}
|
||
if pts[i].x > 200 {
|
||
t.Fatalf("主引擎轨迹不应超调: pts[%d]=%.1f", i, pts[i].x)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestGenerateTrajectoryDelay(t *testing.T) {
|
||
pts := generateTrajectory(150)
|
||
var total time.Duration
|
||
for i, pt := range pts {
|
||
total += pt.delay
|
||
if pt.delay <= 0 || pt.delay > 8*time.Millisecond {
|
||
t.Fatalf("delay[%d]=%v 超出合理范围", i, pt.delay)
|
||
}
|
||
if pt.y < -1 || pt.y > 1 {
|
||
t.Fatalf("y[%d]=%.2f 超出合理抖动", i, pt.y)
|
||
}
|
||
}
|
||
if total < 8*time.Millisecond || total > 35*time.Millisecond {
|
||
t.Fatalf("总轨迹时长不合理: %s", total)
|
||
}
|
||
}
|
||
|
||
func TestCompensatedTrajectoryDelayUsesWallClockBudget(t *testing.T) {
|
||
planned := 3 * time.Millisecond
|
||
if got := compensatedTrajectoryDelay(planned, 600*time.Millisecond, 100*time.Millisecond, 5); got != 100*time.Millisecond {
|
||
t.Fatalf("应按剩余墙钟预算补偿,got %s", got)
|
||
}
|
||
if got := compensatedTrajectoryDelay(planned, 100*time.Millisecond, 120*time.Millisecond, 2); got != planned {
|
||
t.Fatalf("达到目标时长后应保留原始微延迟,got %s", got)
|
||
}
|
||
}
|
||
|
||
func TestSliderSelectorsMatchReferencePriority(t *testing.T) {
|
||
if sliderButtonSelectors[0] != "#nc_1_n1z" || sliderTrackSelectors[0] != "#nc_1_n1t" {
|
||
t.Fatalf("滑块精确选择器必须优先: button=%q track=%q", sliderButtonSelectors[0], sliderTrackSelectors[0])
|
||
}
|
||
wantRetry := []string{"#nc_1_refresh1", ".nc_iconfont.btn_refresh", ".errloading"}
|
||
for i, want := range wantRetry {
|
||
if sliderRetrySelectors[i] != want {
|
||
t.Fatalf("重试选择器[%d]=%q want %q", i, sliderRetrySelectors[i], want)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestSliderRetryTextOnlyAcceptsFailurePrompts(t *testing.T) {
|
||
for _, text := range []string{"验证失败,点击框体重试", "刷新验证码", "Retry verification"} {
|
||
if !sliderRetryText(text) {
|
||
t.Fatalf("应识别重试文案 %q", text)
|
||
}
|
||
}
|
||
if sliderRetryText("请按住滑块,拖动到最右边") {
|
||
t.Fatal("初始滑块容器不应被当作重试控件")
|
||
}
|
||
}
|
||
|
||
func TestIsScratchCaptcha(t *testing.T) {
|
||
if !isScratchCaptcha("<div id='scratch-captcha-btn'>") {
|
||
t.Fatal("应识别 scratch-captcha-btn")
|
||
}
|
||
if !isScratchCaptcha("scratch-captcha-slider") {
|
||
t.Fatal("应识别 scratch-captcha-slider")
|
||
}
|
||
if isScratchCaptcha("<div id='nc_1_n1z'>") {
|
||
t.Fatal("普通滑块不应识别为刮刮乐")
|
||
}
|
||
}
|
||
|
||
func TestIsPunishURL(t *testing.T) {
|
||
for _, rawURL := range []string{
|
||
"https://example/punish?x=1", "https://example/?x5step=2",
|
||
"https://example/?action=captcha", "https://example/PURECAPTCHA",
|
||
"https://example/captcha/check",
|
||
} {
|
||
if !isPunishURL(rawURL) {
|
||
t.Fatalf("应识别风控 URL: %s", rawURL)
|
||
}
|
||
}
|
||
if isPunishURL("https://www.goofish.com/item/1") {
|
||
t.Fatal("普通页面不应识别为风控 URL")
|
||
}
|
||
}
|
||
|
||
func TestCaptchaURLExpiredRequiresReferenceErrorPage(t *testing.T) {
|
||
if !captchaURLExpired("<main>抱歉,页面访问出现了问题</main>") {
|
||
t.Fatal("应识别参考项目定义的验证链接过期页")
|
||
}
|
||
if captchaURLExpired("<main>验证码加载中,请稍候</main>") {
|
||
t.Fatal("普通验证码页面不应被当作链接过期")
|
||
}
|
||
}
|
||
|
||
func TestSliderContainerStatesSucceeded(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
states []sliderContainerState
|
||
want bool
|
||
}{
|
||
{name: "container missing", want: true},
|
||
{name: "container hidden", states: []sliderContainerState{{found: true, visibilityKnown: true}}, want: true},
|
||
{name: "container visible", states: []sliderContainerState{{found: true, visible: true, visibilityKnown: true}}, want: false},
|
||
{name: "visibility unknown", states: []sliderContainerState{{found: true}}, want: false},
|
||
{name: "one visible", states: []sliderContainerState{{found: true, visibilityKnown: true}, {found: true, visible: true, visibilityKnown: true}}, want: false},
|
||
}
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
if got := sliderContainerStatesSucceeded(tt.states); got != tt.want {
|
||
t.Fatalf("sliderContainerStatesSucceeded()=%v want %v", got, tt.want)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestTrajectoryPhysics(t *testing.T) {
|
||
pts := generateTrajectory(100)
|
||
half := len(pts) / 2
|
||
frontIncrement := pts[half-1].x - pts[0].x
|
||
backIncrement := pts[len(pts)-1].x - pts[half].x
|
||
// 允许误差:不严格要求加速,但增量应合理。
|
||
if math.IsNaN(frontIncrement) || math.IsNaN(backIncrement) {
|
||
t.Fatal("轨迹 x 为 NaN")
|
||
}
|
||
}
|