import { describe, expect, it } from "vitest"; import { defineToolcraft } from "./define-toolcraft"; import type { ToolcraftControlSchema } from "./types"; function createAppSchema(control: ToolcraftControlSchema) { return { canvas: { enabled: false }, panels: { controls: { sections: [ { controls: { sections: control }, title: "Fill Sections", }, ], title: "Controls", }, }, }; } function createSourceCollection( overrides: Record = {}, ): ToolcraftControlSchema { return { defaultValue: [{ hex: "#DFFF1A" }], itemControl: { label: false, type: "color" }, itemLabel: "Section", label: "Sections", target: "fills.sections", type: "sourceCollection", ...overrides, } as ToolcraftControlSchema; } describe("defineToolcraft source collection", () => { it("accepts a source-owned collection of built-in item controls", () => { const app = defineToolcraft(createAppSchema(createSourceCollection())); const control = app.panels.controls?.sections.find( (section) => section.title === "Fill Sections", )?.controls.sections; expect(control?.type).toBe("sourceCollection"); expect(control?.itemControl?.type).toBe("color"); }); it("requires source collections to declare their built-in item control", () => { expect(() => defineToolcraft( createAppSchema(createSourceCollection({ itemControl: undefined })), ), ).toThrow( /Toolcraft control validation \[source-collection-item-control\] for target "fills\.sections": itemControl is required/u, ); }); it("rejects unsupported collection item controls", () => { const control = createSourceCollection({ itemControl: { type: "imaginary" }, }); expect(() => defineToolcraft(createAppSchema(control))).toThrow( /Toolcraft control validation \[collection-item-control\] for target "fills\.sections": unsupported collection item control "imaginary"/u, ); }); it("rejects unsupported collectionActions item controls instead of falling back to text", () => { const control = { ...createSourceCollection(), itemControl: { type: "imaginary" }, type: "collectionActions", } as unknown as ToolcraftControlSchema; expect(() => defineToolcraft(createAppSchema(control))).toThrow( /Toolcraft control validation \[collection-item-control\] for target "fills\.sections": unsupported collection item control "imaginary"/u, ); }); });