可靠性与数据一致性:为 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 包通过,失败现状随本次既有工作区一并提交。
165 lines
5.0 KiB
Go
165 lines
5.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// loginHelper 登录并返回会话 cookie。
|
|
func loginHelper(t *testing.T, h http.Handler) *http.Cookie {
|
|
return loginAsHelper(t, h, "admin", "pw")
|
|
}
|
|
|
|
func loginAsHelper(t *testing.T, h http.Handler, username, password string) *http.Cookie {
|
|
t.Helper()
|
|
body, err := json.Marshal(map[string]string{"username": username, "password": password})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader(string(body)))
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK || len(rec.Result().Cookies()) == 0 {
|
|
t.Fatalf("login %q status=%d body=%s", username, rec.Code, rec.Body.String())
|
|
}
|
|
return rec.Result().Cookies()[0]
|
|
}
|
|
|
|
// TestOrderListAndDetail 订单列表 + 详情 + 状态码归一。
|
|
func TestOrderListAndDetail(t *testing.T) {
|
|
srv, store, cleanup := newTestServer(t)
|
|
defer cleanup()
|
|
ctx := context.Background()
|
|
// 插入一条订单(order_status 用数字码 "2" 测试归一)。
|
|
store.DB.ExecContext(ctx, `INSERT INTO orders (order_id, item_id, buyer_id, order_status, cookie_id) VALUES ('ord1','item1','buyer1','2','acc1')`)
|
|
store.DB.ExecContext(ctx, `INSERT INTO item_info (cookie_id, item_id, item_title, item_detail) VALUES ('acc1','item1','测试商品','{"pic_info":{"picUrl":"https://img.example/item.png"}}')`)
|
|
|
|
h := srv.Router()
|
|
cookie := loginHelper(t, h)
|
|
|
|
// 列表。
|
|
req := httptest.NewRequest(http.MethodGet, "/api/orders", nil)
|
|
req.AddCookie(cookie)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("list status=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
var resp struct {
|
|
Success bool `json:"success"`
|
|
Total int `json:"total"`
|
|
Data []map[string]any `json:"data"`
|
|
}
|
|
json.Unmarshal(rec.Body.Bytes(), &resp)
|
|
if !resp.Success || resp.Total != 1 {
|
|
t.Fatalf("订单列表异常: %+v", resp)
|
|
}
|
|
if resp.Data[0]["order_status"] != "pending_ship" {
|
|
t.Errorf("状态归一: got %v want pending_ship", resp.Data[0]["order_status"])
|
|
}
|
|
if resp.Data[0]["item_title"] != "测试商品" {
|
|
t.Errorf("item_title: %v", resp.Data[0]["item_title"])
|
|
}
|
|
if resp.Data[0]["item_image"] != "https://img.example/item.png" {
|
|
t.Errorf("item_image: %v", resp.Data[0]["item_image"])
|
|
}
|
|
|
|
// 详情。
|
|
req2 := httptest.NewRequest(http.MethodGet, "/api/orders/ord1", nil)
|
|
req2.AddCookie(cookie)
|
|
rec2 := httptest.NewRecorder()
|
|
h.ServeHTTP(rec2, req2)
|
|
if rec2.Code != 200 {
|
|
t.Fatalf("detail status=%d", rec2.Code)
|
|
}
|
|
var det map[string]any
|
|
json.Unmarshal(rec2.Body.Bytes(), &det)
|
|
if det["order_id"] != "ord1" {
|
|
t.Errorf("详情异常: %+v", det)
|
|
}
|
|
if det["item_image"] != "https://img.example/item.png" {
|
|
t.Errorf("详情 item_image: %v", det["item_image"])
|
|
}
|
|
|
|
// 删除。
|
|
req3 := httptest.NewRequest(http.MethodDelete, "/api/orders/ord1", nil)
|
|
req3.AddCookie(cookie)
|
|
rec3 := httptest.NewRecorder()
|
|
h.ServeHTTP(rec3, req3)
|
|
if rec3.Code != 200 {
|
|
t.Fatalf("delete status=%d", rec3.Code)
|
|
}
|
|
}
|
|
|
|
// TestCardCRUD 卡券增删改查。
|
|
func TestCardCRUD(t *testing.T) {
|
|
srv, _, cleanup := newTestServer(t)
|
|
defer cleanup()
|
|
h := srv.Router()
|
|
cookie := loginHelper(t, h)
|
|
|
|
// 创建。
|
|
body := `{"name":"测试卡","type":"text","text_content":"卡密ABC","enabled":true}`
|
|
req := httptest.NewRequest(http.MethodPost, "/cards", strings.NewReader(body))
|
|
req.AddCookie(cookie)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("create status=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
var cr map[string]any
|
|
json.Unmarshal(rec.Body.Bytes(), &cr)
|
|
id := cr["id"].(float64)
|
|
if id == 0 {
|
|
t.Fatal("应返回 id")
|
|
}
|
|
|
|
// 列表。
|
|
req2 := httptest.NewRequest(http.MethodGet, "/cards", nil)
|
|
req2.AddCookie(cookie)
|
|
rec2 := httptest.NewRecorder()
|
|
h.ServeHTTP(rec2, req2)
|
|
var arr []map[string]any
|
|
json.Unmarshal(rec2.Body.Bytes(), &arr)
|
|
if len(arr) != 1 || arr[0]["name"] != "测试卡" {
|
|
t.Fatalf("列表异常: %+v", arr)
|
|
}
|
|
|
|
// 更新。
|
|
updBody := `{"name":"改名卡","type":"text","text_content":"卡密XYZ","enabled":true}`
|
|
req3 := httptest.NewRequest(http.MethodPut, "/cards/"+itoa(int64(id)), strings.NewReader(updBody))
|
|
req3.AddCookie(cookie)
|
|
rec3 := httptest.NewRecorder()
|
|
h.ServeHTTP(rec3, req3)
|
|
if rec3.Code != 200 {
|
|
t.Fatalf("update status=%d", rec3.Code)
|
|
}
|
|
|
|
// 获取验证改名。
|
|
req4 := httptest.NewRequest(http.MethodGet, "/cards/"+itoa(int64(id)), nil)
|
|
req4.AddCookie(cookie)
|
|
rec4 := httptest.NewRecorder()
|
|
h.ServeHTTP(rec4, req4)
|
|
var got map[string]any
|
|
json.Unmarshal(rec4.Body.Bytes(), &got)
|
|
if got["name"] != "改名卡" {
|
|
t.Errorf("改名后应=改名卡, got %v", got["name"])
|
|
}
|
|
|
|
// 删除。
|
|
req5 := httptest.NewRequest(http.MethodDelete, "/cards/"+itoa(int64(id)), nil)
|
|
req5.AddCookie(cookie)
|
|
rec5 := httptest.NewRecorder()
|
|
h.ServeHTTP(rec5, req5)
|
|
if rec5.Code != 200 {
|
|
t.Fatalf("delete status=%d", rec5.Code)
|
|
}
|
|
}
|
|
|
|
func itoa(n int64) string { return strconv.FormatInt(n, 10) }
|