peppa/packages/testkit/test/platform-errors.test.ts
Асель 6e54fc69a6 PEP-91: Peppa reports its own errors to Bugsink — backend to PEP-api, browser to PEP-web
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:39:46 +05:00

109 lines
5.7 KiB
TypeScript

// PEP-91: Peppa's own errors → Bugsink (envelope protocol, no SDK): the platform project is
// connected like any project; server errors go to <KEY>-api, browser errors to <KEY>-web,
// the watchdog's failing parts too; throttled, never throwing, silent when not connected.
import assert from "node:assert/strict";
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, waitFor } from "./helpers.ts";
import { MockBugsink } from "../src/mock-bugsink.ts";
import { sql } from "drizzle-orm";
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 });
// Ingest goes to BUGSINK_URL (same host), not to the DSN's public host.
process.env.BUGSINK_URL = bugsink.url;
after(async () => {
await bugsink.stop();
await teardown();
});
describe("PEP-91: Peppa reports its own errors to Bugsink", () => {
test("not connected → nothing is sent and nothing throws", async () => {
process.env.PEPPA_PLATFORM_KEY = "NOPE";
core.resetErrorReporting();
assert.equal(await core.reportPlatformError(db, new Error("boom"), { service: "web" }), false);
assert.equal(bugsink.ingested.length, 0);
});
test("parseDsn and V8 stack → frames (outermost first)", () => {
assert.deepEqual(core.parseDsn("https://abc123@bugsink.telecom.quest/4"), { key: "abc123", origin: "https://bugsink.telecom.quest", projectId: "4" });
assert.equal(core.parseDsn("not a dsn"), null);
const frames = core.stackFrames("Error: x\n at inner (/app/packages/core/src/users.ts:43:17)\n at async outer (/app/node_modules/next/x.js:1:2)\n at node:internal/process:3:4");
assert.equal(frames.length, 3);
assert.equal(frames.at(-1)!.function, "inner");
assert.equal(frames.at(-1)!.in_app, true);
assert.equal(frames[1]!.in_app, false);
});
test("connected platform project: server error → <KEY>-api with release, service and request", 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 api = r.sources.find((s) => s.kind === "api")!.bugsinkProjectId;
const web = r.sources.find((s) => s.kind === "web")!.bugsinkProjectId;
const err = new Error('no such column: "google_sub"');
const ok = await core.reportPlatformError(db, err, { service: "web", where: "GET /", request: { url: "/", method: "GET" }, release: "abc1234", environment: "live" });
assert.equal(ok, true);
const got = bugsink.ingested.at(-1)!;
assert.equal(got.projectId, api);
const ev = got.event as ReturnType<typeof core.errorEvent>;
assert.equal(ev.release, "abc1234");
assert.equal(ev.environment, "live");
assert.equal(ev.tags.service, "web");
assert.equal(ev.tags.where, "GET /");
assert.equal(ev.request?.url, "/");
assert.equal(ev.exception.values[0]!.value, 'no such column: "google_sub"');
assert.ok(ev.exception.values[0]!.stacktrace!.frames.length > 0);
// Bugsink grouped it into an issue — Peppa's own poll turns it into an incident of the project.
const issues = await client.listIssues(api);
assert.equal(issues.length, 1);
assert.equal(await runner.pollBugsink(db, client), 1);
const incidents = await core.listIncidents(db, (await core.getProject(db, p.key)).id);
assert.equal(incidents.length, 1);
// Same error again within a minute: throttled, not sent.
assert.equal(await core.reportPlatformError(db, err, { service: "web", where: "GET /", release: "abc1234" }), false);
assert.equal(bugsink.ingested.filter((i) => i.projectId === api).length, 1);
// Browser errors go to <KEY>-web.
assert.equal(await core.reportPlatformError(db, { name: "TypeError", message: "x is undefined", stack: "TypeError: x\n at f (https://peppa.test/_next/static/a.js:1:2)" }, { service: "browser", kind: "web", platform: "javascript" }), true);
assert.equal(bugsink.ingested.at(-1)!.projectId, web);
});
test("a failing watchdog part is reported (service=watchdog, where=<part>)", async () => {
core.resetErrorReporting();
const before = bugsink.ingested.length;
// A part of the tick blows up: hide the incidents table for one tick.
db.run(sql`ALTER TABLE incidents RENAME TO incidents_x`);
try {
await runner.tick(db, { intervalMs: 1000, stallMs: 60_000 });
} finally {
db.run(sql`ALTER TABLE incidents_x RENAME TO incidents`);
}
// Sent in the background: the tick doesn't wait for Bugsink.
const tags = () => bugsink.ingested.slice(before).map((i) => (i.event as ReturnType<typeof core.errorEvent>).tags);
await waitFor("watchdog error in Bugsink", async () => tags().some((t) => t.service === "watchdog" && t.where === "incidents"), 5000);
});
test("Bugsink down → false within the timeout, no exception", async () => {
core.resetErrorReporting();
process.env.BUGSINK_URL = "http://127.0.0.1:9";
const t0 = Date.now();
assert.equal(await core.reportPlatformError(db, new Error("unreachable"), { service: "bot" }), false);
assert.ok(Date.now() - t0 < 3500);
process.env.BUGSINK_URL = bugsink.url;
});
test("PEPPA_ERRORS=off switches it off", async () => {
core.resetErrorReporting();
process.env.PEPPA_ERRORS = "off";
assert.equal(await core.reportPlatformError(db, new Error("quiet"), { service: "web" }), false);
delete process.env.PEPPA_ERRORS;
});
});