import { describe, it, expect } from 'bun:test';
import { colorForGroup } from '../../../apps/web/src/lib/utils/groupColor';

function isHexColor(s: string): boolean {
  return /^#[0-9A-Fa-f]{6}$/.test(s);
}

describe('groupColor - colorForGroup', () => {
  it('devuelve null para groupId vacío o nulo', () => {
    expect(colorForGroup(null)).toBeNull();
    expect(colorForGroup(undefined)).toBeNull();
    expect(colorForGroup('')).toBeNull();
    expect(colorForGroup('   ')).toBeNull();
  });

  it('es determinista: misma entrada → misma salida', () => {
    const a = colorForGroup('123@g.us');
    const b = colorForGroup('123@g.us');
    expect(a).not.toBeNull();
    expect(b).not.toBeNull();
    expect(a?.border).toBe(b?.border);
    expect(a?.bg).toBe(b?.bg);
    expect(a?.text).toBe(b?.text);
  });

  it('devuelve colores hex válidos', () => {
    const c = colorForGroup('group-xyz@g.us');
    expect(c).not.toBeNull();
    expect(isHexColor(c!.border)).toBe(true);
    expect(isHexColor(c!.bg)).toBe(true);
    expect(isHexColor(c!.text)).toBe(true);
  });

  it('tiene distribución razonable en distintos IDs', () => {
    const uniq = new Set<string>();
    for (let i = 0; i < 30; i++) {
      const c = colorForGroup(`group-${i}@g.us`);
      if (c) uniq.add(`${c.border}|${c.bg}|${c.text}`);
    }
    // Con 12 paletas, deberíamos cubrir bastantes índices con 30 IDs
    expect(uniq.size).toBeGreaterThan(8);
  });
});
