forked from emdevelopa/Stellar_Payment_API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealth.test.js
More file actions
45 lines (35 loc) · 1.28 KB
/
Copy pathhealth.test.js
File metadata and controls
45 lines (35 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { beforeEach, describe, expect, it, vi } from "vitest";
const query = vi.fn();
const isHorizonReachable = vi.fn();
vi.mock("./db.js", () => ({
pool: { query },
}));
vi.mock("./stellar.js", () => ({
isHorizonReachable,
}));
describe("probeHealth", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('runs a "SELECT 1" database probe and reports both services healthy', async () => {
query.mockResolvedValue({ rows: [{ "?column?": 1 }] });
isHorizonReachable.mockResolvedValue(true);
const { probeHealth } = await import("./health.js");
const result = await probeHealth();
expect(query).toHaveBeenCalledWith("SELECT 1");
expect(isHorizonReachable).toHaveBeenCalledTimes(1);
expect(result).toEqual({
database: { ok: true, error: null },
horizon: { ok: true, error: null },
});
});
it("reports the database as unavailable when the SELECT 1 query fails", async () => {
const dbError = new Error("db down");
query.mockRejectedValue(dbError);
isHorizonReachable.mockResolvedValue(true);
const { probeHealth } = await import("./health.js");
const result = await probeHealth();
expect(result.database).toEqual({ ok: false, error: dbError });
expect(result.horizon).toEqual({ ok: true, error: null });
});
});