import { NextRequest } from "next/server"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const loggerInfoMock = vi.fn(); const getWebVitalsStorageModeMock = vi.fn(); const appendLocalWebVitalMock = vi.fn(); vi.mock("../../lib/logger", () => ({ logger: { info: (...args: unknown[]) => loggerInfoMock(...args), warn: vi.fn(), error: vi.fn(), debug: vi.fn(), }, })); vi.mock("../../lib/server/webVitals/mode", () => ({ getWebVitalsStorageMode: () => getWebVitalsStorageModeMock(), })); vi.mock("../../lib/server/webVitals/localFileStore", () => ({ appendLocalWebVital: (...args: unknown[]) => appendLocalWebVitalMock(...args), readLocalAggregatedMetrics: () => ({}), })); import { POST } from "../../app/api/web-vitals/route"; function post(url: string) { return new NextRequest("https://x.test/api/web-vitals", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ metric: "lcp", data: { value: 1200, rating: "good" }, url, userAgent: "test", timestamp: "2026-11-10T12:00:00.000Z", }), }); } beforeEach(() => { loggerInfoMock.mockReset(); getWebVitalsStorageModeMock.mockReset(); appendLocalWebVitalMock.mockReset(); getWebVitalsStorageModeMock.mockReturnValue("external"); }); afterEach(() => { vi.unstubAllEnvs(); }); describe("POST /api/web-vitals", () => { it("logs origin and path without the query string", async () => { const res = await POST( post( "https://communityrule.info/api/invites/rule-stakeholder/verify?token=secret", ), undefined, ); expect(res.status).toBe(200); const line = String(loggerInfoMock.mock.calls[0]?.[0]); expect(line).toContain( "https://communityrule.info/api/invites/rule-stakeholder/verify", ); expect(line).not.toContain("token="); expect(line).not.toContain("secret"); }); it("stores a path-only URL in local mode", async () => { getWebVitalsStorageModeMock.mockReturnValue("local"); const res = await POST( post("https://example.test/create/name?facet.size=small"), undefined, ); expect(res.status).toBe(200); expect(appendLocalWebVitalMock).toHaveBeenCalledWith( expect.objectContaining({ url: "https://example.test/create/name", }), ); }); });