feat: unit tests for security/utility functions (isPrivateIP, isTransientError, markdown, escapeHtml)
This commit is contained in:
parent
0a002f94ef
commit
50a163b12d
10 changed files with 224 additions and 62 deletions
36
src/__tests__/db-utils.test.ts
Normal file
36
src/__tests__/db-utils.test.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { describe, it, expect } from "vitest";
|
||||
import { isTransientError } from "../utils/errors.js";
|
||||
|
||||
describe("isTransientError", () => {
|
||||
describe("transient error codes", () => {
|
||||
for (const code of ["ECONNRESET", "ECONNREFUSED", "EPIPE", "ETIMEDOUT", "CONNECTION_LOST", "57P01", "57P02", "57P03", "08006", "08003", "08001"]) {
|
||||
it(`detects code ${code}`, () => {
|
||||
expect(isTransientError({ code })).toBe(true);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe("message-based matches", () => {
|
||||
it("detects 'no available server'", () => expect(isTransientError(new Error("no available server found"))).toBe(true));
|
||||
it("detects 'connection terminated'", () => expect(isTransientError(new Error("connection terminated unexpectedly"))).toBe(true));
|
||||
it("detects 'connection refused'", () => expect(isTransientError(new Error("connection refused by host"))).toBe(true));
|
||||
it("detects 'server closed the connection'", () => expect(isTransientError(new Error("server closed the connection"))).toBe(true));
|
||||
it("detects 'timeout expired'", () => expect(isTransientError(new Error("timeout expired waiting"))).toBe(true));
|
||||
});
|
||||
|
||||
describe("non-transient errors", () => {
|
||||
it("rejects generic error", () => expect(isTransientError(new Error("something broke"))).toBe(false));
|
||||
it("rejects SQL syntax error", () => expect(isTransientError({ code: "42601", message: "syntax error" })).toBe(false));
|
||||
});
|
||||
|
||||
describe("null/undefined input", () => {
|
||||
it("returns false for null", () => expect(isTransientError(null)).toBe(false));
|
||||
it("returns false for undefined", () => expect(isTransientError(undefined)).toBe(false));
|
||||
});
|
||||
|
||||
describe("partial error objects", () => {
|
||||
it("handles error with code but no message", () => expect(isTransientError({ code: "ECONNRESET" })).toBe(true));
|
||||
it("handles error with message but no code", () => expect(isTransientError({ message: "connection terminated" })).toBe(true));
|
||||
it("rejects error with unrelated code and no message", () => expect(isTransientError({ code: "UNKNOWN" })).toBe(false));
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue