Track HiveGrid Cloud planning reference

This commit is contained in:
david committed 2026-06-26 20:48:01 +02:00
1 parent b3d5ea6764
commit e341c5938a
5784 files changed
+1104929 -3

No files matched your search

@@ -0,0 +1,62 @@
import { describe, expect, it, vi } from "vitest";
import { reportWebVitals } from "./web-vitals";
describe("reportWebVitals", () => {
it("registers handlers for CLS/LCP/INP/TTFB/FCP", async () => {
const reporter = vi.fn();
const onCLS = vi.fn();
const onLCP = vi.fn();
const onINP = vi.fn();
const onTTFB = vi.fn();
const onFCP = vi.fn();
await reportWebVitals({
reporter,
loader: async () => ({ onCLS, onLCP, onINP, onTTFB, onFCP }) as unknown as typeof import("web-vitals"),
});
expect(onCLS).toHaveBeenCalledWith(reporter);
expect(onLCP).toHaveBeenCalledWith(reporter);
expect(onINP).toHaveBeenCalledWith(reporter);
expect(onTTFB).toHaveBeenCalledWith(reporter);
expect(onFCP).toHaveBeenCalledWith(reporter);
});
it("silently no-ops when the library throws", async () => {
await expect(
reportWebVitals({
reporter: vi.fn(),
loader: async () => {
throw new Error("nope");
},
})
).resolves.toBeUndefined();
});
it("delivers reported metric values via the reporter callback", async () => {
const reporter = vi.fn();
type CB = (m: { name: string; value: number; rating: string; delta: number; id: string; navigationType: string }) => void;
let captured: CB | null = null;
await reportWebVitals({
reporter,
loader: async () =>
({
onCLS: (cb: CB) => {
captured = cb;
},
onLCP: () => {},
onINP: () => {},
onTTFB: () => {},
}) as unknown as typeof import("web-vitals"),
});
captured?.({
name: "CLS",
value: 0.05,
rating: "good",
delta: 0.05,
id: "v1-cls",
navigationType: "navigate",
});
expect(reporter).toHaveBeenCalledTimes(1);
expect(reporter.mock.calls[0]?.[0]).toMatchObject({ name: "CLS", value: 0.05 });
});
});
@@ -0,0 +1,79 @@
/**
* Real-User-Monitoring (RUM) bridge for Core Web Vitals → Sentry.
*
* The browser's web-vitals library reports CLS / FID / LCP / INP / TTFB
* once per page load (or per change for CLS). We forward those values to
* Sentry via `captureMessage` with a structured tag-set so the metrics
* surface as searchable events. The dashboard query is documented in
* docs/architecture/lighthouse.md.
*
* Spec: docs/improvement-plan/10-compliance-polish/lighthouse.md (Step (f)).
*/
import type { Metric } from "web-vitals";
/**
* Reporter signature is exposed for tests + the runtime injection. The
* default reporter forwards to Sentry; tests pass a vi.fn() spy.
*/
export type WebVitalsReporter = (metric: Metric) => void;
/**
* Sends a web-vitals Metric to the Sentry sink. We keep the integration
* lazy and tolerant — when Sentry isn't initialised the call is a no-op.
*/
export const sentryReporter: WebVitalsReporter = (metric) => {
void (async () => {
try {
const Sentry = await import("@sentry/react-router");
Sentry.captureMessage(`web-vitals.${metric.name}`, {
level: "info",
tags: {
"web.vital.name": metric.name,
"web.vital.rating": metric.rating,
},
extra: {
value: metric.value,
delta: metric.delta,
id: metric.id,
navigationType: metric.navigationType,
},
});
} catch {
// Sentry not loaded or import failed — silent drop.
}
})();
};
export interface ReportWebVitalsOptions {
reporter?: WebVitalsReporter;
/**
* Allows injecting the web-vitals module (e.g. a mock) in tests.
* Production callers should let this default to dynamic import.
*/
loader?: () => Promise<typeof import("web-vitals")>;
}
/**
* Wires the web-vitals library callbacks. Safe to call on SSR (it bails
* out when `window` is absent). Returns a promise that resolves once the
* underlying library has been registered, useful for tests.
*/
export async function reportWebVitals(options: ReportWebVitalsOptions = {}): Promise<void> {
if (typeof window === "undefined") return;
const reporter = options.reporter ?? sentryReporter;
const loader = options.loader ?? (() => import("web-vitals"));
try {
const mod = await loader();
mod.onCLS(reporter);
mod.onLCP(reporter);
mod.onINP(reporter);
mod.onTTFB(reporter);
if (typeof mod.onFCP === "function") {
mod.onFCP(reporter);
}
} catch {
// Library missing or load error — never break the page.
}
}