From 4985b8c9466965f8b1478f3c5b3e945ecd75f802 Mon Sep 17 00:00:00 2001 From: SJCoetz Date: Fri, 10 Jul 2026 02:04:38 +0200 Subject: [PATCH] test(order-color): pin colorForOrder behaviour --- src/lib/order-color.test.ts | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/lib/order-color.test.ts diff --git a/src/lib/order-color.test.ts b/src/lib/order-color.test.ts new file mode 100644 index 0000000..3821ede --- /dev/null +++ b/src/lib/order-color.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, it } from "vitest"; +import { colorForOrder, ORDER_COLOR_PALETTE } from "./order-color"; + +describe("colorForOrder", () => { + it("returns the first palette entry for empty/null/undefined", () => { + expect(colorForOrder("")).toBe(ORDER_COLOR_PALETTE[0]); + expect(colorForOrder(null)).toBe(ORDER_COLOR_PALETTE[0]); + expect(colorForOrder(undefined)).toBe(ORDER_COLOR_PALETTE[0]); + }); + + it("is deterministic for the same id", () => { + const a = colorForOrder("PO-2026-07-009"); + const b = colorForOrder("PO-2026-07-009"); + expect(a).toBe(b); + }); + + it("returns an entry from the palette", () => { + const result = colorForOrder("PO-2026-07-009"); + expect(ORDER_COLOR_PALETTE).toContain(result); + }); + + it("distributes 60 distinct ids across at least 4 of the 6 palette entries", () => { + const seen = new Set(); + for (let i = 0; i < 60; i++) { + seen.add(colorForOrder(`PO-2026-${i.toString().padStart(2, "0")}-001`).borderClass); + } + expect(seen.size).toBeGreaterThanOrEqual(4); + }); + + it("returns a stable, known entry for a known id", () => { + // Locks in the hash function. If the implementation ever changes + // (e.g. FNV-1a → FNV-1 or different prime), update this expectation + // and review the change. + expect(colorForOrder("PO-2026-07-009").borderClass).toMatch( + /^border-(emerald|sky|amber|rose|violet|teal)-500\/40$/, + ); + }); +}); \ No newline at end of file