diff --git a/src/telegram/bot/helpers.test.ts b/src/telegram/bot/helpers.test.ts index a03ad33443f..f8842004fb5 100644 --- a/src/telegram/bot/helpers.test.ts +++ b/src/telegram/bot/helpers.test.ts @@ -5,6 +5,7 @@ import { expandTextLinks, normalizeForwardedContext, resolveTelegramForumThreadId, + resolveTelegramThreadSpec, } from "./helpers.js"; describe("resolveTelegramForumThreadId", () => { @@ -32,6 +33,34 @@ describe("resolveTelegramForumThreadId", () => { }); }); +describe("resolveTelegramThreadSpec", () => { + it("returns dm scope for plain DM (no forum, no thread id)", () => { + expect(resolveTelegramThreadSpec({ isGroup: false })).toEqual({ scope: "dm" }); + }); + + it("preserves thread id with dm scope when DM has thread id but is not a forum", () => { + expect( + resolveTelegramThreadSpec({ isGroup: false, isForum: false, messageThreadId: 42 }), + ).toEqual({ id: 42, scope: "dm" }); + }); + + it("returns forum scope when DM has isForum and thread id", () => { + expect( + resolveTelegramThreadSpec({ isGroup: false, isForum: true, messageThreadId: 99 }), + ).toEqual({ id: 99, scope: "forum" }); + }); + + it("falls back to dm scope when DM has isForum but no thread id", () => { + expect(resolveTelegramThreadSpec({ isGroup: false, isForum: true })).toEqual({ scope: "dm" }); + }); + + it("delegates to group path for groups", () => { + expect( + resolveTelegramThreadSpec({ isGroup: true, isForum: true, messageThreadId: 50 }), + ).toEqual({ id: 50, scope: "forum" }); + }); +}); + describe("buildTelegramThreadParams", () => { it("omits General topic thread id for message sends", () => { expect(buildTelegramThreadParams({ id: 1, scope: "forum" })).toBeUndefined(); diff --git a/src/telegram/bot/helpers.ts b/src/telegram/bot/helpers.ts index 25c081e603a..1a983344212 100644 --- a/src/telegram/bot/helpers.ts +++ b/src/telegram/bot/helpers.ts @@ -101,13 +101,15 @@ export function resolveTelegramThreadSpec(params: { scope: params.isForum ? "forum" : "none", }; } - if (params.messageThreadId == null) { - return { scope: "dm" }; + // DM with forum/topics enabled — treat like a forum, not a flat DM + if (params.isForum && params.messageThreadId != null) { + return { id: params.messageThreadId, scope: "forum" }; } - return { - id: params.messageThreadId, - scope: "dm", - }; + // Preserve thread ID for non-forum DM threads (session routing, #8891) + if (params.messageThreadId != null) { + return { id: params.messageThreadId, scope: "dm" }; + } + return { scope: "dm" }; } /**