import { describe, expect, it } from "vitest"; import { formatMoney, formatDuration, formatRelativeDateTime, type TFn } from "./format.js"; // The booth's display formatters. Money is integer MINOR units (never a float, matching // the ledger/tariff model); duration is whole minutes; relative dates drive the session/ // log/history rows. These are the numbers an operator reads off the screen. describe("formatMoney", () => { it("renders minor units as a major-unit currency string", () => { // 20000 minor = 200.00; the exact glyph/locale varies, but the number must show. expect(formatMoney(20000, "ALL")).toContain("200"); }); it("falls back to ' ' for a malformed currency code", () => { // Intl requires a 3-letter ISO code; a malformed one throws RangeError → fallback. // (Note: an unknown-but-well-formed code like "ZZZ" does NOT throw — Intl renders it.) expect(formatMoney(12345, "X")).toBe("123.45 X"); }); }); describe("formatDuration", () => { const base = "2026-06-21T10:00:00.000Z"; it("shows minutes under an hour", () => { expect(formatDuration(base, "2026-06-21T10:47:00.000Z")).toBe("47m"); }); it("shows hours and minutes past an hour", () => { expect(formatDuration(base, "2026-06-21T12:14:00.000Z")).toBe("2h 14m"); }); it("renders 0m for a sub-minute span", () => { expect(formatDuration(base, "2026-06-21T10:00:30.000Z")).toBe("0m"); }); it("returns an em dash for a negative or invalid span", () => { expect(formatDuration("2026-06-21T10:00:00.000Z", "2026-06-21T09:00:00.000Z")).toBe("—"); expect(formatDuration("bad", "also-bad")).toBe("—"); }); }); describe("formatRelativeDateTime", () => { // A tiny fake t(): today/yesterday words + the month-name array. const months = ["Jan","Shkurt","Mars","Prill","Maj","Qershor","Korrik","Gusht","Sht","Tet","Nën","Dhj"]; const t = ((key: string, opts?: { returnObjects: true }) => { if (key === "common.today") return "Sot"; if (key === "common.yesterday") return "Dje"; if (key === "common.months" && opts?.returnObjects) return months; return key; }) as TFn; it("labels today with the localized word + HH:MM", () => { const now = new Date(); now.setHours(10, 48, 0, 0); expect(formatRelativeDateTime(now.toISOString(), t)).toMatch(/^Sot \d{2}:\d{2}$/); }); it("appends :ss with the seconds option (entry/exit rows read alike)", () => { const now = new Date(); now.setHours(19, 25, 44, 0); expect(formatRelativeDateTime(now.toISOString(), t, { seconds: true })).toMatch(/^Sot \d{2}:\d{2}:44$/); }); it("labels yesterday with the localized word", () => { const y = new Date(); y.setDate(y.getDate() - 1); y.setHours(17, 33, 0, 0); expect(formatRelativeDateTime(y.toISOString(), t)).toMatch(/^Dje \d{2}:\d{2}$/); }); it("uses the catalog month name for an older date (no Intl dependence)", () => { // A fixed older date in the same year as 'now' would risk year drift; use an // explicit past date and just assert a catalog month name appears. const out = formatRelativeDateTime("2020-03-05T08:15:00.000Z", t); expect(out).toContain("Mars"); expect(out).toContain("2020"); // different year → year shown }); it("returns an em dash for null/invalid", () => { expect(formatRelativeDateTime(null, t)).toBe("—"); expect(formatRelativeDateTime("nope", t)).toBe("—"); }); });