Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/cli/src/commands/ops/doctor.test.ts
T

211 lines
6.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
PROBES,
formatDoctorReport,
runDoctor,
type Probe,
type ProbeResult,
} from "./doctor.js";
function fakeProbe(name: string, result: Omit<ProbeResult, "name">): Probe {
return async () => ({ name, ...result });
}
describe("ops doctor — runDoctor", () => {
it("runs every probe and aggregates a summary", async () => {
const report = await runDoctor({
probes: [
fakeProbe("a", { status: "ok" }),
fakeProbe("b", { status: "warn", message: "soft" }),
fakeProbe("c", { status: "fail", message: "broken" }),
],
});
expect(report.results.map((r) => r.status)).toEqual(["ok", "warn", "fail"]);
expect(report.summary).toEqual({ ok: 1, warn: 1, fail: 1 });
expect(report.exitCode).toBe(1);
});
it("exit code is 0 when no probe fails", async () => {
const report = await runDoctor({
probes: [fakeProbe("a", { status: "ok" }), fakeProbe("b", { status: "warn" })],
});
expect(report.exitCode).toBe(0);
});
it("catches probe exceptions and records them as fail", async () => {
const probe: Probe = async () => {
throw new Error("boom");
};
Object.defineProperty(probe, "name", { value: "explodes" });
const report = await runDoctor({ probes: [probe] });
expect(report.results[0]).toMatchObject({
name: "explodes",
status: "fail",
message: expect.stringContaining("boom"),
});
});
});
describe("ops doctor — built-in probes", () => {
it("flags missing required env vars", async () => {
const env = {};
const probe = PROBES.find((p) => p.name === "envCatalogProbe");
expect(probe).toBeDefined();
const result = await probe!({ env });
expect(result).toMatchObject({
name: "env.required",
status: "fail",
});
expect(result.message).toMatch(/DATABASE_URL/u);
});
it("succeeds when required env vars are present", async () => {
const probe = PROBES.find((p) => p.name === "envCatalogProbe");
const result = await probe!({ env: { DATABASE_URL: "x", SESSION_SECRET: "y" } });
expect(result.status).toBe("ok");
});
it("warns when SENTRY_DSN is unset", async () => {
const probe = PROBES.find((p) => p.name === "sentryDsnProbe");
const result = await probe!({ env: {} });
expect(result.status).toBe("warn");
});
it("calls httpHead for sentry probe when DSN is set", async () => {
const calls: string[] = [];
const probe = PROBES.find((p) => p.name === "sentryDsnProbe");
const result = await probe!({
env: { SENTRY_DSN: "https://abc@sentry.example.com/1" },
httpHead: async (url) => {
calls.push(url);
return { status: 200 };
},
});
expect(calls).toEqual(["https://sentry.example.com"]);
expect(result.status).toBe("ok");
expect(result.message).toBe("https://sentry.example.com");
});
it("fails sentry probe when origin returns 500", async () => {
const probe = PROBES.find((p) => p.name === "sentryDsnProbe");
const result = await probe!({
env: { SENTRY_DSN: "https://abc@sentry.example.com/1" },
httpHead: async () => ({ status: 503 }),
});
expect(result.status).toBe("fail");
});
it("postgresProbe calls pgPing only when DATABASE_URL set", async () => {
const probe = PROBES.find((p) => p.name === "postgresProbe");
const noEnv = await probe!({ env: {} });
expect(noEnv.status).toBe("fail");
const calls: string[] = [];
const okResult = await probe!({
env: { DATABASE_URL: "postgres://u:p@db:5432/x" },
pgPing: async (url) => {
calls.push(url);
},
});
expect(okResult.status).toBe("ok");
expect(okResult.message).toBe("postgres://db:5432");
expect(calls).toEqual(["postgres://u:p@db:5432/x"]);
const failResult = await probe!({
env: { DATABASE_URL: "postgres://u:p@db:5432/x" },
pgPing: async () => {
throw new Error("ECONNREFUSED");
},
});
expect(failResult.status).toBe("fail");
expect(failResult.message).toMatch(/ECONNREFUSED/u);
});
it("redisProbe handles set vs unset", async () => {
const probe = PROBES.find((p) => p.name === "redisProbe");
const noEnv = await probe!({ env: {} });
expect(noEnv.status).toBe("warn");
const ok = await probe!({
env: { REDIS_URL: "redis://localhost:6379" },
redisPing: async () => undefined,
});
expect(ok.status).toBe("ok");
const fail = await probe!({
env: { REDIS_URL: "redis://r" },
redisPing: async () => {
throw new Error("nope");
},
});
expect(fail.status).toBe("fail");
});
it("pnpmVersionProbe reports ok/warn/fail based on exec", async () => {
const probe = PROBES.find((p) => p.name === "pnpmVersionProbe");
const ok = await probe!({ env: {}, exec: async () => ({ stdout: "9.7.0\n", stderr: "" }) });
expect(ok.status).toBe("ok");
const warn = await probe!({ env: {}, exec: async () => ({ stdout: "8.0.0\n", stderr: "" }) });
expect(warn.status).toBe("warn");
const fail = await probe!({
env: {},
exec: async () => {
throw new Error("ENOENT");
},
});
expect(fail.status).toBe("fail");
});
it("dockerProbe warns when docker is missing", async () => {
const probe = PROBES.find((p) => p.name === "dockerProbe");
const result = await probe!({
env: {},
exec: async () => {
throw new Error("cannot connect");
},
});
expect(result.status).toBe("warn");
});
it("prismaMigrationProbe reports pending count", async () => {
const probe = PROBES.find((p) => p.name === "prismaMigrationProbe");
const ok = await probe!({ env: {}, prismaMigrateStatus: async () => ({ pending: 0 }) });
expect(ok.status).toBe("ok");
const warn = await probe!({ env: {}, prismaMigrateStatus: async () => ({ pending: 2 }) });
expect(warn.status).toBe("warn");
expect(warn.message).toMatch(/2 pending/u);
const fail = await probe!({
env: {},
prismaMigrateStatus: async () => {
throw new Error("schema drift");
},
});
expect(fail.status).toBe("fail");
});
});
describe("formatDoctorReport", () => {
it("renders a table with header and summary", async () => {
const out = formatDoctorReport({
results: [
{ name: "a", status: "ok", message: "fine" },
{ name: "b", status: "fail", message: "broken" },
],
summary: { ok: 1, warn: 0, fail: 1 },
exitCode: 1,
});
expect(out).toContain("PROBE");
expect(out).toContain("STATUS");
expect(out).toContain("summary: 1 ok, 0 warn, 1 fail");
expect(out).toContain("a");
expect(out).toContain("b");
expect(out).toContain("fine");
expect(out).toContain("broken");
});
});