import { rgba, phaseFromProgress, PHASES, PHASE_LABELS, type PhaseKey } from "./constants";

describe("optimization/constants — rgba()", () => {
  it("converts a hex string to an rgba() string", () => {
    expect(rgba("#22C55E", 0.5)).toBe("rgba(34, 197, 94, 0.5)");
    expect(rgba("#000000", 1)).toBe("rgba(0, 0, 0, 1)");
    expect(rgba("#FFFFFF", 0)).toBe("rgba(255, 255, 255, 0)");
  });

  it("is case-insensitive for hex digits", () => {
    expect(rgba("#ffffff", 1)).toBe(rgba("#FFFFFF", 1));
  });
});

describe("optimization/constants — phaseFromProgress()", () => {
  it("returns the right phase at each threshold", () => {
    expect(phaseFromProgress(PHASES.chaos)).toBe("chaos");
    expect(phaseFromProgress(PHASES.scan)).toBe("scan");
    expect(phaseFromProgress(PHASES.dissolve)).toBe("dissolve");
    expect(phaseFromProgress(PHASES.optimize)).toBe("optimize");
    expect(phaseFromProgress(PHASES.reorganize)).toBe("reorganize");
    expect(phaseFromProgress(PHASES.metrics)).toBe("metrics");
  });

  it("returns the previous phase just below a threshold", () => {
    expect(phaseFromProgress(PHASES.scan - 0.001)).toBe("chaos");
    expect(phaseFromProgress(PHASES.dissolve - 0.001)).toBe("scan");
    expect(phaseFromProgress(PHASES.metrics - 0.001)).toBe("reorganize");
  });

  it("handles the 0 and 1 extremes", () => {
    expect(phaseFromProgress(0)).toBe("chaos");
    expect(phaseFromProgress(1)).toBe("metrics");
  });

  it("only ever returns a defined phase key across a sweep", () => {
    const keys = Object.keys(PHASES) as PhaseKey[];
    for (let p = 0; p <= 1.0001; p += 0.02) {
      expect(keys).toContain(phaseFromProgress(p));
    }
  });
});

describe("optimization/constants — phase tables", () => {
  it("has strictly increasing phase thresholds", () => {
    const vals = Object.values(PHASES);
    for (let i = 1; i < vals.length; i++) {
      expect(vals[i]).toBeGreaterThan(vals[i - 1]);
    }
  });

  it("has a label for every phase key", () => {
    for (const key of Object.keys(PHASES) as PhaseKey[]) {
      expect(typeof PHASE_LABELS[key]).toBe("string");
      expect(PHASE_LABELS[key].length).toBeGreaterThan(0);
    }
  });
});
