Add cálculo de trial y badge de plan

This commit is contained in:
Carlos Narro
2026-05-30 19:58:18 +02:00
parent df085b6cf1
commit f2fb6d24c6
2 changed files with 40 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
export function trialDaysRemaining(trialEndsAt: Date | null, now: Date = new Date()): number {
if (!trialEndsAt) return 0;
const ms = trialEndsAt.getTime() - now.getTime();
if (ms <= 0) return 0;
return Math.ceil(ms / (24 * 60 * 60 * 1000));
}
export function formatPlanBadge(
planNombre: string | null,
status: string,
trialEndsAt: Date | null,
now: Date = new Date()
): string {
const plan = planNombre ? `Plan ${planNombre}` : 'Sin plan';
if (status === 'trial') {
return `${plan} · trial, ${trialDaysRemaining(trialEndsAt, now)} días restantes`;
}
return `${plan} · ${status}`;
}

View File

@@ -0,0 +1,21 @@
import { describe, it, expect } from 'vitest';
import { trialDaysRemaining, formatPlanBadge } from '@/lib/billing/plan';
describe('plan', () => {
it('calcula días de trial restantes (redondeo hacia arriba, nunca negativo)', () => {
const now = new Date('2026-05-30T12:00:00Z');
expect(trialDaysRemaining(new Date('2026-06-05T12:00:00Z'), now)).toBe(6);
expect(trialDaysRemaining(new Date('2026-05-30T18:00:00Z'), now)).toBe(1);
expect(trialDaysRemaining(new Date('2026-05-29T12:00:00Z'), now)).toBe(0);
expect(trialDaysRemaining(null, now)).toBe(0);
});
it('formatea el badge según estado', () => {
const now = new Date('2026-05-30T12:00:00Z');
expect(formatPlanBadge('Pro', 'trial', new Date('2026-06-05T12:00:00Z'), now))
.toBe('Plan Pro · trial, 6 días restantes');
expect(formatPlanBadge('Pro', 'activo', null, now)).toBe('Plan Pro · activo');
expect(formatPlanBadge(null, 'trial', new Date('2026-06-05T12:00:00Z'), now))
.toBe('Sin plan · trial, 6 días restantes');
});
});