import { describe, it, expect } from 'vitest' import { toRelativeHistogramData } from './transforms' describe('toRelativeHistogramData', () => { it('rewrites each bin count as its share of the series total in 0-100', () => { expect(toRelativeHistogramData([[10, 20, 70]])).toEqual([[10, 20, 70]]) }) it('handles non-100 totals (5/20 = 25%)', () => { expect(toRelativeHistogramData([[5, 15]])).toEqual([[25, 75]]) }) it('handles multiple series independently', () => { expect( toRelativeHistogramData([ [3, 7], [50, 50], ]), ).toEqual([ [30, 70], [50, 50], ]) }) it('returns a series unchanged when its total is zero', () => { expect(toRelativeHistogramData([[0, 0, 0]])).toEqual([[0, 0, 0]]) }) it('produces signed share-of-magnitude percentages for mixed-sign series', () => { // Denominator is 1000 + 990 = 1990 const out = toRelativeHistogramData([[1000, -990]]) as number[][] expect(out[0]?.[0]).toBeCloseTo(50.25126, 4) expect(out[0]?.[1]).toBeCloseTo(-49.74874, 4) }) it('returns the input unchanged when shape does not match number[][]', () => { expect(toRelativeHistogramData(null)).toBe(null) expect(toRelativeHistogramData('not an array')).toBe('not an array') // Series that isn't a flat number array (e.g. named-value or tuple) // is returned as-is — keeps the transform safe to attach to a // widget whose shape changes through other actions. expect(toRelativeHistogramData([[{ name: 'a', value: 10 }]])).toEqual([ [{ name: 'a', value: 10 }], ]) }) })