const { formatAmount } = require('../services/payMongo');
const { parseAmount } = require('../services/payMongo');

describe('formatAmount', () => {
  it('should convert a number amount to cents', () => {
    expect(formatAmount(10)).toBe(1000);
    expect(formatAmount(20.5)).toBe(2050);
    expect(formatAmount(0.99)).toBe(99);
  });

  it('should convert a string amount to cents', () => {
    expect(formatAmount('10')).toBe(1000);
    expect(formatAmount('20.5')).toBe(2050);
    expect(formatAmount('0.99')).toBe(99);
  });

  it('should round to the nearest integer', () => {
    expect(formatAmount(10.125)).toBe(1013);
    expect(formatAmount(10.124)).toBe(1012);
    expect(formatAmount('10.125')).toBe(1013);
  });

  it('should throw an error for invalid string amount', () => {
    expect(() => formatAmount('abc')).toThrow('Invalid amount string: "abc"');
    expect(() => formatAmount('not a number')).toThrow('Invalid amount string: "not a number"');
    expect(() => formatAmount('')).toThrow('Invalid amount string: ""');
  });

  it('should throw an error for NaN number', () => {
    expect(() => formatAmount(NaN)).toThrow('Invalid amount: NaN');
  });

  it('should throw an error for non-number, non-string types', () => {
    // @ts-ignore
    expect(() => formatAmount(null)).toThrow('Invalid amount: null');
    // @ts-ignore
    expect(() => formatAmount(undefined)).toThrow('Invalid amount: undefined');
    // @ts-ignore
    expect(() => formatAmount({})).toThrow();
    // @ts-ignore
    expect(() => formatAmount([])).toThrow();
  });

  it('should handle zero amount', () => {
    expect(formatAmount(0)).toBe(0);
    expect(formatAmount('0')).toBe(0);
  });
});

describe('parseAmount', () => {
  it('should convert cents to dollars', () => {
    expect(parseAmount(1000)).toBe(10);
    expect(parseAmount(2050)).toBe(20.5);
    expect(parseAmount(99)).toBe(0.99);
  });

  it('should handle zero amount', () => {
    expect(parseAmount(0)).toBe(0);
  });

  it('should throw an error for NaN', () => {
    expect(() => parseAmount(NaN)).toThrow('Invalid amount: NaN');
  });

  it('should throw an error for non-number types', () => {
    // @ts-ignore
    expect(() => parseAmount('1000')).toThrow('Invalid amount: 1000');
    // @ts-ignore
    expect(() => parseAmount(null)).toThrow('Invalid amount: null');
    // @ts-ignore
    expect(() => parseAmount(undefined)).toThrow('Invalid amount: undefined');
    // @ts-ignore
    expect(() => parseAmount({})).toThrow();
    // @ts-ignore
    expect(() => parseAmount([])).toThrow();
  });

  it('should handle negative amounts', () => {
    expect(parseAmount(-1000)).toBe(-10);
    expect(parseAmount(-50)).toBe(-0.5);
  });
});
