|
| 1 | +import { describe, expectTypeOf, it } from 'vitest'; |
| 2 | +import { DataMasking } from '../../src/index.js'; |
| 3 | + |
| 4 | +describe('DataMasking type tests', () => { |
| 5 | + const masker = new DataMasking(); |
| 6 | + |
| 7 | + it('erase preserves the input type', () => { |
| 8 | + const data = { name: 'Jane', ssn: '123' }; |
| 9 | + const result = masker.erase(data, { fields: ['ssn'] }); |
| 10 | + |
| 11 | + expectTypeOf(result).toEqualTypeOf<{ name: string; ssn: string }>(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('erase returns the same type for arrays', () => { |
| 15 | + const data = [1, 2, 3]; |
| 16 | + const result = masker.erase(data); |
| 17 | + |
| 18 | + expectTypeOf(result).toEqualTypeOf<number[]>(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('erase returns null for null input', () => { |
| 22 | + const result = masker.erase(null); |
| 23 | + |
| 24 | + expectTypeOf(result).toEqualTypeOf<null>(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('encrypt returns T | string', async () => { |
| 28 | + const data = { secret: 'value' }; |
| 29 | + const result = await masker.encrypt(data); |
| 30 | + |
| 31 | + expectTypeOf(result).toEqualTypeOf<{ secret: string } | string>(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('decrypt infers T from the generic parameter', async () => { |
| 35 | + const result = await masker.decrypt<{ secret: string }>('ciphertext'); |
| 36 | + |
| 37 | + expectTypeOf(result).toEqualTypeOf<{ secret: string }>(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('decrypt infers T from object input', async () => { |
| 41 | + const data = { ssn: 'encrypted' }; |
| 42 | + const result = await masker.decrypt(data, { fields: ['ssn'] }); |
| 43 | + |
| 44 | + expectTypeOf(result).toEqualTypeOf<{ ssn: string }>(); |
| 45 | + }); |
| 46 | +}); |
0 commit comments