import { describe, expect, it } from 'vitest'; import { isE164, phoneLast4, toE164 } from '../src/phone'; /** * These are security tests, not formatting tests. `users.phone` is UNIQUE and * bans are per-account, so any pair of inputs that normalises to two different * strings for one real handset is a way to hold two accounts and to escape a ban. */ describe('toE164', () => { it('passes through an already-normalised number', () => { expect(toE164('+34600111222')).toBe('+34600111222'); }); it('collapses every separator style a human types to ONE stored form', () => { const forms = [ '+34 600 111 222', '+34-600-111-222', '+34 (600) 111.222', ' +34600111222 ', '0034600111222', '0034 600 111 222', ]; const normalised = new Set(forms.map(toE164)); expect(normalised).toEqual(new Set(['+34600111222'])); }); it('refuses a bare national number rather than guessing a country', () => { // Guessing would attach one person's account to another person's number. expect(toE164('600111222')).toBeNull(); expect(toE164('0600111222')).toBeNull(); }); it('rejects junk, empties and letters', () => { expect(toE164(null)).toBeNull(); expect(toE164(undefined)).toBeNull(); expect(toE164('')).toBeNull(); expect(toE164(' ')).toBeNull(); expect(toE164('+34600ABC222')).toBeNull(); expect(toE164('not a phone')).toBeNull(); }); it('enforces E.164 length and a nonzero country digit', () => { expect(toE164('+3460011')).toBeNull(); // too short expect(toE164('+3460011122233344')).toBeNull(); // too long expect(toE164('+0600111222')).toBeNull(); // country code cannot start with 0 }); }); describe('isE164', () => { it('accepts only the canonical stored form', () => { expect(isE164('+34600111222')).toBe(true); expect(isE164('0034600111222')).toBe(false); expect(isE164('+34 600 111 222')).toBe(false); }); }); describe('phoneLast4', () => { it('returns the last four digits for masked display', () => { expect(phoneLast4('+34600111222')).toBe('1222'); }); });