|
1 | | -import { describe, expect, it } from "vitest"; |
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { loadConfig } from "../config/config.js"; |
2 | 3 | import { buildTelegramMessageContextForTest } from "./bot-message-context.test-harness.js"; |
3 | 4 |
|
| 5 | +const { defaultRouteConfig } = vi.hoisted(() => ({ |
| 6 | + defaultRouteConfig: { |
| 7 | + agents: { |
| 8 | + list: [{ id: "main", default: true }, { id: "zu" }, { id: "q" }, { id: "support" }], |
| 9 | + }, |
| 10 | + channels: { telegram: {} }, |
| 11 | + messages: { groupChat: { mentionPatterns: [] } }, |
| 12 | + }, |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("../config/config.js", async (importOriginal) => { |
| 16 | + const actual = await importOriginal<typeof import("../config/config.js")>(); |
| 17 | + return { |
| 18 | + ...actual, |
| 19 | + loadConfig: vi.fn(() => defaultRouteConfig), |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
4 | 23 | describe("buildTelegramMessageContext per-topic agentId routing", () => { |
| 24 | + beforeEach(() => { |
| 25 | + vi.mocked(loadConfig).mockReturnValue(defaultRouteConfig as never); |
| 26 | + }); |
| 27 | + |
5 | 28 | it("uses group-level agent when no topic agentId is set", async () => { |
6 | 29 | const ctx = await buildTelegramMessageContextForTest({ |
7 | 30 | message: { |
@@ -120,6 +143,41 @@ describe("buildTelegramMessageContext per-topic agentId routing", () => { |
120 | 143 | expect(ctx?.ctxPayload?.SessionKey).toContain("agent:main:"); |
121 | 144 | }); |
122 | 145 |
|
| 146 | + it("falls back to default agent when topic agentId does not exist", async () => { |
| 147 | + vi.mocked(loadConfig).mockReturnValue({ |
| 148 | + agents: { |
| 149 | + list: [{ id: "main", default: true }, { id: "zu" }], |
| 150 | + }, |
| 151 | + channels: { telegram: {} }, |
| 152 | + messages: { groupChat: { mentionPatterns: [] } }, |
| 153 | + } as never); |
| 154 | + |
| 155 | + const ctx = await buildTelegramMessageContextForTest({ |
| 156 | + message: { |
| 157 | + message_id: 1, |
| 158 | + chat: { |
| 159 | + id: -1001234567890, |
| 160 | + type: "supergroup", |
| 161 | + title: "Forum", |
| 162 | + is_forum: true, |
| 163 | + }, |
| 164 | + date: 1700000000, |
| 165 | + text: "@bot hello", |
| 166 | + message_thread_id: 3, |
| 167 | + from: { id: 42, first_name: "Alice" }, |
| 168 | + }, |
| 169 | + options: { forceWasMentioned: true }, |
| 170 | + resolveGroupActivation: () => true, |
| 171 | + resolveTelegramGroupConfig: () => ({ |
| 172 | + groupConfig: { requireMention: false }, |
| 173 | + topicConfig: { agentId: "ghost" }, |
| 174 | + }), |
| 175 | + }); |
| 176 | + |
| 177 | + expect(ctx).not.toBeNull(); |
| 178 | + expect(ctx?.ctxPayload?.SessionKey).toContain("agent:main:"); |
| 179 | + }); |
| 180 | + |
123 | 181 | it("routes DM topic to specific agent when agentId is set", async () => { |
124 | 182 | const ctx = await buildTelegramMessageContextForTest({ |
125 | 183 | message: { |
|
0 commit comments