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

import { distinctLabelValues, binaryLabelMask, isMultiLabel } from './labelMask';

describe('distinctLabelValues', () => {
  it('returns sorted distinct non-zero integers, ignoring 0', () => {
    expect(distinctLabelValues([0, 5, 1, 5, 0, 3, 1])).toEqual([1, 3, 5]);
  });

  it('ignores zero-only / empty volumes', () => {
    expect(distinctLabelValues([0, 0, 0])).toEqual([]);
    expect(distinctLabelValues([])).toEqual([]);
  });

  it('skips non-integer (continuous) values — those are images, not labels', () => {
    expect(distinctLabelValues([0, 1.5, 2, 2.0, 3.7])).toEqual([2]);
  });

  it('works on a typed array (the real scalar buffer type)', () => {
    expect(distinctLabelValues(new Uint8Array([0, 2, 2, 117, 1]))).toEqual([1, 2, 117]);
  });
});

describe('binaryLabelMask', () => {
  it('marks 1 only where the scalar equals the value', () => {
    expect(Array.from(binaryLabelMask([0, 5, 1, 5, 3], 5))).toEqual([0, 1, 0, 1, 0]);
  });

  it('returns an all-zero mask for an absent value', () => {
    expect(Array.from(binaryLabelMask([1, 2, 3], 9))).toEqual([0, 0, 0]);
  });

  it('preserves length and is Uint8', () => {
    const m = binaryLabelMask([1, 2, 1], 1);
    expect(m).toBeInstanceOf(Uint8Array);
    expect(m.length).toBe(3);
  });
});

describe('isMultiLabel', () => {
  it('is true with ≥2 distinct organ labels, false otherwise', () => {
    expect(isMultiLabel([0, 1, 2])).toBe(true);
    expect(isMultiLabel([0, 1, 1, 0])).toBe(false);
    expect(isMultiLabel([0, 0])).toBe(false);
  });
});
