import { describe, expect, it } from "vitest"; import { DEFAULT_COLOR_PALETTE } from "../constants"; import { createStrictColorPalette } from "../lib/createStrictColorPalette"; import type { ColorPalette, StrictColorPalette } from "../types"; describe("createStrictColorPalette", () => { describe("when palette is undefined", () => { it("should return StrictColorPalette with all colors from DEFAULT_COLOR_PALETTE", () => { const result = createStrictColorPalette(undefined); expect(result.blue[100]).toBe(DEFAULT_COLOR_PALETTE.blue[100]); expect(result.blue[200]).toBe(DEFAULT_COLOR_PALETTE.blue[200]); expect(result.blue[300]).toBe(DEFAULT_COLOR_PALETTE.blue[300]); expect(result.yellow[100]).toBe(DEFAULT_COLOR_PALETTE.yellow[100]); expect(result.gray[100]).toBe(DEFAULT_COLOR_PALETTE.gray[100]); }); it("should generate alpha colors using color-mix", () => { const result = createStrictColorPalette(undefined); expect(result.blue.alpha[100]).toBe( `color-mix(in srgb, ${DEFAULT_COLOR_PALETTE.blue[100]} 50%, transparent)`, ); expect(result.blue.alpha[200]).toBe( `color-mix(in srgb, ${DEFAULT_COLOR_PALETTE.blue[200]} 50%, transparent)`, ); expect(result.blue.alpha[300]).toBe( `color-mix(in srgb, ${DEFAULT_COLOR_PALETTE.blue[300]} 50%, transparent)`, ); }); }); describe("when palette is empty object", () => { it("should return StrictColorPalette with all colors from DEFAULT_COLOR_PALETTE", () => { const result = createStrictColorPalette({}); expect(result.blue[100]).toBe(DEFAULT_COLOR_PALETTE.blue[100]); expect(result.yellow[200]).toBe(DEFAULT_COLOR_PALETTE.yellow[200]); expect(result.gray[300]).toBe(DEFAULT_COLOR_PALETTE.gray[300]); }); }); describe("when palette has partial colors", () => { it("should use provided colors and fallback to DEFAULT for missing ones", () => { const customBlue = { 100: "#custom100", 200: "#custom200", 300: "#custom300", }; const palette: Partial = { blue: customBlue, }; const result = createStrictColorPalette(palette); // Custom blue colors should be used expect(result.blue[100]).toBe("#custom100"); expect(result.blue[200]).toBe("#custom200"); expect(result.blue[300]).toBe("#custom300"); // Missing yellow and gray should fallback to DEFAULT expect(result.yellow[100]).toBe(DEFAULT_COLOR_PALETTE.yellow[100]); expect(result.gray[100]).toBe(DEFAULT_COLOR_PALETTE.gray[100]); }); it("should generate alpha for custom colors using color-mix", () => { const customBlue = { 100: "#custom100", 200: "#custom200", 300: "#custom300", }; const palette: Partial = { blue: customBlue, }; const result = createStrictColorPalette(palette); expect(result.blue.alpha[100]).toBe("color-mix(in srgb, #custom100 50%, transparent)"); expect(result.blue.alpha[200]).toBe("color-mix(in srgb, #custom200 50%, transparent)"); }); }); describe("when palette has alpha colors", () => { it("should use provided alpha colors instead of generating", () => { const customAlpha = { 100: "#alpha100", 200: "#alpha200", 300: "#alpha300", }; const palette: Partial = { blue: { 100: "#blue100", 200: "#blue200", 300: "#blue300", alpha: customAlpha, }, }; const result = createStrictColorPalette(palette); expect(result.blue.alpha[100]).toBe("#alpha100"); expect(result.blue.alpha[200]).toBe("#alpha200"); expect(result.blue.alpha[300]).toBe("#alpha300"); }); }); describe("return type validation", () => { it("should return a valid StrictColorPalette", () => { const result = createStrictColorPalette(undefined); // Check that all required properties exist const colorNames = ["blue", "yellow", "gray"] as const; const steps = [100, 200, 300] as const; for (const colorName of colorNames) { expect(result[colorName]).toBeDefined(); for (const step of steps) { expect(result[colorName][step]).toBeDefined(); expect(typeof result[colorName][step]).toBe("string"); } expect(result[colorName].alpha).toBeDefined(); for (const step of steps) { expect(result[colorName].alpha[step]).toBeDefined(); expect(typeof result[colorName].alpha[step]).toBe("string"); } } // Type check - this should compile without errors const _typeCheck: StrictColorPalette = result; expect(_typeCheck).toBeDefined(); }); }); });