import { describe, expect, it } from "vitest"; import { validateToolcraftPerformanceCoverage } from "./performance-coverage-validator"; import { deriveToolcraftPerformancePaths } from "./performance-paths"; import { createConfig, createInvalidation, createScenario, groupedSchema, } from "./performance-paths.test-fixtures"; describe("Toolcraft performance path coverage", () => { it("requires one real browser scenario to cover each derived path", () => { const path = deriveToolcraftPerformancePaths(groupedSchema, createConfig())[0]!; const validScenario = createScenario(path); expect( validateToolcraftPerformanceCoverage(groupedSchema, createConfig()), ).toContain( `Performance path ${path.id} must have an automated browser performance scenario.`, ); expect( validateToolcraftPerformanceCoverage( groupedSchema, createConfig({ scenarios: [validScenario] }), ), ).toEqual([]); }); it("validates every path scenario instead of accepting one valid sibling", () => { const path = deriveToolcraftPerformancePaths(groupedSchema, createConfig())[0]!; const errors = validateToolcraftPerformanceCoverage( groupedSchema, createConfig({ scenarios: [ createScenario(path), createScenario(path, { automatedTestName: "", browserTestName: "browser perf: malformed sibling", fixture: "", id: "malformed-sibling", }), ], }), ); expect(errors).toContain("malformed-sibling must name a representative fixture."); expect(errors).toContain( "malformed-sibling must point to an automated performance test.", ); }); it("rejects duplicate coversTargets even when the unique set matches", () => { const path = deriveToolcraftPerformancePaths(groupedSchema, createConfig())[0]!; const errors = validateToolcraftPerformanceCoverage( groupedSchema, createConfig({ scenarios: [ createScenario(path, { coversTargets: [...path.targets, path.targets[0]!], }), ], }), ); expect(errors).toContain( `Performance scenario shared-uniform-drag coversTargets must not contain duplicate targets.`, ); }); it("rejects per-control scenarios that duplicate one derived path", () => { const path = deriveToolcraftPerformancePaths(groupedSchema, createConfig())[0]!; const sharedName = "browser perf: shared uniform drag stays responsive"; expect( validateToolcraftPerformanceCoverage( groupedSchema, createConfig({ scenarios: [ createScenario(path, { browserTestName: sharedName }), createScenario(path, { browserTestName: sharedName, id: "shared-uniform-drag-secondary-assertion", }), ], }), ), ).toContain( `Performance path ${path.id} must have exactly one path scenario; found 2. Equivalent controls share the derived path instead of declaring per-control scenarios.`, ); const distinctConfig = createConfig({ interactions: [ createInvalidation({ targets: ["effect.opacity"] }), createInvalidation({ interaction: "control-change", targets: ["effect.strength"], }), ], }); const [changePath, dragPath] = deriveToolcraftPerformancePaths( groupedSchema, distinctConfig, ); const errors = validateToolcraftPerformanceCoverage( groupedSchema, createConfig({ interactions: distinctConfig.rendererPipeline!.interactionInvalidation, scenarios: [ createScenario(changePath!, { browserTestName: sharedName, id: "change-path", interaction: "control-change", }), createScenario(dragPath!, { browserTestName: sharedName, id: "drag-path", }), ], }), ); expect(errors).toContain( `drag-path browserTestName "${sharedName}" is already used by change-path for a different performance path.`, ); }); it("does not let disabled or blank browser checks own a shared name", () => { const distinctConfig = createConfig({ interactions: [ createInvalidation({ targets: ["effect.opacity"] }), createInvalidation({ interaction: "control-change", targets: ["effect.strength"], }), ], }); const [changePath, dragPath] = deriveToolcraftPerformancePaths( groupedSchema, distinctConfig, ); const errors = validateToolcraftPerformanceCoverage( groupedSchema, createConfig({ interactions: distinctConfig.rendererPipeline!.interactionInvalidation, scenarios: [ createScenario(changePath!, { browserTestName: "browser perf: valid change path", id: "valid-change-path", interaction: "control-change", }), createScenario(dragPath!, { browserTestName: "browser perf: valid drag path", id: "valid-drag-path", }), createScenario(changePath!, { browser: false, browserTestName: "", id: "disabled-change-path", interaction: "control-change", }), createScenario(dragPath!, { browser: false, browserTestName: "", id: "disabled-drag-path", }), ], }), ); expect(errors).toContain( "disabled-change-path must point to a browser performance test.", ); expect(errors).toContain( "disabled-drag-path must point to a browser performance test.", ); expect(errors.some((error) => error.includes("already used"))).toBe(false); }); it("rejects path claims that omit equivalent targets", () => { const path = deriveToolcraftPerformancePaths(groupedSchema, createConfig())[0]!; const errors = validateToolcraftPerformanceCoverage( groupedSchema, createConfig({ scenarios: [ createScenario(path, { coversTargets: ["effect.opacity"], id: "opacity-drag", target: "effect.opacity", }), ], }), ); expect(errors).toContain( `Performance scenario opacity-drag coversTargets must equal path ${path.id} targets ["effect.opacity","effect.strength"].`, ); }); it("rejects scenarios that do not own a canonical path", () => { const path = deriveToolcraftPerformancePaths(groupedSchema, createConfig())[0]!; const errors = validateToolcraftPerformanceCoverage( groupedSchema, createConfig({ scenarios: [ createScenario(path), createScenario(path, { id: "unknown-path", pathId: "performance-path:unknown", }), ], }), ); expect(errors).toContain( 'Performance scenario unknown-path references unknown pathId "performance-path:unknown".', ); }); });