import { describe, expect, it } from "vitest"; import { TOOLCRAFT_BUILT_IN_CONTROL_TYPES } from "../contracts/component-contracts"; import { areToolcraftControlValuesEqual, decodeToolcraftBuiltInControlValue, TOOLCRAFT_CONTROL_VALUE_CODEC_REGISTRY, } from "./control-value-codecs"; describe("Toolcraft built-in control value codecs", () => { it("canonicalizes Color strings and UI payloads to one string shape", () => { const control = { defaultValue: "#112233", target: "shape.fill", type: "color", } as const; expect(decodeToolcraftBuiltInControlValue(control, "#445566")).toEqual({ accepted: true, value: "#445566", }); expect( decodeToolcraftBuiltInControlValue(control, { hex: "#778899" }), ).toEqual({ accepted: true, value: "#778899" }); expect(decodeToolcraftBuiltInControlValue(control, "#abc")).toEqual({ accepted: true, value: "#AABBCC", }); }); it("recursively canonicalizes source-owned Color items", () => { const control = { defaultValue: ["#111111"], itemControl: { label: false, type: "color" }, target: "fills.items", type: "sourceCollection", } as const; expect( decodeToolcraftBuiltInControlValue(control, [ { hex: "#222222" }, "#333333", ]), ).toEqual({ accepted: true, value: ["#222222", "#333333"] }); }); it("keeps Vector numeric regardless of authored or UI ingress shape", () => { const control = { defaultValue: { x: "0.00", y: "0.00" }, type: "vector", } as const; expect( decodeToolcraftBuiltInControlValue(control, { x: "1.25", y: -2 }), ).toEqual({ accepted: true, value: { x: 1.25, y: -2 } }); }); it("keeps only declared FileDrop item settings beside mediaId", () => { const control = { defaultValue: [], itemControls: { depth: { defaultValue: 0.2, max: 1, min: 0, type: "slider" }, tint: { defaultValue: "#112233", type: "color" }, }, target: "media.bumpMaps", type: "fileDrop", } as const; expect( decodeToolcraftBuiltInControlValue(control, [ { depth: 0.6, ignoredBinaryMetadata: "must-not-enter-values", mediaId: "bump-a", tint: { hex: "#445566" }, }, ]), ).toEqual({ accepted: true, value: [{ depth: 0.6, mediaId: "bump-a", tint: "#445566" }], }); }); it("classifies every built-in exactly once", () => { expect(Object.keys(TOOLCRAFT_CONTROL_VALUE_CODEC_REGISTRY).sort()).toEqual( [...TOOLCRAFT_BUILT_IN_CONTROL_TYPES].sort(), ); }); it("compares cloned canonical values structurally", () => { const first = { stops: [{ color: "#FFFFFF", position: "0%" }] }; const equal = { stops: [{ color: "#FFFFFF", position: "0%" }] }; const changed = { stops: [{ color: "#000000", position: "0%" }] }; expect(areToolcraftControlValuesEqual(first, equal)).toBe(true); expect(areToolcraftControlValuesEqual(first, changed)).toBe(false); }); });