73 lines
3.6 KiB
TypeScript
73 lines
3.6 KiB
TypeScript
// PEP-217: Web Vitals of real users — what is stored (route without ids, no user), p75 per route
|
|
// for the last day, one Bugsink warning per route and metric per day.
|
|
import assert from "node:assert/strict";
|
|
import { join } from "node:path";
|
|
import { after, describe, test } from "node:test";
|
|
// helpers must load first: it sets the isolated test env that runner modules read on load.
|
|
import { core, db, project, runner, teardown } from "./helpers.ts";
|
|
import { MockBugsink } from "../src/mock-bugsink.ts";
|
|
|
|
process.env.PEPPA_OBSERVE_DB = join(process.env.PEPPA_HOME!, "vitals-observe.db");
|
|
const { createBugsinkClient } = await import("@peppa/bugsink");
|
|
const bugsink = await new MockBugsink().start();
|
|
const client = createBugsinkClient({ url: bugsink.url, token: bugsink.token, teamId: bugsink.teamId, publicUrl: bugsink.url });
|
|
process.env.BUGSINK_URL = bugsink.url;
|
|
|
|
after(async () => {
|
|
core.closeObserveDb();
|
|
await bugsink.stop();
|
|
await teardown();
|
|
});
|
|
|
|
const sample = (route: string, metric: string, value: number, device = "wide", at = new Date()) => core.recordVital({ route, metric, value, device, mode: "product" }, at);
|
|
|
|
describe("PEP-217: Web Vitals", () => {
|
|
test("Scenario: маршрут без идентификаторов", () => {
|
|
assert.equal(core.normalizeRoute("/t/PEP-217?tab=design"), "/t/:key");
|
|
assert.equal(core.normalizeRoute("/p/PEP/accept"), "/p/:key/accept");
|
|
assert.equal(core.normalizeRoute("/runners/42"), "/runners/:id");
|
|
assert.equal(core.normalizeRoute("/"), "/");
|
|
assert.equal(core.normalizeRoute("https://evil"), null);
|
|
});
|
|
|
|
test("Scenario: плохие значения не записываются", () => {
|
|
assert.equal(sample("/", "FOO", 10), false);
|
|
assert.equal(sample("/", "LCP", -1), false);
|
|
assert.equal(sample("/", "LCP", Number.NaN), false);
|
|
assert.equal(sample("/", "CLS", 5000), false);
|
|
assert.equal(core.vitalsSummary().length, 0);
|
|
});
|
|
|
|
test("Scenario: p75 по маршрутам за сутки, старое не считается", () => {
|
|
for (const v of [1000, 1200, 1400, 3000]) sample("/", "LCP", v);
|
|
sample("/", "INP", 120);
|
|
sample("/", "LCP", 99_000, "wide", new Date(Date.now() - 3 * 86_400_000));
|
|
const r = core.vitalsSummary();
|
|
const home = r.find((x) => x.route === "/")!;
|
|
assert.equal(home.p75.LCP, 1400);
|
|
assert.equal(home.n.LCP, 4);
|
|
assert.equal(home.p75.INP, 120);
|
|
assert.equal(core.vitalsSummary({ device: "narrow" }).length, 0);
|
|
assert.equal(core.p75([]), null);
|
|
});
|
|
|
|
test("Scenario: плохой p75 — одно предупреждение на маршрут в сутки", async () => {
|
|
const p = await project({ turns: [] });
|
|
process.env.PEPPA_PLATFORM_KEY = p.key;
|
|
core.resetErrorReporting();
|
|
const r = await runner.connectErrorTracking(db, await core.getProject(db, p.key), client, "http://peppa.test");
|
|
const web = r.sources.find((s) => s.kind === "web")!.bugsinkProjectId;
|
|
for (const v of [4500, 5000, 6000, 5200, 4800]) sample("/t/PEP-5", "LCP", v);
|
|
for (const v of [100, 100, 100, 100, 100]) sample("/p/PEP", "LCP", v);
|
|
for (let i = 0; i < 3; i++) sample("/chats", "INP", 900); // too few samples: no verdict
|
|
const before = bugsink.ingested.length;
|
|
const sent = await core.vitalsAlertTick(db);
|
|
assert.deepEqual(sent, ["/t/:key:LCP"]);
|
|
const got = bugsink.ingested.slice(before);
|
|
assert.equal(got.length, 1);
|
|
assert.equal(got[0]!.projectId, web);
|
|
assert.equal((got[0]!.event as ReturnType<typeof core.errorEvent>).level, "warning");
|
|
assert.deepEqual(await core.vitalsAlertTick(db), []);
|
|
});
|
|
});
|