Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/mcp/src/deploy-health.test.ts
T

140 lines
4.6 KiB
TypeScript

import { describe, expect, test, vi } from "vitest";
import { runMcpDeployHealthCli } from "./deploy-health.js";
function createIo() {
let stderr = "";
let stdout = "";
return {
io: {
stderr: {
write(chunk: string) {
stderr += chunk;
return true;
},
},
stdout: {
write(chunk: string) {
stdout += chunk;
return true;
},
},
},
readStderr: () => stderr,
readStdout: () => stdout,
};
}
function readyResponse(revision: string, overrides: Record<string, unknown> = {}) {
return {
status: 200,
json: vi.fn(async () => ({
status: "ready",
service: "mcp",
revision,
checks: { database: "ok", redis: "ok" },
...overrides,
})),
};
}
describe("MCP deployment health", () => {
test("probes live dependency readiness for the exact baked image revision", async () => {
const revision = "a".repeat(40);
const fetchReadiness = vi.fn(async () => readyResponse(revision));
const { io, readStderr, readStdout } = createIo();
const exitCode = await runMcpDeployHealthCli(revision, { APP_REVISION: revision, MCP_PORT: "8081" }, io, {
fetchReadiness,
readImageRevision: () => revision,
timeoutSignal: () => new AbortController().signal,
});
expect(exitCode).toBe(0);
expect(JSON.parse(readStdout())).toEqual({ status: "ready", service: "mcp", revision });
expect(readStderr()).toBe("");
expect(fetchReadiness).toHaveBeenCalledWith(
"http://127.0.0.1:8081/readyz",
expect.objectContaining({ headers: { Accept: "application/json" }, signal: expect.any(AbortSignal) })
);
});
test("rejects an environment override that disagrees with the baked image revision", async () => {
const expectedRevision = "b".repeat(40);
const fetchReadiness = vi.fn();
const { io, readStderr, readStdout } = createIo();
const exitCode = await runMcpDeployHealthCli(expectedRevision, { APP_REVISION: expectedRevision }, io, {
fetchReadiness,
readImageRevision: () => "a".repeat(40),
});
expect(exitCode).toBe(1);
expect(readStdout()).toBe("");
expect(readStderr()).toMatch(/revision does not match/);
expect(readStderr()).not.toContain(expectedRevision);
expect(fetchReadiness).not.toHaveBeenCalled();
});
test("rejects unhealthy or mismatched readiness responses", async () => {
const revision = "a".repeat(40);
const scenarios = [
vi.fn(async () => ({ status: 503, json: vi.fn() })),
vi.fn(async () => readyResponse(revision, { service: "api" })),
vi.fn(async () => readyResponse("b".repeat(40))),
vi.fn(async () => readyResponse(revision, { checks: { database: "ok", redis: "error" } })),
vi.fn(async () => readyResponse(revision, { checks: { first: "ok", second: "ok" } })),
vi.fn(async () => readyResponse(revision, { checks: ["ok", "ok"] })),
];
for (const fetchReadiness of scenarios) {
const { io, readStderr, readStdout } = createIo();
const exitCode = await runMcpDeployHealthCli(revision, { APP_REVISION: revision }, io, {
fetchReadiness,
readImageRevision: () => revision,
timeoutSignal: () => new AbortController().signal,
});
expect(exitCode).toBe(1);
expect(readStdout()).toBe("");
expect(readStderr()).toMatch(/readiness/);
}
});
test("redacts readiness transport errors", async () => {
const revision = "a".repeat(40);
const { io, readStderr, readStdout } = createIo();
const exitCode = await runMcpDeployHealthCli(revision, { APP_REVISION: revision }, io, {
fetchReadiness: vi.fn(async () => {
throw new Error("connection failed with secret details");
}),
readImageRevision: () => revision,
timeoutSignal: () => new AbortController().signal,
});
expect(exitCode).toBe(1);
expect(readStdout()).toBe("");
expect(readStderr()).toMatch(/probe failed/);
expect(readStderr()).not.toContain("secret details");
});
test("rejects malformed revisions and ports", async () => {
const revision = "a".repeat(40);
const cases = [
["not-a-sha", { APP_REVISION: revision }],
[revision, { APP_REVISION: revision, MCP_PORT: "70000" }],
] as const;
for (const [expectedRevision, env] of cases) {
const { io, readStderr, readStdout } = createIo();
const exitCode = await runMcpDeployHealthCli(expectedRevision, env, io, {
readImageRevision: () => revision,
});
expect(exitCode).toBe(1);
expect(readStdout()).toBe("");
expect(readStderr()).not.toContain("not-a-sha");
}
});
});