diff --git a/src/process/command-queue.test.ts b/src/process/command-queue.test.ts index 3f1edfeb07..aade88765e 100644 --- a/src/process/command-queue.test.ts +++ b/src/process/command-queue.test.ts @@ -57,16 +57,23 @@ describe("command queue", () => { return id; }; - const results = await Promise.all([ - enqueueCommand(makeTask(1)), - enqueueCommand(makeTask(2)), - enqueueCommand(makeTask(3)), - ]); + vi.useFakeTimers(); + try { + const resultsPromise = Promise.all([ + enqueueCommand(makeTask(1)), + enqueueCommand(makeTask(2)), + enqueueCommand(makeTask(3)), + ]); + await vi.advanceTimersByTimeAsync(20); + const results = await resultsPromise; - expect(results).toEqual([1, 2, 3]); - expect(calls).toEqual([1, 2, 3]); - expect(maxActive).toBe(1); - expect(getQueueSize()).toBe(0); + expect(results).toEqual([1, 2, 3]); + expect(calls).toEqual([1, 2, 3]); + expect(maxActive).toBe(1); + expect(getQueueSize()).toBe(0); + } finally { + vi.useRealTimers(); + } }); it("logs enqueue depth after push", async () => { @@ -82,24 +89,30 @@ describe("command queue", () => { let waited: number | null = null; let queuedAhead: number | null = null; - // First task holds the queue long enough to trigger wait notice. - const first = enqueueCommand(async () => { - await new Promise((resolve) => setTimeout(resolve, 8)); - }); + vi.useFakeTimers(); + try { + // First task holds the queue long enough to trigger wait notice. + const first = enqueueCommand(async () => { + await new Promise((resolve) => setTimeout(resolve, 8)); + }); - const second = enqueueCommand(async () => {}, { - warnAfterMs: 5, - onWait: (ms, ahead) => { - waited = ms; - queuedAhead = ahead; - }, - }); + const second = enqueueCommand(async () => {}, { + warnAfterMs: 5, + onWait: (ms, ahead) => { + waited = ms; + queuedAhead = ahead; + }, + }); - await Promise.all([first, second]); + await vi.advanceTimersByTimeAsync(30); + await Promise.all([first, second]); - expect(waited).not.toBeNull(); - expect(waited as unknown as number).toBeGreaterThanOrEqual(5); - expect(queuedAhead).toBe(0); + expect(waited).not.toBeNull(); + expect(waited as unknown as number).toBeGreaterThanOrEqual(5); + expect(queuedAhead).toBe(0); + } finally { + vi.useRealTimers(); + } }); it("getActiveTaskCount returns count of currently executing tasks", async () => { diff --git a/src/telegram/bot.create-telegram-bot.test.ts b/src/telegram/bot.create-telegram-bot.test.ts index 5cc2f2a40e..de15eaf780 100644 --- a/src/telegram/bot.create-telegram-bot.test.ts +++ b/src/telegram/bot.create-telegram-bot.test.ts @@ -1508,35 +1508,6 @@ describe("createTelegramBot", () => { ).toBeUndefined(); } }); - it("honors replyToMode=first for threaded replies across all chunks of the first reply", async () => { - onSpy.mockReset(); - sendMessageSpy.mockReset(); - replySpy.mockReset(); - replySpy.mockResolvedValue({ - text: "a".repeat(4500), - replyToId: "101", - }); - - createTelegramBot({ token: "tok", replyToMode: "first" }); - const handler = getOnHandler("message") as (ctx: Record) => Promise; - await handler({ - message: { - chat: { id: 5, type: "private" }, - text: "hi", - date: 1736380800, - message_id: 101, - }, - me: { username: "openclaw_bot" }, - getFile: async () => ({ download: async () => new Uint8Array() }), - }); - - expect(sendMessageSpy.mock.calls.length).toBeGreaterThan(1); - for (const call of sendMessageSpy.mock.calls) { - expect((call[2] as { reply_to_message_id?: number } | undefined)?.reply_to_message_id).toBe( - 101, - ); - } - }); it("prefixes final replies with responsePrefix", async () => { onSpy.mockReset(); sendMessageSpy.mockReset(); @@ -1564,33 +1535,38 @@ describe("createTelegramBot", () => { expect(sendMessageSpy).toHaveBeenCalledTimes(1); expect(sendMessageSpy.mock.calls[0][1]).toBe("PFX final reply"); }); - it("honors replyToMode=all for threaded replies", async () => { - onSpy.mockReset(); - sendMessageSpy.mockReset(); - replySpy.mockReset(); - replySpy.mockResolvedValue({ - text: "a".repeat(4500), - replyToId: "101", - }); + it("honors threaded replies for replyToMode=first/all", async () => { + for (const [mode, messageId] of [ + ["first", 101], + ["all", 102], + ] as const) { + onSpy.mockReset(); + sendMessageSpy.mockReset(); + replySpy.mockReset(); + replySpy.mockResolvedValue({ + text: "a".repeat(4500), + replyToId: String(messageId), + }); - createTelegramBot({ token: "tok", replyToMode: "all" }); - const handler = getOnHandler("message") as (ctx: Record) => Promise; - await handler({ - message: { - chat: { id: 5, type: "private" }, - text: "hi", - date: 1736380800, - message_id: 101, - }, - me: { username: "openclaw_bot" }, - getFile: async () => ({ download: async () => new Uint8Array() }), - }); + createTelegramBot({ token: "tok", replyToMode: mode }); + const handler = getOnHandler("message") as (ctx: Record) => Promise; + await handler({ + message: { + chat: { id: 5, type: "private" }, + text: "hi", + date: 1736380800, + message_id: messageId, + }, + me: { username: "openclaw_bot" }, + getFile: async () => ({ download: async () => new Uint8Array() }), + }); - expect(sendMessageSpy.mock.calls.length).toBeGreaterThan(1); - for (const call of sendMessageSpy.mock.calls) { - expect((call[2] as { reply_to_message_id?: number } | undefined)?.reply_to_message_id).toBe( - 101, - ); + expect(sendMessageSpy.mock.calls.length).toBeGreaterThan(1); + for (const call of sendMessageSpy.mock.calls) { + expect((call[2] as { reply_to_message_id?: number } | undefined)?.reply_to_message_id).toBe( + messageId, + ); + } } }); it("blocks group messages when telegram.groups is set without a wildcard", async () => {