mirror of https://github.com/openclaw/openclaw.git
test(agents): dedupe tool-result overflow and telegram account helpers
This commit is contained in:
parent
1437ed76a0
commit
2d0ce40ed6
|
|
@ -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[];
|
||||
|
|
|
|||
|
|
@ -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 () => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue