From 2d0ce40ed6c46667ded95aee455e9a16a54b8ed0 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 18 Feb 2026 13:22:29 +0000 Subject: [PATCH] test(agents): dedupe tool-result overflow and telegram account helpers --- .../tool-result-context-guard.e2e.test.ts | 55 ++++++++----------- src/agents/tools/telegram-actions.e2e.test.ts | 34 +++++------- 2 files changed, 37 insertions(+), 52 deletions(-) diff --git a/src/agents/pi-embedded-runner/tool-result-context-guard.e2e.test.ts b/src/agents/pi-embedded-runner/tool-result-context-guard.e2e.test.ts index 6b614b4726a..00915be4484 100644 --- a/src/agents/pi-embedded-runner/tool-result-context-guard.e2e.test.ts +++ b/src/agents/pi-embedded-runner/tool-result-context-guard.e2e.test.ts @@ -72,25 +72,30 @@ function makeGuardableAgent( return { transformContext }; } +function makeTwoToolResultOverflowContext(): AgentMessage[] { + return [ + makeUser("u".repeat(2_000)), + makeToolResult("call_old", "x".repeat(1_000)), + makeToolResult("call_new", "y".repeat(1_000)), + ]; +} + +async function applyGuardToContext( + agent: { transformContext?: (messages: AgentMessage[], signal: AbortSignal) => unknown }, + contextForNextCall: AgentMessage[], +) { + installToolResultContextGuard({ + agent, + contextWindowTokens: 1_000, + }); + return await agent.transformContext?.(contextForNextCall, new AbortController().signal); +} + describe("installToolResultContextGuard", () => { it("compacts oldest-first when total context overflows, even if each result fits individually", async () => { const agent = makeGuardableAgent(); - - installToolResultContextGuard({ - agent, - contextWindowTokens: 1_000, - }); - - const contextForNextCall = [ - makeUser("u".repeat(2_000)), - makeToolResult("call_old", "x".repeat(1_000)), - makeToolResult("call_new", "y".repeat(1_000)), - ]; - - const transformed = await agent.transformContext?.( - contextForNextCall, - new AbortController().signal, - ); + const contextForNextCall = makeTwoToolResultOverflowContext(); + const transformed = await applyGuardToContext(agent, contextForNextCall); expect(transformed).toBe(contextForNextCall); const oldResultText = getToolResultText(contextForNextCall[1]); @@ -200,22 +205,8 @@ describe("installToolResultContextGuard", () => { }) as unknown as AgentMessage, ); }); - - installToolResultContextGuard({ - agent, - contextWindowTokens: 1_000, - }); - - const contextForNextCall = [ - makeUser("u".repeat(2_000)), - makeToolResult("call_old", "x".repeat(1_000)), - makeToolResult("call_new", "y".repeat(1_000)), - ]; - - const transformed = await agent.transformContext?.( - contextForNextCall, - new AbortController().signal, - ); + const contextForNextCall = makeTwoToolResultOverflowContext(); + const transformed = await applyGuardToContext(agent, contextForNextCall); expect(transformed).not.toBe(contextForNextCall); const transformedMessages = transformed as AgentMessage[]; diff --git a/src/agents/tools/telegram-actions.e2e.test.ts b/src/agents/tools/telegram-actions.e2e.test.ts index 326dba18683..c4e26f403c3 100644 --- a/src/agents/tools/telegram-actions.e2e.test.ts +++ b/src/agents/tools/telegram-actions.e2e.test.ts @@ -595,6 +595,18 @@ describe("readTelegramButtons", () => { }); describe("handleTelegramAction per-account gating", () => { + async function expectAccountStickerSend(cfg: OpenClawConfig, accountId = "media") { + await handleTelegramAction( + { action: "sendSticker", to: "123", fileId: "sticker-id", accountId }, + cfg, + ); + expect(sendStickerTelegram).toHaveBeenCalledWith( + "123", + "sticker-id", + expect.objectContaining({ token: "tok-media" }), + ); + } + it("allows sticker when account config enables it", async () => { const cfg = { channels: { @@ -605,16 +617,7 @@ describe("handleTelegramAction per-account gating", () => { }, }, } as OpenClawConfig; - - await handleTelegramAction( - { action: "sendSticker", to: "123", fileId: "sticker-id", accountId: "media" }, - cfg, - ); - expect(sendStickerTelegram).toHaveBeenCalledWith( - "123", - "sticker-id", - expect.objectContaining({ token: "tok-media" }), - ); + await expectAccountStickerSend(cfg); }); it("blocks sticker when account omits it", async () => { @@ -648,16 +651,7 @@ describe("handleTelegramAction per-account gating", () => { }, }, } as OpenClawConfig; - - await handleTelegramAction( - { action: "sendSticker", to: "123", fileId: "sticker-id", accountId: "media" }, - cfg, - ); - expect(sendStickerTelegram).toHaveBeenCalledWith( - "123", - "sticker-id", - expect.objectContaining({ token: "tok-media" }), - ); + await expectAccountStickerSend(cfg); }); it("inherits top-level reaction gate when account overrides sticker only", async () => {