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

import { SPARK_CHARS, toBeatCells, toSparkline } from "./beatmap";

describe("toBeatCells", () => {
  it("maps min→▁ and max→▇ via linear normalization", () => {
    const cells = toBeatCells([1, 7]);
    expect(cells[0]?.level).toBe(0);
    expect(cells[0]?.char).toBe("▁");
    expect(cells[1]?.level).toBe(SPARK_CHARS.length - 1);
    expect(cells[1]?.char).toBe("▇");
  });

  it("centers all cells when every value is equal", () => {
    const cells = toBeatCells([5, 5, 5]);
    expect(cells.every((c) => c.level === 3)).toBe(true);
    expect(cells.every((c) => c.char === "▄")).toBe(true);
  });

  it("gives every cell a visible min height", () => {
    const cells = toBeatCells([0, 10]);
    expect(cells[0]?.heightPct).toBeGreaterThanOrEqual(12);
    expect(cells[1]?.heightPct).toBe(100);
  });

  it("returns [] for empty or non-finite-only input", () => {
    expect(toBeatCells([])).toEqual([]);
    expect(toBeatCells([Number.NaN, Number.POSITIVE_INFINITY])).toEqual([]);
  });
});

describe("toSparkline", () => {
  it("joins chars into a sparkline string", () => {
    expect(toSparkline([1, 4, 7])).toBe("▁▄▇");
    expect(toSparkline([])).toBe("");
  });
});
