






























1+import os from "node:os";
2+import path from "node:path";
13import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
4+import { resolveStorePath } from "openclaw/plugin-sdk/session-store-runtime";
25import { captureEnv } from "openclaw/plugin-sdk/test-env";
36import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
47import { handleTelegramAction, telegramActionRuntime } from "./action-runtime.js";
58import { beginTelegramInboundEventDeliveryCorrelation } from "./inbound-event-delivery.js";
9+import {
10+getTopicName,
11+resetTopicNameCacheForTest,
12+resolveTopicNameCacheScope,
13+setTelegramTopicNameStoreFactoryForTest,
14+} from "./topic-name-cache.js";
615716const originalTelegramActionRuntime = { ...telegramActionRuntime };
817const reactMessageTelegram = vi.fn(async () => ({ ok: true }));
@@ -43,6 +52,38 @@ const createForumTopicTelegram = vi.fn(async () => ({
4352}));
4453let envSnapshot: ReturnType<typeof captureEnv>;
455455+type TopicNameEntryForTest = {
56+name: string;
57+iconColor?: number;
58+iconCustomEmojiId?: string;
59+closed?: boolean;
60+updatedAt: number;
61+};
62+63+const topicNameStoresForTest = new Map<string, Map<string, TopicNameEntryForTest>>();
64+65+function installTopicNameStoreForTest() {
66+topicNameStoresForTest.clear();
67+setTelegramTopicNameStoreFactoryForTest((namespace) => {
68+const entries = topicNameStoresForTest.get(namespace) ?? new Map();
69+topicNameStoresForTest.set(namespace, entries);
70+return {
71+async register(key, value) {
72+entries.set(key, value);
73+},
74+async entries() {
75+return Array.from(entries, ([key, value]) => ({ key, value }));
76+},
77+async delete(key) {
78+return entries.delete(key);
79+},
80+async clear() {
81+entries.clear();
82+},
83+};
84+});
85+}
86+4687type MockCallSource = {
4788mock: {
4889calls: ArrayLike<ReadonlyArray<unknown>>;
@@ -93,6 +134,10 @@ describe("handleTelegramAction", () => {
93134} as OpenClawConfig;
94135}
95136137+function topicCacheScopeFor(cfg: OpenClawConfig, accountId: string): string {
138+return resolveTopicNameCacheScope(resolveStorePath(cfg.session?.store, { agentId: accountId }));
139+}
140+96141async function sendInlineButtonsMessage(params: {
97142to: string;
98143buttons: Array<Array<{ text: string; callback_data: string; style?: string }>>;
@@ -131,6 +176,8 @@ describe("handleTelegramAction", () => {
131176132177beforeEach(() => {
133178envSnapshot = captureEnv(["TELEGRAM_BOT_TOKEN"]);
179+resetTopicNameCacheForTest();
180+installTopicNameStoreForTest();
134181Object.assign(telegramActionRuntime, originalTelegramActionRuntime, {
135182 reactMessageTelegram,
136183 sendMessageTelegram,
@@ -155,6 +202,9 @@ describe("handleTelegramAction", () => {
155202});
156203157204afterEach(() => {
205+setTelegramTopicNameStoreFactoryForTest(undefined);
206+resetTopicNameCacheForTest();
207+topicNameStoresForTest.clear();
158208envSnapshot.restore();
159209});
160210@@ -865,6 +915,53 @@ describe("handleTelegramAction", () => {
865915},
866916);
867917918+it("stores created forum topic names in the account-scoped cache", async () => {
919+createForumTopicTelegram.mockResolvedValueOnce({
920+topicId: 99,
921+name: "Topic",
922+chatId: "-100123",
923+});
924+const cfg = {
925+ ...telegramConfig({ actions: { createForumTopic: true } }),
926+session: { store: path.join(os.tmpdir(), "openclaw-telegram-action-sessions.json") },
927+} as OpenClawConfig;
928+929+await handleTelegramAction(
930+{ action: "createForumTopic", accountId: "work", chatId: "alias-chat", name: "Topic" },
931+cfg,
932+);
933+934+const scope = topicCacheScopeFor(cfg, "work");
935+await expect(getTopicName("-100123", 99, scope)).resolves.toBe("Topic");
936+await expect(getTopicName("alias-chat", 99, scope)).resolves.toBeUndefined();
937+});
938+939+it("stores edited forum topic names in the account-scoped cache", async () => {
940+editForumTopicTelegram.mockResolvedValueOnce({
941+ok: true,
942+chatId: "-100123",
943+messageThreadId: 42,
944+name: "New",
945+});
946+const cfg = {
947+ ...telegramConfig({ actions: { editForumTopic: true } }),
948+session: { store: path.join(os.tmpdir(), "openclaw-telegram-action-sessions.json") },
949+} as OpenClawConfig;
950+951+await handleTelegramAction(
952+{
953+action: "editForumTopic",
954+accountId: "work",
955+chatId: "alias-chat",
956+messageThreadId: 42,
957+name: "New",
958+},
959+cfg,
960+);
961+962+await expect(getTopicName("-100123", 42, topicCacheScopeFor(cfg, "work"))).resolves.toBe("New");
963+});
964+868965it.each([
869966{
870967name: "media",
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。