import { defineToolcraft } from "../schema/define-toolcraft"; import type { ToolcraftControlSchema } from "../schema/types"; import type { ResolvedToolcraftAppSchema } from "../schema/resolved-app-schema"; import { defineToolcraftPerformance, getToolcraftWorkloadEnvelopeErrors, type ToolcraftEnvelopePerformanceConfig, type ToolcraftWorkloadDimension, } from "./performance"; type SliderControl = Extract; type RangeSliderControl = Extract< ToolcraftControlSchema, { type: "rangeSlider" } >; export const workloadControl = { applicability: { mode: "always" as const }, defaultValue: 4, label: "Count", max: 12, min: 0, performanceReason: "The value changes the amount of work.", performanceRole: "workload", target: "render.count", type: "slider", } satisfies SliderControl; export function createWorkloadControlWithoutNumericFields( overrides: | Partial | (Partial & { type: "rangeSlider" }) = {}, ): ToolcraftControlSchema { return { applicability: { mode: "always" as const }, label: "Count", performanceReason: "The value changes the amount of work.", performanceRole: "workload", target: "render.count", ...(overrides.type === "rangeSlider" ? { ...overrides, type: "rangeSlider" } : { ...overrides, type: "slider" }), }; } export function createSchema( controls: Record = { count: workloadControl }, renderScale = false, ): ResolvedToolcraftAppSchema { return defineToolcraft({ base: { identity: { id: "runtime-test", title: "Toolcraft App" }, canvas: { enabled: true, renderScale, sizing: { mode: "intrinsic-media" }, }, panels: { controls: { sections: [{ id: "test-section-1", controls }], title: "Controls", }, }, }, modules: [], }); } export function createResolvedSchemaFixture( controls: Record, renderScale = false, ): ResolvedToolcraftAppSchema { const schema = createSchema(undefined, renderScale); const panel = schema.panels.controls!; const resolvedFixture = { ...schema, panels: { ...schema.panels, controls: { ...panel, sections: panel.sections.map((section) => section.id === "runtime.setup" || section.id === "runtime.defaults" ? section : { ...section, controls }, ), }, }, }; return resolvedFixture as unknown as ResolvedToolcraftAppSchema; } export function createDimension( overrides: Partial = {}, ): ToolcraftWorkloadDimension { return { defaultValue: 4, id: "count", interactiveMax: 12, mapping: "direct", source: { kind: "schema-target", target: "render.count", workloadBoundary: "maximum", }, unit: "units", ...overrides, }; } export function createEnvelopeConfig( dimensions: readonly ToolcraftWorkloadDimension[], ): ToolcraftEnvelopePerformanceConfig { return defineToolcraftPerformance({ rendererStrategy: "none", scenarios: [], usesCustomRenderer: false, workloadEnvelope: { dimensions }, }); } export function createMalformedEnvelopeConfig( workloadEnvelope: unknown, ): ToolcraftEnvelopePerformanceConfig { return { rendererStrategy: "none", scenarios: [], usesCustomRenderer: false, workloadEnvelope, } as unknown as ToolcraftEnvelopePerformanceConfig; } export function errorsFor( dimensions: readonly ToolcraftWorkloadDimension[], schema = createSchema(), ): string[] { return getToolcraftWorkloadEnvelopeErrors( schema, createEnvelopeConfig(dimensions), ); }