user_subscription_days_remaining_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. //go:build unit
  2. package service
  3. import (
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestUserSubscriptionDaysRemainingAt(t *testing.T) {
  9. now := time.Date(2026, time.July, 19, 12, 0, 0, 0, time.UTC)
  10. tests := []struct {
  11. name string
  12. expiresAt time.Time
  13. want int
  14. }{
  15. {name: "expired", expiresAt: now.Add(-time.Nanosecond), want: 0},
  16. {name: "expires now", expiresAt: now, want: 0},
  17. {name: "less than one day", expiresAt: now.Add(subscriptionDayDuration - time.Nanosecond), want: 1},
  18. {name: "exactly one day", expiresAt: now.Add(subscriptionDayDuration), want: 1},
  19. {name: "over one day", expiresAt: now.Add(subscriptionDayDuration + time.Nanosecond), want: 2},
  20. {name: "less than two days", expiresAt: now.Add(2*subscriptionDayDuration - time.Nanosecond), want: 2},
  21. {name: "exactly two days", expiresAt: now.Add(2 * subscriptionDayDuration), want: 2},
  22. {name: "over two days", expiresAt: now.Add(2*subscriptionDayDuration + time.Nanosecond), want: 3},
  23. {name: "exactly seven days", expiresAt: now.Add(7 * subscriptionDayDuration), want: 7},
  24. {name: "over seven days", expiresAt: now.Add(7*subscriptionDayDuration + time.Nanosecond), want: 8},
  25. }
  26. for _, tt := range tests {
  27. t.Run(tt.name, func(t *testing.T) {
  28. sub := &UserSubscription{ExpiresAt: tt.expiresAt}
  29. require.Equal(t, tt.want, sub.daysRemainingAt(now))
  30. })
  31. }
  32. }