fix: Rock Token 超时按满 2/3 天判定并统一显示口径

This commit is contained in:
2026-11-10 17:25:59 +08:00
parent 162745082b
commit 227d4b93ad
9 changed files with 190 additions and 23 deletions
@@ -98,6 +98,80 @@ func TestListTokens_OverdueFilters(t *testing.T) {
}
}
// TestListTokens_OverdueBoundaryInclusive 锁定「满 2 天 / 满 3 天」的含边界口径:
// 刚好达到或超过阈值必须进入筛选并带超时标记,差 1 小时则不能进入。
func TestListTokens_OverdueBoundaryInclusive(t *testing.T) {
db := newRockTestDB(t)
svc := NewRockService(db, NewRockClient("https://example.com"))
ctx := context.Background()
now := time.Now()
exactBoundAt := now.AddDate(0, 0, -2).Add(-time.Second) // 2 天零 1 秒
almostBoundAt := now.AddDate(0, 0, -2).Add(time.Hour) // 1 天 23 小时
exactUsingAt := now.AddDate(0, 0, -3).Add(-time.Second) // 3 天零 1 秒
almostUsingAt := now.AddDate(0, 0, -3).Add(time.Hour) // 2 天 23 小时
tokens := []model.RockToken{
{UpstreamTokenID: "bound-exact", RedeemCode: "BOUND-1", Status: model.RockStatusBound, BoundAt: &exactBoundAt},
{UpstreamTokenID: "bound-almost", RedeemCode: "BOUND-2", Status: model.RockStatusBound, BoundAt: &almostBoundAt},
{UpstreamTokenID: "using-exact", RedeemCode: "USING-1", Status: model.RockStatusUsing, UsingAt: &exactUsingAt},
{UpstreamTokenID: "using-almost", RedeemCode: "USING-2", Status: model.RockStatusUsing, UsingAt: &almostUsingAt},
}
for i := range tokens {
if err := db.Create(&tokens[i]).Error; err != nil {
t.Fatalf("创建 Token 失败: %v", err)
}
}
boundList, boundTotal, err := svc.ListTokens(ctx, 1, 20, rockTokenStatusBoundOverdue, "")
if err != nil {
t.Fatalf("查询绑定超时失败: %v", err)
}
if boundTotal != 1 || len(boundList) != 1 || boundList[0].ID != tokens[0].ID {
t.Fatalf("绑定超时边界错误: total=%d list=%+v", boundTotal, boundList)
}
if !boundList[0].BoundOverdue || boundList[0].BoundDays < 2 {
t.Fatalf("绑定超时标记/天数错误: overdue=%v days=%.4f", boundList[0].BoundOverdue, boundList[0].BoundDays)
}
usingList, usingTotal, err := svc.ListTokens(ctx, 1, 20, rockTokenStatusUsingOverdue, "")
if err != nil {
t.Fatalf("查询使用超时失败: %v", err)
}
if usingTotal != 1 || len(usingList) != 1 || usingList[0].ID != tokens[2].ID {
t.Fatalf("使用超时边界错误: total=%d list=%+v", usingTotal, usingList)
}
if !usingList[0].UsingOverdue || usingList[0].UsingDays < 3 {
t.Fatalf("使用超时标记/天数错误: overdue=%v days=%.4f", usingList[0].UsingOverdue, usingList[0].UsingDays)
}
all, _, err := svc.ListTokens(ctx, 1, 20, "", "")
if err != nil {
t.Fatalf("查询全部 Token 失败: %v", err)
}
byID := make(map[uint]model.RockToken, len(all))
for _, token := range all {
byID[token.ID] = token
}
if byID[tokens[1].ID].BoundOverdue {
t.Fatal("差 1 小时满 2 天的 Token 不应标记绑定超时")
}
if byID[tokens[3].ID].UsingOverdue {
t.Fatal("差 1 小时满 3 天的 Token 不应标记使用超时")
}
if !byID[tokens[0].ID].BoundOverdue || !byID[tokens[2].ID].UsingOverdue {
t.Fatal("刚好达到阈值的 Token 必须标记超时")
}
stats, err := svc.GetStats(ctx)
if err != nil {
t.Fatalf("获取统计失败: %v", err)
}
if stats.BoundOverdueStock != 1 || stats.UsingOverdueStock != 1 {
t.Fatalf("超时统计不符: bound=%d using=%d", stats.BoundOverdueStock, stats.UsingOverdueStock)
}
}
func TestRefreshStaleTokens_SkipsWhenAutoRefreshDisabled(t *testing.T) {
db := newRockTestDB(t)
var calls int32