test: cover telegram topic threadId auto-injection and subagent origin threading

This commit is contained in:
Clawdbot
2026-02-02 16:55:20 +01:00
committed by Ayaan Zaidi
parent eef247b7a4
commit 6ac5dd2c0e
4 changed files with 253 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../../config/config.js";
import { slackPlugin } from "../../../extensions/slack/src/channel.js";
import { telegramPlugin } from "../../../extensions/telegram/src/channel.js";
import { setActivePluginRegistry } from "../../plugins/runtime.js";
import { createTestRegistry } from "../../test-utils/channel-plugins.js";
@@ -40,12 +41,22 @@ const slackConfig = {
},
} as OpenClawConfig;
describe("runMessageAction Slack threading", () => {
const telegramConfig = {
channels: {
telegram: {
botToken: "telegram-test",
},
},
} as OpenClawConfig;
describe("runMessageAction threading auto-injection", () => {
beforeEach(async () => {
const { createPluginRuntime } = await import("../../plugins/runtime/index.js");
const { setSlackRuntime } = await import("../../../extensions/slack/src/runtime.js");
const { setTelegramRuntime } = await import("../../../extensions/telegram/src/runtime.js");
const runtime = createPluginRuntime();
setSlackRuntime(runtime);
setTelegramRuntime(runtime);
setActivePluginRegistry(
createTestRegistry([
{
@@ -53,6 +64,11 @@ describe("runMessageAction Slack threading", () => {
source: "test",
plugin: slackPlugin,
},
{
pluginId: "telegram",
source: "test",
plugin: telegramPlugin,
},
]),
);
});
@@ -114,4 +130,55 @@ describe("runMessageAction Slack threading", () => {
const call = mocks.executeSendAction.mock.calls[0]?.[0];
expect(call?.ctx?.mirror?.sessionKey).toBe("agent:main:slack:channel:c123:thread:333.444");
});
it("auto-injects telegram threadId from toolContext when omitted", async () => {
mocks.executeSendAction.mockResolvedValue({
handledBy: "plugin",
payload: {},
});
await runMessageAction({
cfg: telegramConfig,
action: "send",
params: {
channel: "telegram",
target: "telegram:123",
message: "hi",
},
toolContext: {
currentChannelId: "telegram:123",
currentThreadTs: "42",
},
agentId: "main",
});
const call = mocks.executeSendAction.mock.calls[0]?.[0] as { ctx?: { params?: any } };
expect(call?.ctx?.params?.threadId).toBe("42");
});
it("uses explicit telegram threadId when provided", async () => {
mocks.executeSendAction.mockResolvedValue({
handledBy: "plugin",
payload: {},
});
await runMessageAction({
cfg: telegramConfig,
action: "send",
params: {
channel: "telegram",
target: "telegram:123",
message: "hi",
threadId: "999",
},
toolContext: {
currentChannelId: "telegram:123",
currentThreadTs: "42",
},
agentId: "main",
});
const call = mocks.executeSendAction.mock.calls[0]?.[0] as { ctx?: { params?: any } };
expect(call?.ctx?.params?.threadId).toBe("999");
});
});

View File

@@ -20,7 +20,7 @@ import { parseReplyDirectives } from "../../auto-reply/reply/reply-directives.js
import { dispatchChannelMessageAction } from "../../channels/plugins/message-actions.js";
import { extensionForMime } from "../../media/mime.js";
import { parseSlackTarget } from "../../slack/targets.js";
import { parseTelegramTarget } from "../../telegram/targets.js";
// parseTelegramTarget no longer used (telegram auto-threading uses string matching)
import {
isDeliverableMessageChannel,
normalizeMessageChannel,
@@ -259,13 +259,12 @@ function resolveTelegramAutoThreadId(params: {
if (!context?.currentThreadTs || !context.currentChannelId) {
return undefined;
}
// Parse both targets to extract base chat IDs, ignoring topic suffixes and
// internal prefixes (e.g. "telegram:group:123:topic:456" → "123").
// This mirrors Slack's parseSlackTarget approach — compare canonical chat IDs
// so auto-threading applies even when representations differ.
const parsedTo = parseTelegramTarget(params.to);
const parsedChannel = parseTelegramTarget(context.currentChannelId);
if (parsedTo.chatId.toLowerCase() !== parsedChannel.chatId.toLowerCase()) {
// Only apply when the target matches the originating chat.
// Note: Telegram topic routing is carried via threadId/message_thread_id;
// `currentChannelId` (and most agent targets) are typically the base chat id.
const normalizedTo = params.to.trim().toLowerCase();
const normalizedChannel = context.currentChannelId.trim().toLowerCase();
if (normalizedTo !== normalizedChannel) {
return undefined;
}
return context.currentThreadTs;