import { describe, expect, it } from "vitest"; import { defineToolcraft } from "../schema/define-toolcraft"; import { getToolcraftPerformanceFixtureAdapterErrors } from "./performance-fixture-adapters"; import { compileToolcraftPerformanceFixturePlan, defineToolcraftDiscreteFixtureAdapter, defineToolcraftFixtureAdapter, defineToolcraftSchemaDiscreteFixtureAdapter, deriveToolcraftPerformancePaths, validateToolcraftPerformanceCoverage, type ToolcraftEnvelopePerformanceConfig, type ToolcraftPerformanceFixtureAdapter, } from "./performance"; const schema = defineToolcraft({ canvas: { enabled: true }, panels: { controls: { sections: [{ controls: { mode: { defaultValue: "medium", label: "Mode", options: [ { label: "Low", value: "low" }, { label: "Medium", value: "medium" }, { label: "High", value: "high" }, ], performanceRole: "workload", target: "render.mode", type: "select", }, }, title: "Render", }], title: "Controls", }, }, }); const segmentedSchema = defineToolcraft({ canvas: { enabled: true }, panels: { controls: { sections: [{ controls: { mode: { defaultValue: "medium", label: "Mode", options: [ { label: "Low", value: "low" }, { label: "Medium", value: "medium" }, { label: "High", value: "high" }, ], performanceRole: "workload", target: "render.mode", type: "segmented", }, }, title: "Render", }], title: "Controls", }, }, }); const tabsSchema = defineToolcraft({ canvas: { enabled: true }, panels: { controls: { sections: [{ controls: { mode: { defaultValue: "medium", label: "Mode", options: [ { label: "Low", value: "low" }, { label: "Medium", value: "medium" }, { label: "High", value: "high" }, ], performanceRole: "workload", target: "render.mode", type: "tabs", }, }, title: "Render", }], title: "Controls", }, }, }); const entries = [ { appliedValue: "low", value: 20 }, { appliedValue: "medium", value: 60 }, { appliedValue: "high", value: 100 }, ] as const; function createConfig( adapter: ToolcraftPerformanceFixtureAdapter = defineToolcraftSchemaDiscreteFixtureAdapter(schema, { dimensionId: "count", entries, target: "render.mode", }), ): ToolcraftEnvelopePerformanceConfig { return { fixtureAdapters: { dimensions: { count: adapter } }, rendererPipeline: { interactionInvalidation: [{ interaction: "control-change", invalidates: ["composite"], targets: ["render.mode"], }], passes: [{ cost: { dimensions: ["count"], frequency: "discrete", relationship: "linear", }, id: "composite", inputs: ["render.mode"], invalidatedBy: ["render.mode"], kind: "composite", output: "preview", quality: "full", runsOn: "main", }], runtimeId: "discrete-domain-v1", }, rendererStrategy: "dom", scenarios: [], usesCustomRenderer: true, workloadEnvelope: { dimensions: [{ defaultValue: 60, id: "count", interactiveMax: 100, mapping: "direct", source: { kind: "schema-target", target: "render.mode" }, unit: "items", }], }, }; } describe("Toolcraft exhaustive discrete fixture domains", () => { it("derives apply and observe from one exhaustive schema option mapping", () => { const config = createConfig(); const adapter = config.fixtureAdapters!.dimensions.count!; expect( validateToolcraftPerformanceCoverage(schema, config).filter((error) => error.startsWith("fixtureAdapters"), ), ).toEqual([]); expect(adapter.apply(100)).toBe("high"); expect(adapter.observe("medium")).toBe(60); }); it.each([ ["select", schema], ["segmented", segmentedSchema], ["tabs", tabsSchema], ] as const)( "rejects a continuous adapter bypass for a finite %s schema target", (_controlType, finiteSchema) => { const continuous = defineToolcraftFixtureAdapter({ apply: (value: number) => value, dimensionId: "count", observe: (value: number) => value, }); const config = createConfig(continuous); const expected = 'workload dimension "count" targets finite schema control "render.mode" and must use an exhaustive-discrete schema-options adapter.'; expect(validateToolcraftPerformanceCoverage(finiteSchema, config)).toContain( expected, ); }, ); it.each([ { entries: [ { appliedValue: "low", value: 20 }, { appliedValue: "high", value: 100 }, ], expected: /is missing schema option "medium"/u, }, { entries: [ ...entries, { appliedValue: "extra", value: 120 }, ], expected: /has extra applied value "extra"/u, }, { entries: [ ...entries, { appliedValue: "high", value: 120 }, ], expected: /must not contain duplicate applied values/u, }, { entries: [ ...entries, { appliedValue: "extra", value: 100 }, ], expected: /must not contain duplicate numeric values/u, }, ])("rejects non-bijective schema entries", ({ entries: invalid, expected }) => { const adapter = defineToolcraftSchemaDiscreteFixtureAdapter(schema, { dimensionId: "count", entries: invalid, target: "render.mode", }); const config = createConfig(adapter); const path = deriveToolcraftPerformancePaths(schema, config)[0]!; expect(validateToolcraftPerformanceCoverage(schema, config)).toEqual( expect.arrayContaining([expect.stringMatching(expected)]), ); expect(() => compileToolcraftPerformanceFixturePlan(config, path), ).toThrow(expected); }); it("requires non-schema exhaustive provenance to match the dimension source", () => { const adapter = defineToolcraftDiscreteFixtureAdapter({ dimensionId: "count", domain: { attestation: "All runtime modes are enumerated by the reducer.", kind: "runtime-state", path: "runtime.other", }, entries, }); const config = createConfig(adapter); const dimension = config.workloadEnvelope.dimensions[0]!; const malformed = { ...config, workloadEnvelope: { dimensions: [{ ...dimension, source: { kind: "runtime-state", path: "runtime.mode" }, }], }, } as ToolcraftEnvelopePerformanceConfig; expect(getToolcraftPerformanceFixtureAdapterErrors(malformed)).toContain( 'fixtureAdapters.dimensions["count"].domain path "runtime.other" must match workload dimension source path "runtime.mode".', ); }); it("requires the schema domain target to match the dimension source", () => { const config = createConfig(); const dimension = config.workloadEnvelope.dimensions[0]!; const malformed = { ...config, workloadEnvelope: { dimensions: [{ ...dimension, source: { kind: "schema-target", target: "render.other" }, }], }, } as ToolcraftEnvelopePerformanceConfig; expect(getToolcraftPerformanceFixtureAdapterErrors(malformed)).toContain( 'fixtureAdapters.dimensions["count"].domain target "render.mode" must match workload dimension source target "render.other".', ); }); });