import { describe, expect, it, vi } from "vitest"; import { compileToolcraftPerformanceFixturePlan, defineToolcraftDiscreteFixtureAdapter, executeToolcraftPerformanceFixtureCheckpoint, type ToolcraftPerformanceCompiledFixturePlan, validateToolcraftPerformanceCoverage, } from "./performance"; import { createCanonicalAdapter, createCanonicalConfig, createCanonicalDimension, createCanonicalPass, getCanonicalPath, withCanonicalEvidence, } from "./performance-fixture-plan.test-fixtures"; import { groupedSchema } from "./performance-paths.test-fixtures"; describe("Toolcraft compiled fixture execution", () => { it("keeps validation and compilation pure, then executes exact callbacks", () => { let applyCount = 0; let observeCount = 0; const config = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count", { apply: (value) => { applyCount += 1; return value; }, observe: (value) => { observeCount += 1; return value; }, }), }, dimensions: [createCanonicalDimension("count")], passes: [createCanonicalPass("composite", ["count"], "linear")], }); const path = getCanonicalPath(config); validateToolcraftPerformanceCoverage(groupedSchema, config); const plan = compileToolcraftPerformanceFixturePlan(config, path); expect({ applyCount, observeCount }).toEqual({ applyCount: 0, observeCount: 0 }); const result = executeToolcraftPerformanceFixtureCheckpoint( config, plan, "maximum", ); expect({ applyCount, observeCount }).toEqual({ applyCount: 1, observeCount: 1 }); expect(result.observed).toEqual({ count: 100 }); }); it("rejects quantized development round-trip mismatches", () => { const config = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count", { apply: (value) => Math.round(value), }), }, dimensions: [ createCanonicalDimension("count", { batchMax: 3, defaultValue: 0, interactiveMax: 3, }), ], passes: [createCanonicalPass("composite", ["count"], "linear")], }); const path = getCanonicalPath(config); const plan = compileToolcraftPerformanceFixturePlan(config, path); expect(plan.development).toMatchObject({ status: "available" }); expect(() => executeToolcraftPerformanceFixtureCheckpoint(config, plan, "development"), ).toThrow(/observed 2 after applying exact compiled value 2.4/); }); it("marks an inexact discrete development unavailable and still executes maximum exactly", () => { const domainValues = [20, 60, 100] as const; const adapter = defineToolcraftDiscreteFixtureAdapter({ dimensionId: "count", domain: { attestation: "All fixture options are enumerated.", kind: "runtime-state", path: "fixture.count", }, entries: domainValues.map((value) => ({ appliedValue: `option-${value}`, value, })), }); const apply = vi.spyOn(adapter, "apply"); const observe = vi.spyOn(adapter, "observe"); const config = createCanonicalConfig({ adapters: { count: adapter, }, dimensions: [createCanonicalDimension("count", { batchMax: undefined })], passes: [createCanonicalPass("composite", ["count"], "linear")], }); const plan = compileToolcraftPerformanceFixturePlan( config, getCanonicalPath(config), ); expect(plan.development).toMatchObject({ reason: expect.stringContaining( "cannot reach exact normalized development pressure 0.8", ), status: "unavailable", }); expect(() => executeToolcraftPerformanceFixtureCheckpoint(config, plan, "development"), ).toThrow(/cannot reach exact normalized development pressure 0\.8/u); expect(apply).not.toHaveBeenCalled(); expect(observe).not.toHaveBeenCalled(); expect(executeToolcraftPerformanceFixtureCheckpoint(config, plan, "maximum")).toMatchObject({ applied: { count: "option-100" }, observed: { count: 100 }, values: { count: 100 }, }); expect(apply).toHaveBeenCalledTimes(1); expect(observe).toHaveBeenCalledTimes(1); }); it("rejects losing a tiny 5e-7 expected value during observation", () => { const config = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count", { observe: () => 0 }), }, dimensions: [ createCanonicalDimension("count", { batchMax: 5e-7, defaultValue: 0, interactiveMax: 5e-7, }), ], passes: [createCanonicalPass("composite", ["count"], "linear")], }); const plan = compileToolcraftPerformanceFixturePlan( config, getCanonicalPath(config), ); expect(() => executeToolcraftPerformanceFixtureCheckpoint(config, plan, "maximum"), ).toThrow(/observed 0 after applying exact compiled value 5e-7/); }); it("accepts a large-magnitude round trip within a few ULPs", () => { const config = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count", { apply: (value) => Math.sqrt(value), observe: (value) => value ** 2, }), }, dimensions: [ createCanonicalDimension("count", { batchMax: 1_000_000_001_000_000, defaultValue: 1_000_000_000_000_000, interactiveMax: 1_000_000_001_000_000, }), ], passes: [createCanonicalPass("composite", ["count"], "linear")], }); const plan = compileToolcraftPerformanceFixturePlan( config, getCanonicalPath(config), ); expect(() => executeToolcraftPerformanceFixtureCheckpoint(config, plan, "maximum"), ).not.toThrow(); }); it("rejects a forged or stale compiled plan", () => { const config = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count") }, dimensions: [createCanonicalDimension("count")], passes: [createCanonicalPass("composite", ["count"], "linear")], }); const path = getCanonicalPath(config); const plan = compileToolcraftPerformanceFixturePlan(config, path); const forged = { ...plan, maximum: { ...plan.maximum, values: { count: 99 } }, } as ToolcraftPerformanceCompiledFixturePlan; expect(() => executeToolcraftPerformanceFixtureCheckpoint(config, forged, "maximum"), ).toThrow(/compiled fixture plan does not match the current config and path/); }); it("accepts only the measured custom development value, never a generic fallback", () => { const baseConfig = createCanonicalConfig({ adapters: { count: createCanonicalAdapter("count") }, dimensions: [ createCanonicalDimension("count", { customMappingReason: "Measured custom growth.", mapping: "custom", }), ], passes: [createCanonicalPass("custom-pass", ["count"], "linear")], }); const config = withCanonicalEvidence(baseConfig, [ { measuredResult: "Value 72 measured at development pressure.", normalizedPressure: 0.8, passId: "custom-pass", values: { count: 72 }, }, ]); const plan = compileToolcraftPerformanceFixturePlan( config, getCanonicalPath(config), ); expect(plan.development).toMatchObject({ checkpoint: { normalizedPressure: 0.8, values: { count: 72 } }, status: "available", }); expect(plan.maximum).toMatchObject({ normalizedPressure: 1, values: { count: 100 }, }); const forged = { ...plan, development: { checkpoint: { kind: "development", normalizedPressure: 0.8, values: { count: 84 }, }, status: "available", }, } as ToolcraftPerformanceCompiledFixturePlan; expect(() => executeToolcraftPerformanceFixtureCheckpoint(config, forged, "development"), ).toThrow(/compiled fixture plan does not match the current config and path/); }); });