import { describe, it, expect } from "vitest"; import { toTitleCase, resolveStatus, STATUS_VARIANTS, STATUS_LABELS } from "./status"; describe("toTitleCase", () => { it("title-cases a single-word enum value", () => { expect(toTitleCase("DRAFT")).toBe("Draft"); expect(toTitleCase("POSTED")).toBe("Posted"); }); it("title-cases each word of a snake_case enum value", () => { expect(toTitleCase("PARTIALLY_RECEIVED")).toBe("Partially Received"); expect(toTitleCase("IN_TRANSIT")).toBe("In Transit"); expect(toTitleCase("NOT_BILLED")).toBe("Not Billed"); }); it("preserves known domain acronyms in full uppercase", () => { expect(toTitleCase("NO_PO_FOUND")).toBe("No PO Found"); expect(toTitleCase("NO_GR_FOUND")).toBe("No GR Found"); expect(toTitleCase("SKU")).toBe("SKU"); expect(toTitleCase("ASN")).toBe("ASN"); }); it("treats non-acronym tokens as normal words", () => { // QTY is not in the acronym set, so it is title-cased like any word. expect(toTitleCase("QTY_MISMATCH")).toBe("Qty Mismatch"); }); it("normalizes already-spaced and lower-case input", () => { expect(toTitleCase("in progress")).toBe("In Progress"); expect(toTitleCase("shopify_pos")).toBe("Shopify POS"); }); it("returns an empty string for empty input", () => { expect(toTitleCase("")).toBe(""); }); }); describe("resolveStatus", () => { it("returns the canonical label + variant for a registered status", () => { expect(resolveStatus("CANCELLED")).toEqual({ label: "Cancelled", variant: "error", }); expect(resolveStatus("ACTIVE")).toEqual({ label: "Active", variant: "success", }); expect(resolveStatus("POSTED")).toEqual({ label: "Posted", variant: "success", }); }); it("uses outline-info for informational / in-progress states", () => { expect(resolveStatus("PROCESSING").variant).toBe("outline-info"); expect(resolveStatus("AI_RECONCILIATION").variant).toBe("outline-info"); }); it("prefers an explicit registry label over the title-cased fallback", () => { // toTitleCase("QTY_MISMATCH") would be "Qty Mismatch"; the registry // overrides it with the fuller "Quantity Mismatch". expect(toTitleCase("QTY_MISMATCH")).toBe("Qty Mismatch"); expect(resolveStatus("QTY_MISMATCH").label).toBe("Quantity Mismatch"); // AI_RECONCILIATION is deliberately shortened to "AI". expect(resolveStatus("AI_RECONCILIATION").label).toBe("AI"); }); it("falls back to a title-cased label and neutral variant for unknown values", () => { expect(resolveStatus("SOME_NEW_STATUS")).toEqual({ label: "Some New Status", variant: "neutral", }); // Acronyms are still preserved in the fallback. expect(resolveStatus("NO_PO_PENDING").label).toBe("No PO Pending"); }); }); describe("canonical mappings", () => { it("renders the same status identically wherever it appears", () => { // DRAFT is neutral everywhere (master-data used to colour it warning). expect(resolveStatus("DRAFT").variant).toBe("neutral"); // CANCELLED is error everywhere (bills used to colour it neutral). expect(resolveStatus("CANCELLED").variant).toBe("error"); }); it("derives STATUS_VARIANTS and STATUS_LABELS from the same registry", () => { expect(STATUS_LABELS.IN_TRANSIT).toBe("In Transit"); expect(STATUS_VARIANTS.IN_TRANSIT).toBe("warning"); // The two maps must stay in lock-step (same keys). expect(Object.keys(STATUS_VARIANTS).sort()).toEqual(Object.keys(STATUS_LABELS).sort()); // Each registered key resolves consistently through both paths. for (const key of Object.keys(STATUS_VARIANTS)) { expect(resolveStatus(key)).toEqual({ label: STATUS_LABELS[key], variant: STATUS_VARIANTS[key], }); } }); });