import { describe, expect, it } from 'vitest' import { communicationSettingsSchema, createManualPaymentSubmissionSchema, manualCheckoutSchema, } from './subscription.schemas' import { addBillingPeriod, normalizeExternalReference } from './subscription.manual.service' describe('manual subscription payment contracts', () => { it('accepts only bank transfer and check manual checkout', () => { const base = { plan: 'GROWTH', billingPeriod: 'ANNUAL', currency: 'MAD', idempotencyKey: crypto.randomUUID() } expect(manualCheckoutSchema.safeParse({ ...base, method: 'BANK_TRANSFER' }).success).toBe(true) expect(manualCheckoutSchema.safeParse({ ...base, method: 'STRIPE' }).success).toBe(false) }) it('normalizes references without discarding the display value', () => { const submittedReference = ' bank txn-123 ' const parsed = createManualPaymentSubmissionSchema.parse({ method: 'BANK_TRANSFER', submittedReference, idempotencyKey: crypto.randomUUID() }) expect(parsed.submittedReference).toBe('bank txn-123') expect(normalizeExternalReference(parsed.submittedReference)).toBe('BANK TXN-123') }) it('enforces the company-enabled communication language set', () => { const valid = { timezone: 'Africa/Casablanca', reminderLocalTime: '09:00', enabledCommunicationLocales: ['ar', 'fr'], defaultCommunicationLocale: 'fr', contacts: [{ email: 'billing@example.ma', locale: 'ar', isPrimary: true, receivePaymentNotices: true, isActive: true }], } expect(communicationSettingsSchema.safeParse(valid).success).toBe(true) expect(communicationSettingsSchema.safeParse({ ...valid, defaultCommunicationLocale: 'en' }).success).toBe(false) expect(communicationSettingsSchema.safeParse({ ...valid, contacts: [{ ...valid.contacts[0], locale: 'en' }] }).success).toBe(false) expect(communicationSettingsSchema.safeParse({ ...valid, enabledCommunicationLocales: [] }).success).toBe(false) }) it('clamps month-end and leap-year billing periods', () => { expect(addBillingPeriod(new Date('2028-01-31T12:00:00.000Z'), 'MONTHLY').toISOString()).toBe('2028-02-29T12:00:00.000Z') expect(addBillingPeriod(new Date('2028-02-29T12:00:00.000Z'), 'ANNUAL').toISOString()).toBe('2029-02-28T12:00:00.000Z') }) })