import { afterAll, beforeAll, describe, expect, it } from "vitest"; import mysql from "mysql2/promise"; import type { RowDataPacket } from "mysql2"; import { buildApp } from "./buildApp.js"; import type { IngestEnv } from "./config/env.js"; import { sha256Hex } from "./lib/hash.js"; const _testUrl = process.env.ERROR_REPORTING_TEST_DATABASE_URL; describe.skipIf(!_testUrl)("two-project passive ingest (BIT-179)", () => { let _pool: mysql.Pool; let _app: Awaited>; const _tokenA = "er_tst_a_secret_one"; const _tokenB = "er_tst_b_secret_two"; let _projectIdA = ""; let _projectIdB = ""; beforeAll(async () => { _pool = mysql.createPool({ uri: _testUrl!, connectionLimit: 4, waitForConnections: true }); const _c = await _pool.getConnection(); try { await _c.query("SET FOREIGN_KEY_CHECKS = 0"); await _c.query(`TRUNCATE TABLE er_ingest_idempotency`); await _c.query(`TRUNCATE TABLE er_incident_occurrences`); await _c.query(`TRUNCATE TABLE er_incidents`); await _c.query(`TRUNCATE TABLE er_ingest_keys`); await _c.query(`TRUNCATE TABLE er_projects`); await _c.query("SET FOREIGN_KEY_CHECKS = 1"); } finally { _c.release(); } await _pool.execute(`INSERT INTO er_projects (slug, name) VALUES ('pa', 'Project A')`); await _pool.execute(`INSERT INTO er_projects (slug, name) VALUES ('pb', 'Project B')`); const [rows] = await _pool.execute( `SELECT CAST(id AS CHAR) AS id, slug FROM er_projects WHERE slug IN ('pa', 'pb') ORDER BY slug`, ); if (rows.length < 2) { throw new Error("seed projects failed"); } _projectIdA = String((rows[0] as { id: string }).id); _projectIdB = String((rows[1] as { id: string }).id); await _pool.execute( `INSERT INTO er_ingest_keys (project_id, label, token_prefix, token_hash, status) VALUES (?, 'default', 'tok_pr_a', ?, 'active')`, [_projectIdA, sha256Hex(_tokenA)], ); await _pool.execute( `INSERT INTO er_ingest_keys (project_id, label, token_prefix, token_hash, status) VALUES (?, 'default', 'tok_pr_b', ?, 'active')`, [_projectIdB, sha256Hex(_tokenB)], ); const _env: IngestEnv = { PORT: 0, DATABASE_URL: _testUrl!, INGEST_RATE_LIMIT_MAX: 10_000, INGEST_RATE_LIMIT_WINDOW_MS: 60_000, NODE_ENV: "test", }; _app = await buildApp({ env: _env, pool: _pool }); await _app.ready(); }); afterAll(async () => { await _app.close(); await _pool.end(); }); it("ingests distinct events for project A and B with separate keys", async () => { const _resA = await _app.inject({ method: "POST", url: "/v1/ingest/events", headers: { authorization: `Bearer ${_tokenA}` }, payload: { events: [ { schema_version: 1, occurred_at: "2026-03-22T10:00:00.000Z", level: "error", message: "failure from project A", fingerprint: "fp-a-1", }, ], }, }); expect(_resA.statusCode).toBe(200); const _bodyA = JSON.parse(_resA.body) as { results: Array<{ incident_id: string; fingerprint: string }>; }; expect(_bodyA.results).toHaveLength(1); expect(_bodyA.results[0]?.fingerprint).toBe("fp-a-1"); const _resB = await _app.inject({ method: "POST", url: "/v1/ingest/events", headers: { "x-api-key": _tokenB }, payload: { events: [ { schema_version: 1, occurred_at: "2026-03-22T10:00:01.000Z", level: "warn", message: "warning from project B", fingerprint: "fp-b-1", }, ], }, }); expect(_resB.statusCode).toBe(200); const _bodyB = JSON.parse(_resB.body) as { results: Array<{ incident_id: string; fingerprint: string }>; }; expect(_bodyB.results[0]?.fingerprint).toBe("fp-b-1"); expect(_bodyB.results[0]?.incident_id).not.toBe(_bodyA.results[0]?.incident_id); const [checkA] = await _pool.execute( `SELECT COUNT(*) AS c FROM er_incidents WHERE project_id = ?`, [_projectIdA], ); expect(Number((checkA[0] as { c: number }).c)).toBe(1); const [checkB] = await _pool.execute( `SELECT COUNT(*) AS c FROM er_incidents WHERE project_id = ?`, [_projectIdB], ); expect(Number((checkB[0] as { c: number }).c)).toBe(1); }); it("replays identical idempotent requests", async () => { const _payload = { events: [ { schema_version: 1, occurred_at: "2026-03-22T11:00:00.000Z", level: "info", message: "idempotent row", fingerprint: "fp-idem-1", }, ], }; const _headers = { authorization: `Bearer ${_tokenA}`, "idempotency-key": "idem-key-1", }; const _first = await _app.inject({ method: "POST", url: "/v1/ingest/events", headers: _headers, payload: _payload, }); expect(_first.statusCode).toBe(200); const _second = await _app.inject({ method: "POST", url: "/v1/ingest/events", headers: _headers, payload: _payload, }); expect(_second.statusCode).toBe(200); expect(_second.body).toBe(_first.body); const [occ] = await _pool.execute( `SELECT COUNT(*) AS c FROM er_incident_occurrences`, ); expect(Number((occ[0] as { c: number }).c)).toBe(3); }); it("returns 409 when idempotency key is reused with a different body", async () => { const _headers = { authorization: `Bearer ${_tokenA}`, "idempotency-key": "idem-key-2", }; const _r1 = await _app.inject({ method: "POST", url: "/v1/ingest/events", headers: _headers, payload: { events: [ { schema_version: 1, occurred_at: "2026-03-22T12:00:00.000Z", level: "error", message: "first", fingerprint: "fp-conflict-a", }, ], }, }); expect(_r1.statusCode).toBe(200); const _r2 = await _app.inject({ method: "POST", url: "/v1/ingest/events", headers: _headers, payload: { events: [ { schema_version: 1, occurred_at: "2026-03-22T12:00:00.000Z", level: "error", message: "second different", fingerprint: "fp-conflict-b", }, ], }, }); expect(_r2.statusCode).toBe(409); }); });