Files

103 lines
3.9 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("~/utils/db.server", () => ({
db: {
appConfiguration: {
count: vi.fn(),
},
},
}));
vi.mock("@hivegrid/db/portal-rls-runtime-readiness.server", () => ({
assertPortalRlsRuntimeReady: vi.fn(),
}));
describe("web readyz route", () => {
beforeEach(async () => {
const { db } = await import("~/utils/db.server");
const { assertPortalRlsRuntimeReady } = await import("@hivegrid/db/portal-rls-runtime-readiness.server");
const { resetWebReadinessCacheForTests } = await import("~/utils/readiness-cache.server");
resetWebReadinessCacheForTests();
vi.mocked(db.appConfiguration.count).mockReset();
vi.mocked(assertPortalRlsRuntimeReady).mockReset();
vi.mocked(assertPortalRlsRuntimeReady).mockResolvedValue({ ready: true } as Awaited<ReturnType<typeof assertPortalRlsRuntimeReady>>);
});
afterEach(() => vi.unstubAllEnvs());
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: "hivegrid-web", surface: "core", revision: "unknown" });
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: "hivegrid-web",
surface: "core",
revision: "unknown",
error: "database_unavailable",
});
expect(text).not.toContain("postgres://");
expect(text).not.toContain("password");
expect(text).not.toContain("secret");
expect(db.appConfiguration.count).toHaveBeenCalledTimes(1);
});
it("checks the dedicated database login on the portal surface", async () => {
const { db } = await import("~/utils/db.server");
const { assertPortalRlsRuntimeReady } = await import("@hivegrid/db/portal-rls-runtime-readiness.server");
vi.stubEnv("WEB_RUNTIME_SURFACE", "portal");
vi.stubEnv("APP_REVISION", "0123456789abcdef0123456789abcdef01234567");
const { loader } = await import("./readyz");
const response = await loader();
expect(response.status).toBe(200);
await expect(response.json()).resolves.toEqual({
status: "ready",
service: "hivegrid-portal",
surface: "portal",
revision: "0123456789abcdef0123456789abcdef01234567",
});
expect(assertPortalRlsRuntimeReady).toHaveBeenCalledTimes(1);
expect(db.appConfiguration.count).not.toHaveBeenCalled();
});
it("fails closed when the portal database login is not active", async () => {
const { assertPortalRlsRuntimeReady } = await import("@hivegrid/db/portal-rls-runtime-readiness.server");
vi.stubEnv("WEB_RUNTIME_SURFACE", "portal");
vi.stubEnv("APP_REVISION", "0123456789abcdef0123456789abcdef01234567");
vi.mocked(assertPortalRlsRuntimeReady).mockResolvedValueOnce({ ready: false } as Awaited<ReturnType<typeof assertPortalRlsRuntimeReady>>);
const { loader } = await import("./readyz");
const response = await loader();
expect(response.status).toBe(503);
await expect(response.json()).resolves.toEqual({
status: "not_ready",
service: "hivegrid-portal",
surface: "portal",
revision: "0123456789abcdef0123456789abcdef01234567",
error: "database_unavailable",
});
});
});