Files
sub2api/backend/internal/service/user_subscription_days_remaining_test.go
T
李建琦 6d655c9903
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
Sub2API v1.0 - AI API 网关(二开初始版本,基于上游 Wei-Shaw/sub2api)
2026-08-21 18:30:13 +08:00

38 lines
1.4 KiB
Go

//go:build unit
package service
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestUserSubscriptionDaysRemainingAt(t *testing.T) {
now := time.Date(2026, time.July, 19, 12, 0, 0, 0, time.UTC)
tests := []struct {
name string
expiresAt time.Time
want int
}{
{name: "expired", expiresAt: now.Add(-time.Nanosecond), want: 0},
{name: "expires now", expiresAt: now, want: 0},
{name: "less than one day", expiresAt: now.Add(subscriptionDayDuration - time.Nanosecond), want: 1},
{name: "exactly one day", expiresAt: now.Add(subscriptionDayDuration), want: 1},
{name: "over one day", expiresAt: now.Add(subscriptionDayDuration + time.Nanosecond), want: 2},
{name: "less than two days", expiresAt: now.Add(2*subscriptionDayDuration - time.Nanosecond), want: 2},
{name: "exactly two days", expiresAt: now.Add(2 * subscriptionDayDuration), want: 2},
{name: "over two days", expiresAt: now.Add(2*subscriptionDayDuration + time.Nanosecond), want: 3},
{name: "exactly seven days", expiresAt: now.Add(7 * subscriptionDayDuration), want: 7},
{name: "over seven days", expiresAt: now.Add(7*subscriptionDayDuration + time.Nanosecond), want: 8},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sub := &UserSubscription{ExpiresAt: tt.expiresAt}
require.Equal(t, tt.want, sub.daysRemainingAt(now))
})
}
}