import { describe, it, expect, beforeEach } from 'vitest';
import {
  createFleetGroup,
  groupsForAgent,
  loadFleetGroups,
  normalizeGroupColor,
  primaryGroupForAgent,
  saveFleetGroups,
} from './fleetGroups';

describe('fleetGroups', () => {
  beforeEach(() => {
    localStorage.clear();
  });

  it('normalizeGroupColor accepts hex', () => {
    expect(normalizeGroupColor('#abc')).toBe('#aabbcc');
    expect(normalizeGroupColor('#aabbcc')).toBe('#aabbcc');
  });

  it('persists and loads groups', () => {
    const g = createFleetGroup('Rack A', '#ff00ff', ['a1', 'a2']);
    saveFleetGroups([g]);
    const loaded = loadFleetGroups();
    expect(loaded).toHaveLength(1);
    expect(loaded[0].name).toBe('Rack A');
    expect(loaded[0].agentIds).toEqual(['a1', 'a2']);
  });

  it('groupsForAgent and primaryGroupForAgent', () => {
    const groups = [
      createFleetGroup('G1', '#00f5ff', ['x']),
      createFleetGroup('G2', '#ff0000', ['x', 'y']),
    ];
    expect(groupsForAgent(groups, 'x').map((g) => g.name)).toEqual(['G1', 'G2']);
    expect(primaryGroupForAgent(groups, 'x')?.name).toBe('G1');
    saveFleetGroups(groups);
    expect(loadFleetGroups()).toHaveLength(2);
  });
});
