Files
HiveGrid-Wireframes/hivegrid-cloud/apps/web/app/routes/readyz.test.ts
T

53 lines
1.6 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("~/utils/db.server", () => ({
db: {
appConfiguration: {
count: vi.fn(),
},
},
}));
describe("web readyz route", () => {
beforeEach(async () => {
const { db } = await import("~/utils/db.server");
vi.mocked(db.appConfiguration.count).mockReset();
});
it("returns readiness when the database count succeeds", async () => {
const { db } = await import("~/utils/db.server");
vi.mocked(db.appConfiguration.count).mockResolvedValueOnce(1);
const { loader } = await import("./readyz");
const response = await loader();
const body = await response.json();
expect(response.status).toBe(200);
expect(body).toEqual({ status: "ready", service: "web" });
expect(db.appConfiguration.count).toHaveBeenCalledTimes(1);
});
it("returns a redacted readiness failure when the database count throws", async () => {
const { db } = await import("~/utils/db.server");
vi.mocked(db.appConfiguration.count).mockRejectedValueOnce(
new Error("postgres://user:password@example.internal/app?token=secret"),
);
const { loader } = await import("./readyz");
const response = await loader();
const body = await response.json();
const text = JSON.stringify(body);
expect(response.status).toBe(503);
expect(body).toEqual({
status: "not_ready",
service: "web",
error: "database_unavailable",
});
expect(text).not.toContain("postgres://");
expect(text).not.toContain("password");
expect(text).not.toContain("secret");
expect(db.appConfiguration.count).toHaveBeenCalledTimes(1);
});
});