mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 11:47:41 +00:00
test(agents): dedupe tool-result overflow and telegram account helpers
This commit is contained in:
@@ -72,25 +72,30 @@ function makeGuardableAgent(
|
|||||||
return { transformContext };
|
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", () => {
|
describe("installToolResultContextGuard", () => {
|
||||||
it("compacts oldest-first when total context overflows, even if each result fits individually", async () => {
|
it("compacts oldest-first when total context overflows, even if each result fits individually", async () => {
|
||||||
const agent = makeGuardableAgent();
|
const agent = makeGuardableAgent();
|
||||||
|
const contextForNextCall = makeTwoToolResultOverflowContext();
|
||||||
installToolResultContextGuard({
|
const transformed = await applyGuardToContext(agent, contextForNextCall);
|
||||||
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,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(transformed).toBe(contextForNextCall);
|
expect(transformed).toBe(contextForNextCall);
|
||||||
const oldResultText = getToolResultText(contextForNextCall[1]);
|
const oldResultText = getToolResultText(contextForNextCall[1]);
|
||||||
@@ -200,22 +205,8 @@ describe("installToolResultContextGuard", () => {
|
|||||||
}) as unknown as AgentMessage,
|
}) as unknown as AgentMessage,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
const contextForNextCall = makeTwoToolResultOverflowContext();
|
||||||
installToolResultContextGuard({
|
const transformed = await applyGuardToContext(agent, contextForNextCall);
|
||||||
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,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(transformed).not.toBe(contextForNextCall);
|
expect(transformed).not.toBe(contextForNextCall);
|
||||||
const transformedMessages = transformed as AgentMessage[];
|
const transformedMessages = transformed as AgentMessage[];
|
||||||
|
|||||||
@@ -595,6 +595,18 @@ describe("readTelegramButtons", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("handleTelegramAction per-account gating", () => {
|
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 () => {
|
it("allows sticker when account config enables it", async () => {
|
||||||
const cfg = {
|
const cfg = {
|
||||||
channels: {
|
channels: {
|
||||||
@@ -605,16 +617,7 @@ describe("handleTelegramAction per-account gating", () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} as OpenClawConfig;
|
} as OpenClawConfig;
|
||||||
|
await expectAccountStickerSend(cfg);
|
||||||
await handleTelegramAction(
|
|
||||||
{ action: "sendSticker", to: "123", fileId: "sticker-id", accountId: "media" },
|
|
||||||
cfg,
|
|
||||||
);
|
|
||||||
expect(sendStickerTelegram).toHaveBeenCalledWith(
|
|
||||||
"123",
|
|
||||||
"sticker-id",
|
|
||||||
expect.objectContaining({ token: "tok-media" }),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("blocks sticker when account omits it", async () => {
|
it("blocks sticker when account omits it", async () => {
|
||||||
@@ -648,16 +651,7 @@ describe("handleTelegramAction per-account gating", () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} as OpenClawConfig;
|
} as OpenClawConfig;
|
||||||
|
await expectAccountStickerSend(cfg);
|
||||||
await handleTelegramAction(
|
|
||||||
{ action: "sendSticker", to: "123", fileId: "sticker-id", accountId: "media" },
|
|
||||||
cfg,
|
|
||||||
);
|
|
||||||
expect(sendStickerTelegram).toHaveBeenCalledWith(
|
|
||||||
"123",
|
|
||||||
"sticker-id",
|
|
||||||
expect.objectContaining({ token: "tok-media" }),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("inherits top-level reaction gate when account overrides sticker only", async () => {
|
it("inherits top-level reaction gate when account overrides sticker only", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user