import { describe, expect, it } from 'vitest';

import { compactHistogramLabel } from './DualHistogram';

describe('compactHistogramLabel', () => {
  it('rounds low-cardinality count labels to integers', () => {
    const fmt = (value: number) => value.toFixed(2);
    const labels = [0, 0.99, 2.98, 4.96, 5.95].map((center, index) =>
      compactHistogramLabel(index, 5, 0, 5.95, center, fmt, true)
    );

    expect(labels).toEqual(['', '1', '3', '5', '6+']);
  });

  it('does not show 0 twice when the low-outlier bin starts at zero', () => {
    const fmt = (value: number) => value.toFixed(2);
    // Integer-binned count feature (e.g. school catchments): p1=0, p99=3,
    // one-unit-wide middle bins. Bin 0 (< 0) is empty, bin 1 holds the value 0.
    const labels = [0, 0.5, 1.5, 2.5, 3].map((center, index) =>
      compactHistogramLabel(index, 5, 0, 3, center, fmt, true)
    );

    expect(labels).toEqual(['', '0', '1', '2', '3+']);
    expect(labels.filter((label) => label === '0')).toHaveLength(1);
  });

  it('labels the first integer count bucket as zero when it means below one', () => {
    const fmt = (value: number) => value.toFixed(2);

    expect(compactHistogramLabel(0, 5, 0.99, 5.95, 0.99, fmt, true)).toBe('0');
  });

  it('keeps fractional labels when integer labels are not requested', () => {
    const fmt = (value: number) => value.toFixed(2);

    expect(compactHistogramLabel(1, 5, 0, 5.95, 0.99, fmt, false)).toBe('0.99');
  });
});
