Web vitals: prefer external RUM

This commit is contained in:
adilallo
2026-04-21 07:08:31 -06:00
parent aaa3e4d654
commit 2d58887a15
17 changed files with 713 additions and 372 deletions
+36
View File
@@ -0,0 +1,36 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { getWebVitalsStorageMode } from "../../lib/server/webVitals/mode";
describe("getWebVitalsStorageMode", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
it("returns external when WEB_VITALS_STORAGE=external", () => {
vi.stubEnv("WEB_VITALS_STORAGE", "external");
vi.stubEnv("NODE_ENV", "development");
expect(getWebVitalsStorageMode()).toBe("external");
});
it("returns local when WEB_VITALS_STORAGE=local", () => {
vi.stubEnv("WEB_VITALS_STORAGE", "local");
vi.stubEnv("NODE_ENV", "production");
expect(getWebVitalsStorageMode()).toBe("local");
});
it("defaults to external in production when unset", () => {
vi.stubEnv("NODE_ENV", "production");
expect(getWebVitalsStorageMode()).toBe("external");
});
it("defaults to local in development when unset", () => {
vi.stubEnv("NODE_ENV", "development");
expect(getWebVitalsStorageMode()).toBe("local");
});
it("maps database to external until implemented", () => {
vi.stubEnv("WEB_VITALS_STORAGE", "database");
vi.stubEnv("NODE_ENV", "development");
expect(getWebVitalsStorageMode()).toBe("external");
});
});
+45
View File
@@ -0,0 +1,45 @@
import { describe, expect, it } from "vitest";
import { webVitalIngestSchema } from "../../lib/server/validation/webVitalsSchema";
describe("webVitalIngestSchema", () => {
it("accepts a valid payload", () => {
const parsed = webVitalIngestSchema.safeParse({
metric: "lcp",
data: { value: 1200, rating: "good" },
url: "https://example.com/path",
userAgent: "jest",
timestamp: "2026-01-01T00:00:00.000Z",
});
expect(parsed.success).toBe(true);
});
it("accepts numeric timestamp", () => {
const parsed = webVitalIngestSchema.safeParse({
metric: "cls",
data: { value: 0.05, rating: "good" },
url: "https://example.com/",
timestamp: 1_700_000_000_000,
});
expect(parsed.success).toBe(true);
});
it("rejects empty metric", () => {
const parsed = webVitalIngestSchema.safeParse({
metric: "",
data: { value: 1, rating: "good" },
url: "https://example.com/",
timestamp: "2026-01-01T00:00:00.000Z",
});
expect(parsed.success).toBe(false);
});
it("rejects empty url", () => {
const parsed = webVitalIngestSchema.safeParse({
metric: "lcp",
data: { value: 1, rating: "good" },
url: "",
timestamp: "2026-01-01T00:00:00.000Z",
});
expect(parsed.success).toBe(false);
});
});