// src/utils/priceParser.test.ts
import { describe, it, expect } from 'vitest';
import { parsePriceToCents } from './priceParser';

describe('parsePriceToCents', () => {
  // Test cases for standard dollar formats
  it('should parse standard dollar formats correctly', () => {
    expect(parsePriceToCents('$10.99')).toBe(1099);
    expect(parsePriceToCents('10.99')).toBe(1099);
    expect(parsePriceToCents('$0.99')).toBe(99);
    expect(parsePriceToCents('.99')).toBe(99);
    expect(parsePriceToCents('$5')).toBe(500);
    expect(parsePriceToCents('5')).toBe(500);
  });

  // Test cases for cents format
  it('should parse cents format correctly', () => {
    expect(parsePriceToCents('99¢')).toBe(99);
    expect(parsePriceToCents('99 ¢')).toBe(99);
    expect(parsePriceToCents('125¢')).toBe(125);
  });

  // Test cases for rounding
  it('should round decimal cents to the nearest whole number', () => {
    expect(parsePriceToCents('89.9¢')).toBe(90);
    expect(parsePriceToCents('89.1¢')).toBe(89);
  });

  it('should round decimal dollars to the nearest cent', () => {
    expect(parsePriceToCents('$10.995')).toBe(1100);
    expect(parsePriceToCents('1.234')).toBe(123);
  });

  it('should treat whole numbers as dollars', () => {
    expect(parsePriceToCents('99')).toBe(9900);
    expect(parsePriceToCents('1')).toBe(100);
  });

  // Test cases for invalid or unhandled formats
  it('should return null for "X for Y" formats', () => {
    expect(parsePriceToCents('2 for $5.00')).toBeNull();
    expect(parsePriceToCents('3 for 10')).toBeNull();
  });

  it('should return null for non-price strings', () => {
    expect(parsePriceToCents('FREE')).toBeNull();
    expect(parsePriceToCents('See in store')).toBeNull();
    expect(parsePriceToCents('abc')).toBeNull();
  });

  it('should return null for empty or invalid inputs', () => {
    expect(parsePriceToCents('')).toBeNull();
    expect(parsePriceToCents(' ')).toBeNull();
    expect(parsePriceToCents(null as unknown as string)).toBeNull();
    expect(parsePriceToCents(undefined as unknown as string)).toBeNull();
  });

  // Test cases for whitespace handling
  it('should handle leading/trailing whitespace', () => {
    expect(parsePriceToCents('  $10.99  ')).toBe(1099);
    expect(parsePriceToCents('  99¢  ')).toBe(99);
  });
});