|
| 1 | +/** |
| 2 | + * Dominican Republic Cedula de Identidad y Electoral (Issue #57) |
| 3 | + */ |
| 4 | +import { Cedula } from '../countries/dom'; |
| 5 | +import { CEDULA_LUHN_EXCEPTION_SET } from '../countries/dom/exceptions'; |
| 6 | +import { validateNationalId, parseIdInfo } from '../index'; |
| 7 | + |
| 8 | +describe('Dominican Republic Cedula (DOM)', () => { |
| 9 | + describe('METADATA', () => { |
| 10 | + it('should have a valid example that passes validateNationalId', () => { |
| 11 | + expect(validateNationalId('DOM', Cedula.METADATA.example!).isValid).toBe(true); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should declare the expected shape', () => { |
| 15 | + expect(Cedula.METADATA.iso3166Alpha2).toBe('DO'); |
| 16 | + expect(Cedula.METADATA.minLength).toBe(11); |
| 17 | + expect(Cedula.METADATA.maxLength).toBe(11); |
| 18 | + expect(Cedula.METADATA.parsable).toBe(true); |
| 19 | + expect(Cedula.METADATA.checksum).toBe(true); |
| 20 | + expect(Cedula.METADATA.officialName).toBe('Cédula de Identidad y Electoral'); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('validate() - standard Luhn vectors', () => { |
| 25 | + it('accepts a valid modern 402-series number (sum=8, check=2)', () => { |
| 26 | + expect(Cedula.validate('40200000012')).toBe(true); |
| 27 | + }); |
| 28 | + |
| 29 | + it('accepts a valid legacy-series number (sum=3, check=7)', () => { |
| 30 | + expect(Cedula.validate('00100000017')).toBe(true); |
| 31 | + }); |
| 32 | + |
| 33 | + it('accepts a valid number that exercises the >9 digit-doubling fold', () => { |
| 34 | + // 5*2=10->1, 7*2=14->5; sum=33, check=7 |
| 35 | + expect(Cedula.validate('03112345677')).toBe(true); |
| 36 | + }); |
| 37 | + |
| 38 | + it('rejects the 402-series vector with the check digit off by one', () => { |
| 39 | + expect(Cedula.validate('40200000013')).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + it('rejects the legacy-series vector with the check digit off by one', () => { |
| 43 | + expect(Cedula.validate('00100000018')).toBe(false); |
| 44 | + }); |
| 45 | + |
| 46 | + it('rejects the fold-exercising vector with the check digit off by one', () => { |
| 47 | + expect(Cedula.validate('03112345678')).toBe(false); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('validate() - exception list (the critical case)', () => { |
| 52 | + it('accepts a real modern-series cedula that fails standard Luhn', () => { |
| 53 | + // Without the exception list this returns false: 4,0,2,0,0,0,4,0,1,3 -> check=9, not 4. |
| 54 | + expect(Cedula.validate('40200401324')).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it('rejects the exception-list vector if the check digit is altered', () => { |
| 58 | + // Sanity check: the exception match is exact-string, not "close enough". |
| 59 | + expect(Cedula.validate('40200401325')).toBe(false); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('validate() - series range', () => { |
| 64 | + it('does not reject series "000"', () => { |
| 65 | + expect(Cedula.validate('00000000018')).toBe(true); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('validate() - format handling', () => { |
| 70 | + it('accepts the canonical NNN-NNNNNNN-N display format', () => { |
| 71 | + expect(Cedula.validate('402-0000001-2')).toBe(true); |
| 72 | + }); |
| 73 | + |
| 74 | + it('accepts input with surrounding/embedded whitespace', () => { |
| 75 | + expect(Cedula.validate(' 402 0000001 2 ')).toBe(true); |
| 76 | + }); |
| 77 | + |
| 78 | + it('rejects non-string input', () => { |
| 79 | + expect(Cedula.validate(undefined as unknown as string)).toBe(false); |
| 80 | + expect(Cedula.validate(12345678901 as unknown as string)).toBe(false); |
| 81 | + }); |
| 82 | + |
| 83 | + it('rejects input that is too short', () => { |
| 84 | + expect(Cedula.validate('4020000001')).toBe(false); |
| 85 | + }); |
| 86 | + |
| 87 | + it('rejects input that is too long', () => { |
| 88 | + expect(Cedula.validate('402000000122')).toBe(false); |
| 89 | + }); |
| 90 | + |
| 91 | + it('rejects input containing non-digit characters', () => { |
| 92 | + expect(Cedula.validate('4020000001A')).toBe(false); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('parse()', () => { |
| 97 | + it('returns series, sequence, and checkDigit for a valid number', () => { |
| 98 | + expect(Cedula.parse('40200000012')).toEqual({ |
| 99 | + series: '402', |
| 100 | + sequence: '0000001', |
| 101 | + checkDigit: 2, |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it('parses formatted input identically to compact input', () => { |
| 106 | + expect(Cedula.parse('402-0000001-2')).toEqual(Cedula.parse('40200000012')); |
| 107 | + }); |
| 108 | + |
| 109 | + it('returns null for an invalid number', () => { |
| 110 | + expect(Cedula.parse('40200000013')).toBeNull(); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('checksum()', () => { |
| 115 | + it('computes the expected Luhn check digit from the first 10 digits', () => { |
| 116 | + expect(Cedula.checksum('40200000012')).toBe(2); |
| 117 | + expect(Cedula.checksum('00100000017')).toBe(7); |
| 118 | + expect(Cedula.checksum('03112345677')).toBe(7); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('exception data set', () => { |
| 123 | + it('contains exactly 576 entries', () => { |
| 124 | + expect(CEDULA_LUHN_EXCEPTION_SET.size).toBe(576); |
| 125 | + }); |
| 126 | + |
| 127 | + it('contains only 11-digit numeric strings', () => { |
| 128 | + for (const entry of CEDULA_LUHN_EXCEPTION_SET) { |
| 129 | + expect(entry).toMatch(/^\d{11}$/); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + it('does not contain the two known upstream 10-digit typo entries', () => { |
| 134 | + expect(CEDULA_LUHN_EXCEPTION_SET.has('0094662667')).toBe(false); |
| 135 | + expect(CEDULA_LUHN_EXCEPTION_SET.has('0710208838')).toBe(false); |
| 136 | + }); |
| 137 | + |
| 138 | + it('every entry fails the standard Luhn check (that is the point of the list)', () => { |
| 139 | + for (const entry of CEDULA_LUHN_EXCEPTION_SET) { |
| 140 | + const expectedCheck = Cedula.checksum(entry); |
| 141 | + const actualCheck = parseInt(entry[10], 10); |
| 142 | + expect(expectedCheck).not.toBe(actualCheck); |
| 143 | + } |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('registry integration', () => { |
| 148 | + it('validates via the DOM alpha-3 key', () => { |
| 149 | + expect(validateNationalId('DOM', '40200000012').isValid).toBe(true); |
| 150 | + }); |
| 151 | + |
| 152 | + it('validates via the DO alpha-2 alias with the same result', () => { |
| 153 | + const viaAlpha3 = validateNationalId('DOM', '40200000012'); |
| 154 | + const viaAlpha2 = validateNationalId('DO', '40200000012'); |
| 155 | + expect(viaAlpha2.isValid).toBe(viaAlpha3.isValid); |
| 156 | + expect(viaAlpha2.countryCode).toBe('DOM'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('parses via the registry for a valid number', () => { |
| 160 | + expect(parseIdInfo('DOM', '40200000012')).toEqual({ |
| 161 | + series: '402', |
| 162 | + sequence: '0000001', |
| 163 | + checkDigit: 2, |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + it('returns null via the registry for an invalid number', () => { |
| 168 | + expect(parseIdInfo('DOM', '40200000013')).toBeNull(); |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments