import { describe, expect, it } from "vitest"; import { timelineModule } from "../../../modules/built-ins/timeline/timeline-module"; 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({ base: { identity: { id: "runtime-test", title: "Toolcraft App" }, canvas: { enabled: true }, panels: { controls: { sections: [ { id: "test-section-1", 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", }, }, }, modules: [], }); 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({ base: { identity: { id: "runtime-test", title: "Toolcraft App" }, canvas: { enabled: true, renderScale: true, sizing: { mode: "editable-output" }, }, panels: { controls: { sections: [ { id: "test-section-2", controls: { compactOnly: { defaultValue: 25, label: "Compact value", target: "demo.compact", type: "slider", applicability: { all: [ { equals: false, target: "panels.timeline.extended", }, ], mode: "conditional" as const, }, }, extendedOnly: { defaultValue: 75, disabledWhen: { equals: false, target: "panels.timeline.extended", }, label: "Extended value", target: "demo.extended", type: "slider", applicability: { all: [ { equals: true, target: "panels.timeline.extended", }, ], mode: "conditional" as const, }, }, }, title: "Extended Timeline", visibleWhen: { equals: true, target: "panels.timeline.visible", }, }, ], title: "Controls", }, }, }, modules: [ timelineModule({ 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); }); });