import { describe, expect, it } from "vitest"; import { defineToolcraft } from "../../../schema/define-toolcraft"; import { createToolcraftState } from "../../../state/create-template-state"; import { isToolcraftControlDisabled, isToolcraftControlVisible, isToolcraftSectionVisible, } from "../conditions/control-conditions"; describe("Toolcraft control conditions", () => { it("requires every applicability predicate to match", () => { const schema = defineToolcraft({ canvas: { enabled: true }, panels: { controls: { sections: [ { controls: { sides: { applicability: { all: [ { equals: "create", target: "source.mode" }, { oneOf: ["polygon", "star"], target: "shape.kind", }, ], mode: "conditional", }, target: "shape.sides", type: "slider", }, }, title: "Shape", }, ], title: "Controls", }, }, }); const control = schema.panels.controls?.sections.find( (section) => section.title === "Shape", )?.controls.sides; expect(control).toBeTruthy(); expect( isToolcraftControlVisible( createToolcraftState(schema, { values: { "shape.kind": "star", "source.mode": "upload" }, }), control!, ), ).toBe(false); expect( isToolcraftControlVisible( createToolcraftState(schema, { values: { "shape.kind": "heart", "source.mode": "create" }, }), control!, ), ).toBe(false); expect( isToolcraftControlVisible( createToolcraftState(schema, { values: { "shape.kind": "star", "source.mode": "create" }, }), control!, ), ).toBe(true); }); it("resolves timeline runtime targets through the canonical condition helpers", () => { const schema = defineToolcraft({ canvas: { enabled: true, renderScale: true, sizing: { mode: "editable-output" } }, panels: { controls: { sections: [ { controls: { compactOnly: { defaultValue: 25, label: "Compact value", target: "demo.compact", type: "slider", visibleWhen: { equals: false, target: "panels.timeline.extended", }, }, extendedOnly: { defaultValue: 75, disabledWhen: { equals: false, target: "panels.timeline.extended", }, label: "Extended value", target: "demo.extended", type: "slider", visibleWhen: { equals: true, target: "panels.timeline.extended", }, }, }, title: "Extended Timeline", visibleWhen: { equals: true, target: "panels.timeline.visible", }, }, ], title: "Controls", }, timeline: { mode: "playback" }, }, }); const section = schema.panels.controls?.sections.find( (entry) => entry.title === "Extended Timeline", ); const compactControl = section?.controls.compactOnly; const extendedControl = section?.controls.extendedOnly; expect(section).toBeTruthy(); expect(compactControl).toBeTruthy(); expect(extendedControl).toBeTruthy(); const compactState = createToolcraftState(schema); expect(isToolcraftSectionVisible(compactState, section!)).toBe(true); expect(isToolcraftControlVisible(compactState, compactControl!)).toBe(true); expect(isToolcraftControlVisible(compactState, extendedControl!)).toBe(false); expect(isToolcraftControlDisabled(compactState, extendedControl!)).toBe(true); const extendedState = createToolcraftState(schema, { panels: { timeline: { extended: true } }, }); expect(isToolcraftSectionVisible(extendedState, section!)).toBe(true); expect(isToolcraftControlVisible(extendedState, compactControl!)).toBe(false); expect(isToolcraftControlVisible(extendedState, extendedControl!)).toBe(true); expect(isToolcraftControlDisabled(extendedState, extendedControl!)).toBe(false); const hiddenState = createToolcraftState(schema, { panels: { timeline: { hidden: true } }, }); expect(isToolcraftSectionVisible(hiddenState, section!)).toBe(false); }); });