From 1b31ede435bb5f07d87a5570d07e5d0d2dd5cccf Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Mon, 16 Mar 2026 18:26:33 +0530 Subject: [PATCH] fix: bypass telegram runtime proxy during health checks --- extensions/telegram/src/channel.test.ts | 61 ++++++++++++++----------- extensions/telegram/src/channel.ts | 14 +++--- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/extensions/telegram/src/channel.test.ts b/extensions/telegram/src/channel.test.ts index 476260f2969..7c810cd2226 100644 --- a/extensions/telegram/src/channel.test.ts +++ b/extensions/telegram/src/channel.test.ts @@ -4,10 +4,13 @@ import type { OpenClawConfig, PluginRuntime, } from "openclaw/plugin-sdk/telegram"; -import { describe, expect, it, vi } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; import { createRuntimeEnv } from "../../test-utils/runtime-env.js"; import type { ResolvedTelegramAccount } from "./accounts.js"; +import * as auditModule from "./audit.js"; import { telegramPlugin } from "./channel.js"; +import * as monitorModule from "./monitor.js"; +import * as probeModule from "./probe.js"; import { setTelegramRuntime } from "./runtime.js"; function createCfg(): OpenClawConfig { @@ -53,32 +56,34 @@ function createStartAccountCtx(params: { } function installGatewayRuntime(params?: { probeOk?: boolean; botUsername?: string }) { - const monitorTelegramProvider = vi.fn(async () => undefined); - const probeTelegram = vi.fn(async () => - params?.probeOk ? { ok: true, bot: { username: params.botUsername ?? "bot" } } : { ok: false }, - ); - const collectUnmentionedGroupIds = vi.fn(() => ({ - groupIds: [] as string[], - unresolvedGroups: 0, - hasWildcardUnmentionedGroups: false, - })); - const auditGroupMembership = vi.fn(async () => ({ - ok: true, - checkedGroups: 0, - unresolvedGroups: 0, - hasWildcardUnmentionedGroups: false, - groups: [], - elapsedMs: 0, - })); + const monitorTelegramProvider = vi + .spyOn(monitorModule, "monitorTelegramProvider") + .mockImplementation(async () => undefined); + const probeTelegram = vi + .spyOn(probeModule, "probeTelegram") + .mockImplementation(async () => + params?.probeOk + ? { ok: true, bot: { username: params.botUsername ?? "bot" } } + : { ok: false }, + ); + const collectUnmentionedGroupIds = vi + .spyOn(auditModule, "collectTelegramUnmentionedGroupIds") + .mockImplementation(() => ({ + groupIds: [] as string[], + unresolvedGroups: 0, + hasWildcardUnmentionedGroups: false, + })); + const auditGroupMembership = vi + .spyOn(auditModule, "auditTelegramGroupMembership") + .mockImplementation(async () => ({ + ok: true, + checkedGroups: 0, + unresolvedGroups: 0, + hasWildcardUnmentionedGroups: false, + groups: [], + elapsedMs: 0, + })); setTelegramRuntime({ - channel: { - telegram: { - monitorTelegramProvider, - probeTelegram, - collectUnmentionedGroupIds, - auditGroupMembership, - }, - }, logging: { shouldLogVerbose: () => false, }, @@ -115,6 +120,10 @@ function installSendMessageRuntime( return sendMessageTelegram; } +afterEach(() => { + vi.restoreAllMocks(); +}); + describe("telegramPlugin duplicate token guard", () => { it("marks secondary account as not configured when token is shared", async () => { const cfg = createCfg(); diff --git a/extensions/telegram/src/channel.ts b/extensions/telegram/src/channel.ts index cfb5e8a5f8d..5b3ce7279c6 100644 --- a/extensions/telegram/src/channel.ts +++ b/extensions/telegram/src/channel.ts @@ -47,15 +47,17 @@ import { type ResolvedTelegramAccount, } from "./accounts.js"; import { buildTelegramExecApprovalButtons } from "./approval-buttons.js"; +import { auditTelegramGroupMembership, collectTelegramUnmentionedGroupIds } from "./audit.js"; import { buildTelegramGroupPeerId } from "./bot/helpers.js"; import { isTelegramExecApprovalClientEnabled, resolveTelegramExecApprovalTarget, } from "./exec-approvals.js"; +import { monitorTelegramProvider } from "./monitor.js"; import { looksLikeTelegramTargetId, normalizeTelegramMessagingTarget } from "./normalize.js"; import { sendTelegramPayloadMessages } from "./outbound-adapter.js"; import { parseTelegramReplyToMessageId, parseTelegramThreadId } from "./outbound-params.js"; -import type { TelegramProbe } from "./probe.js"; +import { probeTelegram, type TelegramProbe } from "./probe.js"; import { getTelegramRuntime } from "./runtime.js"; import { sendTypingTelegram } from "./send.js"; import { telegramSetupAdapter } from "./setup-core.js"; @@ -697,7 +699,7 @@ export const telegramPlugin: ChannelPlugin buildTokenChannelStatusSummary(snapshot), probeAccount: async ({ account, timeoutMs }) => - getTelegramRuntime().channel.telegram.probeTelegram(account.token, timeoutMs, { + probeTelegram(account.token, timeoutMs, { accountId: account.accountId, proxyUrl: account.config.proxy, network: account.config.network, @@ -731,7 +733,7 @@ export const telegramPlugin: ChannelPlugin