feat(billing): planes Free/Pro con Suscripciones de Mercado Pago
- Módulo billing: suscripción Pro mensual vía /preapproval con credenciales de plataforma por país, webhook firmado y reconciliación por polling. - PlanGuard en los use-cases que crean grupos/alumnos, cobran con MP o activan automatizaciones (402 plan_limit_reached / plan_feature_required / plan_read_only), detrás de BILLING_ENFORCEMENT. - Onboarding: paso Plan obligatorio (POST /onboarding/plan); con Free se saltean Cobros y WhatsApp. - Web: pantalla /billing, aviso de Pro desde el MutationCache. - Migración billing_subscriptions y docs/billing.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
85
apps/web/test/billing-copy.test.ts
Normal file
85
apps/web/test/billing-copy.test.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
import type { BillingStatusDto } from '@gruperly/shared'
|
||||
import { PLAN_LIMITS } from '@gruperly/shared'
|
||||
import {
|
||||
formatUsage,
|
||||
hasPlanFeature,
|
||||
isPlanProblem,
|
||||
planBadgeVariant,
|
||||
planFeatureList,
|
||||
planNoticeMessage,
|
||||
subscriptionStatusLabel,
|
||||
} from '../src/features/billing/copy'
|
||||
|
||||
function status(overrides: Partial<BillingStatusDto> = {}): BillingStatusDto {
|
||||
return {
|
||||
plan: 'FREE',
|
||||
status: null,
|
||||
readOnly: false,
|
||||
graceUntil: null,
|
||||
paidThrough: null,
|
||||
nextPaymentDate: null,
|
||||
payerEmail: null,
|
||||
priceAmount: 15000,
|
||||
currency: 'ARS',
|
||||
country: 'AR',
|
||||
availableInCountry: true,
|
||||
enforced: true,
|
||||
usage: { groups: 1, attendees: 3 },
|
||||
limits: PLAN_LIMITS.FREE,
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
/* El copy del plan lo comparten Ajustes, /billing, el onboarding y los avisos. */
|
||||
describe('copy del plan', () => {
|
||||
test('lista lo que incluye cada plan', () => {
|
||||
expect(planFeatureList(PLAN_LIMITS.FREE)).toEqual([
|
||||
{ label: '1 grupo', included: true },
|
||||
{ label: 'Hasta 15 alumnos activos', included: true },
|
||||
{ label: 'Cobros con Mercado Pago', included: false },
|
||||
{ label: 'Mensajes automáticos por WhatsApp', included: false },
|
||||
])
|
||||
expect(planFeatureList(PLAN_LIMITS.PRO).every((feature) => feature.included)).toBe(true)
|
||||
})
|
||||
|
||||
test('muestra el uso contra el límite', () => {
|
||||
expect(formatUsage(3, 15)).toBe('3 de 15')
|
||||
expect(formatUsage(42, null)).toBe('42')
|
||||
})
|
||||
|
||||
test('nombra cada estado de la suscripción', () => {
|
||||
expect(subscriptionStatusLabel('AUTHORIZED')).toBe('Activa')
|
||||
expect(subscriptionStatusLabel('PENDING')).toBe('Pendiente de pago')
|
||||
expect(subscriptionStatusLabel(null)).toBe('Sin suscripción')
|
||||
})
|
||||
|
||||
test('avisa solo lectura antes que gracia', () => {
|
||||
expect(planNoticeMessage(status())).toBeNull()
|
||||
expect(planNoticeMessage(status({ readOnly: true }))?.tone).toBe('danger')
|
||||
expect(planNoticeMessage(status({ plan: 'PRO', status: 'AUTHORIZED', graceUntil: '2026-11-01T00:00:00.000Z' }))?.tone).toBe('warning')
|
||||
expect(planBadgeVariant(status({ readOnly: true }))).toBe('danger')
|
||||
expect(planBadgeVariant(status({ plan: 'PRO', limits: PLAN_LIMITS.PRO }))).toBe('success')
|
||||
})
|
||||
|
||||
test('el aviso de gracia distingue la cancelación de un cobro rechazado', () => {
|
||||
const graceUntil = '2026-11-01T12:00:00.000Z'
|
||||
expect(planNoticeMessage(status({ plan: 'PRO', status: 'CANCELLED', graceUntil }))?.text).toContain('Cancelaste Pro')
|
||||
expect(planNoticeMessage(status({ plan: 'PRO', status: 'AUTHORIZED', graceUntil }))?.text).toContain('No pudimos cobrar')
|
||||
})
|
||||
|
||||
test('deja pasar las funciones sin datos o sin límites aplicados', () => {
|
||||
expect(hasPlanFeature(undefined, 'mpCharges')).toBe(true)
|
||||
expect(hasPlanFeature(status({ enforced: false }), 'mpCharges')).toBe(true)
|
||||
expect(hasPlanFeature(status(), 'mpCharges')).toBe(false)
|
||||
expect(hasPlanFeature(status({ limits: PLAN_LIMITS.PRO }), 'whatsappAutomations')).toBe(true)
|
||||
})
|
||||
|
||||
test('reconoce un 402 de plan y nada más', () => {
|
||||
expect(isPlanProblem({ status: 402, problem: { code: 'plan_limit_reached' } })).toBe(true)
|
||||
expect(isPlanProblem({ status: 402, problem: { code: 'otra_cosa' } })).toBe(false)
|
||||
expect(isPlanProblem({ status: 409, problem: { code: 'plan_limit_reached' } })).toBe(false)
|
||||
expect(isPlanProblem(new Error('x'))).toBe(false)
|
||||
expect(isPlanProblem(null)).toBe(false)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user