fix(auto-reply): prefer Pi RPC by default

This commit is contained in:
Peter Steinberger
2025-12-12 17:30:34 +00:00
parent 79818f73c0
commit 0780859a4d
6 changed files with 102 additions and 153 deletions

View File

@@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { HealthSummary } from "./health.js";
import { healthCommand } from "./health.js";
const runtime = {
@@ -8,110 +9,58 @@ const runtime = {
exit: vi.fn(),
};
vi.mock("../config/config.js", () => ({
loadConfig: () => ({ web: {}, inbound: {} }),
}));
vi.mock("../config/sessions.js", () => ({
resolveStorePath: vi.fn(() => "/tmp/sessions.json"),
loadSessionStore: vi.fn(() => ({
"+1555": { updatedAt: Date.now() - 60_000 },
})),
}));
const waitForWaConnection = vi.fn();
const webAuthExists = vi.fn();
const fetchMock = vi.fn();
vi.stubGlobal("fetch", fetchMock);
vi.mock("../web/session.js", () => ({
createWaSocket: vi.fn(async () => ({
ws: { close: vi.fn() },
ev: { on: vi.fn() },
})),
waitForWaConnection: (...args: unknown[]) => waitForWaConnection(...args),
webAuthExists: (...args: unknown[]) => webAuthExists(...args),
getStatusCode: vi.fn(() => 440),
getWebAuthAgeMs: () => 5000,
logWebSelfId: vi.fn(),
}));
vi.mock("../web/reconnect.js", () => ({
resolveHeartbeatSeconds: () => 60,
const callGatewayMock = vi.fn();
vi.mock("../gateway/call.js", () => ({
callGateway: (...args: unknown[]) => callGatewayMock(...args),
}));
describe("healthCommand", () => {
beforeEach(() => {
vi.clearAllMocks();
delete process.env.TELEGRAM_BOT_TOKEN;
fetchMock.mockReset();
});
it("outputs JSON when linked and connect succeeds", async () => {
webAuthExists.mockResolvedValue(true);
waitForWaConnection.mockResolvedValue(undefined);
process.env.TELEGRAM_BOT_TOKEN = "123:abc";
fetchMock
.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ ok: true, result: { id: 1, username: "bot" } }),
})
.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ ok: true, result: { url: "https://hook" } }),
});
it("outputs JSON from gateway", async () => {
const snapshot: HealthSummary = {
ts: Date.now(),
durationMs: 5,
web: {
linked: true,
authAgeMs: 5000,
connect: { ok: true, elapsedMs: 10 },
},
telegram: { configured: true, probe: { ok: true, elapsedMs: 1 } },
heartbeatSeconds: 60,
sessions: {
path: "/tmp/sessions.json",
count: 1,
recent: [{ key: "+1555", updatedAt: Date.now(), age: 0 }],
},
};
callGatewayMock.mockResolvedValueOnce(snapshot);
await healthCommand({ json: true, timeoutMs: 5000 }, runtime as never);
expect(runtime.exit).not.toHaveBeenCalled();
const logged = runtime.log.mock.calls[0][0] as string;
const parsed = JSON.parse(logged);
const logged = runtime.log.mock.calls[0]?.[0] as string;
const parsed = JSON.parse(logged) as HealthSummary;
expect(parsed.web.linked).toBe(true);
expect(parsed.web.connect.ok).toBe(true);
expect(parsed.telegram.configured).toBe(true);
expect(parsed.telegram.probe.ok).toBe(true);
expect(parsed.sessions.count).toBe(1);
});
it("exits non-zero when not linked", async () => {
webAuthExists.mockResolvedValue(false);
await healthCommand({ json: true }, runtime as never);
expect(runtime.exit).toHaveBeenCalledWith(1);
});
it("prints text summary when not json", async () => {
callGatewayMock.mockResolvedValueOnce({
ts: Date.now(),
durationMs: 5,
web: { linked: false, authAgeMs: null },
telegram: { configured: false },
heartbeatSeconds: 60,
sessions: { path: "/tmp/sessions.json", count: 0, recent: [] },
} satisfies HealthSummary);
it("exits non-zero when connect fails", async () => {
webAuthExists.mockResolvedValue(true);
waitForWaConnection.mockRejectedValueOnce({ output: { statusCode: 440 } });
await healthCommand({ json: false }, runtime as never);
await healthCommand({ json: true }, runtime as never);
expect(runtime.exit).toHaveBeenCalledWith(1);
const logged = runtime.log.mock.calls[0][0] as string;
const parsed = JSON.parse(logged);
expect(parsed.web.connect.ok).toBe(false);
expect(parsed.web.connect.status).toBe(440);
});
it("exits non-zero when telegram probe fails", async () => {
webAuthExists.mockResolvedValue(true);
waitForWaConnection.mockResolvedValue(undefined);
process.env.TELEGRAM_BOT_TOKEN = "123:abc";
fetchMock.mockResolvedValue({
ok: false,
status: 401,
json: async () => ({ ok: false, description: "unauthorized" }),
});
await healthCommand({ json: true }, runtime as never);
expect(runtime.exit).toHaveBeenCalledWith(1);
const logged = runtime.log.mock.calls[0][0] as string;
const parsed = JSON.parse(logged);
expect(parsed.telegram.configured).toBe(true);
expect(parsed.telegram.probe.ok).toBe(false);
expect(parsed.telegram.probe.status).toBe(401);
expect(runtime.exit).not.toHaveBeenCalled();
expect(runtime.log).toHaveBeenCalled();
});
});