feat: 完善 Rock 发货异常闭环与绑定判定
This commit is contained in:
@@ -10,8 +10,13 @@ import (
|
||||
"time"
|
||||
|
||||
"affiliate_dash/internal/model"
|
||||
"affiliate_dash/internal/pkg/timeutil"
|
||||
)
|
||||
|
||||
func recentRockBindTime(base time.Time) string {
|
||||
return base.Add(time.Minute).In(timeutil.Location()).Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
func TestRefreshTokenBinding_BoundMatchesExpectedUID(t *testing.T) {
|
||||
db := newRockTestDB(t)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -25,6 +30,7 @@ func TestRefreshTokenBinding_BoundMatchesExpectedUID(t *testing.T) {
|
||||
"status": "bound",
|
||||
"bound_account": "痛写心扉",
|
||||
"bound_account_id": "3132710334",
|
||||
"bind_changed_at": recentRockBindTime(time.Now()),
|
||||
"region": "安卓-微信",
|
||||
},
|
||||
"binding": map[string]interface{}{
|
||||
@@ -79,8 +85,8 @@ func TestRefreshTokenBinding_BoundMatchesExpectedUID(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 刷新链路只同步上游实时状态:即使本地预期 UID 与扫码 UID 不一致,也应直接标记为已绑定。
|
||||
func TestRefreshTokenBinding_IgnoresUIDMismatchAndMarksBound(t *testing.T) {
|
||||
// 本轮真实绑定与预期 UID 不一致时必须进入 mismatch。
|
||||
func TestRefreshTokenBinding_UIDMismatchMarksMismatch(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")
|
||||
@@ -89,6 +95,7 @@ func TestRefreshTokenBinding_IgnoresUIDMismatchAndMarksBound(t *testing.T) {
|
||||
"status": "bound",
|
||||
"bound_account": "痛写心扉",
|
||||
"bound_account_id": "3132710334",
|
||||
"bind_changed_at": recentRockBindTime(time.Now()),
|
||||
"region": "安卓-微信",
|
||||
},
|
||||
})
|
||||
@@ -116,19 +123,19 @@ func TestRefreshTokenBinding_IgnoresUIDMismatchAndMarksBound(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("刷新绑定状态失败: %v", err)
|
||||
}
|
||||
if res.Status != model.RockStatusBound || !res.Bound {
|
||||
t.Fatalf("UID 不一致时也应同步为 bound,实际 %+v", res)
|
||||
if res.Status != model.RockStatusMismatch || !res.Bound {
|
||||
t.Fatalf("UID 不一致时应同步为 mismatch,实际 %+v", res)
|
||||
}
|
||||
|
||||
var saved model.RockToken
|
||||
if err := db.First(&saved, token.ID).Error; err != nil {
|
||||
t.Fatalf("读取 Token 失败: %v", err)
|
||||
}
|
||||
if saved.Status != model.RockStatusBound || saved.ActualRoleID != "3132710334" {
|
||||
t.Fatalf("应同步为 bound 并写入实际 UID,实际 %+v", saved)
|
||||
if saved.Status != model.RockStatusMismatch || saved.ActualRoleID != "3132710334" {
|
||||
t.Fatalf("应同步为 mismatch 并写入实际 UID,实际 %+v", saved)
|
||||
}
|
||||
if saved.MismatchAt != nil || saved.MismatchReason != "" {
|
||||
t.Fatalf("同步为 bound 后应清空绑定错误标记,实际 %+v", saved)
|
||||
if saved.MismatchAt == nil || saved.MismatchReason == "" {
|
||||
t.Fatalf("UID 不一致必须写入绑定错误标记,实际 %+v", saved)
|
||||
}
|
||||
if saved.BoundAt == nil {
|
||||
t.Fatalf("bound_at 未写入")
|
||||
@@ -141,6 +148,9 @@ func TestRefreshTokenBinding_EmptyExpectedUIDMarksBound(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"token": map[string]interface{}{
|
||||
"bind_changed_at": recentRockBindTime(time.Now()),
|
||||
},
|
||||
"binding": map[string]interface{}{
|
||||
"bound": true,
|
||||
"role": "痛写心扉",
|
||||
@@ -154,11 +164,13 @@ func TestRefreshTokenBinding_EmptyExpectedUIDMarksBound(t *testing.T) {
|
||||
svc := NewRockService(db, NewRockClient(server.URL))
|
||||
setRockTestBearer(t, svc, "bearer-test")
|
||||
|
||||
now := time.Now()
|
||||
token := model.RockToken{
|
||||
UpstreamTokenID: "9003",
|
||||
RedeemCode: "CODE-NOUID",
|
||||
Color: model.RockColorBlack,
|
||||
Status: model.RockStatusUsing,
|
||||
ClaimedAt: &now,
|
||||
}
|
||||
if err := db.Create(&token).Error; err != nil {
|
||||
t.Fatalf("创建 Token 失败: %v", err)
|
||||
@@ -186,6 +198,7 @@ func TestRefreshTokenBinding_EmptyExpectedUIDMarksBound(t *testing.T) {
|
||||
|
||||
func TestRefreshTokenBinding_SyncsRedeemed(t *testing.T) {
|
||||
db := newRockTestDB(t)
|
||||
now := time.Now()
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
@@ -193,10 +206,10 @@ func TestRefreshTokenBinding_SyncsRedeemed(t *testing.T) {
|
||||
"status": "redeemed",
|
||||
"bound_account": "痛写心扉",
|
||||
"bound_account_id": "3132710334",
|
||||
"redeemed_at": "2026-10-29 12:00:00",
|
||||
"redeemed_at": now.Add(-20 * time.Second).In(timeutil.Location()).Format("2006-01-02 15:04:05"),
|
||||
},
|
||||
"deliveryList": []map[string]interface{}{
|
||||
{"fulfillId": 4519960, "time": "2026-10-29T12:00:00.1", "role": "安卓-微信-痛写心扉"},
|
||||
{"fulfillId": 4519960, "time": now.Add(-10 * time.Second).Format("2006-01-02T15:04:05.1"), "role": "安卓-微信-痛写心扉"},
|
||||
},
|
||||
})
|
||||
}))
|
||||
@@ -212,6 +225,7 @@ func TestRefreshTokenBinding_SyncsRedeemed(t *testing.T) {
|
||||
Status: model.RockStatusUsing,
|
||||
AssignedOrderNo: "FO-REFRESH-4",
|
||||
ExpectedUID: "3132710334",
|
||||
UsingAt: &now,
|
||||
}
|
||||
if err := db.Create(&token).Error; err != nil {
|
||||
t.Fatalf("创建 Token 失败: %v", err)
|
||||
@@ -234,6 +248,140 @@ func TestRefreshTokenBinding_SyncsRedeemed(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefreshTokenBinding_IgnoresHistoricalRedeemedAt(t *testing.T) {
|
||||
db := newRockTestDB(t)
|
||||
now := time.Now()
|
||||
historical := now.Add(-48 * time.Hour)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"token": map[string]interface{}{
|
||||
"status": "redeemed",
|
||||
"redeemed_at": historical.In(timeutil.Location()).Format("2006-01-02 15:04:05"),
|
||||
},
|
||||
"deliveryList": []map[string]interface{}{
|
||||
{"fulfillId": 100, "time": historical.Format("2006-01-02T15:04:05.1")},
|
||||
},
|
||||
})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
svc := NewRockService(db, NewRockClient(server.URL))
|
||||
setRockTestBearer(t, svc, "bearer-test")
|
||||
token := model.RockToken{
|
||||
UpstreamTokenID: "9005",
|
||||
RedeemCode: "CODE-HISTORICAL-REDEEMED",
|
||||
Color: model.RockColorBlack,
|
||||
Status: model.RockStatusUsing,
|
||||
AssignedOrderNo: "FO-REFRESH-5",
|
||||
UsingAt: &now,
|
||||
}
|
||||
if err := db.Create(&token).Error; err != nil {
|
||||
t.Fatalf("创建 Token 失败: %v", err)
|
||||
}
|
||||
|
||||
res, err := svc.RefreshTokenBinding(context.Background(), token.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("刷新绑定状态失败: %v", err)
|
||||
}
|
||||
if res.Status != model.RockStatusUsing || res.Bound {
|
||||
t.Fatalf("历史兑换记录不能推进本轮状态,实际 %+v", res)
|
||||
}
|
||||
var saved model.RockToken
|
||||
if err := db.First(&saved, token.ID).Error; err != nil {
|
||||
t.Fatalf("读取 Token 失败: %v", err)
|
||||
}
|
||||
if saved.Status != model.RockStatusUsing || saved.RedeemedAt != nil {
|
||||
t.Fatalf("Token 不应被历史兑换记录改成已发货: %+v", saved)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefreshTokenBinding_SnapshotEventAcceptsDifferentTimeFormat(t *testing.T) {
|
||||
db := newRockTestDB(t)
|
||||
now := time.Now()
|
||||
boundAt := now.Add(time.Minute)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"token": map[string]interface{}{
|
||||
"status": "bound",
|
||||
"bound_account": "上一任角色",
|
||||
"bound_account_id": "99001",
|
||||
"bind_changed_at": boundAt.Format(time.RFC3339Nano),
|
||||
},
|
||||
})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
svc := NewRockService(db, NewRockClient(server.URL))
|
||||
setRockTestBearer(t, svc, "bearer-test")
|
||||
token := model.RockToken{
|
||||
UpstreamTokenID: "9006",
|
||||
RedeemCode: "CODE-SNAPSHOT-FORMAT",
|
||||
Color: model.RockColorBlack,
|
||||
Status: model.RockStatusUsing,
|
||||
AssignedOrderNo: "FO-REFRESH-6",
|
||||
UsingAt: &now,
|
||||
InitialBoundSnapshot: `{"bound":true,"role_id":"99001","bind_changed_at":"` + boundAt.In(timeutil.Location()).Format("2006-01-02 15:04:05") + `"}`,
|
||||
}
|
||||
if err := db.Create(&token).Error; err != nil {
|
||||
t.Fatalf("创建 Token 失败: %v", err)
|
||||
}
|
||||
|
||||
res, err := svc.RefreshTokenBinding(context.Background(), token.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("刷新绑定状态失败: %v", err)
|
||||
}
|
||||
if res.Status != model.RockStatusUsing || res.Bound {
|
||||
t.Fatalf("与初始快照相同的绑定事件必须按残留处理,实际 %+v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefreshTokenBinding_MissingBaselineDoesNotDowngradeBound(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")
|
||||
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"token": map[string]interface{}{
|
||||
"status": "bound",
|
||||
"bound_account": "历史角色",
|
||||
"bound_account_id": "88001",
|
||||
"bind_changed_at": time.Now().In(timeutil.Location()).Add(-time.Hour).Format("2006-01-02 15:04:05"),
|
||||
},
|
||||
})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
svc := NewRockService(db, NewRockClient(server.URL))
|
||||
setRockTestBearer(t, svc, "bearer-test")
|
||||
token := model.RockToken{
|
||||
UpstreamTokenID: "9007",
|
||||
RedeemCode: "CODE-MISSING-BASELINE",
|
||||
Color: model.RockColorBlack,
|
||||
Status: model.RockStatusBound,
|
||||
AssignedOrderNo: "FO-REFRESH-7",
|
||||
ExpectedUID: "88001",
|
||||
}
|
||||
if err := db.Create(&token).Error; err != nil {
|
||||
t.Fatalf("创建 Token 失败: %v", err)
|
||||
}
|
||||
|
||||
res, err := svc.RefreshTokenBinding(context.Background(), token.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("刷新绑定状态失败: %v", err)
|
||||
}
|
||||
if res.Status != model.RockStatusBound || !res.Warning {
|
||||
t.Fatalf("缺少时间基线时只能告警,不能自动降级,实际 %+v", res)
|
||||
}
|
||||
var saved model.RockToken
|
||||
if err := db.First(&saved, token.ID).Error; err != nil {
|
||||
t.Fatalf("读取 Token 失败: %v", err)
|
||||
}
|
||||
if saved.Status != model.RockStatusBound {
|
||||
t.Fatalf("缺少时间基线时不得自动改写 Token 状态,实际 %s", saved.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefreshTokenBinding_UpstreamErrorKeepsState(t *testing.T) {
|
||||
db := newRockTestDB(t)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -281,6 +429,7 @@ func TestRefreshTokenBinding_FallsBackToAdminRefresh(t *testing.T) {
|
||||
"status": "bound",
|
||||
"bound_account": "痛写心扉",
|
||||
"bound_account_id": "3132710334",
|
||||
"bind_changed_at": recentRockBindTime(time.Now()),
|
||||
},
|
||||
})
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user