import { describe, expect, it } from "vitest";
import {
  collapseAllAllocationGroups,
  createInitialCollapsedAllocationGroups,
  expandAllAllocationGroups,
  toggleCollapsedAllocationGroup,
} from "./allocationGroupState.js";

describe("allocationGroupState", () => {
  it("starts grouped allocations expanded so rows are visible on first load", () => {
    const initial = createInitialCollapsedAllocationGroups();

    expect(initial).toBeInstanceOf(Set);
    expect([...initial]).toEqual([]);
  });

  it("collapses and re-expands a single group from the expanded default", () => {
    const collapsed = toggleCollapsedAllocationGroup(createInitialCollapsedAllocationGroups(), ["r1", "r2"], "r1");
    expect(collapsed).toBeInstanceOf(Set);
    expect([...(collapsed as Set<string>)]).toEqual(["r1"]);

    const expandedAgain = toggleCollapsedAllocationGroup(collapsed, ["r1", "r2"], "r1");
    expect(expandedAgain).toBeInstanceOf(Set);
    expect([...(expandedAgain as Set<string>)]).toEqual([]);
  });

  it("keeps collapse-all behavior available for bulk controls", () => {
    expect(collapseAllAllocationGroups()).toBe("all");

    const expandedOnlyR2 = toggleCollapsedAllocationGroup("all", ["r1", "r2"], "r2");
    expect(expandedOnlyR2).toBeInstanceOf(Set);
    expect([...(expandedOnlyR2 as Set<string>)].sort()).toEqual(["r1"]);

    const expandedAll = expandAllAllocationGroups();
    expect(expandedAll).toBeInstanceOf(Set);
    expect([...(expandedAll as Set<string>)]).toEqual([]);
  });
});
