feat: 完善 Rock 发货异常闭环与绑定判定
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"affiliate_dash/internal/model"
|
||||
"affiliate_dash/internal/pkg/timeutil"
|
||||
)
|
||||
|
||||
func TestUpdateConfig_BoundWarnDaysAndAutoRefresh(t *testing.T) {
|
||||
@@ -65,7 +66,15 @@ func TestListTokens_OverdueFilters(t *testing.T) {
|
||||
boundOld := model.RockToken{UpstreamTokenID: "overdue-bound", RedeemCode: "C3", Status: model.RockStatusBound, BoundAt: &old}
|
||||
boundFresh := model.RockToken{UpstreamTokenID: "fresh-bound", RedeemCode: "C4", Status: model.RockStatusBound, BoundAt: &fresh}
|
||||
redeemedOld := model.RockToken{UpstreamTokenID: "old-redeemed", RedeemCode: "C5", Status: model.RockStatusRedeemed, BoundAt: &old}
|
||||
for _, token := range []*model.RockToken{&usingOld, &usingFresh, &boundOld, &boundFresh, &redeemedOld} {
|
||||
requestedAt := now.Add(-time.Minute)
|
||||
boundPending := model.RockToken{
|
||||
UpstreamTokenID: "overdue-bound-pending",
|
||||
RedeemCode: "C6",
|
||||
Status: model.RockStatusBound,
|
||||
BoundAt: &old,
|
||||
RedeemRequestedAt: &requestedAt,
|
||||
}
|
||||
for _, token := range []*model.RockToken{&usingOld, &usingFresh, &boundOld, &boundFresh, &redeemedOld, &boundPending} {
|
||||
if err := db.Create(token).Error; err != nil {
|
||||
t.Fatalf("创建 Token 失败: %v", err)
|
||||
}
|
||||
@@ -96,6 +105,15 @@ func TestListTokens_OverdueFilters(t *testing.T) {
|
||||
t.Fatalf("过期预警统计不符: using_overdue=%d using_danger=%d bound_overdue=%d",
|
||||
stats.UsingOverdueStock, stats.UsingDangerStock, stats.BoundOverdueStock)
|
||||
}
|
||||
all, _, err := svc.ListTokens(ctx, 1, 20, "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("查询全部 Token 失败: %v", err)
|
||||
}
|
||||
for _, token := range all {
|
||||
if token.ID == boundPending.ID && token.BoundOverdue {
|
||||
t.Fatal("发货确认中的 Token 不应进入绑定后未发货")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestListTokens_OverdueBoundaryInclusive 锁定「满 2 天 / 满 3 天」的含边界口径:
|
||||
@@ -211,15 +229,17 @@ func TestRefreshStaleTokens_SkipsWhenAutoRefreshDisabled(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefreshAllStaleTokens_FixesMismatch(t *testing.T) {
|
||||
func TestRefreshAllStaleTokens_KeepsUnresolvedMismatch(t *testing.T) {
|
||||
db := newRockTestDB(t)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
bindingAt := time.Now().In(timeutil.Location()).Add(-30 * time.Minute).Format("2006-01-02 15:04:05")
|
||||
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"token": map[string]interface{}{
|
||||
"status": "bound",
|
||||
"bound_account": "痛写心扉",
|
||||
"bound_account_id": "3132710334",
|
||||
"bind_changed_at": bindingAt,
|
||||
},
|
||||
})
|
||||
}))
|
||||
@@ -228,11 +248,13 @@ func TestRefreshAllStaleTokens_FixesMismatch(t *testing.T) {
|
||||
svc := NewRockService(db, NewRockClient(server.URL))
|
||||
setRockTestBearer(t, svc, "bearer-test")
|
||||
|
||||
usingAt := time.Now().Add(-time.Hour)
|
||||
token := model.RockToken{
|
||||
UpstreamTokenID: "scan-on",
|
||||
RedeemCode: "CODE-SCAN-ON",
|
||||
Status: model.RockStatusMismatch,
|
||||
ExpectedUID: "1111111111",
|
||||
UsingAt: &usingAt,
|
||||
}
|
||||
if err := db.Create(&token).Error; err != nil {
|
||||
t.Fatalf("创建 Token 失败: %v", err)
|
||||
@@ -246,7 +268,7 @@ func TestRefreshAllStaleTokens_FixesMismatch(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("一键刷新失败: %v", err)
|
||||
}
|
||||
if result.Scanned != 1 || result.Fixed != 1 || result.Remaining != 0 || result.Failed != 0 {
|
||||
if result.Scanned != 1 || result.Fixed != 0 || result.Remaining != 1 || result.Failed != 0 {
|
||||
t.Fatalf("一键刷新结果不符: %+v", result)
|
||||
}
|
||||
|
||||
@@ -254,8 +276,8 @@ func TestRefreshAllStaleTokens_FixesMismatch(t *testing.T) {
|
||||
if err := db.First(&saved, token.ID).Error; err != nil {
|
||||
t.Fatalf("读取 Token 失败: %v", err)
|
||||
}
|
||||
if saved.Status != model.RockStatusBound || saved.MismatchAt != nil {
|
||||
t.Fatalf("应修复为 bound 并清空 mismatch 标记,实际 %+v", saved)
|
||||
if saved.Status != model.RockStatusMismatch || saved.MismatchAt == nil {
|
||||
t.Fatalf("UID 不一致时应保持 mismatch,实际 %+v", saved)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,6 +293,7 @@ func TestRefreshAllStaleTokens_IncludesOverdueTokens(t *testing.T) {
|
||||
"status": "bound",
|
||||
"bound_account": "新角色",
|
||||
"bound_account_id": "10001",
|
||||
"bind_changed_at": time.Now().AddDate(0, 0, -10).Add(time.Hour).In(timeutil.Location()).Format("2006-01-02 15:04:05"),
|
||||
},
|
||||
})
|
||||
case "/api/redeem/CODE-BOUND-OLD/refresh":
|
||||
@@ -302,6 +325,7 @@ func TestRefreshAllStaleTokens_IncludesOverdueTokens(t *testing.T) {
|
||||
UpstreamTokenID: "stale-bound",
|
||||
RedeemCode: "CODE-BOUND-OLD",
|
||||
Status: model.RockStatusBound,
|
||||
UsingAt: &old,
|
||||
BoundAt: &old,
|
||||
}
|
||||
for _, token := range []*model.RockToken{&usingToken, &boundToken} {
|
||||
@@ -347,6 +371,7 @@ func TestRefreshStaleTokens_IncludesOverdueWhenEnabled(t *testing.T) {
|
||||
"status": "bound",
|
||||
"bound_account": "自动刷新角色",
|
||||
"bound_account_id": "20001",
|
||||
"bind_changed_at": time.Now().AddDate(0, 0, -10).Add(time.Hour).In(timeutil.Location()).Format("2006-01-02 15:04:05"),
|
||||
},
|
||||
})
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user