UNPKG

asksuite-core

Version:
172 lines (119 loc) 4.51 kB
const encoder = require('../util/IntEncoder')(); const iterations = 100; const range = 20000000; describe('Encoding should be reversible', () => { const numsToTest = []; for (let i = 0; i < iterations; i++) { numsToTest.push(Math.floor(Math.random() * range)); } test.each(numsToTest)('%i', numToTest => { const encoded = encoder.encode(numToTest); const decoded = encoder.decode(encoded); expect(decoded).toEqual(numToTest); console.log(`Input ${numToTest} generated the output ${encoded}`); expect(encoder.validate(encoded)).toStrictEqual(true); }); }); describe('Encoding', () => { test('Should process a number', () => { const encoded = encoder.encode(1234); expect(encoded).toEqual('t4'); }); test('Number zero should be processed', () => { const encoded = encoder.encode(0); expect(encoded).not.toBe(null); expect(encoded).toEqual('a'); }); test('Processing a string should fail', () => { const encoded = encoder.encode('abcd'); expect(encoded).toBe(null); }); }); describe('Decoding', () => { test('Should process a string', () => { const decoded = encoder.decode('abcd'); expect(decoded).toEqual(3971); }); test('Should process a number', () => { const decoded = encoder.decode(1234); expect(decoded).toEqual(12842426); }); test('Number zero should be processed', () => { const decoded = encoder.decode(0); expect(decoded).toEqual(52); }); test('Undefined value should fail', () => { const decoded = encoder.decode(); expect(decoded).toBe(null); }); test('Null value should fail', () => { const decoded = encoder.decode(null); expect(decoded).toBe(null); }); test('Empty string should fail', () => { const decoded = encoder.decode(''); expect(decoded).toBe(null); }); test('Unexpected characters should fail', () => { const decoded = encoder.decode('abçdef'); expect(decoded).toBe(null); }); }); describe('Extracting', () => { test('Should extract string at the end', () => { const extracted = encoder.extract('Continuar atendimento #f54Eg'); expect(extracted).toEqual('f54Eg'); }); test('Should extract string at the beginning', () => { const extracted = encoder.extract('#f54Eg Continuar atendimento'); expect(extracted).toEqual('f54Eg'); }); test('Should extract string in the middle', () => { const extracted = encoder.extract('Continuar atendimento #f54Eg iniciado no site'); expect(extracted).toEqual('f54Eg'); }); test('Should extract whole string', () => { const extracted = encoder.extract('#f54Eg'); expect(extracted).toEqual('f54Eg'); }); test('Should extract integer', () => { const extracted = encoder.extract('Continuar atendimento #1568'); expect(extracted).toEqual('1568'); }); test('Should fail when nothing to extract', () => { const extracted = encoder.extract('Quero continuar com o atendimento'); expect(extracted).toBe(null); }); test('Should fail when misformatted group id [1]', () => { const extracted = encoder.extract('Continuar atendimento #çd86s'); expect(extracted).toBe(null); }); test('Should fail when misformatted group id [2]', () => { const extracted = encoder.extract('Continuar atendimento #d8ç6s'); expect(extracted).toBe(null); }); test('Should extract complex string at the end', () => { const extracted = encoder.extract('Continuar atendimento #f54Eg-k4Du'); expect(extracted).toBe('f54Eg-k4Du'); }); test('Should extract complex string at the beginning', () => { const extracted = encoder.extract('#f54Eg-k4Du continuar atendimento'); expect(extracted).toBe('f54Eg-k4Du'); }); test('Should extract complex string in the middle', () => { const extracted = encoder.extract('Continuar #f54Eg-k4Du atendimento'); expect(extracted).toBe('f54Eg-k4Du'); }); test('Should fail when misformatted complex string [1]', () => { const extracted = encoder.extract('Continuar atendimento #f54Eg-d8ç6s'); expect(extracted).toBe(null); }); test('Should fail when misformatted complex string [2]', () => { const extracted = encoder.extract('Continuar atendimento #f54Eg-d#8c6s'); expect(extracted).toBe(null); }); test('Should fail when misformatted complex string [3]', () => { const extracted = encoder.extract('Continuar atendimento #f54Eg-d8c-6s'); expect(extracted).toBe(null); }); });