import { describe, expect, test } from 'vitest';
import { inferColumnType } from './type-inference';

describe('inferColumnType', () => {
  test('detects a clean number column', () => {
    expect(inferColumnType([1, 2, 3])).toBe('number');
    expect(inferColumnType(['1', '2.5', ' 3 '])).toBe('number');
  });

  test('detects a clean string column', () => {
    expect(inferColumnType(['apple', 'banana', 'cherry'])).toBe('string');
  });

  test('detects a clean date column', () => {
    expect(inferColumnType(['2024-01-01', '2025-12-31'])).toBe('date');
    expect(inferColumnType(['2024/01/01', '12/31/2024'])).toBe('date');
  });

  test('detects a clean boolean column (any case)', () => {
    expect(inferColumnType(['true', 'false'])).toBe('boolean');
    expect(inferColumnType(['TRUE', 'False'])).toBe('boolean');
    expect(inferColumnType([true, false])).toBe('boolean');
  });

  test('a mixed column falls to string', () => {
    expect(inferColumnType([1, 2, 'three'])).toBe('string');
    expect(inferColumnType(['2024-01-01', 'not-a-date'])).toBe('string');
  });

  test('ignores empty / whitespace cells before classifying', () => {
    expect(inferColumnType([1, null, 2, undefined, '  ', 3])).toBe('number');
    expect(inferColumnType(['true', '', 'false', '   '])).toBe('boolean');
  });

  test('an all-empty or zero-length column is string', () => {
    expect(inferColumnType([])).toBe('string');
    expect(inferColumnType([null, undefined, '', '   '])).toBe('string');
  });

  test('precedence: a true/false column is boolean, not string', () => {
    expect(inferColumnType(['true', 'false'])).toBe('boolean');
  });

  test('precedence: a bare-year column is number, not date', () => {
    expect(inferColumnType(['2024', '2025'])).toBe('number');
  });

  test('date shape guard rejects bare numbers and words even if Date.parse might accept them', () => {
    expect(inferColumnType(['42'])).toBe('number');
    expect(inferColumnType(['hello'])).toBe('string');
    expect(inferColumnType(['March'])).toBe('string');
  });

  test('0 and 1 are number, never boolean', () => {
    expect(inferColumnType([0, 1, 0, 1])).toBe('number');
    expect(inferColumnType(['0', '1'])).toBe('number');
  });
});
