Files
dygou_go/internal/seckill/xiaodian_report_test.go
T

57 lines
2.2 KiB
Go

package seckill
import (
"testing"
"douyu_go/internal/types"
)
func TestReportFailureReasonUsesXiaodianStopReason(t *testing.T) {
message := "失败|和平小店兑换失败: 对不起,Livelink登录态校验失败!"
if got := reportFailureReason(message); got != "和平小店绑定已失效,需重新绑定" {
t.Fatalf("reportFailureReason() = %q", got)
}
}
func TestStartXiaodianReportAppendsWithinSession(t *testing.T) {
scheduler := &Scheduler{}
firstAccounts := []types.Account{{Name: "账号A"}}
firstProducts := map[string]types.Product{
"账号A": {CommodityID: "goods-a", CommodityName: "商品A"},
}
scheduler.startXiaodianReport(firstAccounts, firstProducts, "stock-session")
scheduler.updateXiaodianReport(map[string]types.ExchangeResult{
"账号A": {OK: true, Account: "账号A", Message: "成功|角色A"},
})
secondAccounts := []types.Account{{Name: "账号B"}}
secondProducts := map[string]types.Product{
"账号B": {CommodityID: "goods-b", CommodityName: "商品B"},
}
scheduler.startXiaodianReport(secondAccounts, secondProducts, "stock-session")
report, ok := scheduler.LatestXiaodianReport()
if !ok || len(report.Accounts) != 2 {
t.Fatalf("同一监控会话应保留两批账号,报告=%+v", report)
}
if report.Accounts[0].ProductID != "goods-a" || report.Accounts[1].ProductID != "goods-b" {
t.Fatalf("账号商品映射丢失: %+v", report.Accounts)
}
if report.Accounts[0].Status != "成功" || report.Accounts[0].Attempts != 1 {
t.Fatalf("前一批结果被覆盖: %+v", report.Accounts[0])
}
}
func TestStartXiaodianReportDifferentSessionReplacesReport(t *testing.T) {
scheduler := &Scheduler{}
scheduler.startXiaodianReport([]types.Account{{Name: "账号A"}}, map[string]types.Product{
"账号A": {CommodityID: "goods-a"},
}, "session-a")
scheduler.startXiaodianReport([]types.Account{{Name: "账号B"}}, map[string]types.Product{
"账号B": {CommodityID: "goods-b"},
}, "session-b")
report, ok := scheduler.LatestXiaodianReport()
if !ok || report.SessionID != "session-b" || len(report.Accounts) != 1 || report.Accounts[0].AccountName != "账号B" {
t.Fatalf("不同会话不应串报告: %+v", report)
}
}