test: raise vitest coverage

This commit is contained in:
Peter Steinberger
2025-12-13 20:37:56 +00:00
parent 41dd3b11b7
commit cf28ea0d1c
10 changed files with 1433 additions and 1 deletions

View File

@@ -0,0 +1,68 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { HealthSummary } from "./health.js";
import { healthCommand } from "./health.js";
const callGatewayMock = vi.fn();
const logWebSelfIdMock = vi.fn();
vi.mock("../gateway/call.js", () => ({
callGateway: (...args: unknown[]) => callGatewayMock(...args),
}));
vi.mock("../web/session.js", () => ({
webAuthExists: vi.fn(async () => true),
getWebAuthAgeMs: vi.fn(() => 0),
logWebSelfId: (...args: unknown[]) => logWebSelfIdMock(...args),
}));
describe("healthCommand (coverage)", () => {
const runtime = {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
};
beforeEach(() => {
vi.clearAllMocks();
});
it("prints the rich text summary when linked and configured", async () => {
callGatewayMock.mockResolvedValueOnce({
ok: true,
ts: Date.now(),
durationMs: 5,
web: {
linked: true,
authAgeMs: 5 * 60_000,
connect: { ok: true, status: 200, elapsedMs: 10 },
},
telegram: {
configured: true,
probe: {
ok: true,
elapsedMs: 7,
bot: { username: "bot" },
webhook: { url: "https://example.com/h" },
},
},
heartbeatSeconds: 60,
sessions: {
path: "/tmp/sessions.json",
count: 2,
recent: [
{ key: "main", updatedAt: Date.now() - 60_000, age: 60_000 },
{ key: "foo", updatedAt: null, age: null },
],
},
} satisfies HealthSummary);
await healthCommand({ json: false, timeoutMs: 1000 }, runtime as never);
expect(runtime.exit).not.toHaveBeenCalled();
expect(runtime.log.mock.calls.map((c) => String(c[0])).join("\n")).toMatch(
/Web: linked/i,
);
expect(logWebSelfIdMock).toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,141 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import type { HealthSummary } from "./health.js";
import { getHealthSnapshot } from "./health.js";
let testConfig: Record<string, unknown> = {};
let testStore: Record<string, { updatedAt?: number }> = {};
vi.mock("../config/config.js", () => ({
loadConfig: () => testConfig,
}));
vi.mock("../config/sessions.js", () => ({
resolveStorePath: () => "/tmp/sessions.json",
loadSessionStore: () => testStore,
}));
vi.mock("../web/session.js", () => ({
webAuthExists: vi.fn(async () => true),
getWebAuthAgeMs: vi.fn(() => 1234),
logWebSelfId: vi.fn(),
}));
vi.mock("../web/reconnect.js", () => ({
resolveHeartbeatSeconds: vi.fn(() => 60),
}));
describe("getHealthSnapshot", () => {
afterEach(() => {
vi.unstubAllGlobals();
vi.unstubAllEnvs();
});
it("skips telegram probe when not configured", async () => {
testConfig = { inbound: { reply: { session: { store: "/tmp/x" } } } };
testStore = {
global: { updatedAt: Date.now() },
unknown: { updatedAt: Date.now() },
main: { updatedAt: 1000 },
foo: { updatedAt: 2000 },
};
vi.stubEnv("TELEGRAM_BOT_TOKEN", "");
const snap = (await getHealthSnapshot(10)) satisfies HealthSummary;
expect(snap.ok).toBe(true);
expect(snap.telegram.configured).toBe(false);
expect(snap.telegram.probe).toBeUndefined();
expect(snap.sessions.count).toBe(2);
expect(snap.sessions.recent[0]?.key).toBe("foo");
});
it("probes telegram getMe + webhook info when configured", async () => {
testConfig = { telegram: { botToken: "t-1" } };
testStore = {};
const calls: string[] = [];
vi.stubGlobal(
"fetch",
vi.fn(async (url: string) => {
calls.push(url);
if (url.includes("/getMe")) {
return {
ok: true,
status: 200,
json: async () => ({
ok: true,
result: { id: 1, username: "bot" },
}),
} as unknown as Response;
}
if (url.includes("/getWebhookInfo")) {
return {
ok: true,
status: 200,
json: async () => ({
ok: true,
result: {
url: "https://example.com/h",
has_custom_certificate: false,
},
}),
} as unknown as Response;
}
return {
ok: false,
status: 404,
json: async () => ({ ok: false, description: "nope" }),
} as unknown as Response;
}),
);
const snap = await getHealthSnapshot(25);
expect(snap.telegram.configured).toBe(true);
expect(snap.telegram.probe?.ok).toBe(true);
expect(snap.telegram.probe?.bot?.username).toBe("bot");
expect(snap.telegram.probe?.webhook?.url).toMatch(/^https:/);
expect(calls.some((c) => c.includes("/getMe"))).toBe(true);
expect(calls.some((c) => c.includes("/getWebhookInfo"))).toBe(true);
});
it("returns a structured telegram probe error when getMe fails", async () => {
testConfig = { telegram: { botToken: "bad-token" } };
testStore = {};
vi.stubGlobal(
"fetch",
vi.fn(async (url: string) => {
if (url.includes("/getMe")) {
return {
ok: false,
status: 401,
json: async () => ({ ok: false, description: "unauthorized" }),
} as unknown as Response;
}
throw new Error("unexpected");
}),
);
const snap = await getHealthSnapshot(25);
expect(snap.telegram.configured).toBe(true);
expect(snap.telegram.probe?.ok).toBe(false);
expect(snap.telegram.probe?.status).toBe(401);
expect(snap.telegram.probe?.error).toMatch(/unauthorized/i);
});
it("captures unexpected probe exceptions as errors", async () => {
testConfig = { telegram: { botToken: "t-err" } };
testStore = {};
vi.stubGlobal(
"fetch",
vi.fn(async () => {
throw new Error("network down");
}),
);
const snap = await getHealthSnapshot(25);
expect(snap.telegram.configured).toBe(true);
expect(snap.telegram.probe?.ok).toBe(false);
expect(snap.telegram.probe?.error).toMatch(/network down/i);
});
});