import { describe, expect, it } from "vitest"; import { compileToolcraftPerformanceFixturePlan, defineToolcraftDiscreteFixtureAdapter, toolcraftDiscreteCombinationBudget, } from "./performance"; import { getToolcraftPerformanceFixtureAdapterErrors } from "./performance-fixture-adapters"; import { createCanonicalAdapter, createCanonicalConfig, createCanonicalDimension, createCanonicalPass, getCanonicalPath, withCanonicalEvidence, } from "./performance-fixture-plan.test-fixtures"; function createDiscreteAdapter(dimensionId: string, values: readonly number[]) { return defineToolcraftDiscreteFixtureAdapter({ dimensionId, domain: { attestation: "The runtime fixture reducer enumerates every numeric mode.", kind: "runtime-state", path: `fixture.${dimensionId}`, }, entries: values.map((value) => ({ appliedValue: value, value })), }); } const overBudgetDomainValues = [ 20, 72, 73, 100, ...Array.from( { length: toolcraftDiscreteCombinationBudget + 1 }, (_, index) => 20 + ((index + 1) * 80) / (toolcraftDiscreteCombinationBudget + 2), ), ]; function createOverBudgetCustomConfig( passes = [createCanonicalPass("custom-pass", ["count"], "linear")], ) { return createCanonicalConfig({ adapters: { count: createDiscreteAdapter("count", overBudgetDomainValues), }, dimensions: [ createCanonicalDimension("count", { batchMax: undefined, customMappingReason: "Measured custom growth.", mapping: "custom", }), ], passes, }); } function getCompileErrorMessage(config: ReturnType) { try { compileToolcraftPerformanceFixturePlan(config, getCanonicalPath(config)); } catch (error) { return error instanceof Error ? error.message : String(error); } throw new Error("Expected fixture plan compilation to fail."); } function getOverBudgetDiagnostic(pathId: string) { return `Performance path "${pathId}" requires ${overBudgetDomainValues.length} discrete combinations, exceeding the deterministic path-level budget ${toolcraftDiscreteCombinationBudget}.`; } describe("Toolcraft reachable fixture values", () => { it("accepts a discrete generic development checkpoint only at exact pressure", () => { const config = createCanonicalConfig({ adapters: { count: createDiscreteAdapter("count", [20, 84, 100]), }, dimensions: [ createCanonicalDimension("count", { batchMax: undefined }), ], passes: [createCanonicalPass("composite", ["count"], "linear")], }); const plan = compileToolcraftPerformanceFixturePlan( config, getCanonicalPath(config), ); expect(plan.development).toMatchObject({ checkpoint: { normalizedPressure: 0.8, values: { count: 84 } }, status: "available", }); }); it("accepts inverse evidence when every value is reachable within tolerance", () => { const dimension = createCanonicalDimension("count", { batchMax: undefined, customMappingReason: "Measured custom growth.", mapping: "custom", }); const pass = createCanonicalPass("custom-pass", ["count"], "linear"); const baseConfig = createCanonicalConfig({ adapters: { count: createDiscreteAdapter("count", [ 20, 72 + Number.EPSILON * 72, 100, ]), }, dimensions: [dimension], passes: [pass], }); const config = withCanonicalEvidence(baseConfig, [{ measuredResult: "Measured reachable inverse value.", normalizedPressure: 0.8, passId: pass.id, values: { count: 72 }, }]); expect( compileToolcraftPerformanceFixturePlan(config, getCanonicalPath(config)) .development, ).toMatchObject({ status: "available" }); }); it("rejects an adapter-unreachable inverse value before apply", () => { const dimension = createCanonicalDimension("count", { batchMax: undefined, customMappingReason: "Measured custom growth.", mapping: "custom", }); const pass = createCanonicalPass("custom-pass", ["count"], "linear"); const baseConfig = createCanonicalConfig({ adapters: { count: createDiscreteAdapter("count", [20, 70, 100]), }, dimensions: [dimension], passes: [pass], }); const config = withCanonicalEvidence(baseConfig, [{ measuredResult: "Measured value outside the adapter domain.", normalizedPressure: 0.8, passId: pass.id, values: { count: 72 }, }]); expect(() => compileToolcraftPerformanceFixturePlan(config, getCanonicalPath(config)), ).toThrow( /value 72 for dimension "count" is not present in fixtureAdapters\.dimensions\["count"\]\.entries/u, ); }); it("rejects a multi-dimension inverse vector with an unreachable value", () => { const dimensions = [ createCanonicalDimension("count", { batchMax: undefined, customMappingReason: "Measured custom growth.", mapping: "custom", }), createCanonicalDimension("scale", { batchMax: undefined, defaultValue: 1, interactiveMax: 9, }), ]; const pass = createCanonicalPass( "benchmark-pass", ["count", "scale"], "benchmark", ); const baseConfig = createCanonicalConfig({ adapters: { count: createDiscreteAdapter("count", [20, 72, 100]), scale: createDiscreteAdapter("scale", [1, 5, 9]), }, dimensions, passes: [pass], }); const config = withCanonicalEvidence(baseConfig, [{ measuredResult: "Combined vector with one unreachable value.", normalizedPressure: 0.8, passId: pass.id, values: { count: 72, scale: 6 }, }]); expect(() => compileToolcraftPerformanceFixturePlan(config, getCanonicalPath(config)), ).toThrow( /value 6 for dimension "scale" is not present in fixtureAdapters\.dimensions\["scale"\]\.entries/u, ); }); it("accepts a multi-dimension inverse vector when every value belongs to its domain", () => { const dimensions = [ createCanonicalDimension("count", { batchMax: undefined, customMappingReason: "Measured custom growth.", mapping: "custom", }), createCanonicalDimension("scale", { batchMax: undefined, defaultValue: 1, interactiveMax: 9, }), ]; const pass = createCanonicalPass( "benchmark-pass", ["count", "scale"], "benchmark", ); const baseConfig = createCanonicalConfig({ adapters: { count: createDiscreteAdapter("count", [20, 72, 100]), scale: createDiscreteAdapter("scale", [1, 5, 9]), }, dimensions, passes: [pass], }); const config = withCanonicalEvidence(baseConfig, [{ measuredResult: "Every combined value belongs to its exhaustive domain.", normalizedPressure: 0.8, passId: pass.id, values: { count: 72, scale: 5 }, }]); expect( compileToolcraftPerformanceFixturePlan(config, getCanonicalPath(config)) .development, ).toMatchObject({ checkpoint: { values: { count: 72, scale: 5 } }, status: "available", }); }); it("accepts the exact path combination budget", () => { const values = Array.from( { length: toolcraftDiscreteCombinationBudget }, (_, index) => 20 + (80 * index) / (toolcraftDiscreteCombinationBudget - 1), ); const config = createCanonicalConfig({ adapters: { count: createDiscreteAdapter("count", values) }, dimensions: [ createCanonicalDimension("count", { batchMax: undefined }), ], passes: [createCanonicalPass("composite", ["count"], "linear")], }); expect( getToolcraftPerformanceFixtureAdapterErrors(config, [ getCanonicalPath(config), ]), ).toEqual([]); }); it("rejects an over-budget path as a planning error", () => { const values = Array.from( { length: toolcraftDiscreteCombinationBudget + 1 }, (_, index) => 20 + (80 * index) / toolcraftDiscreteCombinationBudget, ); const config = createCanonicalConfig({ adapters: { count: createDiscreteAdapter("count", values) }, dimensions: [ createCanonicalDimension("count", { batchMax: undefined }), ], passes: [createCanonicalPass("composite", ["count"], "linear")], }); const path = getCanonicalPath(config); const expected = `requires ${toolcraftDiscreteCombinationBudget + 1} discrete combinations, exceeding the deterministic path-level budget ${toolcraftDiscreteCombinationBudget}`; expect( getToolcraftPerformanceFixtureAdapterErrors(config, [path]), ).toEqual([expect.stringContaining(expected)]); expect(() => compileToolcraftPerformanceFixturePlan(config, path), ).toThrow(expected); }); it("budgets only exhaustive values reachable within the selected path boundary", () => { const batchOnlyValues = Array.from( { length: toolcraftDiscreteCombinationBudget }, (_, index) => 101 + index, ); const config = createCanonicalConfig({ adapters: { count: createDiscreteAdapter("count", [20, 84, 100, ...batchOnlyValues]), }, dimensions: [ createCanonicalDimension("count", { batchMax: batchOnlyValues[batchOnlyValues.length - 1], }), ], passes: [createCanonicalPass("composite", ["count"], "linear")], }); const path = getCanonicalPath(config); expect(getToolcraftPerformanceFixtureAdapterErrors(config, [path])).toEqual([]); expect(compileToolcraftPerformanceFixturePlan(config, path).development).toMatchObject({ checkpoint: { values: { count: 84 } }, status: "available", }); }); it("bypasses unused discrete search budget for a valid exact inverse checkpoint", () => { const pass = createCanonicalPass("custom-pass", ["count"], "linear"); const baseConfig = createOverBudgetCustomConfig([pass]); const config = withCanonicalEvidence(baseConfig, [{ measuredResult: "Measured exact inverse checkpoint.", normalizedPressure: 0.8, passId: pass.id, values: { count: 72 }, }]); expect( compileToolcraftPerformanceFixturePlan(config, getCanonicalPath(config)) .development, ).toMatchObject({ checkpoint: { normalizedPressure: 0.8, values: { count: 72 } }, status: "available", }); const pathWithoutInverse = getCanonicalPath(baseConfig); expect(() => compileToolcraftPerformanceFixturePlan(baseConfig, pathWithoutInverse), ).toThrow(/exceeding the deterministic path-level budget/u); }); it("reports malformed inverse evidence before the applicable search budget", () => { const pass = createCanonicalPass("custom-pass", ["count"], "linear"); const baseConfig = createOverBudgetCustomConfig([pass]); const config = withCanonicalEvidence(baseConfig, [{ measuredResult: "Malformed vector missing its dimension.", normalizedPressure: 0.8, passId: pass.id, values: {}, }]); const path = getCanonicalPath(config); expect(getCompileErrorMessage(config)).toBe([ `Performance path "${path.id}" checkpoint for pass "${pass.id}" is missing dimension key "count".`, `Performance path "${path.id}" inverse checkpoints must describe the same combined values vector.`, getOverBudgetDiagnostic(path.id), ].join("\n")); }); it("reports unreachable inverse evidence before the applicable search budget", () => { const pass = createCanonicalPass("custom-pass", ["count"], "linear"); const baseConfig = createOverBudgetCustomConfig([pass]); const config = withCanonicalEvidence(baseConfig, [{ measuredResult: "Value outside the exhaustive domain.", normalizedPressure: 0.8, passId: pass.id, values: { count: 72.5 }, }]); const path = getCanonicalPath(config); expect(getCompileErrorMessage(config)).toBe([ `Performance path "${path.id}" checkpoint for pass "${pass.id}" value 72.5 for dimension "count" is not present in fixtureAdapters.dimensions["count"].entries.`, getOverBudgetDiagnostic(path.id), ].join("\n")); }); it("reports non-development-pressure inverse evidence before the applicable search budget", () => { const pass = createCanonicalPass("custom-pass", ["count"], "linear"); const baseConfig = createOverBudgetCustomConfig([pass]); const config = withCanonicalEvidence(baseConfig, [{ measuredResult: "Measured at the wrong pressure.", normalizedPressure: 0.7, passId: pass.id, values: { count: 72 }, }]); const path = getCanonicalPath(config); expect(getCompileErrorMessage(config)).toBe([ `Performance path "${path.id}" checkpoint for pass "${pass.id}" must have normalizedPressure 0.8.`, getOverBudgetDiagnostic(path.id), ].join("\n")); }); it("reports inconsistent inverse evidence before the applicable search budget", () => { const firstPass = createCanonicalPass("custom-a", ["count"], "linear"); const secondPass = createCanonicalPass("custom-b", ["count"], "linear"); const baseConfig = createOverBudgetCustomConfig([firstPass, secondPass]); const config = withCanonicalEvidence(baseConfig, [ { measuredResult: "First measured inverse vector.", normalizedPressure: 0.8, passId: firstPass.id, values: { count: 72 }, }, { measuredResult: "Second measured inverse vector.", normalizedPressure: 0.8, passId: secondPass.id, values: { count: 73 }, }, ]); const path = getCanonicalPath(config); expect(getCompileErrorMessage(config)).toBe([ `Performance path "${path.id}" inverse checkpoints must describe the same combined values vector.`, getOverBudgetDiagnostic(path.id), ].join("\n")); }); });