Files
Ydisks-Xianyu-Helper/internal/server/analytics_handlers_test.go
T
ChristandClaude Opus 4.8 980de41b53 chore: 重构配套改动 + 包文档 + 构建产物清理
automation 重构期间的配套修改:
- browser:订单抓取/密码登录/二维码刷新/stealth 等适配新流程,删 search.go
- db:cookies/orders_ext/extras/sessions/users/password/repository 等调整
- xianyu:mtop client、protocol(sign/decrypt/msgpack/cookies)、qrlogin、ws、ua
- server:admin/analytics/auth/cookie/helpers/item/item_publish_batch handlers
- frontend:App/Dashboard/ItemList/Rules/Sidebar/api/types,删 Keywords/TestPage
- auth、notify、webui、go.mod 等

本次会话补充:
- cmd/server、cmd/init-admin 改为 // Package main 开头的标准包文档
- .gitignore 加 internal/webui/static/assets/(hash 化构建产物不入库)
- internal/webui/static/index.html 重新构建

全量 go build/vet/test + race + 前端 test/build 通过。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-30 21:41:03 +08:00

72 lines
2.6 KiB
Go

package server
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestValidOrdersMatchesAnalyticsScope(t *testing.T) {
srv, store, cleanup := newTestServer(t)
defer cleanup()
ctx := context.Background()
_, _ = store.DB.ExecContext(ctx, `
INSERT INTO orders (order_id, item_id, buyer_id, quantity, amount, order_status, cookie_id, created_at) VALUES
('ord-valid', 'item1', 'buyer1', '2', '¥12.50', 'pending_ship', 'acc1', '2026-06-28 10:00:00'),
('ord-no-amount', 'item1', 'buyer2', '1', '', 'pending_ship', 'acc1', '2026-06-28 10:00:00'),
('ord-bad-status', 'item1', 'buyer3', '1', '9.90', 'cancelled', 'acc1', '2026-06-28 10:00:00')
`)
_, _ = 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, "/analytics/orders?start_date=2026-06-28&end_date=2026-06-28", nil)
req.AddCookie(cookie)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("analytics status=%d body=%s", rec.Code, rec.Body.String())
}
var analytics struct {
RevenueStats struct {
TotalOrders int `json:"total_orders"`
TotalAmount float64 `json:"total_amount"`
} `json:"revenue_stats"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &analytics); err != nil {
t.Fatal(err)
}
if analytics.RevenueStats.TotalOrders != 1 || analytics.RevenueStats.TotalAmount != 12.5 {
t.Fatalf("统计口径异常: %+v", analytics.RevenueStats)
}
req2 := httptest.NewRequest(http.MethodGet, "/analytics/orders/valid?start_date=2026-06-28&end_date=2026-06-28", nil)
req2.AddCookie(cookie)
rec2 := httptest.NewRecorder()
h.ServeHTTP(rec2, req2)
if rec2.Code != http.StatusOK {
t.Fatalf("valid orders status=%d body=%s", rec2.Code, rec2.Body.String())
}
var valid struct {
Orders []map[string]any `json:"orders"`
}
if err := json.Unmarshal(rec2.Body.Bytes(), &valid); err != nil {
t.Fatal(err)
}
if len(valid.Orders) != 1 {
t.Fatalf("有效订单明细数量应与统计订单数一致,got %d body=%s", len(valid.Orders), rec2.Body.String())
}
order := valid.Orders[0]
if order["order_id"] != "ord-valid" || order["item_title"] != "测试商品" || !strings.Contains(order["item_image"].(string), "img.example") {
t.Fatalf("有效订单明细字段异常: %+v", order)
}
if order["status"] != "pending_ship" || order["order_status"] != "pending_ship" {
t.Fatalf("状态字段异常: %+v", order)
}
}