import { describe, it, expect } from 'vitest';
import { ProductNumberAdapter } from './product-number.adapter';

describe('ProductNumberAdapter', () => {
  describe('toNumber', () => {
    it('should convert valid numeric string to number', () => {
      expect(ProductNumberAdapter.toNumber('123456')).toBe(123456);
      expect(ProductNumberAdapter.toNumber('0')).toBe(0);
      expect(ProductNumberAdapter.toNumber('999999999')).toBe(999999999);
    });

    it('should return undefined for undefined input', () => {
      expect(ProductNumberAdapter.toNumber(undefined)).toBeUndefined();
    });

    it('should return undefined for empty string', () => {
      expect(ProductNumberAdapter.toNumber('')).toBeUndefined();
    });

    it('should return undefined for invalid numeric string', () => {
      expect(ProductNumberAdapter.toNumber('abc')).toBeUndefined();
      expect(ProductNumberAdapter.toNumber('12.34')).toBe(12); // parseInt behavior
      expect(ProductNumberAdapter.toNumber('12abc')).toBe(12); // parseInt behavior
    });

    it('should handle leading zeros', () => {
      expect(ProductNumberAdapter.toNumber('00123')).toBe(123);
    });
  });

  describe('toString', () => {
    it('should convert valid number to string', () => {
      expect(ProductNumberAdapter.toString(123456)).toBe('123456');
      expect(ProductNumberAdapter.toString(0)).toBe('0');
      expect(ProductNumberAdapter.toString(999999999)).toBe('999999999');
    });

    it('should return undefined for undefined input', () => {
      expect(ProductNumberAdapter.toString(undefined)).toBeUndefined();
    });

    it('should handle negative numbers', () => {
      expect(ProductNumberAdapter.toString(-123)).toBe('-123');
    });

    it('should handle decimal numbers', () => {
      expect(ProductNumberAdapter.toString(12.34)).toBe('12.34');
    });
  });

  describe('isValid', () => {
    it('should return true for valid numeric strings', () => {
      expect(ProductNumberAdapter.isValid('123')).toBe(true);
      expect(ProductNumberAdapter.isValid('0')).toBe(true);
    });

    it('should return true for valid numbers', () => {
      expect(ProductNumberAdapter.isValid(123)).toBe(true);
      expect(ProductNumberAdapter.isValid(0)).toBe(true);
    });

    it('should return false for invalid strings', () => {
      expect(ProductNumberAdapter.isValid('abc')).toBe(false);
      expect(ProductNumberAdapter.isValid('')).toBe(false);
    });

    it('should return false for invalid numbers', () => {
      expect(ProductNumberAdapter.isValid(NaN)).toBe(false);
      expect(ProductNumberAdapter.isValid(Infinity)).toBe(false);
      expect(ProductNumberAdapter.isValid(-Infinity)).toBe(false);
    });

    it('should return false for non-string/non-number types', () => {
      expect(ProductNumberAdapter.isValid(null)).toBe(false);
      expect(ProductNumberAdapter.isValid(undefined)).toBe(false);
      expect(ProductNumberAdapter.isValid({})).toBe(false);
      expect(ProductNumberAdapter.isValid([])).toBe(false);
      expect(ProductNumberAdapter.isValid(true)).toBe(false);
    });
  });
});
