import { mount, config } from "@vue/test-utils"; import { describe, expect, test, vi } from "vite-plus/test"; import formatter, { POWER_UNIT } from "./formatter"; import * as units from "../units"; import { defineComponent } from "vue"; import { CURRENCY } from "@/types/evcc"; const is12hSpy = vi.spyOn(units, "is12hFormat").mockReturnValue(false); config.global.mocks["$i18n"] = { locale: "de" }; config.global.mocks["$t"] = (a: string) => a; const fmt = mount( defineComponent({ mixins: [formatter], render() { return null; }, }) ).vm; describe("fmtW", () => { test("should format with units", () => { expect(fmt.fmtW(0, POWER_UNIT.AUTO)).eq("0,0 kW"); expect(fmt.fmtW(1200000, POWER_UNIT.AUTO)).eq("1.200,0 kW"); expect(fmt.fmtW(0, POWER_UNIT.MW)).eq("0,0 MW"); expect(fmt.fmtW(1200000, POWER_UNIT.MW)).eq("1,2 MW"); expect(fmt.fmtW(0, POWER_UNIT.KW)).eq("0,0 kW"); expect(fmt.fmtW(1200000, POWER_UNIT.KW)).eq("1.200,0 kW"); expect(fmt.fmtW(0, POWER_UNIT.W)).eq("0 W"); expect(fmt.fmtW(1200000, POWER_UNIT.W)).eq("1.200.000 W"); }); test("should format without units", () => { expect(fmt.fmtW(0, POWER_UNIT.AUTO, false)).eq("0,0"); expect(fmt.fmtW(1200000, POWER_UNIT.AUTO, false)).eq("1.200,0"); expect(fmt.fmtW(0, POWER_UNIT.MW, false)).eq("0,0"); expect(fmt.fmtW(1200000, POWER_UNIT.MW, false)).eq("1,2"); expect(fmt.fmtW(0, POWER_UNIT.KW, false)).eq("0,0"); expect(fmt.fmtW(1200000, POWER_UNIT.KW, false)).eq("1.200,0"); expect(fmt.fmtW(0, POWER_UNIT.W, false)).eq("0"); expect(fmt.fmtW(1200000, POWER_UNIT.W, false)).eq("1.200.000"); }); test("should format a given number of digits", () => { expect(fmt.fmtW(12345, POWER_UNIT.AUTO, true, 0)).eq("12 kW"); expect(fmt.fmtW(12345, POWER_UNIT.AUTO, true, 1)).eq("12,3 kW"); expect(fmt.fmtW(12345, POWER_UNIT.AUTO, true, 2)).eq("12,35 kW"); expect(fmt.fmtW(12345, POWER_UNIT.MW, true, 0)).eq("0 MW"); expect(fmt.fmtW(12345, POWER_UNIT.MW, true, 1)).eq("0,0 MW"); expect(fmt.fmtW(12345, POWER_UNIT.MW, true, 2)).eq("0,01 MW"); expect(fmt.fmtW(12345, POWER_UNIT.KW, true, 0)).eq("12 kW"); expect(fmt.fmtW(12345, POWER_UNIT.KW, true, 1)).eq("12,3 kW"); expect(fmt.fmtW(12345, POWER_UNIT.KW, true, 2)).eq("12,35 kW"); expect(fmt.fmtW(12345, POWER_UNIT.W, true, 0)).eq("12.345 W"); expect(fmt.fmtW(12345, POWER_UNIT.W, true, 1)).eq("12.345,0 W"); expect(fmt.fmtW(12345, POWER_UNIT.W, true, 2)).eq("12.345,00 W"); }); }); describe("getPowerUnit", () => { test("should pick unit based on largest value", () => { expect(fmt.getPowerUnit(0)).eq(POWER_UNIT.W); expect(fmt.getPowerUnit(999)).eq(POWER_UNIT.W); expect(fmt.getPowerUnit(1000)).eq(POWER_UNIT.KW); expect(fmt.getPowerUnit(9_999_999)).eq(POWER_UNIT.KW); expect(fmt.getPowerUnit(10_000_000)).eq(POWER_UNIT.MW); }); }); describe("fmtWh", () => { test("should format with units", () => { expect(fmt.fmtWh(0, POWER_UNIT.AUTO)).eq("0,0 kWh"); expect(fmt.fmtWh(1200000, POWER_UNIT.AUTO)).eq("1.200,0 kWh"); expect(fmt.fmtWh(0, POWER_UNIT.MW)).eq("0,0 MWh"); expect(fmt.fmtWh(1200000, POWER_UNIT.MW)).eq("1,2 MWh"); expect(fmt.fmtWh(0, POWER_UNIT.KW)).eq("0,0 kWh"); expect(fmt.fmtWh(1200000, POWER_UNIT.KW)).eq("1.200,0 kWh"); expect(fmt.fmtWh(0, POWER_UNIT.W)).eq("0 Wh"); expect(fmt.fmtWh(1200000, POWER_UNIT.W)).eq("1.200.000 Wh"); }); test("should format without units", () => { expect(fmt.fmtWh(0, POWER_UNIT.AUTO, false)).eq("0,0"); expect(fmt.fmtWh(1200000, POWER_UNIT.AUTO, false)).eq("1.200,0"); expect(fmt.fmtWh(0, POWER_UNIT.MW, false)).eq("0,0"); expect(fmt.fmtWh(1200000, POWER_UNIT.MW, false)).eq("1,2"); expect(fmt.fmtWh(0, POWER_UNIT.KW, false)).eq("0,0"); expect(fmt.fmtWh(1200000, POWER_UNIT.KW, false)).eq("1.200,0"); expect(fmt.fmtWh(0, POWER_UNIT.W, false)).eq("0"); expect(fmt.fmtWh(1200000, POWER_UNIT.W, false)).eq("1.200.000"); }); test("should format a given number of digits", () => { expect(fmt.fmtWh(12345, POWER_UNIT.AUTO, true, 0)).eq("12 kWh"); expect(fmt.fmtWh(12345, POWER_UNIT.AUTO, true, 1)).eq("12,3 kWh"); expect(fmt.fmtWh(12345, POWER_UNIT.AUTO, true, 2)).eq("12,35 kWh"); expect(fmt.fmtWh(12345, POWER_UNIT.MW, true, 0)).eq("0 MWh"); expect(fmt.fmtWh(12345, POWER_UNIT.MW, true, 1)).eq("0,0 MWh"); expect(fmt.fmtWh(12345, POWER_UNIT.MW, true, 2)).eq("0,01 MWh"); expect(fmt.fmtWh(12345, POWER_UNIT.KW, true, 0)).eq("12 kWh"); expect(fmt.fmtWh(12345, POWER_UNIT.KW, true, 1)).eq("12,3 kWh"); expect(fmt.fmtWh(12345, POWER_UNIT.KW, true, 2)).eq("12,35 kWh"); expect(fmt.fmtWh(12345, POWER_UNIT.W, true, 0)).eq("12.345 Wh"); expect(fmt.fmtWh(12345, POWER_UNIT.W, true, 1)).eq("12.345,0 Wh"); expect(fmt.fmtWh(12345, POWER_UNIT.W, true, 2)).eq("12.345,00 Wh"); }); }); describe("fmtPricePerKWh", () => { test("should format with units", () => { expect(fmt.fmtPricePerKWh(0.2, CURRENCY.EUR)).eq("20,0 ct/kWh"); expect(fmt.fmtPricePerKWh(0.2, CURRENCY.EUR, true)).eq("20,0 ct"); expect(fmt.fmtPricePerKWh(0.234, CURRENCY.USD)).eq("23,4 ¢/kWh"); expect(fmt.fmtPricePerKWh(1234, CURRENCY.SEK)).eq("123.400,0 öre/kWh"); expect(fmt.fmtPricePerKWh(0.2, CURRENCY.EUR, false, false)).eq("20,0"); expect(fmt.fmtPricePerKWh(0.123, CURRENCY.CHF)).eq("12,3 Rp./kWh"); }); }); describe("pricePerKWhUnit", () => { test("should return correct unit", () => { expect(fmt.pricePerKWhUnit(CURRENCY.EUR)).eq("ct/kWh"); expect(fmt.pricePerKWhUnit(CURRENCY.EUR, true)).eq("ct"); expect(fmt.pricePerKWhUnit(CURRENCY.USD)).eq("¢/kWh"); expect(fmt.pricePerKWhUnit(CURRENCY.SEK)).eq("öre/kWh"); expect(fmt.pricePerKWhUnit(CURRENCY.SEK, true)).eq("öre"); expect(fmt.pricePerKWhUnit(CURRENCY.CHF)).eq("Rp./kWh"); }); }); describe("energyPriceSubunit", () => { test("should return subunit or undefined", () => { expect(fmt.energyPriceSubunit(CURRENCY.EUR)).eq("ct"); expect(fmt.energyPriceSubunit(CURRENCY.CNY)).eq(undefined); }); test("should handle CHF locale-specific logic", () => { config.global.mocks["$i18n"].locale = "de"; expect(fmt.energyPriceSubunit(CURRENCY.CHF)).eq("Rp."); config.global.mocks["$i18n"].locale = "fr"; expect(fmt.energyPriceSubunit(CURRENCY.CHF)).eq("ct."); config.global.mocks["$i18n"].locale = "de"; }); }); describe("fmtDuration", () => { test("should format zero duration", () => { expect(fmt.fmtDuration(0)).eq("—"); expect(fmt.fmtDuration(-100)).eq("—"); }); test("should format seconds", () => { expect(fmt.fmtDuration(1)).eq("1\u202Fs"); expect(fmt.fmtDuration(59)).eq("59\u202Fs"); expect(fmt.fmtDuration(59, false)).eq("59"); expect(fmt.fmtDuration(59, true, "m")).eq("0:59\u202Fm"); expect(fmt.fmtDuration(59, true, "h")).eq("0:00\u202Fh"); }); test("should format minutes", () => { expect(fmt.fmtDuration(60)).eq("1:00\u202Fm"); expect(fmt.fmtDuration(150)).eq("2:30\u202Fm"); expect(fmt.fmtDuration(150, false)).eq("2:30"); expect(fmt.fmtDuration(150, true, "h")).eq("0:02\u202Fh"); }); test("should format hours", () => { expect(fmt.fmtDuration(60 * 60)).eq("1:00\u202Fh"); expect(fmt.fmtDuration(60 * 60 * 2.5)).eq("2:30\u202Fh"); expect(fmt.fmtDuration(60 * 60 * 2.5, false)).eq("2:30"); }); test("should format internationalized", () => { config.global.mocks["$i18n"].locale = "ar-EG"; expect(fmt.fmtDuration(30, false)).eq("٣٠"); expect(fmt.fmtDuration(90, false)).eq("١:٣٠"); expect(fmt.fmtDuration(60 * 100, false)).eq("١:٤٠"); config.global.mocks["$i18n"].locale = "de-DE"; }); }); describe("getWeekdaysList", () => { test("should return the correct weekday-order", () => { expect(fmt.getWeekdaysList("long")).toEqual([ { name: "Montag", value: 1 }, { name: "Dienstag", value: 2 }, { name: "Mittwoch", value: 3 }, { name: "Donnerstag", value: 4 }, { name: "Freitag", value: 5 }, { name: "Samstag", value: 6 }, { name: "Sonntag", value: 0 }, ]); }); }); describe("getMonthsList", () => { test("should return the correct month-order", () => { expect(fmt.getMonthsList("long")).toEqual([ { name: "Januar", value: 0 }, { name: "Februar", value: 1 }, { name: "März", value: 2 }, { name: "April", value: 3 }, { name: "Mai", value: 4 }, { name: "Juni", value: 5 }, { name: "Juli", value: 6 }, { name: "August", value: 7 }, { name: "September", value: 8 }, { name: "Oktober", value: 9 }, { name: "November", value: 10 }, { name: "Dezember", value: 11 }, ]); }); test("should return short month names", () => { const shortMonths = fmt.getMonthsList("short"); expect(shortMonths).toHaveLength(12); expect(shortMonths[0]).toEqual({ name: "Jan", value: 0 }); expect(shortMonths[11]).toEqual({ name: "Dez", value: 11 }); }); }); describe("fmtWeekdaysRange", () => { test("should format single days", () => { expect(fmt.fmtWeekdaysRange([0])).eq("So"); expect(fmt.fmtWeekdaysRange([0, 2, 4, 6])).eq("Di, Do, Sa, So"); expect(fmt.fmtWeekdaysRange([6])).eq("Sa"); expect(fmt.fmtWeekdaysRange([3, 6])).eq("Mi, Sa"); }); test("should format ranges", () => { expect(fmt.fmtWeekdaysRange([1, 2])).eq("Mo, Di"); expect(fmt.fmtWeekdaysRange([0, 1, 2, 3, 4, 5, 6])).eq("Mo – So"); expect(fmt.fmtWeekdaysRange([0, 1, 3, 4, 5])).eq("Mo, Mi – Fr, So"); }); test("should format single days and ranges", () => { expect(fmt.fmtWeekdaysRange([0, 1, 3, 5, 6])).eq("Mo, Mi, Fr – So"); expect(fmt.fmtWeekdaysRange([0, 2, 3, 5, 6])).eq("Di, Mi, Fr – So"); }); }); describe("fmtMonthsRange", () => { test("should format single months", () => { expect(fmt.fmtMonthsRange([0])).eq("Jan"); expect(fmt.fmtMonthsRange([0, 3, 6, 9])).eq("Jan, Apr, Jul, Okt"); expect(fmt.fmtMonthsRange([11])).eq("Dez"); expect(fmt.fmtMonthsRange([2, 8])).eq("Mär, Sep"); }); test("should format ranges", () => { expect(fmt.fmtMonthsRange([0, 1])).eq("Jan, Feb"); expect(fmt.fmtMonthsRange([0, 1, 2])).eq("Jan – Mär"); expect(fmt.fmtMonthsRange([9, 10, 11])).eq("Okt – Dez"); expect(fmt.fmtMonthsRange([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])).eq("Jan – Dez"); }); test("should format single months and ranges", () => { expect(fmt.fmtMonthsRange([0, 1, 2, 5, 9, 10, 11])).eq("Jan – Mär, Jun, Okt – Dez"); expect(fmt.fmtMonthsRange([0, 3, 4, 5, 8])).eq("Jan, Apr – Jun, Sep"); }); test("should handle empty array", () => { expect(fmt.fmtMonthsRange([])).eq("–"); }); }); describe("fmtPercentage", () => { test("should format percentage", () => { expect(fmt.fmtPercentage(0)).eq("0 %"); expect(fmt.fmtPercentage(100)).eq("100 %"); expect(fmt.fmtPercentage(100, 1)).eq("100,0 %"); }); test("should force signed", () => { expect(fmt.fmtPercentage(100, 0, true)).eq("+100 %"); expect(fmt.fmtPercentage(100, 0, false)).eq("100 %"); expect(fmt.fmtPercentage(-100, 0, true)).eq("-100 %"); expect(fmt.fmtPercentage(-100, 0, false)).eq("-100 %"); }); }); const testDate = new Date(2023, 0, 15, 15, 30); // Jan 15, 2023, 15:30 (3:30 PM) describe("12h/24h time format", () => { test("12h format", () => { is12hSpy.mockReturnValue(true); expect(fmt.fmtHourMinute(testDate)).toBe("3:30 PM"); expect(fmt.fmtFullDateTime(testDate, false)).toBe("So., 15. Jan., 3:30 PM"); expect(fmt.fmtWeekdayTime(testDate)).toBe("So. 3:30 PM"); expect(fmt.fmtAbsoluteDate(testDate)).toBe("So 3:30 PM"); }); test("24h format", () => { is12hSpy.mockReturnValue(false); expect(fmt.fmtHourMinute(testDate)).toBe("15:30"); expect(fmt.fmtFullDateTime(testDate, false)).toBe("So., 15. Jan., 15:30"); expect(fmt.fmtWeekdayTime(testDate)).toBe("So., 15:30"); expect(fmt.fmtAbsoluteDate(testDate)).toBe("So 15:30"); }); }); describe("fmtTimeRange", () => { test("should format time ranges in 24h format", () => { is12hSpy.mockReturnValue(false); expect(fmt.fmtTimeRange("09:00-17:00")).toBe("9:00 – 17:00"); expect(fmt.fmtTimeRange("06:00-12:00")).toBe("6:00 – 12:00"); }); test("should format time ranges in 12h format", () => { is12hSpy.mockReturnValue(true); expect(fmt.fmtTimeRange("09:00-17:00")).toBe("9:00 AM – 5:00 PM"); expect(fmt.fmtTimeRange("00:00-23:59")).toBe("12:00 AM – 11:59 PM"); }); test("should always include minutes", () => { is12hSpy.mockReturnValue(false); expect(fmt.fmtTimeRange("09:00-17:00")).toBe("9:00 – 17:00"); expect(fmt.fmtTimeRange("09:15-17:45")).toBe("9:15 – 17:45"); }); test("should handle empty input", () => { expect(fmt.fmtTimeRange("")).toBe(""); }); }); describe("hourShort", () => { const afternoon = new Date(2026, 0, 1, 16); test("should strip locale suffixes in 24h format", () => { is12hSpy.mockReturnValue(false); for (const locale of ["de", "fr", "en", "ja"]) { config.global.mocks["$i18n"].locale = locale; expect(fmt.hourShort(afternoon)).toBe("16"); } config.global.mocks["$i18n"].locale = "de"; }); test("should include day period in 12h format", () => { is12hSpy.mockReturnValue(true); const expected = { de: "4 PM", fr: "4 PM", en: "4 PM", ja: "午後 4" }; for (const [locale, value] of Object.entries(expected)) { config.global.mocks["$i18n"].locale = locale; expect(fmt.hourShort(afternoon)).toBe(value); } config.global.mocks["$i18n"].locale = "en"; expect(fmt.hourShort(new Date(2026, 0, 1, 0))).toBe("12 AM"); is12hSpy.mockReturnValue(false); config.global.mocks["$i18n"].locale = "de"; }); }); describe("relativeDayName", () => { const day = (offset: number) => { const d = new Date(); d.setDate(d.getDate() + offset); return d; }; test("should name adjacent days", () => { expect(fmt.relativeDayName(day(-1))).toBe("gestern"); expect(fmt.relativeDayName(day(0))).toBe("heute"); expect(fmt.relativeDayName(day(1))).toBe("morgen"); }); test("should return null beyond one day", () => { expect(fmt.relativeDayName(day(-2))).toBeNull(); expect(fmt.relativeDayName(day(2))).toBeNull(); }); });