From 2eab4b92cc5cb241e1bd56265d5b87a265cb5962 Mon Sep 17 00:00:00 2001 From: ramvignesh-b Date: Mon, 11 May 2026 23:59:01 +0530 Subject: [PATCH] test: implement redis unavailibility health check --- src/index.ts | 2 +- tests/integration/api.test.ts | 11 +++++++++++ tests/setup.ts | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index cf0f7e6..2a0aba6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -66,7 +66,7 @@ app.onError((err, c) => { app.get("/health", async (c) => { if (redis.status !== "ready") { - return c.json({ status: "error", message: "Redis down" }, 503); + return c.json({ status: "error", message: "Redis down", redis: redis.status }, 503); } return c.json({ status: "ok" }); }); diff --git a/tests/integration/api.test.ts b/tests/integration/api.test.ts index f455100..da11da8 100644 --- a/tests/integration/api.test.ts +++ b/tests/integration/api.test.ts @@ -35,6 +35,17 @@ describe("API Integration", () => { expect(body.status).toBe("ok"); }); + it("should return 503 for health check if redis is down", async () => { + redis.status = "connecting"; + + const res = await app.request("/health"); + const body = await res.json(); + + expect(res.status).toBe(503); + expect(body.status).toBe("error"); + expect(body.redis).toBe("connecting"); + }); + it("should return 200 for status with valid API Key", async () => { redis.keys.mockReturnValue(Promise.resolve(["config:trakt"])); redis.get.mockImplementation((key) => { diff --git a/tests/setup.ts b/tests/setup.ts index 883bb21..e67f249 100644 --- a/tests/setup.ts +++ b/tests/setup.ts @@ -8,8 +8,10 @@ process.env.PORT = "3000"; // Global Redis mock mock.module("../src/core/RedisClient", () => ({ redis: { + status: "ready", get: mock(() => Promise.resolve(null)), set: mock(() => Promise.resolve()), keys: mock(() => Promise.resolve([])), + on: mock(() => {}), }, }));