import { describe, expect, it } from "vitest"; import { compileToolcraftPerformanceFixturePlan, type ToolcraftPerformanceCompiledFixturePlan, } from "./performance"; import { createCanonicalAdapter, createCanonicalConfig, createCanonicalDimension, createCanonicalPass, getCanonicalPath, withCanonicalEvidence, } from "./performance-fixture-plan.test-fixtures"; function expectAvailableDevelopment( plan: ToolcraftPerformanceCompiledFixturePlan, ) { expect(plan.development.status).toBe("available"); if (plan.development.status !== "available") { throw new Error(plan.development.reason); } return plan.development.checkpoint; } describe("Toolcraft compiled performance fixture plans", () => { it("marks an unreachable degenerate development checkpoint unavailable", () => { const dimension = createCanonicalDimension("count", { defaultValue: 5, interactiveMax: 5, }); const config = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count") }, dimensions: [dimension], passes: [createCanonicalPass("composite", ["count"], "linear")], }); const plan = compileToolcraftPerformanceFixturePlan( config, getCanonicalPath(config), ); expect(plan.kind).toBe("interactive"); expect(plan.development).toMatchObject({ status: "unavailable", }); expect( plan.development.status === "unavailable" && plan.development.reason, ).toContain("cannot reach exact normalized development pressure 0.8"); expect(plan.maximum).toMatchObject({ kind: "interactive-max", values: { count: 5 }, }); }); it("treats a 0..5e-7 range as reachable rather than degenerate", () => { const config = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count") }, dimensions: [ createCanonicalDimension("count", { batchMax: 5e-7, defaultValue: 0, interactiveMax: 5e-7, }), ], passes: [createCanonicalPass("composite", ["count"], "linear")], }); const checkpoint = expectAvailableDevelopment( compileToolcraftPerformanceFixturePlan(config, getCanonicalPath(config)), ); expect(checkpoint.normalizedPressure).toBeCloseTo(0.8, 4); expect(checkpoint.values.count).toBeCloseTo(4e-7, 12); }); it("derives direct linear development values without depending on dimension names", () => { const config = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count"), scale: createCanonicalAdapter("scale"), }, dimensions: [ createCanonicalDimension("count", { defaultValue: 0 }), createCanonicalDimension("scale", { defaultValue: 0 }), ], passes: [createCanonicalPass("composite", ["count", "scale"], "linear")], }); const checkpoint = expectAvailableDevelopment( compileToolcraftPerformanceFixturePlan(config, getCanonicalPath(config)), ); expect(checkpoint.normalizedPressure).toBeCloseTo(0.8, 4); expect(checkpoint.values.count).toBeCloseTo(80, 4); expect(checkpoint.values.scale).toBeCloseTo(80, 4); const renamedConfig = createCanonicalConfig({ adapters: { alpha: createCanonicalAdapter("alpha"), zeta: createCanonicalAdapter("zeta"), }, dimensions: [ createCanonicalDimension("zeta", { defaultValue: 0 }), createCanonicalDimension("alpha", { defaultValue: 0 }), ], passes: [createCanonicalPass("composite", ["zeta", "alpha"], "linear")], }); const renamedCheckpoint = expectAvailableDevelopment( compileToolcraftPerformanceFixturePlan( renamedConfig, getCanonicalPath(renamedConfig), ), ); expect(renamedCheckpoint.values.alpha).toBeCloseTo(80, 4); expect(renamedCheckpoint.values.zeta).toBeCloseTo(80, 4); }); it("derives exact development pressure toward a numerically lower workload boundary", () => { const config = createCanonicalConfig({ adapters: { sampleStep: createCanonicalAdapter("sampleStep") }, dimensions: [ createCanonicalDimension("sampleStep", { batchMax: 1, defaultValue: 20, interactiveMax: 1, }), ], passes: [ createCanonicalPass("sample-pass", ["sampleStep"], "linear"), ], }); const plan = compileToolcraftPerformanceFixturePlan( config, getCanonicalPath(config), ); const checkpoint = expectAvailableDevelopment(plan); expect(checkpoint.normalizedPressure).toBeCloseTo(0.8, 4); expect(checkpoint.values.sampleStep).toBeCloseTo(4.8, 4); expect(plan.maximum.values.sampleStep).toBe(1); }); it.each([ { mapping: "direct" as const, relationship: "quadratic" as const }, { mapping: "area" as const, relationship: "linear" as const }, { mapping: "direct" as const, relationship: "product" as const }, ])( "solves generic $mapping + $relationship combined cost at development pressure", ({ mapping, relationship }) => { const config = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count"), scale: createCanonicalAdapter("scale"), }, dimensions: [ createCanonicalDimension("count", { defaultValue: 0, mapping }), createCanonicalDimension("scale", { defaultValue: 0, mapping }), ], passes: [ createCanonicalPass( "composite", ["count", "scale"], relationship, ), ], }); const checkpoint = expectAvailableDevelopment( compileToolcraftPerformanceFixturePlan(config, getCanonicalPath(config)), ); expect(checkpoint.normalizedPressure).toBeCloseTo(0.8, 4); expect(Object.values(checkpoint.values).every(Number.isFinite)).toBe(true); }, ); it.each([ { mapping: "custom" as const, relationship: "quadratic" as const }, { mapping: "custom" as const, relationship: "product" as const }, { mapping: "direct" as const, relationship: "benchmark" as const }, ])( "uses measured combined evidence for $mapping + $relationship without transforming 0.8", ({ mapping, relationship }) => { const dimensions = [ createCanonicalDimension("count", { customMappingReason: mapping === "custom" ? "Measured custom growth." : undefined, mapping, }), createCanonicalDimension("scale", { customMappingReason: mapping === "custom" ? "Measured custom growth." : undefined, defaultValue: 1, interactiveMax: 9, mapping, }), ]; const pass = createCanonicalPass( "composite", ["count", "scale"], relationship, ); const baseConfig = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count"), scale: createCanonicalAdapter("scale"), }, dimensions, passes: [pass], }); const config = withCanonicalEvidence(baseConfig, [ { measuredResult: "Combined vector measured at development pressure.", normalizedPressure: 0.8, passId: pass.id, values: { count: 72, scale: 6 }, }, ]); const plan = compileToolcraftPerformanceFixturePlan( config, getCanonicalPath(config), ); const checkpoint = expectAvailableDevelopment(plan); expect(checkpoint.normalizedPressure).toBe(0.8); expect(checkpoint.values).toEqual({ count: 72, scale: 6 }); }, ); it("rejects incomplete, extra, and incompatible combined evidence vectors", () => { const dimensions = [ createCanonicalDimension("count", { customMappingReason: "Measured custom growth.", mapping: "custom", }), createCanonicalDimension("scale", { defaultValue: 1, interactiveMax: 9 }), ]; const passes = [ createCanonicalPass("custom-pass", ["count"], "linear"), createCanonicalPass("benchmark-pass", ["count", "scale"], "benchmark"), ]; const baseConfig = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count"), scale: createCanonicalAdapter("scale"), }, dimensions, passes, }); const config = withCanonicalEvidence(baseConfig, [ { measuredResult: "Missing scale.", normalizedPressure: 0.8, passId: "custom-pass", values: { count: 72 }, }, { measuredResult: "Conflicting and extra values.", normalizedPressure: 0.8, passId: "benchmark-pass", values: { count: 73, extra: 1, scale: 6 }, }, ]); expect(() => compileToolcraftPerformanceFixturePlan(config, getCanonicalPath(config)), ).toThrow(/missing dimension key "scale"[\s\S]*unexpected dimension key "extra"[\s\S]*same combined values vector/); }); it("rejects evidence vectors that overload a generic pass above 0.8", () => { const dimensions = [ createCanonicalDimension("custom", { customMappingReason: "Measured custom growth.", mapping: "custom", }), createCanonicalDimension("generic", { defaultValue: 0, interactiveMax: 10 }), ]; const passes = [ createCanonicalPass("custom-pass", ["custom"], "linear"), createCanonicalPass("generic-pass", ["generic"], "linear"), ]; const baseConfig = createCanonicalConfig({ adapters: { custom: createCanonicalAdapter("custom"), generic: createCanonicalAdapter("generic"), }, dimensions, passes, }); const config = withCanonicalEvidence(baseConfig, [ { measuredResult: "Custom pass measured at 0.8.", normalizedPressure: 0.8, passId: "custom-pass", values: { custom: 72, generic: 10 }, }, ]); expect(() => compileToolcraftPerformanceFixturePlan(config, getCanonicalPath(config)), ).toThrow(/generic pass "generic-pass" pressure 1 exceeds development target 0.8/); }); it("fails compilation with the complete deterministic adapter error set", () => { const config = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("wrong-id"), }, dimensions: [createCanonicalDimension("count")], passes: [createCanonicalPass("composite", ["count"], "linear")], }); expect(() => compileToolcraftPerformanceFixturePlan(config, getCanonicalPath(config)), ).toThrow(/dimensionId must equal its registry key "count"/); }); it("accepts one coherent combined vector for multiple evidence-required passes", () => { const dimensions = [ createCanonicalDimension("count", { customMappingReason: "Measured custom growth.", mapping: "custom", }), createCanonicalDimension("scale", { defaultValue: 1, interactiveMax: 9 }), ]; const passes = [ createCanonicalPass("custom-pass", ["count"], "linear"), createCanonicalPass("benchmark-pass", ["count", "scale"], "benchmark"), ]; const baseConfig = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count"), scale: createCanonicalAdapter("scale"), }, dimensions, passes, }); const vector = { count: 72, scale: 6 }; const config = withCanonicalEvidence(baseConfig, passes.map((pass) => ({ measuredResult: `${pass.id} measured at combined development pressure.`, normalizedPressure: 0.8, passId: pass.id, values: vector, }))); const checkpoint = expectAvailableDevelopment( compileToolcraftPerformanceFixturePlan(config, getCanonicalPath(config)), ); expect(checkpoint).toMatchObject({ normalizedPressure: 0.8, values: vector }); }); it("returns discriminated interactive and batch plans with complete maxima", () => { const dimension = createCanonicalDimension("count"); const pass = createCanonicalPass("composite", ["count"], "linear"); const interactiveConfig = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count") }, dimensions: [dimension], passes: [pass], }); const batchConfig = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count") }, dimensions: [dimension], interaction: "export", passes: [pass], }); const interactivePlan = compileToolcraftPerformanceFixturePlan( interactiveConfig, getCanonicalPath(interactiveConfig), ); const batchPlan = compileToolcraftPerformanceFixturePlan( batchConfig, getCanonicalPath(batchConfig), ); expect(interactivePlan).toMatchObject({ dimensionIds: ["count"], kind: "interactive", maximum: { kind: "interactive-max", values: { count: 100 } }, }); expect(batchPlan).toMatchObject({ dimensionIds: ["count"], kind: "batch", maximum: { kind: "batch-max", values: { count: 200 } }, }); }); it("compiles a batch-only dimension without manufacturing an interactive maximum", () => { const config = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count") }, dimensions: [ createCanonicalDimension("count", { batchMax: 200, interactiveMax: undefined, }), ], interaction: "export", passes: [createCanonicalPass("export-pass", ["count"], "linear")], }); const plan = compileToolcraftPerformanceFixturePlan( config, getCanonicalPath(config), ); expect(plan).toEqual(expect.objectContaining({ dimensionIds: ["count"], kind: "batch", maximum: { kind: "batch-max", normalizedPressure: 1, values: { count: 200 }, }, })); expect("interactiveMax" in plan).toBe(false); }); it("rejects a mixed batch path instead of returning a partial maximum", () => { const config = createCanonicalConfig({ adapters: { batch: createCanonicalAdapter("batch"), interactive: createCanonicalAdapter("interactive"), }, dimensions: [ createCanonicalDimension("batch", { interactiveMax: undefined }), createCanonicalDimension("interactive", { batchMax: undefined }), ], interaction: "export", passes: [ createCanonicalPass("export-pass", ["batch", "interactive"], "linear"), ], }); expect(() => compileToolcraftPerformanceFixturePlan(config, getCanonicalPath(config)), ).toThrow(/requires workload dimension "interactive" to declare batchMax/); }); });