70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import {
|
|
collectServiceHealth,
|
|
createCachedHealthReader,
|
|
type ServiceHealthProbe,
|
|
type ServiceHealthSnapshot,
|
|
} from "@/lib/admin/service-health-core";
|
|
|
|
function probe(
|
|
id: string,
|
|
check: ServiceHealthProbe["check"],
|
|
): ServiceHealthProbe {
|
|
return { id, label: id, category: "core", check };
|
|
}
|
|
|
|
describe("admin service health", () => {
|
|
test("reports healthy and unavailable services without rejecting the snapshot", async () => {
|
|
const snapshot = await collectServiceHealth(
|
|
[
|
|
probe("healthy", async () => true),
|
|
probe("unhealthy", async () => false),
|
|
probe("failed", async () => {
|
|
throw new Error("offline");
|
|
}),
|
|
],
|
|
{ now: () => new Date("2026-08-27T12:00:00.000Z") },
|
|
);
|
|
|
|
expect(snapshot.checkedAt).toBe("2026-08-27T12:00:00.000Z");
|
|
expect(snapshot.services.map(({ id, status }) => ({ id, status }))).toEqual([
|
|
{ id: "healthy", status: "operational" },
|
|
{ id: "unhealthy", status: "unavailable" },
|
|
{ id: "failed", status: "unavailable" },
|
|
]);
|
|
});
|
|
|
|
test("marks probes unavailable after the timeout", async () => {
|
|
const snapshot = await collectServiceHealth(
|
|
[probe("slow", () => new Promise(() => undefined))],
|
|
{ timeoutMs: 1 },
|
|
);
|
|
|
|
expect(snapshot.services[0]?.status).toBe("unavailable");
|
|
});
|
|
|
|
test("reuses snapshots until the cache expires", async () => {
|
|
let currentTime = 0;
|
|
let loads = 0;
|
|
const snapshot: ServiceHealthSnapshot = {
|
|
checkedAt: "2026-08-27T12:00:00.000Z",
|
|
services: [],
|
|
};
|
|
const read = createCachedHealthReader(
|
|
async () => {
|
|
loads += 1;
|
|
return snapshot;
|
|
},
|
|
{ ttlMs: 60_000, now: () => currentTime },
|
|
);
|
|
|
|
expect(await read()).toBe(snapshot);
|
|
expect(await read()).toBe(snapshot);
|
|
expect(loads).toBe(1);
|
|
|
|
currentTime = 60_000;
|
|
expect(await read()).toBe(snapshot);
|
|
expect(loads).toBe(2);
|
|
});
|
|
});
|