import { createElement } from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { describe, expect, it } from "vitest"; import { computeLineRects, RibbonArt } from "@/components/awards/ribbonDesign/RibbonArt"; import { DEFAULT_RIBBON } from "@/components/awards/ribbonDesign/constants"; import type { RibbonDesign } from "@/components/awards/ribbonDesign/types"; import { validateRibbonDesign } from "@/lib/awards/ribbonValidation"; // The canonical canvas RibbonArt draws into (RIBBON_CONSTANTS); kept literal so // percentage math in these tests fails loudly if the canvas ever changes. const W = 366; const H = 80; const baseDesign = (overrides: Partial = {}): RibbonDesign => ({ stripes: [ { color: "#b91c1c", width: 50 }, { color: "#facc15", width: 50 }, ], devices: [], mirrored: false, ...overrides, }); /** All tags of a rendered design, in document order, as attribute maps. */ function renderedRects(design: RibbonDesign): Record[] { const html = renderToStaticMarkup(createElement(RibbonArt, { design })); return [...html.matchAll(/]*)>/g)].map((tag) => { const attrs: Record = {}; for (const pair of tag[1].matchAll(/([\w-]+)="([^"]*)"/g)) attrs[pair[1]] = pair[2]; return attrs; }); } describe("computeLineRects", () => { it("draws nothing for missing or empty line sets", () => { expect(computeLineRects(undefined, W, H)).toEqual([]); expect(computeLineRects([], W, H)).toEqual([]); }); it("places a centered 10%-height line at the canvas mid-band", () => { const rects = computeLineRects( [{ id: "l1", color: "#ca8a04", positionY: 50, thickness: 10 }], W, H, ); expect(rects).toHaveLength(1); // Center 50%, thickness 10% => 45..55% of 80px. expect(rects[0].y).toBeCloseTo(36, 6); expect(rects[0].height).toBeCloseTo(8, 6); expect(rects[0].x).toBe(0); expect(rects[0].width).toBe(W); expect(rects[0].color).toBe("#ca8a04"); }); it("clamps overhanging lines to the canvas face", () => { const rects = computeLineRects( [{ id: "l1", color: "#f5f5f4", positionY: 96, thickness: 10 }], W, H, ); // 91..101 clamps to 91..100 => top 72.8, height 7.2, still inside the face. expect(rects[0].y).toBeCloseTo(72.8, 6); expect(rects[0].y + rects[0].height).toBeLessThanOrEqual(H + 1e-9); }); it("skips malformed entries without throwing", () => { const junk = [ { id: "zero", color: "#ffffff", positionY: 50, thickness: 0 }, { id: "negative", color: "#ffffff", positionY: 50, thickness: -4 }, { id: "nan", color: "#ffffff", positionY: Number.NaN, thickness: 5 }, { id: "nocolo", positionY: 50, thickness: 5 } as unknown as RibbonDesign["lines"] extends (infer T)[] | undefined ? T : never, ]; expect(computeLineRects(junk as RibbonDesign["lines"], W, H)).toEqual([]); }); it("keeps authored order for multiple lines", () => { const rects = computeLineRects( [ { id: "top", color: "#18181b", positionY: 10, thickness: 2 }, { id: "bottom", color: "#f5f5f4", positionY: 90, thickness: 2 }, ], W, H, ); expect(rects.map((r) => r.y)).toEqual([expect.closeTo(7.2, 6), expect.closeTo(71.2, 6)]); }); }); describe("validateRibbonDesign (horizontal-line additions)", () => { it("still accepts legacy designs that carry neither new key", () => { expect(validateRibbonDesign(baseDesign())).toEqual({ ok: true }); expect(validateRibbonDesign(DEFAULT_RIBBON).ok).toBe(true); }); it("accepts explicit orientations and well-formed line sets", () => { expect( validateRibbonDesign(baseDesign({ stripeOrientation: "vertical" })), ).toEqual({ ok: true }); expect( validateRibbonDesign( baseDesign({ stripeOrientation: "horizontal", lines: [{ id: "l1", color: "#ca8a04", positionY: 50, thickness: 10 }], }), ), ).toEqual({ ok: true }); }); it("rejects an unknown orientation", () => { const result = validateRibbonDesign({ ...baseDesign(), stripeOrientation: "diagonal", }); expect(result.ok).toBe(false); if (!result.ok) expect(result.error).toContain("Stripe orientation"); }); it("enforces the five-line cap", () => { const lines = Array.from({ length: 6 }, (_, i) => ({ id: `l${i}`, color: "#ca8a04", positionY: 50, thickness: 5, })); const result = validateRibbonDesign(baseDesign({ lines })); expect(result.ok).toBe(false); if (!result.ok) expect(result.error).toMatch(/at most 5/); }); it.each([ ["hex color", { color: "gold", positionY: 50, thickness: 5 }], ["position", { color: "#ca8a04", positionY: 101, thickness: 5 }], ["thickness", { color: "#ca8a04", positionY: 50, thickness: 0 }], ["oversize thickness", { color: "#ca8a04", positionY: 50, thickness: 120 }], ])("rejects a line with an invalid %s", (_label, partial) => { const result = validateRibbonDesign( baseDesign({ lines: [{ id: "l1", ...partial }] as RibbonDesign["lines"] }), ); expect(result.ok).toBe(false); }); }); describe("RibbonArt horizontal-line rendering", () => { it("runs bands left-to-right by default and top-to-bottom when horizontal", () => { const vertical = renderedRects(baseDesign()).filter((r) => r.fill === "#b91c1c" || r.fill === "#facc15"); // Vertical: two 183px-wide columns, zero height offset. expect(vertical[0]).toMatchObject({ x: "0", width: "183", y: "0", height: "80" }); expect(vertical[1]).toMatchObject({ x: "183", width: "183", y: "0", height: "80" }); const horizontal = renderedRects(baseDesign({ stripeOrientation: "horizontal" })).filter( (r) => r.fill === "#b91c1c" || r.fill === "#facc15", ); expect(horizontal[0]).toMatchObject({ x: "0", width: "366", y: "0", height: "40" }); expect(horizontal[1]).toMatchObject({ x: "0", width: "366", y: "40", height: "40" }); }); it("treats an absent orientation key as vertical (legacy payloads)", () => { const legacy = baseDesign() as Record; delete legacy.stripeOrientation; const bands = renderedRects(legacy as unknown as RibbonDesign).filter((r) => r.width === "183"); expect(bands).toHaveLength(2); }); it("renders one line rect per valid entry", () => { const html = renderToStaticMarkup( createElement(RibbonArt, { design: baseDesign({ lines: [ { id: "a", color: "#ca8a04", positionY: 25, thickness: 4 }, { id: "b", color: "#18181b", positionY: 75, thickness: 4 }, { id: "c", color: "#f5f5f4", positionY: 50, thickness: 0 }, // invalid, dropped ], }), }), ); expect(html.match(/data-line="true"/g)).toHaveLength(2); }); it("draws lines over the stripe bands but under the fabric overlay", () => { const html = renderToStaticMarkup( createElement(RibbonArt, { design: baseDesign({ lines: [{ id: "a", color: "#ca8a04", positionY: 50, thickness: 6 }], }), }), ); const lastBand = Math.max( ...renderedRects(baseDesign()) .slice(1) // drop the backdrop rect .map((r) => html.indexOf(`fill="${r.fill}"`)), ); const firstLine = html.indexOf('data-line="true"'); const overlay = html.indexOf("/images/ribbon-overlay.png"); expect(firstLine).toBeGreaterThan(lastBand); expect(firstLine).toBeLessThan(overlay); }); it("keeps horizontal mirroring stacking bands down the canvas", () => { const html = renderToStaticMarkup( createElement(RibbonArt, { design: baseDesign({ stripeOrientation: "horizontal", mirrored: true }) }), ); // Mirror semantics are the existing palindrome ([A,B] -> [A,B,A]) in either // direction; check fills so the 366-wide backdrop cannot pollute this. const bands = [...html.matchAll(/]*fill="(#[0-9a-fA-F]{6})"[^>]*>/g)]; expect(bands.map((b) => b[1])).toEqual(["#18181b", "#b91c1c", "#facc15", "#b91c1c"]); const stripeYs = renderedRects( baseDesign({ stripeOrientation: "horizontal", mirrored: true }), ) .slice(1) .map((r) => Number(r.y)); expect(stripeYs).toHaveLength(3); expect(stripeYs[1]).toBeGreaterThan(stripeYs[0]); expect(stripeYs[2]).toBeGreaterThan(stripeYs[1]); }); });