2025-12-13 20:37:56 +00:00
|
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
2026-01-18 08:32:19 +00:00
|
|
|
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
2026-02-01 10:03:47 +09:00
|
|
|
import { stripAnsi } from "../terminal/ansi.js";
|
2026-01-18 08:32:19 +00:00
|
|
|
import { createTestRegistry } from "../test-utils/channel-plugins.js";
|
2026-02-18 01:34:35 +00:00
|
|
|
import type { HealthSummary } from "./health.js";
|
2026-02-01 10:03:47 +09:00
|
|
|
import { healthCommand } from "./health.js";
|
2025-12-13 20:37:56 +00:00
|
|
|
|
|
|
|
|
const callGatewayMock = vi.fn();
|
|
|
|
|
const logWebSelfIdMock = vi.fn();
|
|
|
|
|
|
2026-02-16 16:48:46 +00:00
|
|
|
function createRecentSessionRows(now = Date.now()) {
|
|
|
|
|
return [
|
|
|
|
|
{ key: "main", updatedAt: now - 60_000, age: 60_000 },
|
|
|
|
|
{ key: "foo", updatedAt: null, age: null },
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-13 20:37:56 +00:00
|
|
|
vi.mock("../gateway/call.js", () => ({
|
|
|
|
|
callGateway: (...args: unknown[]) => callGatewayMock(...args),
|
|
|
|
|
}));
|
|
|
|
|
|
2026-03-14 02:44:55 -07:00
|
|
|
vi.mock("../../extensions/whatsapp/src/auth-store.js", () => ({
|
2025-12-13 20:37:56 +00:00
|
|
|
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();
|
2026-01-18 08:32:19 +00:00
|
|
|
setActivePluginRegistry(
|
|
|
|
|
createTestRegistry([
|
|
|
|
|
{
|
|
|
|
|
pluginId: "whatsapp",
|
|
|
|
|
source: "test",
|
|
|
|
|
plugin: {
|
|
|
|
|
id: "whatsapp",
|
|
|
|
|
meta: {
|
|
|
|
|
id: "whatsapp",
|
|
|
|
|
label: "WhatsApp",
|
|
|
|
|
selectionLabel: "WhatsApp",
|
|
|
|
|
docsPath: "/channels/whatsapp",
|
|
|
|
|
blurb: "WhatsApp test stub.",
|
|
|
|
|
},
|
|
|
|
|
capabilities: { chatTypes: ["direct", "group"] },
|
|
|
|
|
config: {
|
|
|
|
|
listAccountIds: () => ["default"],
|
|
|
|
|
resolveAccount: () => ({}),
|
|
|
|
|
},
|
|
|
|
|
status: {
|
|
|
|
|
logSelfId: () => logWebSelfIdMock(),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]),
|
|
|
|
|
);
|
2025-12-13 20:37:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("prints the rich text summary when linked and configured", async () => {
|
2026-02-16 16:48:46 +00:00
|
|
|
const recent = createRecentSessionRows();
|
2025-12-13 20:37:56 +00:00
|
|
|
callGatewayMock.mockResolvedValueOnce({
|
|
|
|
|
ok: true,
|
|
|
|
|
ts: Date.now(),
|
|
|
|
|
durationMs: 5,
|
2026-01-13 06:16:43 +00:00
|
|
|
channels: {
|
2026-01-11 11:45:25 +00:00
|
|
|
whatsapp: {
|
2026-01-17 01:14:06 +00:00
|
|
|
accountId: "default",
|
2026-01-11 11:45:25 +00:00
|
|
|
linked: true,
|
|
|
|
|
authAgeMs: 5 * 60_000,
|
|
|
|
|
},
|
|
|
|
|
telegram: {
|
2026-01-17 01:14:06 +00:00
|
|
|
accountId: "default",
|
2026-01-11 11:45:25 +00:00
|
|
|
configured: true,
|
|
|
|
|
probe: {
|
|
|
|
|
ok: true,
|
|
|
|
|
elapsedMs: 7,
|
|
|
|
|
bot: { username: "bot" },
|
|
|
|
|
webhook: { url: "https://example.com/h" },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
discord: {
|
2026-01-17 01:14:06 +00:00
|
|
|
accountId: "default",
|
2026-01-11 11:45:25 +00:00
|
|
|
configured: false,
|
2025-12-13 20:37:56 +00:00
|
|
|
},
|
|
|
|
|
},
|
2026-01-13 06:16:43 +00:00
|
|
|
channelOrder: ["whatsapp", "telegram", "discord"],
|
|
|
|
|
channelLabels: {
|
2026-01-11 11:45:25 +00:00
|
|
|
whatsapp: "WhatsApp",
|
|
|
|
|
telegram: "Telegram",
|
|
|
|
|
discord: "Discord",
|
2025-12-15 10:11:18 -06:00
|
|
|
},
|
2025-12-13 20:37:56 +00:00
|
|
|
heartbeatSeconds: 60,
|
2026-01-17 01:14:06 +00:00
|
|
|
defaultAgentId: "main",
|
|
|
|
|
agents: [
|
|
|
|
|
{
|
|
|
|
|
agentId: "main",
|
|
|
|
|
isDefault: true,
|
|
|
|
|
heartbeat: {
|
|
|
|
|
enabled: true,
|
|
|
|
|
every: "1m",
|
|
|
|
|
everyMs: 60_000,
|
|
|
|
|
prompt: "hi",
|
|
|
|
|
target: "last",
|
|
|
|
|
ackMaxChars: 160,
|
|
|
|
|
},
|
|
|
|
|
sessions: {
|
|
|
|
|
path: "/tmp/sessions.json",
|
|
|
|
|
count: 2,
|
2026-02-16 16:48:46 +00:00
|
|
|
recent,
|
2026-01-17 01:14:06 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
2025-12-13 20:37:56 +00:00
|
|
|
sessions: {
|
|
|
|
|
path: "/tmp/sessions.json",
|
|
|
|
|
count: 2,
|
2026-02-16 16:48:46 +00:00
|
|
|
recent,
|
2025-12-13 20:37:56 +00:00
|
|
|
},
|
|
|
|
|
} satisfies HealthSummary);
|
|
|
|
|
|
|
|
|
|
await healthCommand({ json: false, timeoutMs: 1000 }, runtime as never);
|
|
|
|
|
|
|
|
|
|
expect(runtime.exit).not.toHaveBeenCalled();
|
2026-01-15 08:28:33 +00:00
|
|
|
expect(stripAnsi(runtime.log.mock.calls.map((c) => String(c[0])).join("\n"))).toMatch(
|
|
|
|
|
/WhatsApp: linked/i,
|
|
|
|
|
);
|
2025-12-13 20:37:56 +00:00
|
|
|
expect(logWebSelfIdMock).toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
});
|