mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-13 04:10:34 +00:00
test: raise vitest coverage
This commit is contained in:
141
src/commands/health.snapshot.test.ts
Normal file
141
src/commands/health.snapshot.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user