
























@@ -0,0 +1,118 @@
1+import {
2+createPluginRuntimeMock,
3+createStartAccountContext,
4+} from "openclaw/plugin-sdk/channel-test-helpers";
5+import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
6+import { afterEach, describe, expect, it, vi } from "vitest";
7+import { telegramPlugin } from "./channel.js";
8+import type { TelegramMonitorFn } from "./monitor.types.js";
9+import { clearTelegramRuntime, setTelegramRuntime } from "./runtime.js";
10+import type { TelegramProbeFn } from "./runtime.types.js";
11+import type { TelegramRuntime } from "./runtime.types.js";
12+13+const probeTelegram = vi.fn();
14+const monitorTelegramProvider = vi.fn();
15+16+function installTelegramRuntime() {
17+const runtime = createPluginRuntimeMock();
18+setTelegramRuntime({
19+ ...runtime,
20+channel: {
21+ ...runtime.channel,
22+telegram: {
23+probeTelegram: probeTelegram as TelegramProbeFn,
24+monitorTelegramProvider: monitorTelegramProvider as TelegramMonitorFn,
25+},
26+},
27+} as unknown as TelegramRuntime);
28+}
29+30+function createTelegramConfig(accountId = "default"): OpenClawConfig {
31+if (accountId === "default") {
32+return {
33+channels: {
34+telegram: {
35+botToken: "123456:bad-token",
36+},
37+},
38+} as OpenClawConfig;
39+}
40+41+return {
42+channels: {
43+telegram: {
44+accounts: {
45+[accountId]: {
46+botToken: "123456:bad-token",
47+},
48+},
49+},
50+},
51+} as OpenClawConfig;
52+}
53+54+function startTelegramAccount(accountId = "default") {
55+const cfg = createTelegramConfig(accountId);
56+const account = telegramPlugin.config.resolveAccount(cfg, accountId);
57+const startAccount = telegramPlugin.gateway?.startAccount;
58+expect(startAccount).toBeDefined();
59+const ctx = createStartAccountContext({
60+ account,
61+ cfg,
62+});
63+return {
64+ ctx,
65+task: startAccount!(ctx),
66+};
67+}
68+69+afterEach(() => {
70+clearTelegramRuntime();
71+probeTelegram.mockReset();
72+monitorTelegramProvider.mockReset();
73+});
74+75+describe("telegramPlugin gateway startup", () => {
76+it("stops before monitor startup when getMe rejects the token", async () => {
77+installTelegramRuntime();
78+probeTelegram.mockResolvedValue({
79+ok: false,
80+status: 401,
81+error: "Unauthorized",
82+elapsedMs: 12,
83+});
84+85+const { ctx, task } = startTelegramAccount("ops");
86+87+await expect(task).rejects.toThrow(
88+'Telegram bot token unauthorized for account "ops" (getMe returned 401',
89+);
90+await expect(task).rejects.toThrow("channels.telegram.accounts.ops.botToken/tokenFile");
91+expect(monitorTelegramProvider).not.toHaveBeenCalled();
92+expect(ctx.log?.error).toHaveBeenCalledWith(
93+expect.stringContaining('Telegram bot token unauthorized for account "ops"'),
94+);
95+});
96+97+it("keeps existing fallback startup for non-auth probe failures", async () => {
98+installTelegramRuntime();
99+probeTelegram.mockResolvedValue({
100+ok: false,
101+status: 500,
102+error: "Bad Gateway",
103+elapsedMs: 12,
104+});
105+monitorTelegramProvider.mockResolvedValue(undefined);
106+107+const { task } = startTelegramAccount();
108+109+await expect(task).resolves.toBeUndefined();
110+expect(monitorTelegramProvider).toHaveBeenCalledWith(
111+expect.objectContaining({
112+token: "123456:bad-token",
113+accountId: "default",
114+useWebhook: false,
115+}),
116+);
117+});
118+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。