import { test } from "node:test"; import assert from "node:assert/strict"; import { checkServiceError, checkDifficultyPresetError, ServiceError, } from "../lib/service-error"; test("expected preset validation and duplicate-name errors return 400/409 without database log noise", (t) => { const log = t.mock.method(console, "error", () => {}); for (const [code, status] of [ ["23514", 400], ["23505", 409], ] as const) assert.throws( () => checkDifficultyPresetError({ code, message: "private database constraint details", }), (error) => { assert.ok(error instanceof ServiceError); assert.equal(error.status, status); assert.doesNotMatch(error.message, /private database|constraint/); return true; }, ); assert.equal(log.mock.callCount(), 0); assert.doesNotThrow(() => checkDifficultyPresetError(null)); }); test("backend failures use 503 and keep database internals out of browser errors", (t) => { t.mock.method(console, "error", () => {}); assert.throws( () => checkServiceError({ code: "42P01", message: 'relation "private_table" does not exist', }), (error) => { assert.ok(error instanceof ServiceError); assert.equal(error.status, 503); assert.doesNotMatch(error.message, /private_table|42P01/); return true; }, ); }); test("missing records and expected RPC validation keep actionable statuses and messages", () => { assert.throws( () => checkServiceError({ code: "PGRST116", message: "no rows" }), (error) => error instanceof ServiceError && error.status === 404, ); assert.throws( () => checkServiceError({ code: "P0001", message: "Guess outside map" }), /Guess outside map/, ); assert.doesNotThrow(() => checkServiceError(null)); });