helpers_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package quotaview
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/Wei-Shaw/sub2api/internal/pkg/timezone"
  6. "github.com/Wei-Shaw/sub2api/internal/service"
  7. )
  8. // TestNextMonthlyResetTimeFrom_FromStart 验证:start 已知时返回 start+30d,不随 now 漂移。
  9. func TestNextMonthlyResetTimeFrom_FromStart(t *testing.T) {
  10. t0 := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
  11. now := t0.Add(15 * 24 * time.Hour) // t0 + 15d
  12. want := t0.Add(30 * 24 * time.Hour) // t0 + 30d
  13. got := NextMonthlyResetTimeFrom(&t0, now)
  14. if !got.Equal(want) {
  15. t.Errorf("NextMonthlyResetTimeFrom: want %v, got %v", want, got)
  16. }
  17. }
  18. // TestNextMonthlyResetTimeFrom_NilStart 验证:start=nil 时退化为 now+30d(不 panic)。
  19. func TestNextMonthlyResetTimeFrom_NilStart(t *testing.T) {
  20. now := time.Date(2024, 3, 15, 12, 0, 0, 0, time.UTC)
  21. want := now.Add(30 * 24 * time.Hour)
  22. got := NextMonthlyResetTimeFrom(nil, now)
  23. if !got.Equal(want) {
  24. t.Errorf("NextMonthlyResetTimeFrom(nil): want %v, got %v", want, got)
  25. }
  26. }
  27. // TestLazyZeroQuotaForResponse_MonthlyResetsAt_NotDrifting 验证:
  28. // 连续两次以不同 now 调用、但 MonthlyWindowStart 相同的 record,
  29. // monthly_window_resets_at 始终等于 windowStart+30d,不随 now 漂移。
  30. func TestLazyZeroQuotaForResponse_MonthlyResetsAt_NotDrifting(t *testing.T) {
  31. windowStart := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
  32. wantResetsAt := windowStart.Add(30 * 24 * time.Hour).Format(time.RFC3339)
  33. r := service.UserPlatformQuotaRecord{
  34. Platform: "openai",
  35. MonthlyUsageUSD: 5.0,
  36. MonthlyWindowStart: &windowStart,
  37. }
  38. // 第一次调用:now = windowStart + 5d
  39. now1 := windowStart.Add(5 * 24 * time.Hour)
  40. out1 := LazyZeroQuotaForResponse(r, now1, false)
  41. resetsAt1, ok1 := out1["monthly_window_resets_at"]
  42. if !ok1 || resetsAt1 == nil {
  43. t.Fatal("first call: monthly_window_resets_at should be set for active window")
  44. }
  45. s1, ok := resetsAt1.(*string)
  46. if !ok || s1 == nil {
  47. t.Fatalf("first call: monthly_window_resets_at should be *string, got %T", resetsAt1)
  48. }
  49. if *s1 != wantResetsAt {
  50. t.Errorf("first call: want %s, got %s", wantResetsAt, *s1)
  51. }
  52. // 第二次调用:now = windowStart + 10d(不同 now,但 resetsAt 应不变)
  53. now2 := windowStart.Add(10 * 24 * time.Hour)
  54. out2 := LazyZeroQuotaForResponse(r, now2, false)
  55. resetsAt2, ok2 := out2["monthly_window_resets_at"]
  56. if !ok2 || resetsAt2 == nil {
  57. t.Fatal("second call: monthly_window_resets_at should be set for active window")
  58. }
  59. s2, ok := resetsAt2.(*string)
  60. if !ok || s2 == nil {
  61. t.Fatalf("second call: monthly_window_resets_at should be *string, got %T", resetsAt2)
  62. }
  63. if *s2 != wantResetsAt {
  64. t.Errorf("second call: want %s, got %s", wantResetsAt, *s2)
  65. }
  66. // 两次结果必须相等
  67. if *s1 != *s2 {
  68. t.Errorf("resetsAt drifted between calls: %s vs %s", *s1, *s2)
  69. }
  70. }
  71. // TestNeedsDailyReset_FollowsServerTimezone 验证日窗口过期判断按全局时区(北京 0 点)而非 UTC。
  72. func TestNeedsDailyReset_FollowsServerTimezone(t *testing.T) {
  73. if err := timezone.Init("Asia/Shanghai"); err != nil {
  74. t.Fatalf("Init: %v", err)
  75. }
  76. t.Cleanup(func() { _ = timezone.Init("UTC") })
  77. // now = 2026-05-25 23:00 UTC = 2026-05-26 07:00 +08(北京 5/26)
  78. now := time.Date(2026, 5, 25, 23, 0, 0, 0, time.UTC)
  79. // start = 2026-05-25 10:00 UTC = 2026-05-25 18:00 +08(北京 5/25)→ 应判定为过期
  80. startPrevBeijingDay := time.Date(2026, 5, 25, 10, 0, 0, 0, time.UTC)
  81. if !NeedsDailyReset(&startPrevBeijingDay, now) {
  82. t.Error("上一个北京日的窗口应判定为过期")
  83. }
  84. // start = 2026-05-25 20:00 UTC = 2026-05-26 04:00 +08(北京 5/26 同日)→ 不应过期
  85. startSameBeijingDay := time.Date(2026, 5, 25, 20, 0, 0, 0, time.UTC)
  86. if NeedsDailyReset(&startSameBeijingDay, now) {
  87. t.Error("同一北京日的窗口不应判定为过期")
  88. }
  89. }
  90. // TestNextDailyResetTime_FollowsServerTimezone 验证下次日重置 = 次日北京 0 点。
  91. func TestNextDailyResetTime_FollowsServerTimezone(t *testing.T) {
  92. if err := timezone.Init("Asia/Shanghai"); err != nil {
  93. t.Fatalf("Init: %v", err)
  94. }
  95. t.Cleanup(func() { _ = timezone.Init("UTC") })
  96. now := time.Date(2026, 5, 25, 23, 0, 0, 0, time.UTC) // 北京 5/26 07:00
  97. want := time.Date(2026, 5, 27, 0, 0, 0, 0, timezone.Location()) // 北京 5/27 00:00
  98. if got := nextDailyResetTime(now); !got.Equal(want) {
  99. t.Errorf("nextDailyResetTime = %v, want %v", got, want)
  100. }
  101. }
  102. // TestNextWeeklyResetTime_FollowsServerTimezone 验证下次周重置 = 下周一北京 0 点。
  103. func TestNextWeeklyResetTime_FollowsServerTimezone(t *testing.T) {
  104. if err := timezone.Init("Asia/Shanghai"); err != nil {
  105. t.Fatalf("Init: %v", err)
  106. }
  107. t.Cleanup(func() { _ = timezone.Init("UTC") })
  108. // 北京 2026-05-26(周二)→ 下周一是 2026-06-01
  109. now := time.Date(2026, 5, 25, 23, 0, 0, 0, time.UTC) // 北京 5/26 07:00 周二
  110. want := time.Date(2026, 6, 1, 0, 0, 0, 0, timezone.Location())
  111. if got := nextWeeklyResetTime(now); !got.Equal(want) {
  112. t.Errorf("nextWeeklyResetTime = %v, want %v", got, want)
  113. }
  114. }