import { describe, expect, it } from "vitest"; import type { ToolcraftControlSchema, ToolcraftNonModelControlSchema, } from "../schema/types"; import { createDimension, createSchema, createWorkloadControlWithoutNumericFields, errorsFor, workloadControl, } from "./performance-workload-envelope-test-fixtures"; describe("Toolcraft workload envelope schema validation", () => { it("rejects unknown and non-workload schema targets", () => { expect( errorsFor([ createDimension({ source: { kind: "schema-target", target: "render.missing" }, }), ]), ).toEqual([ 'Workload dimension "count" references unknown schema target "render.missing".', "Workload control render.count must map to exactly one workloadEnvelope dimension.", ]); const responsivenessSchema = createSchema({ count: { ...workloadControl, performanceRole: "responsiveness" }, }); expect(errorsFor([createDimension()], responsivenessSchema)).toEqual([ 'Workload dimension "count" schema target "render.count" must reference exactly one explicit workload control.', ]); }); it("rejects a schema target mapped by multiple dimensions", () => { expect( errorsFor([createDimension(), createDimension({ id: "secondary" })]), ).toEqual([ 'Schema target "render.count" must not map to more than one workloadEnvelope dimension.', "Workload control render.count must map to exactly one workloadEnvelope dimension.", ]); }); it("matches authored numeric defaults and declared boundaries to schema bounds", () => { expect( errorsFor([createDimension({ batchMax: 12, interactiveMax: undefined })]), ).toEqual([]); expect( errorsFor([createDimension({ defaultValue: 3, interactiveMax: 11 })]), ).toEqual([ 'Workload dimension "count" defaultValue must match schema target "render.count" defaultValue 4.', 'Workload dimension "count" interactiveMax must match schema target "render.count" maximum 12 selected by source.workloadBoundary.', ]); }); it("supports numeric controls whose maximum workload is at the schema minimum", () => { const schema = createSchema({ count: { ...workloadControl, defaultValue: 20, max: 100, min: 1, }, }); const inverseSource = { kind: "schema-target" as const, target: "render.count", workloadBoundary: "minimum" as const, }; expect( errorsFor( [ createDimension({ defaultValue: 20, interactiveMax: 1, source: inverseSource, }), ], schema, ), ).toEqual([]); expect( errorsFor( [ createDimension({ batchMax: 1, defaultValue: 20, interactiveMax: undefined, source: inverseSource, }), ], schema, ), ).toEqual([]); expect( errorsFor( [ createDimension({ defaultValue: 20, interactiveMax: 100, source: inverseSource, }), ], schema, ), ).toEqual([ 'Workload dimension "count" interactiveMax must match schema target "render.count" minimum 1 selected by source.workloadBoundary.', ]); }); it("supports explicit custom numeric domains in either workload direction", () => { const schema = createSchema({ count: { defaultValue: 20, label: "Custom density", max: 100, min: 1, performanceReason: "The custom control changes rendered density.", performanceRole: "workload", target: "render.count", type: "productDensityControl", }, }); expect( errorsFor( [ createDimension({ defaultValue: 20, interactiveMax: 1, source: { kind: "schema-target", target: "render.count", workloadBoundary: "minimum", }, }), ], schema, ), ).toEqual([]); expect( errorsFor( [createDimension({ defaultValue: 20, interactiveMax: 100 })], schema, ), ).toEqual([]); }); it("rejects partial and malformed custom numeric domains", () => { const partialSchema = createSchema({ count: { defaultValue: 4, label: "Custom count", max: 12, performanceReason: "The custom control changes rendered work.", performanceRole: "workload", target: "render.count", type: "productCountControl", }, }); const invertedSchema = createSchema({ count: { ...workloadControl, defaultValue: 20, max: 1, min: 100, type: "productCountControl", }, }); expect(errorsFor([createDimension()], partialSchema)).toEqual([ 'Schema target "render.count" minimum must be a finite number.', ]); expect(errorsFor([createDimension({ defaultValue: 20 })], invertedSchema)).toEqual( expect.arrayContaining([ 'Schema target "render.count" minimum must not exceed its maximum.', 'Schema target "render.count" defaultValue must be within its numeric domain.', ]), ); }); it("requires an explicit workload direction for numeric schema controls", () => { expect( errorsFor([ createDimension({ source: { kind: "schema-target", target: "render.count" }, }), ]), ).toEqual([ 'Workload dimension "count" source.workloadBoundary must be "minimum" or "maximum" for numeric schema target "render.count".', ]); }); it("resolves omitted slider maxima to the runtime maximum", () => { const schema = createSchema({ count: createWorkloadControlWithoutNumericFields({ defaultValue: 4, min: 0, }), }); expect(errorsFor([createDimension({ interactiveMax: 99 })], schema)).toEqual([ 'Workload dimension "count" interactiveMax must match schema target "render.count" maximum 100 selected by source.workloadBoundary.', ]); expect(errorsFor([createDimension({ interactiveMax: 100 })], schema)).toEqual( [], ); }); it("treats omitted and explicit undefined slider numeric fields identically", () => { const omittedFieldsSchema = createSchema({ count: createWorkloadControlWithoutNumericFields(), }); const undefinedFieldsSchema = createSchema({ count: createWorkloadControlWithoutNumericFields({ defaultValue: undefined, max: undefined, min: undefined, }), }); const dimension = createDimension({ defaultValue: 0, interactiveMax: 100 }); expect(errorsFor([dimension], omittedFieldsSchema)).toEqual([]); expect(errorsFor([dimension], undefinedFieldsSchema)).toEqual( errorsFor([dimension], omittedFieldsSchema), ); }); it("uses the runtime slider default fallback after reporting a malformed default", () => { const schema = createSchema({ count: createWorkloadControlWithoutNumericFields({ defaultValue: "4" }), }); expect( errorsFor([createDimension({ defaultValue: 4, interactiveMax: 100 })], schema), ).toEqual([ 'Schema target "render.count" defaultValue must be a finite number.', 'Workload dimension "count" defaultValue must match schema target "render.count" defaultValue 0.', ]); }); it("rejects malformed non-nullish slider minima", () => { for (const min of ["0", Number.NaN, Number.POSITIVE_INFINITY]) { const schema = createSchema({ count: createWorkloadControlWithoutNumericFields({ defaultValue: 4, max: 100, min, } as unknown as ToolcraftNonModelControlSchema), }); expect( errorsFor([createDimension({ interactiveMax: 100 })], schema), ).toEqual([ 'Schema target "render.count" minimum must be a finite number.', ]); } }); it("resolves omitted and undefined rangeSlider bounds like the runtime", () => { const omittedBoundsSchema = createSchema({ count: createWorkloadControlWithoutNumericFields({ defaultValue: [20, 80], type: "rangeSlider", }), }); const undefinedBoundsSchema = createSchema({ count: createWorkloadControlWithoutNumericFields({ defaultValue: [20, 80], max: undefined, min: undefined, type: "rangeSlider", }), }); const matchingDimension = createDimension({ interactiveMax: 100 }); expect(errorsFor([matchingDimension], omittedBoundsSchema)).toEqual([]); expect(errorsFor([matchingDimension], undefinedBoundsSchema)).toEqual( errorsFor([matchingDimension], omittedBoundsSchema), ); expect( errorsFor([createDimension({ interactiveMax: 99 })], omittedBoundsSchema), ).toEqual([ 'Workload dimension "count" interactiveMax must match schema target "render.count" maximum 100 selected by source.workloadBoundary.', ]); }); it("rejects non-finite numeric schema bounds", () => { const nonFiniteDefaultSchema = createSchema({ count: { ...workloadControl, defaultValue: Number.NaN }, }); const nonFiniteMaximumSchema = createSchema({ count: { ...workloadControl, max: Number.POSITIVE_INFINITY }, }); const nonNumericDefaultSchema = createSchema({ count: { ...workloadControl, defaultValue: "4" }, }); const nonNumericMaximumSchema = createSchema({ count: { ...workloadControl, max: "12" } as unknown as ToolcraftControlSchema, }); expect(errorsFor([createDimension()], nonFiniteDefaultSchema)).toEqual([ 'Schema target "render.count" defaultValue must be a finite number.', 'Workload dimension "count" defaultValue must match schema target "render.count" defaultValue 0.', ]); expect(errorsFor([createDimension()], nonFiniteMaximumSchema)).toEqual([ 'Schema target "render.count" maximum must be a finite number.', ]); expect(errorsFor([createDimension()], nonNumericDefaultSchema)).toEqual([ 'Schema target "render.count" defaultValue must be a finite number.', 'Workload dimension "count" defaultValue must match schema target "render.count" defaultValue 0.', ]); expect(errorsFor([createDimension()], nonNumericMaximumSchema)).toEqual([ 'Schema target "render.count" maximum must be a finite number.', ]); }); it("does not invent a schema maximum when the control exposes no numeric bound", () => { const schema = createSchema({ content: { defaultValue: "", label: "Content", performanceReason: "The value changes the amount of work.", performanceRole: "workload", target: "render.content", type: "text", }, }); expect( errorsFor( [ createDimension({ customMappingReason: "The dimension measures the input length.", defaultValue: 0, id: "content-length", interactiveMax: 100, mapping: "custom", source: { kind: "schema-target", target: "render.content" }, }), ], schema, ), ).toEqual([]); }); it("keeps workload direction on numeric schema controls only", () => { const schema = createSchema({ content: { defaultValue: "", label: "Content", performanceReason: "The value changes the amount of work.", performanceRole: "workload", target: "render.content", type: "text", }, }); expect( errorsFor( [ createDimension({ customMappingReason: "The dimension measures the input length.", defaultValue: 0, id: "content-length", interactiveMax: 100, mapping: "custom", source: { kind: "schema-target", target: "render.content", workloadBoundary: "maximum", }, }), ], schema, ), ).toEqual([ 'Workload dimension "content-length" source.workloadBoundary must be omitted for non-numeric schema target "render.content".', ]); }); });