mirror of
https://github.com/bitbaum/heidi.git
synced 2026-12-04 19:58:24 +01:00
Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T Co-authored-by: Claude <noreply@anthropic.com>
140 lines
7.1 KiB
TypeScript
140 lines
7.1 KiB
TypeScript
import { test, describe, before, after } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { readdirSync, readFileSync } from "node:fs";
|
|
import { PGlite } from "@electric-sql/pglite";
|
|
import type { Run } from "../../db/run.ts";
|
|
import { KEEP_DAYS, MAX_KINDS, MAX_WRITES, kindOf, newBudget, recentErrors, report, scrub, spend } from "./sink.ts";
|
|
|
|
const DIR = new URL("../../../drizzle/", import.meta.url);
|
|
const FILES = readdirSync(DIR).filter((f) => /^\d{4}_.*\.sql$/.test(f)).sort();
|
|
const MIGRATION = "0018_error_events.sql";
|
|
const T0 = Date.UTC(2026, 10, 25, 12);
|
|
|
|
describe("what is kept of an error", () => {
|
|
test("nothing that could be somebody's survives the scrub", () => {
|
|
const cases: [string, string[]][] = [
|
|
['duplicate key value violates unique constraint "users_email_key" Key (email)=(anna.muster@example.ch) already exists.', ["anna", "example.ch", "users_email_key"]],
|
|
["fetch failed for https://api.example.com/v1/x?token=abc123&user=anna", ["example.com", "abc123", "anna"]],
|
|
["no session for 3f2b8c1e-7a4d-4c2b-9f1e-0a1b2c3d4e5f", ["3f2b8c1e"]],
|
|
["invalid input syntax: 'Grüezi, ich bin Anna'", ["Anna", "Grüezi"]],
|
|
["bad token " + "tok" + "_" + "QWERTZUIOPASDFGHJKLYXCVB", ["QWERTZUI"]],
|
|
["called from +41 79 123 45 67 in row 987654", ["123", "987654"]],
|
|
["write to anna@example.ch failed", ["anna@"]],
|
|
];
|
|
for (const [message, secrets] of cases) {
|
|
const cleaned = scrub(message);
|
|
for (const secret of secrets) assert.ok(!cleaned.includes(secret), `«${secret}» survived in «${cleaned}»`);
|
|
assert.ok(cleaned.length > 0);
|
|
}
|
|
assert.ok(scrub("x ".repeat(500)).length <= 200);
|
|
});
|
|
|
|
test("the same error is the same kind whoever it happened to, and a different route is a different kind", () => {
|
|
const a = kindOf("/api/chat", new TypeError("no row for anna@example.ch"));
|
|
const b = kindOf("/api/chat", new TypeError("no row for bert@example.org"));
|
|
assert.equal(a.hash, b.hash);
|
|
assert.equal(a.name, "TypeError");
|
|
assert.notEqual(kindOf("/api/speak", new TypeError("no row for anna@example.ch")).hash, a.hash);
|
|
assert.notEqual(kindOf("/api/chat", new RangeError("no row for anna@example.ch")).hash, a.hash);
|
|
});
|
|
|
|
test("a route is kept as a pattern: a query string never reaches the table", () => {
|
|
assert.equal(kindOf("/api/chat?email=anna@example.ch#x", new Error("x")).route, "/api/chat");
|
|
assert.equal(kindOf("", "a string was thrown").route, "unknown");
|
|
assert.equal(kindOf("/x", { not: "an error" }).name, "object");
|
|
});
|
|
|
|
test("a process writes a bounded number a minute and counts what it dropped", () => {
|
|
const budget = newBudget();
|
|
let written = 0;
|
|
for (let i = 0; i < MAX_WRITES + 25; i++) if (spend(budget, T0)) written += 1;
|
|
assert.equal(written, MAX_WRITES);
|
|
assert.equal(budget.dropped, 25);
|
|
assert.equal(spend(budget, T0 + 60_000), true, "the next minute starts again");
|
|
});
|
|
});
|
|
|
|
describe("the sink in Postgres", () => {
|
|
let db: PGlite;
|
|
let statements = 0;
|
|
const run: Run = async (text, params) => {
|
|
statements += 1;
|
|
return (await db.query<Record<string, unknown>>(text, [...params])).rows;
|
|
};
|
|
|
|
before(async () => {
|
|
db = new PGlite();
|
|
for (const file of FILES) {
|
|
for (const statement of readFileSync(new URL(file, DIR), "utf8").split("--> statement-breakpoint")) {
|
|
if (statement.trim()) await db.exec(statement);
|
|
}
|
|
}
|
|
});
|
|
after(async () => db.close());
|
|
|
|
test("the migration only adds: one new table and its index", () => {
|
|
const sql = readFileSync(new URL(MIGRATION, DIR), "utf8");
|
|
assert.match(sql, /CREATE TABLE "error_events"/);
|
|
assert.doesNotMatch(sql, /\b(DROP|ALTER|DELETE|UPDATE|TRUNCATE)\b/i);
|
|
});
|
|
|
|
test("the table has no column that could hold a person, a request or a body", async () => {
|
|
const { rows } = await db.query<{ column_name: string }>(`select column_name from information_schema.columns where table_name = 'error_events' order by 1`);
|
|
assert.deepEqual(rows.map((r) => r.column_name), ["count", "first_seen", "hash", "last_seen", "message", "name", "route"]);
|
|
});
|
|
|
|
test("the same error twice is one row counted twice, in one statement each", async () => {
|
|
const budget = { ...newBudget(), swept: T0 };
|
|
statements = 0;
|
|
await report(run, "/api/chat", new Error("upstream said 503 for anna@example.ch"), budget, T0);
|
|
await report(run, "/api/chat", new Error("upstream said 503 for bert@example.org"), budget, T0 + 1);
|
|
assert.equal(statements, 2);
|
|
const seen = await recentErrors(run);
|
|
assert.equal(seen.length, 1);
|
|
assert.equal(seen[0]!.count, 2);
|
|
assert.equal(seen[0]!.route, "/api/chat");
|
|
assert.ok(!JSON.stringify(seen).includes("example"));
|
|
});
|
|
|
|
test("the table cannot grow past its cap, and a known kind is still counted when it is full", async () => {
|
|
const budget = { ...newBudget(), swept: T0 };
|
|
await db.exec(`insert into error_events (hash, route, name, message) select 'fill-' || g, '/fill', 'Error', 'm' from generate_series(1, ${MAX_KINDS}) g`);
|
|
budget.writes = 0;
|
|
await report(run, "/api/new", new Error("a kind nobody has seen"), budget, T0 + 120_000);
|
|
const total = await db.query<{ n: number }>(`select count(*)::int as n from error_events`);
|
|
assert.ok(total.rows[0]!.n <= MAX_KINDS + 1, "no new kind is added past the cap");
|
|
assert.equal((await db.query(`select 1 from error_events where route = '/api/new'`)).rows.length, 0);
|
|
await report(run, "/api/chat", new Error("upstream said 503 for carl@example.org"), budget, T0 + 120_001);
|
|
const known = await db.query<{ count: number }>(`select count from error_events where route = '/api/chat'`);
|
|
assert.equal(known.rows[0]!.count, 3);
|
|
await db.exec(`delete from error_events where route = '/fill'`);
|
|
});
|
|
|
|
test("a storm costs a counter, not a query per failure", async () => {
|
|
const budget = { ...newBudget(), swept: T0 + 600_000 };
|
|
statements = 0;
|
|
for (let i = 0; i < MAX_WRITES * 4; i++) await report(run, "/api/storm", new Error("down"), budget, T0 + 600_000);
|
|
assert.equal(statements, MAX_WRITES);
|
|
assert.equal(budget.dropped, MAX_WRITES * 3);
|
|
});
|
|
|
|
test("kinds not seen for a month are removed, and the sweep runs at most once an hour", async () => {
|
|
await db.exec(`insert into error_events (hash, route, name, message, last_seen) values ('stale', '/old', 'Error', 'm', now() - interval '${KEEP_DAYS + 1} days')`);
|
|
const budget = newBudget();
|
|
const now = Date.now();
|
|
statements = 0;
|
|
await report(run, "/api/sweep", new Error("first"), budget, now);
|
|
assert.equal(statements, 2, "the write, and the sweep that was due");
|
|
assert.equal((await db.query(`select 1 from error_events where hash = 'stale'`)).rows.length, 0);
|
|
await report(run, "/api/sweep", new Error("first"), budget, now + 1_000);
|
|
assert.equal(statements, 3, "no second sweep within the hour");
|
|
});
|
|
|
|
test("a database that cannot answer is swallowed: the sink is never the error", async () => {
|
|
const broken: Run = async () => {
|
|
throw new Error("connection refused");
|
|
};
|
|
await assert.doesNotReject(report(broken, "/api/chat", new Error("x"), newBudget(), T0));
|
|
});
|
|
});
|