import { describe, expect, it } from "vitest";
import { groupMeasures, measureCategory, measureLabel } from "./measures";

describe("measureCategory", () => {
  it("maps known measure types to their category", () => {
    expect(measureCategory("cavity_wall_insulation")).toBe("Wall insulation");
    expect(measureCategory("solar_pv")).toBe("Solar");
    expect(measureCategory("air_source_heat_pump")).toBe("Heating");
  });

  it("falls back to Other for unknown measure types", () => {
    expect(measureCategory("mystery_measure")).toBe("Other");
  });
});

describe("measureLabel", () => {
  it("title-cases a snake_case measure type", () => {
    expect(measureLabel("air_source_heat_pump")).toBe("Air Source Heat Pump");
  });
});

describe("groupMeasures", () => {
  const measures = [
    { measureType: "loft_insulation", homesCount: 146, totalCost: 186_000, averageCost: 1274 },
    { measureType: "solar_pv", homesCount: 121, totalCost: 684_000, averageCost: 5653 },
    { measureType: "cavity_wall_insulation", homesCount: 78, totalCost: 349_000, averageCost: 4474 },
    { measureType: "external_wall_insulation", homesCount: 14, totalCost: 63_000, averageCost: 4500 },
  ];

  it("orders categories by spend, biggest first", () => {
    const groups = groupMeasures(measures);
    expect(groups.map((g) => g.category)).toEqual([
      "Solar",
      "Wall insulation",
      "Roof insulation",
    ]);
  });

  it("rolls up cost and homes within a category", () => {
    const wall = groupMeasures(measures).find(
      (g) => g.category === "Wall insulation",
    )!;
    expect(wall.costTotal).toBe(412_000);
    expect(wall.homesTotal).toBe(92);
    // rows ordered by spend within the category
    expect(wall.rows.map((r) => r.measureType)).toEqual([
      "cavity_wall_insulation",
      "external_wall_insulation",
    ]);
  });
});
