|
| 1 | +// Telegram tests cover forum topic recovery from the real message cache. |
| 2 | +import type { Message } from "grammy/types"; |
| 3 | +import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts"; |
| 4 | +import { beforeEach, describe, expect, it } from "vitest"; |
| 5 | +import { createTelegramMessageContextRuntime } from "./bot-handlers.message-context.runtime.js"; |
| 6 | +import type { RegisterTelegramHandlerParams } from "./bot-native-commands.js"; |
| 7 | +import { resetTelegramMessageCacheForTest } from "./runtime.test-support.js"; |
| 8 | + |
| 9 | +const CHAT_ID = 5678; |
| 10 | +const TOPIC_ID = 77; |
| 11 | + |
| 12 | +let storeScopeId = 0; |
| 13 | + |
| 14 | +/** |
| 15 | + * Builds the runtime against the real message cache so the reaction path's topic |
| 16 | + * recovery is proven through the cache it actually reads, not a stub. |
| 17 | + */ |
| 18 | +function createRuntime() { |
| 19 | + storeScopeId += 1; |
| 20 | + const cfg: OpenClawConfig = {}; |
| 21 | + return createTelegramMessageContextRuntime({ |
| 22 | + cfg, |
| 23 | + accountId: "default", |
| 24 | + opts: { token: "test" }, |
| 25 | + telegramCfg: {}, |
| 26 | + telegramDeps: { |
| 27 | + resolveStorePath: () => `/tmp/openclaw-telegram-thread-recovery-${storeScopeId}/store.json`, |
| 28 | + } as RegisterTelegramHandlerParams["telegramDeps"], |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +function forumMessage(messageId: number, threadId?: number): Message { |
| 33 | + return { |
| 34 | + chat: { id: CHAT_ID, type: "supergroup", title: "Forum", is_forum: true }, |
| 35 | + message_id: messageId, |
| 36 | + date: 1736380800, |
| 37 | + text: "topic message", |
| 38 | + from: { id: 10, is_bot: false, first_name: "Bob" }, |
| 39 | + ...(threadId === undefined ? {} : { message_thread_id: threadId }), |
| 40 | + } as Message; |
| 41 | +} |
| 42 | + |
| 43 | +describe("resolveCachedMessageThreadId", () => { |
| 44 | + beforeEach(() => { |
| 45 | + resetTelegramMessageCacheForTest(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("recovers the topic of a recorded forum message", async () => { |
| 49 | + const runtime = createRuntime(); |
| 50 | + await runtime.recordMessageForReplyChain(forumMessage(100, TOPIC_ID), TOPIC_ID); |
| 51 | + |
| 52 | + await expect( |
| 53 | + runtime.resolveCachedMessageThreadId({ chatId: CHAT_ID, messageId: 100 }), |
| 54 | + ).resolves.toBe(TOPIC_ID); |
| 55 | + }); |
| 56 | + |
| 57 | + it("returns undefined for a message that is not in the cache", async () => { |
| 58 | + const runtime = createRuntime(); |
| 59 | + |
| 60 | + // Cache miss must stay unknown; the reaction handler drops rather than |
| 61 | + // attributing the reaction to the General topic. |
| 62 | + await expect( |
| 63 | + runtime.resolveCachedMessageThreadId({ chatId: CHAT_ID, messageId: 404 }), |
| 64 | + ).resolves.toBeUndefined(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("returns undefined for a recorded message that carries no topic", async () => { |
| 68 | + const runtime = createRuntime(); |
| 69 | + await runtime.recordMessageForReplyChain(forumMessage(101)); |
| 70 | + |
| 71 | + await expect( |
| 72 | + runtime.resolveCachedMessageThreadId({ chatId: CHAT_ID, messageId: 101 }), |
| 73 | + ).resolves.toBeUndefined(); |
| 74 | + }); |
| 75 | +}); |
0 commit comments