import { describe, expect, it } from "vitest";
import {
  displayQuantity,
  kcalToKilojoules,
  saltGramsToSodiumMg,
  sodiumMgToSaltGrams,
} from "../src/measurement.js";

describe("måttvisning (i18n-spec §9–10, M5)", () => {
  it("METRIC: gram uppgraderas läsbart, kökenheter behålls", () => {
    expect(displayQuantity(500, "GRAM", "METRIC")).toEqual({
      value: 500,
      unit: "GRAM",
      approximate: false,
    });
    expect(displayQuantity(1500, "GRAM", "METRIC")).toEqual({
      value: 1.5,
      unit: "KILOGRAM",
      approximate: false,
    });
    expect(displayQuantity(2, "TABLESPOON", "METRIC")).toEqual({
      value: 2,
      unit: "TABLESPOON",
      approximate: false,
    });
    expect(displayQuantity(250, "MILLILITER", "METRIC")).toEqual({
      value: 2.5,
      unit: "DECILITER",
      approximate: false,
    });
  });

  it("US_CUSTOMARY: vikt -> oz/lb", () => {
    const oz = displayQuantity(100, "GRAM", "US_CUSTOMARY");
    expect(oz.unit).toBe("OUNCE");
    expect(oz.value).toBeCloseTo(3.5, 1);
    const lb = displayQuantity(1, "KILOGRAM", "US_CUSTOMARY");
    expect(lb.unit).toBe("POUND");
    expect(lb.value).toBeCloseTo(2.2, 1);
  });

  it("US_CUSTOMARY: volym -> tsp/tbsp/cup med kvartsavrundning", () => {
    const cup = displayQuantity(2.5, "DECILITER", "US_CUSTOMARY");
    expect(cup.unit).toBe("CUP_US");
    expect(cup.value).toBeCloseTo(1, 1);
    const tsp = displayQuantity(5, "MILLILITER", "US_CUSTOMARY");
    expect(tsp.unit).toBe("TEASPOON");
    expect(tsp.value).toBe(1);
    const tbsp = displayQuantity(15, "MILLILITER", "US_CUSTOMARY");
    expect(tbsp.unit).toBe("TABLESPOON");
    expect(tbsp.value).toBe(1);
  });

  it("MIXED beter sig metriskt (GB/CA-kök)", () => {
    expect(displayQuantity(500, "GRAM", "MIXED").unit).toBe("GRAM");
  });

  it("styck-enheter konverteras aldrig", () => {
    expect(displayQuantity(3, "COUNT", "US_CUSTOMARY")).toEqual({
      value: 3,
      unit: "COUNT",
      approximate: false,
    });
  });

  it("lagring påverkas aldrig – ren visningsfunktion utan sidoeffekter", () => {
    const before = { q: 500, u: "GRAM" as const };
    displayQuantity(before.q, before.u, "US_CUSTOMARY");
    expect(before).toEqual({ q: 500, u: "GRAM" });
  });
});

describe("energi & natrium (i18n-spec §18–19, M6)", () => {
  it("kcal -> kJ med 4.184", () => {
    expect(kcalToKilojoules(100)).toBe(418);
    expect(kcalToKilojoules(650)).toBe(2720);
  });

  it("salt <-> natrium med faktor 2.5", () => {
    expect(sodiumMgToSaltGrams(400)).toBe(1);
    expect(saltGramsToSodiumMg(1)).toBe(400);
    expect(saltGramsToSodiumMg(sodiumMgToSaltGrams(1234))).toBeCloseTo(1234, -1);
  });
});
