fix(telegram): sequence runner updates and cap concurrency

This commit is contained in:
Peter Steinberger
2026-01-07 21:55:52 +01:00
parent 315b0938e3
commit 068b1872fa
6 changed files with 127 additions and 9 deletions

View File

@@ -40,6 +40,7 @@ vi.mock("./pairing-store.js", () => ({
}));
const useSpy = vi.fn();
const middlewareUseSpy = vi.fn();
const onSpy = vi.fn();
const stopSpy = vi.fn();
const commandSpy = vi.fn();
@@ -71,6 +72,7 @@ const apiStub: ApiStub = {
vi.mock("grammy", () => ({
Bot: class {
api = apiStub;
use = middlewareUseSpy;
on = onSpy;
stop = stopSpy;
command = commandSpy;
@@ -80,6 +82,16 @@ vi.mock("grammy", () => ({
webhookCallback: vi.fn(),
}));
const sequentializeMiddleware = vi.fn();
const sequentializeSpy = vi.fn(() => sequentializeMiddleware);
let sequentializeKey: ((ctx: unknown) => string) | undefined;
vi.mock("@grammyjs/runner", () => ({
sequentialize: (keyFn: (ctx: unknown) => string) => {
sequentializeKey = keyFn;
return sequentializeSpy();
},
}));
const throttlerSpy = vi.fn(() => "throttler");
vi.mock("@grammyjs/transformer-throttler", () => ({
@@ -104,6 +116,9 @@ describe("createTelegramBot", () => {
sendPhotoSpy.mockReset();
setMessageReactionSpy.mockReset();
setMyCommandsSpy.mockReset();
middlewareUseSpy.mockReset();
sequentializeSpy.mockReset();
sequentializeKey = undefined;
});
it("installs grammY throttler", () => {
@@ -112,6 +127,30 @@ describe("createTelegramBot", () => {
expect(useSpy).toHaveBeenCalledWith("throttler");
});
it("sequentializes updates by chat and thread", () => {
createTelegramBot({ token: "tok" });
expect(sequentializeSpy).toHaveBeenCalledTimes(1);
expect(middlewareUseSpy).toHaveBeenCalledWith(
sequentializeSpy.mock.results[0]?.value,
);
expect(sequentializeKey).toBeDefined();
expect(
sequentializeKey?.({
message: { chat: { id: 123 } },
}),
).toBe("telegram:123");
expect(
sequentializeKey?.({
message: { chat: { id: 123 }, message_thread_id: 9 },
}),
).toBe("telegram:123:topic:9");
expect(
sequentializeKey?.({
update: { message: { chat: { id: 555 } } },
}),
).toBe("telegram:555");
});
it("wraps inbound message with Telegram envelope", async () => {
const originalTz = process.env.TZ;
process.env.TZ = "Europe/Vienna";