mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 13:17:39 +00:00
refactor(telegram): dedupe topic agent routing tests
This commit is contained in:
@@ -21,58 +21,51 @@ vi.mock("../config/config.js", async (importOriginal) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("buildTelegramMessageContext per-topic agentId routing", () => {
|
describe("buildTelegramMessageContext per-topic agentId routing", () => {
|
||||||
|
function buildForumMessage(threadId = 3) {
|
||||||
|
return {
|
||||||
|
message_id: 1,
|
||||||
|
chat: {
|
||||||
|
id: -1001234567890,
|
||||||
|
type: "supergroup" as const,
|
||||||
|
title: "Forum",
|
||||||
|
is_forum: true,
|
||||||
|
},
|
||||||
|
date: 1700000000,
|
||||||
|
text: "@bot hello",
|
||||||
|
message_thread_id: threadId,
|
||||||
|
from: { id: 42, first_name: "Alice" },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function buildForumContext(params: {
|
||||||
|
threadId?: number;
|
||||||
|
topicConfig?: Record<string, unknown>;
|
||||||
|
}) {
|
||||||
|
return await buildTelegramMessageContextForTest({
|
||||||
|
message: buildForumMessage(params.threadId),
|
||||||
|
options: { forceWasMentioned: true },
|
||||||
|
resolveGroupActivation: () => true,
|
||||||
|
resolveTelegramGroupConfig: () => ({
|
||||||
|
groupConfig: { requireMention: false },
|
||||||
|
...(params.topicConfig ? { topicConfig: params.topicConfig } : {}),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.mocked(loadConfig).mockReturnValue(defaultRouteConfig as never);
|
vi.mocked(loadConfig).mockReturnValue(defaultRouteConfig as never);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("uses group-level agent when no topic agentId is set", async () => {
|
it("uses group-level agent when no topic agentId is set", async () => {
|
||||||
const ctx = await buildTelegramMessageContextForTest({
|
const ctx = await buildForumContext({ topicConfig: { systemPrompt: "Be nice" } });
|
||||||
message: {
|
|
||||||
message_id: 1,
|
|
||||||
chat: {
|
|
||||||
id: -1001234567890,
|
|
||||||
type: "supergroup",
|
|
||||||
title: "Forum",
|
|
||||||
is_forum: true,
|
|
||||||
},
|
|
||||||
date: 1700000000,
|
|
||||||
text: "@bot hello",
|
|
||||||
message_thread_id: 3,
|
|
||||||
from: { id: 42, first_name: "Alice" },
|
|
||||||
},
|
|
||||||
options: { forceWasMentioned: true },
|
|
||||||
resolveGroupActivation: () => true,
|
|
||||||
resolveTelegramGroupConfig: () => ({
|
|
||||||
groupConfig: { requireMention: false },
|
|
||||||
topicConfig: { systemPrompt: "Be nice" },
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(ctx).not.toBeNull();
|
expect(ctx).not.toBeNull();
|
||||||
expect(ctx?.ctxPayload?.SessionKey).toBe("agent:main:telegram:group:-1001234567890:topic:3");
|
expect(ctx?.ctxPayload?.SessionKey).toBe("agent:main:telegram:group:-1001234567890:topic:3");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("routes to topic-specific agent when agentId is set", async () => {
|
it("routes to topic-specific agent when agentId is set", async () => {
|
||||||
const ctx = await buildTelegramMessageContextForTest({
|
const ctx = await buildForumContext({
|
||||||
message: {
|
topicConfig: { agentId: "zu", systemPrompt: "I am Zu" },
|
||||||
message_id: 1,
|
|
||||||
chat: {
|
|
||||||
id: -1001234567890,
|
|
||||||
type: "supergroup",
|
|
||||||
title: "Forum",
|
|
||||||
is_forum: true,
|
|
||||||
},
|
|
||||||
date: 1700000000,
|
|
||||||
text: "@bot hello",
|
|
||||||
message_thread_id: 3,
|
|
||||||
from: { id: 42, first_name: "Alice" },
|
|
||||||
},
|
|
||||||
options: { forceWasMentioned: true },
|
|
||||||
resolveGroupActivation: () => true,
|
|
||||||
resolveTelegramGroupConfig: () => ({
|
|
||||||
groupConfig: { requireMention: false },
|
|
||||||
topicConfig: { agentId: "zu", systemPrompt: "I am Zu" },
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(ctx).not.toBeNull();
|
expect(ctx).not.toBeNull();
|
||||||
@@ -82,27 +75,7 @@ describe("buildTelegramMessageContext per-topic agentId routing", () => {
|
|||||||
|
|
||||||
it("different topics route to different agents", async () => {
|
it("different topics route to different agents", async () => {
|
||||||
const buildForTopic = async (threadId: number, agentId: string) =>
|
const buildForTopic = async (threadId: number, agentId: string) =>
|
||||||
await buildTelegramMessageContextForTest({
|
await buildForumContext({ threadId, topicConfig: { agentId } });
|
||||||
message: {
|
|
||||||
message_id: 1,
|
|
||||||
chat: {
|
|
||||||
id: -1001234567890,
|
|
||||||
type: "supergroup",
|
|
||||||
title: "Forum",
|
|
||||||
is_forum: true,
|
|
||||||
},
|
|
||||||
date: 1700000000,
|
|
||||||
text: "@bot hello",
|
|
||||||
message_thread_id: threadId,
|
|
||||||
from: { id: 42, first_name: "Alice" },
|
|
||||||
},
|
|
||||||
options: { forceWasMentioned: true },
|
|
||||||
resolveGroupActivation: () => true,
|
|
||||||
resolveTelegramGroupConfig: () => ({
|
|
||||||
groupConfig: { requireMention: false },
|
|
||||||
topicConfig: { agentId },
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const ctxA = await buildForTopic(1, "main");
|
const ctxA = await buildForTopic(1, "main");
|
||||||
const ctxB = await buildForTopic(3, "zu");
|
const ctxB = await buildForTopic(3, "zu");
|
||||||
@@ -117,26 +90,8 @@ describe("buildTelegramMessageContext per-topic agentId routing", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("ignores whitespace-only agentId and uses group-level agent", async () => {
|
it("ignores whitespace-only agentId and uses group-level agent", async () => {
|
||||||
const ctx = await buildTelegramMessageContextForTest({
|
const ctx = await buildForumContext({
|
||||||
message: {
|
topicConfig: { agentId: " ", systemPrompt: "Be nice" },
|
||||||
message_id: 1,
|
|
||||||
chat: {
|
|
||||||
id: -1001234567890,
|
|
||||||
type: "supergroup",
|
|
||||||
title: "Forum",
|
|
||||||
is_forum: true,
|
|
||||||
},
|
|
||||||
date: 1700000000,
|
|
||||||
text: "@bot hello",
|
|
||||||
message_thread_id: 3,
|
|
||||||
from: { id: 42, first_name: "Alice" },
|
|
||||||
},
|
|
||||||
options: { forceWasMentioned: true },
|
|
||||||
resolveGroupActivation: () => true,
|
|
||||||
resolveTelegramGroupConfig: () => ({
|
|
||||||
groupConfig: { requireMention: false },
|
|
||||||
topicConfig: { agentId: " ", systemPrompt: "Be nice" },
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(ctx).not.toBeNull();
|
expect(ctx).not.toBeNull();
|
||||||
@@ -152,27 +107,7 @@ describe("buildTelegramMessageContext per-topic agentId routing", () => {
|
|||||||
messages: { groupChat: { mentionPatterns: [] } },
|
messages: { groupChat: { mentionPatterns: [] } },
|
||||||
} as never);
|
} as never);
|
||||||
|
|
||||||
const ctx = await buildTelegramMessageContextForTest({
|
const ctx = await buildForumContext({ topicConfig: { agentId: "ghost" } });
|
||||||
message: {
|
|
||||||
message_id: 1,
|
|
||||||
chat: {
|
|
||||||
id: -1001234567890,
|
|
||||||
type: "supergroup",
|
|
||||||
title: "Forum",
|
|
||||||
is_forum: true,
|
|
||||||
},
|
|
||||||
date: 1700000000,
|
|
||||||
text: "@bot hello",
|
|
||||||
message_thread_id: 3,
|
|
||||||
from: { id: 42, first_name: "Alice" },
|
|
||||||
},
|
|
||||||
options: { forceWasMentioned: true },
|
|
||||||
resolveGroupActivation: () => true,
|
|
||||||
resolveTelegramGroupConfig: () => ({
|
|
||||||
groupConfig: { requireMention: false },
|
|
||||||
topicConfig: { agentId: "ghost" },
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(ctx).not.toBeNull();
|
expect(ctx).not.toBeNull();
|
||||||
expect(ctx?.ctxPayload?.SessionKey).toContain("agent:main:");
|
expect(ctx?.ctxPayload?.SessionKey).toContain("agent:main:");
|
||||||
|
|||||||
Reference in New Issue
Block a user