import { parseTtl } from '../../src/ttl/parse_ttl';

describe('parseTtl (util)', () => {
  describe('Base', () => {
    it('Should work for number', () => {
      const [out] = parseTtl('1');
      const [one] = parseTtl(1);

      expect(out).toEqual(one);
      expect(one).toEqual(1);
    });
    it('Should work for seconds', () => {
      const [seconds1] = parseTtl('1s');

      expect(seconds1).toBeGreaterThan(0);
    });
    it('Should work for seconds - double digit', () => {
      const [seconds1] = parseTtl('10s');

      expect(seconds1).toBeGreaterThan(9);
    });
    it('Should work for days', () => {
      const [day1] = parseTtl('1d');

      expect(day1).toBeGreaterThan(0);
    });
    it('Should work for days - double digit', () => {
      const [day1] = parseTtl('10d');

      expect(day1).toBeGreaterThan(10);
    });
    it('Should work for hours', () => {
      const [hour1] = parseTtl('1h');

      expect(hour1).toBeGreaterThan(0);
    });
    it('Should work for weeks', () => {
      const [week1] = parseTtl('1w');

      expect(week1).toBeGreaterThan(0);
    });
    it('Should work for months', () => {
      const [month1] = parseTtl('1m');

      expect(month1).toBeGreaterThan(0);
    });
    it('Should work for years', () => {
      const [year1] = parseTtl('1y');

      expect(year1).toBeGreaterThan(0);
    });
  });

  describe('Combinations', () => {
    it('Should work for combination', () => {
      const [result, error] = parseTtl('1m1w1d');

      expect(result).toEqual(3110400);
      expect(error).toBeFalsy();
    });
    // could make it not work...
    it('Should work for doubles', () => {
      const [result, error] = parseTtl('1m1m');

      expect(result).toEqual(parseTtl('1m')[0] + parseTtl('1m')[0]);
      expect(error).toBeFalsy();
    });
    it('Should calculate the same all the way up', () => {
      const [seconds3600] = parseTtl('3600s');
      const [hour1] = parseTtl('1h');

      expect(seconds3600).toEqual(hour1);

      const [day1] = parseTtl('1d');
      const [hour24] = parseTtl('24h');

      expect(day1).toEqual(hour24);

      const [day7] = parseTtl('7d');
      const [week1] = parseTtl('1w');

      expect(day7).toEqual(week1);

      const [week4] = parseTtl('4w');
      const [month1] = parseTtl('1m');

      expect(week4).toEqual(month1);

      const [month12] = parseTtl('12m');
      const [year1] = parseTtl('1y');

      expect(month12).toEqual(year1);

      const [combination] = parseTtl('1m1w1d24h3600s');

      expect(combination).toEqual(
        month1! + week1! + day1! + hour24! + seconds3600!
      );
    });
  });

  describe('Errors', () => {
    it('Should return error when value is too big', () => {
      const [, error] = parseTtl('420y');

      expect(error).toBeTruthy();
    });
    it('Should work for fallback', () => {
      const [result = 42] = parseTtl(null as any);

      expect(result).toEqual(42);
    });
    it('Should not work for spaces', () => {
      const [value, error] = parseTtl('100 s');

      expect(value).toBeFalsy();
      expect(error).toBeTruthy();
    });
    it('Should not work for invalid - unless we use default', () => {
      const [value = -42] = parseTtl('100 s');

      expect(value).toEqual(-42);
    });
    it('Should not work for invalid letters', () => {
      const [, error] = parseTtl('100z');

      expect(error).toBeTruthy();
    });
    it('Should not work for double letters', () => {
      const [, error] = parseTtl('100yy');

      expect(error).toBeTruthy();
    });
  });
});
