refactor(test): share telegram dm topic setup

This commit is contained in:
Peter Steinberger
2026-02-14 22:25:48 +00:00
parent 200aa441df
commit de43e884e7

View File

@@ -14,26 +14,25 @@ describe("buildTelegramMessageContext DM topic threadId in deliveryContext (#889
messages: { groupChat: { mentionPatterns: [] } }, messages: { groupChat: { mentionPatterns: [] } },
} as never; } as never;
beforeEach(() => { async function buildCtx(params: {
recordInboundSessionMock.mockClear(); message: Record<string, unknown>;
}); options?: Record<string, unknown>;
resolveGroupActivation?: () => unknown;
it("passes threadId to updateLastRoute for DM topics", async () => { }): Promise<Awaited<ReturnType<typeof buildTelegramMessageContext>>> {
const ctx = await buildTelegramMessageContext({ return await buildTelegramMessageContext({
primaryCtx: { primaryCtx: {
message: { message: {
message_id: 1, message_id: 1,
chat: { id: 1234, type: "private" },
date: 1700000000, date: 1700000000,
text: "hello", text: "hello",
message_thread_id: 42, // DM Topic ID
from: { id: 42, first_name: "Alice" }, from: { id: 42, first_name: "Alice" },
...params.message,
}, },
me: { id: 7, username: "bot" }, me: { id: 7, username: "bot" },
} as never, } as never,
allMedia: [], allMedia: [],
storeAllowFrom: [], storeAllowFrom: [],
options: {}, options: params.options ?? {},
bot: { bot: {
api: { api: {
sendChatAction: vi.fn(), sendChatAction: vi.fn(),
@@ -49,121 +48,72 @@ describe("buildTelegramMessageContext DM topic threadId in deliveryContext (#889
groupAllowFrom: [], groupAllowFrom: [],
ackReactionScope: "off", ackReactionScope: "off",
logger: { info: vi.fn() }, logger: { info: vi.fn() },
resolveGroupActivation: () => undefined, resolveGroupActivation: params.resolveGroupActivation ?? (() => undefined),
resolveGroupRequireMention: () => false, resolveGroupRequireMention: () => false,
resolveTelegramGroupConfig: () => ({ resolveTelegramGroupConfig: () => ({
groupConfig: { requireMention: false }, groupConfig: { requireMention: false },
topicConfig: undefined, topicConfig: undefined,
}), }),
}); });
}
function getUpdateLastRoute(): unknown {
const callArgs = recordInboundSessionMock.mock.calls[0]?.[0] as { updateLastRoute?: unknown };
return callArgs?.updateLastRoute;
}
beforeEach(() => {
recordInboundSessionMock.mockClear();
});
it("passes threadId to updateLastRoute for DM topics", async () => {
const ctx = await buildCtx({
message: {
chat: { id: 1234, type: "private" },
message_thread_id: 42, // DM Topic ID
},
});
expect(ctx).not.toBeNull(); expect(ctx).not.toBeNull();
expect(recordInboundSessionMock).toHaveBeenCalled(); expect(recordInboundSessionMock).toHaveBeenCalled();
// Check that updateLastRoute includes threadId // Check that updateLastRoute includes threadId
const callArgs = recordInboundSessionMock.mock.calls[0]?.[0] as { const updateLastRoute = getUpdateLastRoute() as { threadId?: string } | undefined;
updateLastRoute?: { threadId?: string }; expect(updateLastRoute).toBeDefined();
}; expect(updateLastRoute?.threadId).toBe("42");
expect(callArgs?.updateLastRoute).toBeDefined();
expect(callArgs?.updateLastRoute?.threadId).toBe("42");
}); });
it("does not pass threadId for regular DM without topic", async () => { it("does not pass threadId for regular DM without topic", async () => {
const ctx = await buildTelegramMessageContext({ const ctx = await buildCtx({
primaryCtx: { message: {
message: { chat: { id: 1234, type: "private" },
message_id: 1, },
chat: { id: 1234, type: "private" },
date: 1700000000,
text: "hello",
// No message_thread_id
from: { id: 42, first_name: "Alice" },
},
me: { id: 7, username: "bot" },
} as never,
allMedia: [],
storeAllowFrom: [],
options: {},
bot: {
api: {
sendChatAction: vi.fn(),
setMessageReaction: vi.fn(),
},
} as never,
cfg: baseConfig,
account: { accountId: "default" } as never,
historyLimit: 0,
groupHistories: new Map(),
dmPolicy: "open",
allowFrom: [],
groupAllowFrom: [],
ackReactionScope: "off",
logger: { info: vi.fn() },
resolveGroupActivation: () => undefined,
resolveGroupRequireMention: () => false,
resolveTelegramGroupConfig: () => ({
groupConfig: { requireMention: false },
topicConfig: undefined,
}),
}); });
expect(ctx).not.toBeNull(); expect(ctx).not.toBeNull();
expect(recordInboundSessionMock).toHaveBeenCalled(); expect(recordInboundSessionMock).toHaveBeenCalled();
// Check that updateLastRoute does NOT include threadId // Check that updateLastRoute does NOT include threadId
const callArgs = recordInboundSessionMock.mock.calls[0]?.[0] as { const updateLastRoute = getUpdateLastRoute() as { threadId?: string } | undefined;
updateLastRoute?: { threadId?: string }; expect(updateLastRoute).toBeDefined();
}; expect(updateLastRoute?.threadId).toBeUndefined();
expect(callArgs?.updateLastRoute).toBeDefined();
expect(callArgs?.updateLastRoute?.threadId).toBeUndefined();
}); });
it("does not set updateLastRoute for group messages", async () => { it("does not set updateLastRoute for group messages", async () => {
const ctx = await buildTelegramMessageContext({ const ctx = await buildCtx({
primaryCtx: { message: {
message: { chat: { id: -1001234567890, type: "supergroup", title: "Test Group" },
message_id: 1, text: "@bot hello",
chat: { id: -1001234567890, type: "supergroup", title: "Test Group" }, message_thread_id: 99,
date: 1700000000, },
text: "@bot hello",
message_thread_id: 99,
from: { id: 42, first_name: "Alice" },
},
me: { id: 7, username: "bot" },
} as never,
allMedia: [],
storeAllowFrom: [],
options: { forceWasMentioned: true }, options: { forceWasMentioned: true },
bot: {
api: {
sendChatAction: vi.fn(),
setMessageReaction: vi.fn(),
},
} as never,
cfg: baseConfig,
account: { accountId: "default" } as never,
historyLimit: 0,
groupHistories: new Map(),
dmPolicy: "open",
allowFrom: [],
groupAllowFrom: [],
ackReactionScope: "off",
logger: { info: vi.fn() },
resolveGroupActivation: () => true, resolveGroupActivation: () => true,
resolveGroupRequireMention: () => false,
resolveTelegramGroupConfig: () => ({
groupConfig: { requireMention: false },
topicConfig: undefined,
}),
}); });
expect(ctx).not.toBeNull(); expect(ctx).not.toBeNull();
expect(recordInboundSessionMock).toHaveBeenCalled(); expect(recordInboundSessionMock).toHaveBeenCalled();
// Check that updateLastRoute is undefined for groups // Check that updateLastRoute is undefined for groups
const callArgs = recordInboundSessionMock.mock.calls[0]?.[0] as { expect(getUpdateLastRoute()).toBeUndefined();
updateLastRoute?: unknown;
};
expect(callArgs?.updateLastRoute).toBeUndefined();
}); });
}); });