import { readFile } from "node:fs/promises"; 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, featureRouteInteractionMs: 500 }; expect( evaluateLabBudget( { metrics: { lcpMs: 1000, cls: 0, namedInteractionMs: 50, targetDeepLinkCls: 0, featureRouteInteractionMs: null } }, thresholds, ), ).toMatchObject({ passed: false }); expect( evaluateLabBudget( { context: { runner: {}, browser: {}, viewport: {}, network: {}, cpu: {}, cache: {}, build: {}, }, metrics: { lcpMs: 2501, cls: 0, namedInteractionMs: 50, targetDeepLinkCls: 0, featureRouteInteractionMs: null }, }, 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, featureRouteInteractionMs: 500 }; const context = { runner: {}, browser: {}, viewport: {}, network: {}, cpu: {}, cache: {}, build: {}, }; const metrics = { lcpMs: 1000, cls: 0, namedInteractionMs: 50, targetDeepLinkCls: 0.0046, featureRouteInteractionMs: 406 }; 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 }); }); /** * 1105/15-04. The lab timed only the platform screen, which loads no * feature code. A feature route's first entry took 406 ms (403-410) under * the lab conditions at 8a44226, two request rounds (the context route * module, then the page chunk, the second about 100 ms), and no gate * measured it. The committed budget passes that entry and fails it with * one more round of that size. */ it("judges the feature route entry against its committed budget", async () => { const { lab: thresholds } = JSON.parse( await readFile("config/performance/budgets.json", "utf8"), ) as { lab: Parameters[1] }; const context = { runner: {}, browser: {}, viewport: {}, network: {}, cpu: {}, cache: {}, build: {}, }; const metrics = { lcpMs: 1000, cls: 0, namedInteractionMs: 150, targetDeepLinkCls: 0, featureRouteInteractionMs: 406, }; const judged = (featureRouteInteractionMs: number | null) => evaluateLabBudget({ context, metrics: { ...metrics, featureRouteInteractionMs } }, thresholds); expect(thresholds.featureRouteInteractionMs).toEqual(expect.any(Number)); expect(judged(406).passed).toBe(true); expect(judged(406 + 110)).toMatchObject({ passed: false, results: { namedInteraction: true, featureRouteInteraction: false }, }); // No installed feature owns a public navigation route: nothing to judge. expect(judged(null).passed).toBe(true); }); 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 }); }); });