From e1ecfb25b8376e434e3d7402b1cedcfa81979977 Mon Sep 17 00:00:00 2001 From: Clawdbot Date: Tue, 27 Jan 2026 09:43:38 +0100 Subject: [PATCH] test: add tests for onToolResult in DM vs group sessions - provides onToolResult in DM sessions (ChatType=direct) - does not provide onToolResult in group sessions (ChatType=group) - sends tool results via dispatcher in DM sessions Replaces the old cross-provider test that expected onToolResult to always be undefined. --- .../reply/dispatch-from-config.test.ts | 65 ++++++++++++++++--- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/src/auto-reply/reply/dispatch-from-config.test.ts b/src/auto-reply/reply/dispatch-from-config.test.ts index c080ef55f..8f450cb89 100644 --- a/src/auto-reply/reply/dispatch-from-config.test.ts +++ b/src/auto-reply/reply/dispatch-from-config.test.ts @@ -138,7 +138,7 @@ describe("dispatchReplyFromConfig", () => { ); }); - it("does not provide onToolResult when routing cross-provider", async () => { + it("provides onToolResult in DM sessions", async () => { mocks.tryFastAbortFromMessage.mockResolvedValue({ handled: false, aborted: false, @@ -147,9 +147,34 @@ describe("dispatchReplyFromConfig", () => { const cfg = {} as MoltbotConfig; const dispatcher = createDispatcher(); const ctx = buildTestCtx({ - Provider: "slack", - OriginatingChannel: "telegram", - OriginatingTo: "telegram:999", + Provider: "telegram", + ChatType: "direct", + }); + + const replyResolver = async ( + _ctx: MsgContext, + opts: GetReplyOptions | undefined, + _cfg: ClawdbotConfig, + ) => { + expect(opts?.onToolResult).toBeDefined(); + expect(typeof opts?.onToolResult).toBe("function"); + return { text: "hi" } satisfies ReplyPayload; + }; + + await dispatchReplyFromConfig({ ctx, cfg, dispatcher, replyResolver }); + expect(dispatcher.sendFinalReply).toHaveBeenCalledTimes(1); + }); + + it("does not provide onToolResult in group sessions", async () => { + mocks.tryFastAbortFromMessage.mockResolvedValue({ + handled: false, + aborted: false, + }); + const cfg = {} as ClawdbotConfig; + const dispatcher = createDispatcher(); + const ctx = buildTestCtx({ + Provider: "telegram", + ChatType: "group", }); const replyResolver = async ( @@ -162,12 +187,36 @@ describe("dispatchReplyFromConfig", () => { }; await dispatchReplyFromConfig({ ctx, cfg, dispatcher, replyResolver }); + expect(dispatcher.sendFinalReply).toHaveBeenCalledTimes(1); + }); - expect(mocks.routeReply).toHaveBeenCalledWith( - expect.objectContaining({ - payload: expect.objectContaining({ text: "hi" }), - }), + it("sends tool results via dispatcher in DM sessions", async () => { + mocks.tryFastAbortFromMessage.mockResolvedValue({ + handled: false, + aborted: false, + }); + const cfg = {} as ClawdbotConfig; + const dispatcher = createDispatcher(); + const ctx = buildTestCtx({ + Provider: "telegram", + ChatType: "direct", + }); + + const replyResolver = async ( + _ctx: MsgContext, + opts: GetReplyOptions | undefined, + _cfg: ClawdbotConfig, + ) => { + // Simulate tool result emission + await opts?.onToolResult?.({ text: "🔧 exec: ls" }); + return { text: "done" } satisfies ReplyPayload; + }; + + await dispatchReplyFromConfig({ ctx, cfg, dispatcher, replyResolver }); + expect(dispatcher.sendToolResult).toHaveBeenCalledWith( + expect.objectContaining({ text: "🔧 exec: ls" }), ); + expect(dispatcher.sendFinalReply).toHaveBeenCalledTimes(1); }); it("fast-aborts without calling the reply resolver", async () => {