mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-24 23:21:18 +00:00
fix(frontend): correct subscription expiry labels
This commit is contained in:
1 parent
5a6143097d
commit
fb40211305
6 files changed
+161
-11
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { getExpirationDateRelation, getRemainingExpiryDuration } from '../subscriptionQuota'
|
||||
|
||||
describe('subscription expiry timing', () => {
|
||||
it('uses local calendar dates for today and tomorrow', () => {
|
||||
const now = new Date(2026, 2, 7, 23, 30)
|
||||
|
||||
expect(getExpirationDateRelation(new Date(2026, 2, 7, 23, 45), now)).toBe('today')
|
||||
expect(getExpirationDateRelation(new Date(2026, 2, 8, 3, 30), now)).toBe('tomorrow')
|
||||
})
|
||||
|
||||
it('treats the exact expiry instant and elapsed expiries as expired', () => {
|
||||
const now = new Date(2026, 6, 30, 9, 0)
|
||||
|
||||
expect(getExpirationDateRelation(now, now)).toBe('expired')
|
||||
expect(getRemainingExpiryDuration(now, now)).toBeNull()
|
||||
expect(getExpirationDateRelation(new Date(2026, 6, 30, 8, 59), now)).toBe('expired')
|
||||
expect(getRemainingExpiryDuration(new Date(2026, 6, 30, 8, 59), now)).toBeNull()
|
||||
})
|
||||
|
||||
it('rejects invalid target and current dates', () => {
|
||||
const invalid = new Date('invalid')
|
||||
const valid = new Date(2026, 6, 30, 9, 0)
|
||||
|
||||
expect(getExpirationDateRelation(invalid, valid)).toBeNull()
|
||||
expect(getExpirationDateRelation(valid, invalid)).toBeNull()
|
||||
expect(getRemainingExpiryDuration(invalid, valid)).toBeNull()
|
||||
expect(getRemainingExpiryDuration(valid, invalid)).toBeNull()
|
||||
})
|
||||
|
||||
it('returns rounded-up hours and minutes for an expiry under 24 hours away', () => {
|
||||
const now = new Date(2026, 6, 30, 9, 0)
|
||||
|
||||
expect(getRemainingExpiryDuration(new Date(2026, 6, 31, 8, 30), now)).toEqual({
|
||||
unit: 'hoursMinutes',
|
||||
hours: 23,
|
||||
minutes: 30
|
||||
})
|
||||
expect(getRemainingExpiryDuration(new Date(now.getTime() + 1), now)).toEqual({
|
||||
unit: 'hoursMinutes',
|
||||
hours: 0,
|
||||
minutes: 1
|
||||
})
|
||||
expect(getRemainingExpiryDuration(new Date(now.getTime() + 23 * 60 * 60 * 1000 + 1), now)).toEqual({
|
||||
unit: 'hoursMinutes',
|
||||
hours: 23,
|
||||
minutes: 1
|
||||
})
|
||||
})
|
||||
|
||||
it('preserves rounded-up day display from 24 hours onward', () => {
|
||||
const now = new Date(2026, 6, 30, 9, 0)
|
||||
|
||||
expect(getRemainingExpiryDuration(new Date(now.getTime() + 24 * 60 * 60 * 1000), now)).toEqual({
|
||||
unit: 'days',
|
||||
days: 1
|
||||
})
|
||||
expect(getRemainingExpiryDuration(new Date(now.getTime() + 24 * 60 * 60 * 1000 + 1), now)).toEqual({
|
||||
unit: 'days',
|
||||
days: 2
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -2,6 +2,12 @@ import type { UserSubscription } from '@/types'
|
||||
|
||||
const ONE_DAY_MS = 24 * 60 * 60 * 1000
|
||||
|
||||
export type ExpirationDateRelation = 'expired' | 'today' | 'tomorrow' | 'later'
|
||||
|
||||
export type RemainingExpiryDuration =
|
||||
| { unit: 'days'; days: number }
|
||||
| { unit: 'hoursMinutes'; hours: number; minutes: number }
|
||||
|
||||
export interface RemainingDurationParts {
|
||||
days: number
|
||||
hours: number
|
||||
@@ -40,3 +46,46 @@ export function getRemainingDurationParts(
|
||||
|
||||
return { days, hours, minutes }
|
||||
}
|
||||
|
||||
export function getExpirationDateRelation(
|
||||
targetAt: Date | string,
|
||||
now: Date = new Date()
|
||||
): ExpirationDateRelation | null {
|
||||
const target = targetAt instanceof Date ? targetAt : new Date(targetAt)
|
||||
const targetTime = target.getTime()
|
||||
const nowTime = now.getTime()
|
||||
|
||||
if (!Number.isFinite(targetTime) || !Number.isFinite(nowTime)) return null
|
||||
if (targetTime <= nowTime) return 'expired'
|
||||
|
||||
const targetDay = Date.UTC(target.getFullYear(), target.getMonth(), target.getDate())
|
||||
const currentDay = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate())
|
||||
const calendarDays = Math.round((targetDay - currentDay) / ONE_DAY_MS)
|
||||
|
||||
if (calendarDays === 0) return 'today'
|
||||
if (calendarDays === 1) return 'tomorrow'
|
||||
return 'later'
|
||||
}
|
||||
|
||||
export function getRemainingExpiryDuration(
|
||||
targetAt: Date | string,
|
||||
now: Date = new Date()
|
||||
): RemainingExpiryDuration | null {
|
||||
const targetTime = targetAt instanceof Date ? targetAt.getTime() : new Date(targetAt).getTime()
|
||||
const nowTime = now.getTime()
|
||||
|
||||
if (!Number.isFinite(targetTime) || !Number.isFinite(nowTime)) return null
|
||||
|
||||
const diffMs = targetTime - nowTime
|
||||
if (diffMs <= 0) return null
|
||||
if (diffMs >= ONE_DAY_MS) {
|
||||
return { unit: 'days', days: Math.ceil(diffMs / ONE_DAY_MS) }
|
||||
}
|
||||
|
||||
const totalMinutes = Math.ceil(diffMs / (60 * 1000))
|
||||
return {
|
||||
unit: 'hoursMinutes',
|
||||
hours: Math.floor(totalMinutes / 60),
|
||||
minutes: totalMinutes % 60
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user