import { describe, expect, it } from "vitest";
import { parseMeasures } from "./parseMeasures";

describe("parseMeasures", () => {
  it("returns [] for null", () => {
    expect(parseMeasures(null)).toEqual([]);
  });

  it("returns [] for undefined", () => {
    expect(parseMeasures(undefined)).toEqual([]);
  });

  it("returns [] for empty string", () => {
    expect(parseMeasures("")).toEqual([]);
  });

  it("returns [] for whitespace-only string", () => {
    expect(parseMeasures("   ")).toEqual([]);
    expect(parseMeasures("\t\n  ")).toEqual([]);
  });

  it("splits semicolon-separated HubSpot output and trims each entry", () => {
    expect(parseMeasures("ASHP;Solar PV;Loft insulation")).toEqual([
      "ASHP",
      "Solar PV",
      "Loft insulation",
    ]);
  });

  it("trims whitespace around semicolon-separated entries", () => {
    expect(parseMeasures(" ASHP ;  Solar PV ;Loft insulation ")).toEqual([
      "ASHP",
      "Solar PV",
      "Loft insulation",
    ]);
  });

  it("splits legacy comma-separated input and trims", () => {
    expect(parseMeasures("ASHP, Solar PV, Loft insulation")).toEqual([
      "ASHP",
      "Solar PV",
      "Loft insulation",
    ]);
  });

  it("tolerates a mix of semicolons and commas", () => {
    expect(parseMeasures("ASHP; Solar PV, Loft insulation")).toEqual([
      "ASHP",
      "Solar PV",
      "Loft insulation",
    ]);
  });

  it("drops empty entries from trailing or duplicated separators", () => {
    expect(parseMeasures("ASHP;;Solar PV;")).toEqual(["ASHP", "Solar PV"]);
    expect(parseMeasures(",ASHP,,Solar PV,")).toEqual(["ASHP", "Solar PV"]);
  });

  it("returns a single entry when the input contains no separator", () => {
    expect(parseMeasures("ASHP")).toEqual(["ASHP"]);
    expect(parseMeasures("  ASHP  ")).toEqual(["ASHP"]);
  });
});
