import { describe, expect, it } from "vitest"; import { evaluateBundleBudget, evaluateFieldBudget, evaluateLabBudget, percentile75, } from "../../scripts/lib/performance-budgets.ts"; describe("performance budgets", () => { it("rejects initial and lazy JavaScript above their named limits", () => { const thresholds = { initialJsGzipBytes: 200, initialJsFiles: 2, lazyChunkGzipBytes: 120, }; expect( evaluateBundleBudget( { initialJsGzipBytes: 201, initialJsFiles: 1, lazyChunks: [] }, thresholds, ).passed, ).toBe(false); expect( evaluateBundleBudget( { initialJsGzipBytes: 100, initialJsFiles: 1, lazyChunks: [{ path: "lazy.js", gzipBytes: 121 }], }, thresholds, ).passed, ).toBe(false); expect( evaluateBundleBudget( { initialJsGzipBytes: 100, initialJsFiles: 3, lazyChunks: [] }, thresholds, ), ).toMatchObject({ initialPassed: true, initialFilesPassed: false, passed: false }); }); it("fails lab evidence when context is absent or a metric is over budget", () => { const thresholds = { lcpMs: 2500, cls: 0.1, namedInteractionMs: 200 }; expect( evaluateLabBudget( { metrics: { lcpMs: 1000, cls: 0, namedInteractionMs: 50, targetDeepLinkCls: 0 } }, thresholds, ), ).toMatchObject({ passed: false }); expect( evaluateLabBudget( { context: { runner: {}, browser: {}, viewport: {}, network: {}, cpu: {}, cache: {}, build: {}, }, metrics: { lcpMs: 2501, cls: 0, namedInteractionMs: 50, targetDeepLinkCls: 0 }, }, thresholds, ).passed, ).toBe(false); }); /** * 1105/15-01. The lab entered its target route only by a click, and a * shift within 500 ms of a click is left out of CLS. The target screen * pushed its content 21 px after its title and the lab reported 0. The * direct visit's CLS is judged against the same threshold. */ it("fails lab evidence when a direct visit to the target route shifts over the CLS budget", () => { const thresholds = { lcpMs: 2500, cls: 0.1, namedInteractionMs: 200 }; const context = { runner: {}, browser: {}, viewport: {}, network: {}, cpu: {}, cache: {}, build: {}, }; const metrics = { lcpMs: 1000, cls: 0, namedInteractionMs: 50, targetDeepLinkCls: 0.0046 }; expect(evaluateLabBudget({ context, metrics }, thresholds).passed).toBe(true); const shifted = evaluateLabBudget( { context, metrics: { ...metrics, targetDeepLinkCls: 0.11 } }, thresholds, ); expect(shifted.passed).toBe(false); expect(shifted.results).toMatchObject({ cls: true, targetDeepLinkCls: false }); }); it("uses the nearest-rank p75 and fails closed while sample minimum is deferred", () => { expect(percentile75([4, 1, 3, 2])).toBe(3); expect( evaluateFieldBudget( { metrics: { p75LcpMs: 1000, p75Cls: 0.01, p75InpMs: 50 }, eligibleSamples: 100, }, { p75LcpMs: 2500, p75Cls: 0.1, p75InpMs: 200, minimumEligibleSamples: null, }, ), ).toEqual({ status: "FAIL_UNVERIFIED", passed: false }); }); });