import { describe, expect, it } from "vitest"; import { fitPaddingAxis, fitSwatchPadding, fitSwatchPaddingShorthand, parsePaddingShorthand, parsePx, toPaddingShorthand, } from "./swatchPadding"; const defaults = { top: 8, right: 16, bottom: 8, left: 16 }; const max = { top: 48, right: 48, bottom: 48, left: 48 }; const none = { top: 0, right: 0, bottom: 0, left: 0 }; describe("fitPaddingAxis", () => { it("keeps padding that already fits", () => { expect(fitPaddingAxis(16, 16, { size: 60, border: 2 })).toEqual([16, 16]); }); it("scales both sides down so padding + border never exceeds the box", () => { // 24px box, 1px border each side: 22px of room for 32px of padding. expect(fitPaddingAxis(16, 16, { size: 24, border: 2 })).toEqual([11, 11]); expect(fitPaddingAxis(48, 48, { size: 24, border: 2 })).toEqual([11, 11]); expect(fitPaddingAxis(48, 48, { size: 60, border: 2 })).toEqual([29, 29]); }); it("reserves content room before fitting", () => { expect( fitPaddingAxis(16, 16, { size: 24, border: 2, reserve: 14 }), ).toEqual([4, 4]); }); it("keeps asymmetric padding proportional", () => { expect(fitPaddingAxis(4, 12, { size: 10, border: 2 })).toEqual([2, 6]); }); it("leaves a free axis untouched", () => { expect(fitPaddingAxis(48, 48, {})).toEqual([48, 48]); expect(fitPaddingAxis(48, 48, { size: undefined })).toEqual([48, 48]); }); it("collapses to zero when the box cannot hold any padding", () => { expect(fitPaddingAxis(8, 8, { size: 4, border: 4 })).toEqual([0, 0]); expect(fitPaddingAxis(8, 8, { size: 0 })).toEqual([0, 0]); }); it("treats negative or invalid input as zero", () => { expect(fitPaddingAxis(-4, Number.NaN, { size: 24 })).toEqual([0, 0]); }); }); describe("fitSwatchPadding", () => { it("holds a 24px color box at 24px for default and max padding", () => { expect( fitSwatchPadding(defaults, { width: 24, height: 24, borderX: 2, borderY: 2, }), ).toEqual({ top: 8, right: 11, bottom: 8, left: 11 }); expect( fitSwatchPadding(max, { width: 24, height: 24, borderX: 2, borderY: 2 }), ).toEqual({ top: 11, right: 11, bottom: 11, left: 11 }); expect( fitSwatchPadding(none, { width: 24, height: 24, borderX: 2, borderY: 2 }), ).toEqual(none); }); it("keeps default padding untouched inside a 60px box", () => { expect( fitSwatchPadding(defaults, { width: 60, height: 60, borderX: 2, borderY: 2, }), ).toEqual(defaults); }); it("fits only the fixed height when the width is free (text buttons)", () => { expect( fitSwatchPadding(defaults, { height: 24, borderY: 4, reserveY: 14 }), ).toEqual({ top: 3, right: 16, bottom: 3, left: 16 }); }); }); describe("shorthand helpers", () => { it("round-trips a four-value shorthand", () => { expect(parsePaddingShorthand("8px 16px 8px 16px")).toEqual(defaults); expect(toPaddingShorthand(defaults)).toBe("8px 16px 8px 16px"); }); it("expands one, two and three value shorthands like CSS", () => { expect(parsePaddingShorthand("0")).toEqual(none); expect(parsePaddingShorthand("8px 16px")).toEqual(defaults); expect(parsePaddingShorthand("1px 2px 3px")).toEqual({ top: 1, right: 2, bottom: 3, left: 2, }); expect(parsePaddingShorthand(undefined)).toEqual(none); }); it("fits a shorthand in place", () => { expect( fitSwatchPaddingShorthand("48px 48px 48px 48px", { width: 24, height: 24, borderX: 2, borderY: 2, }), ).toBe("11px 11px 11px 11px"); }); it("parses px values with a fallback", () => { expect(parsePx("14px", 10)).toBe(14); expect(parsePx(12, 10)).toBe(12); expect(parsePx(undefined, 10)).toBe(10); expect(parsePx("auto", 10)).toBe(10); }); });