// @vitest-environment jsdom import { createElement as h } from "react"; import { afterEach, describe, expect, it } from "vitest"; import { cleanup, render } from "@testing-library/react"; import { RibbonArt, computeDeviceSlots, resolveDisplayedDeviceTypes, MAX_TIER_DEVICES, } from "@/components/awards/ribbonDesign/RibbonArt"; import type { RibbonDesign } from "@/components/awards/ribbonDesign/types"; afterEach(cleanup); const design = (deviceTypes: string[]): RibbonDesign => ({ stripes: [ { color: "#b91c1c", width: 50 }, { color: "#1e3a5f", width: 50 }, ], devices: deviceTypes.map((type, i) => ({ id: `dev-${i}`, // Cast to satisfy the union; tests only use valid types or assert fallback. type: type as RibbonDesign["devices"][number]["type"], position: 17 + i * 11, positionY: 83, })), mirrored: false, }); describe("resolveDisplayedDeviceTypes (tier-driven count)", () => { const authored = ["star", "oakLeaf"] as const; it("shows exactly the authored set for base, missing, and invalid tiers", () => { for (const tier of [undefined, null, 1, 0, -2, Number.NaN]) { expect(resolveDisplayedDeviceTypes(design([...authored]), tier)).toEqual(["star", "oakLeaf"]); } }); it("maps tier N to N-1 devices using the service-star convention", () => { const d = design([...authored]); expect(resolveDisplayedDeviceTypes(d, 2)).toEqual(["star"]); expect(resolveDisplayedDeviceTypes(d, 3)).toEqual(["star", "oakLeaf"]); expect(resolveDisplayedDeviceTypes(d, 4)).toEqual(["star", "oakLeaf", "star"]); }); it("caps the derived cluster at MAX_TIER_DEVICES", () => { const result = resolveDisplayedDeviceTypes(design([...authored]), 99); expect(result).toHaveLength(MAX_TIER_DEVICES); }); it("defaults to stars when a tiered series authored no devices", () => { const result = resolveDisplayedDeviceTypes(design([]), 3); expect(result).toEqual(["star", "star"]); }); }); describe("computeDeviceSlots (centered auto-layout)", () => { const W = 366; const H = 80; it("returns no slots for zero/negative counts", () => { expect(computeDeviceSlots(0, W, H)).toEqual([]); expect(computeDeviceSlots(-3, W, H)).toEqual([]); }); it("centers a single device on the ribbon face", () => { const [slot] = computeDeviceSlots(1, W, H); expect(slot.cx).toBeCloseTo(W / 2, 6); expect(slot.cy).toBeCloseTo(H / 2, 6); expect(slot.size).toBeGreaterThan(0); }); it("places N devices as an evenly spaced, centered cluster", () => { const slots = computeDeviceSlots(4, W, H); expect(slots).toHaveLength(4); const spacings = slots.slice(1).map((s, i) => s.cx - slots[i].cx); spacings.forEach((gap) => expect(gap).toBeCloseTo(spacings[0], 6)); // Symmetric about the centerline. const meanCx = slots.reduce((sum, s) => sum + s.cx, 0) / slots.length; expect(meanCx).toBeCloseTo(W / 2, 6); // All vertically centered. for (const slot of slots) expect(slot.cy).toBeCloseTo(H / 2, 6); }); it("shrinks and keeps the cluster inside the face when crowded", () => { // A tall, narrow canvas makes ideal sizing overflow at high counts, which // exercises the uniform shrink path (compare like-for-like canvas). const NARROW_W = 100; const TALL_H = 200; const few = computeDeviceSlots(1, NARROW_W, TALL_H); const many = computeDeviceSlots(MAX_TIER_DEVICES, NARROW_W, TALL_H); expect(many[many.length - 1].size).toBeLessThan(few[0].size); const span = many[many.length - 1].cx + many[many.length - 1].size / 2 - (many[0].cx - many[0].size / 2); expect(span).toBeLessThanOrEqual(NARROW_W * 0.8 + 1e-6); }); }); describe("RibbonArt rendering", () => { it("renders vector device groups (no Unicode text elements) with tier counts", () => { const { container } = render(h(RibbonArt, { design: design(["star", "oakLeaf"]), tier: 3 })); const devices = container.querySelectorAll("[data-device]"); expect(devices).toHaveLength(2); expect(Array.from(devices).map((d) => d.getAttribute("data-device"))).toEqual([ "star", "oakLeaf", ]); // Glyphs are inline paths now; no nodes and no emoji fallbacks. expect(container.querySelectorAll("text")).toHaveLength(0); expect(container.querySelectorAll("path").length).toBeGreaterThan(0); }); it("keeps preview identical to production by drawing the fabric overlay once", () => { const { container } = render(h(RibbonArt, { design: design([]), tier: 1 })); expect(container.querySelectorAll("image[href='/images/ribbon-overlay.png']")).toHaveLength(1); }); it("drops the overlay when showOverlay is false", () => { const { container } = render(h(RibbonArt, { design: design([]), showOverlay: false })); expect(container.querySelector("image")).toBeNull(); }); it("renders mirrored designs with stripes doubled", () => { const mirrored: RibbonDesign = { ...design(["star"]), mirrored: true }; const { container } = render(h(RibbonArt, { design: mirrored })); // mirrored doubles every stripe band; two colors -> four rects over base bg const rects = container.querySelectorAll("rect"); expect(rects.length).toBeGreaterThanOrEqual(4); }); it("returns null for an explicitly stripe-less design and falls back for an absent one", () => { // Matches legacy RibbonViewer/RibbonPreview behavior: a valid object with // no stripes draws nothing... const { container: empty } = render( h(RibbonArt, { design: { stripes: [], devices: [], mirrored: false } }), ); expect(empty.querySelector("svg")).toBeNull(); // ...while an unusable value falls back to DEFAULT_RIBBON and still renders. const { container: fallback } = render(h(RibbonArt, { design: undefined })); expect(fallback.querySelector("svg")).not.toBeNull(); }); });