|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { buildTelegramMessageContextForTest } from "./bot-message-context.test-harness.js"; |
| 3 | + |
| 4 | +describe("buildTelegramMessageContext per-topic agentId routing", () => { |
| 5 | + it("uses group-level agent when no topic agentId is set", async () => { |
| 6 | + const ctx = await buildTelegramMessageContextForTest({ |
| 7 | + message: { |
| 8 | + message_id: 1, |
| 9 | + chat: { |
| 10 | + id: -1001234567890, |
| 11 | + type: "supergroup", |
| 12 | + title: "Forum", |
| 13 | + is_forum: true, |
| 14 | + }, |
| 15 | + date: 1700000000, |
| 16 | + text: "@bot hello", |
| 17 | + message_thread_id: 3, |
| 18 | + from: { id: 42, first_name: "Alice" }, |
| 19 | + }, |
| 20 | + options: { forceWasMentioned: true }, |
| 21 | + resolveGroupActivation: () => true, |
| 22 | + resolveTelegramGroupConfig: () => ({ |
| 23 | + groupConfig: { requireMention: false }, |
| 24 | + topicConfig: { systemPrompt: "Be nice" }, |
| 25 | + }), |
| 26 | + }); |
| 27 | + |
| 28 | + expect(ctx).not.toBeNull(); |
| 29 | + expect(ctx?.ctxPayload?.SessionKey).toBe("agent:main:telegram:group:-1001234567890:topic:3"); |
| 30 | + }); |
| 31 | + |
| 32 | + it("routes to topic-specific agent when agentId is set", async () => { |
| 33 | + const ctx = await buildTelegramMessageContextForTest({ |
| 34 | + message: { |
| 35 | + message_id: 1, |
| 36 | + chat: { |
| 37 | + id: -1001234567890, |
| 38 | + type: "supergroup", |
| 39 | + title: "Forum", |
| 40 | + is_forum: true, |
| 41 | + }, |
| 42 | + date: 1700000000, |
| 43 | + text: "@bot hello", |
| 44 | + message_thread_id: 3, |
| 45 | + from: { id: 42, first_name: "Alice" }, |
| 46 | + }, |
| 47 | + options: { forceWasMentioned: true }, |
| 48 | + resolveGroupActivation: () => true, |
| 49 | + resolveTelegramGroupConfig: () => ({ |
| 50 | + groupConfig: { requireMention: false }, |
| 51 | + topicConfig: { agentId: "zu", systemPrompt: "I am Zu" }, |
| 52 | + }), |
| 53 | + }); |
| 54 | + |
| 55 | + expect(ctx).not.toBeNull(); |
| 56 | + expect(ctx?.ctxPayload?.SessionKey).toContain("agent:zu:"); |
| 57 | + expect(ctx?.ctxPayload?.SessionKey).toContain("telegram:group:-1001234567890:topic:3"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("different topics route to different agents", async () => { |
| 61 | + const buildForTopic = async (threadId: number, agentId: string) => |
| 62 | + await buildTelegramMessageContextForTest({ |
| 63 | + message: { |
| 64 | + message_id: 1, |
| 65 | + chat: { |
| 66 | + id: -1001234567890, |
| 67 | + type: "supergroup", |
| 68 | + title: "Forum", |
| 69 | + is_forum: true, |
| 70 | + }, |
| 71 | + date: 1700000000, |
| 72 | + text: "@bot hello", |
| 73 | + message_thread_id: threadId, |
| 74 | + from: { id: 42, first_name: "Alice" }, |
| 75 | + }, |
| 76 | + options: { forceWasMentioned: true }, |
| 77 | + resolveGroupActivation: () => true, |
| 78 | + resolveTelegramGroupConfig: () => ({ |
| 79 | + groupConfig: { requireMention: false }, |
| 80 | + topicConfig: { agentId }, |
| 81 | + }), |
| 82 | + }); |
| 83 | + |
| 84 | + const ctxA = await buildForTopic(1, "main"); |
| 85 | + const ctxB = await buildForTopic(3, "zu"); |
| 86 | + const ctxC = await buildForTopic(5, "q"); |
| 87 | + |
| 88 | + expect(ctxA?.ctxPayload?.SessionKey).toContain("agent:main:"); |
| 89 | + expect(ctxB?.ctxPayload?.SessionKey).toContain("agent:zu:"); |
| 90 | + expect(ctxC?.ctxPayload?.SessionKey).toContain("agent:q:"); |
| 91 | + |
| 92 | + expect(ctxA?.ctxPayload?.SessionKey).not.toBe(ctxB?.ctxPayload?.SessionKey); |
| 93 | + expect(ctxB?.ctxPayload?.SessionKey).not.toBe(ctxC?.ctxPayload?.SessionKey); |
| 94 | + }); |
| 95 | + |
| 96 | + it("ignores whitespace-only agentId and uses group-level agent", async () => { |
| 97 | + const ctx = await buildTelegramMessageContextForTest({ |
| 98 | + message: { |
| 99 | + message_id: 1, |
| 100 | + chat: { |
| 101 | + id: -1001234567890, |
| 102 | + type: "supergroup", |
| 103 | + title: "Forum", |
| 104 | + is_forum: true, |
| 105 | + }, |
| 106 | + date: 1700000000, |
| 107 | + text: "@bot hello", |
| 108 | + message_thread_id: 3, |
| 109 | + from: { id: 42, first_name: "Alice" }, |
| 110 | + }, |
| 111 | + options: { forceWasMentioned: true }, |
| 112 | + resolveGroupActivation: () => true, |
| 113 | + resolveTelegramGroupConfig: () => ({ |
| 114 | + groupConfig: { requireMention: false }, |
| 115 | + topicConfig: { agentId: " ", systemPrompt: "Be nice" }, |
| 116 | + }), |
| 117 | + }); |
| 118 | + |
| 119 | + expect(ctx).not.toBeNull(); |
| 120 | + expect(ctx?.ctxPayload?.SessionKey).toContain("agent:main:"); |
| 121 | + }); |
| 122 | + |
| 123 | + it("routes DM topic to specific agent when agentId is set", async () => { |
| 124 | + const ctx = await buildTelegramMessageContextForTest({ |
| 125 | + message: { |
| 126 | + message_id: 1, |
| 127 | + chat: { |
| 128 | + id: 123456789, |
| 129 | + type: "private", |
| 130 | + }, |
| 131 | + date: 1700000000, |
| 132 | + text: "@bot hello", |
| 133 | + message_thread_id: 99, |
| 134 | + from: { id: 42, first_name: "Alice" }, |
| 135 | + }, |
| 136 | + options: { forceWasMentioned: true }, |
| 137 | + resolveGroupActivation: () => true, |
| 138 | + resolveTelegramGroupConfig: () => ({ |
| 139 | + groupConfig: { requireMention: false }, |
| 140 | + topicConfig: { agentId: "support", systemPrompt: "I am support" }, |
| 141 | + }), |
| 142 | + }); |
| 143 | + |
| 144 | + expect(ctx).not.toBeNull(); |
| 145 | + expect(ctx?.ctxPayload?.SessionKey).toContain("agent:support:"); |
| 146 | + }); |
| 147 | +}); |
0 commit comments