diff --git a/src/agents/session-tool-result-guard.tool-result-persist-hook.test.ts b/src/agents/session-tool-result-guard.tool-result-persist-hook.test.ts index 3cce8a8fe60..27770876e3f 100644 --- a/src/agents/session-tool-result-guard.tool-result-persist-hook.test.ts +++ b/src/agents/session-tool-result-guard.tool-result-persist-hook.test.ts @@ -59,6 +59,35 @@ function getPersistedToolResult(sm: ReturnType) return messages.find((m) => (m as any).role === "toolResult") as any; } +function initializeTempPlugin(params: { tmpPrefix: string; id: string; body: string }) { + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), params.tmpPrefix)); + process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins"; + const plugin = writeTempPlugin({ + dir: tmp, + id: params.id, + body: params.body, + }); + const registry = loadOpenClawPlugins({ + cache: false, + workspaceDir: tmp, + config: { + plugins: { + load: { paths: [plugin] }, + allow: [params.id], + }, + }, + }); + initializeGlobalHookRunner(registry); +} + +function expectPersistedToolResultTextCapped(sm: ReturnType) { + const toolResult = getPersistedToolResult(sm); + const text = toolResult.content.find((block: { type: string }) => block.type === "text")?.text; + expect(typeof text).toBe("string"); + expect(text.length).toBeLessThanOrEqual(120); + expect(text).toContain("truncated"); +} + afterEach(() => { resetGlobalHookRunner(); if (originalBundledPluginsDir === undefined) { @@ -136,11 +165,8 @@ describe("tool_result_persist hook", () => { }); it("reapplies the cap after tool_result_persist expands a tool result", () => { - const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-toolpersist-expand-")); - process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins"; - - const plugin = writeTempPlugin({ - dir: tmp, + initializeTempPlugin({ + tmpPrefix: "openclaw-toolpersist-expand-", id: "persist-expand", body: `export default { id: "persist-expand", register(api) { api.on("tool_result_persist", (event) => { @@ -150,22 +176,10 @@ describe("tool_result_persist hook", () => { content: [{ type: "text", text: "y".repeat(5000) }], }, }; - }, { priority: 10 }); -} };`, + }, { priority: 10 }); + } };`, }); - const registry = loadOpenClawPlugins({ - cache: false, - workspaceDir: tmp, - config: { - plugins: { - load: { paths: [plugin] }, - allow: ["persist-expand"], - }, - }, - }); - initializeGlobalHookRunner(registry); - const sm = guardSessionManager(SessionManager.inMemory(), { agentId: "main", sessionKey: "main", @@ -173,41 +187,22 @@ describe("tool_result_persist hook", () => { }); appendToolCallAndResult(sm); - const toolResult = getPersistedToolResult(sm); - const text = toolResult.content.find((block: { type: string }) => block.type === "text")?.text; - expect(typeof text).toBe("string"); - expect(text.length).toBeLessThanOrEqual(120); - expect(text).toContain("truncated"); + expectPersistedToolResultTextCapped(sm); }); }); describe("before_message_write hook", () => { it("continues persistence when a before_message_write hook throws", () => { - const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-before-write-")); - process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins"; - - const plugin = writeTempPlugin({ - dir: tmp, + initializeTempPlugin({ + tmpPrefix: "openclaw-before-write-", id: "before-write-throws", body: `export default { id: "before-write-throws", register(api) { api.on("before_message_write", () => { throw new Error("boom"); - }, { priority: 10 }); -} };`, + }, { priority: 10 }); + } };`, }); - const registry = loadOpenClawPlugins({ - cache: false, - workspaceDir: tmp, - config: { - plugins: { - load: { paths: [plugin] }, - allow: ["before-write-throws"], - }, - }, - }); - initializeGlobalHookRunner(registry); - const sm = guardSessionManager(SessionManager.inMemory(), { agentId: "main", sessionKey: "main", @@ -229,11 +224,8 @@ describe("before_message_write hook", () => { }); it("reapplies the cap after before_message_write expands a tool result", () => { - const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-before-write-expand-")); - process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins"; - - const plugin = writeTempPlugin({ - dir: tmp, + initializeTempPlugin({ + tmpPrefix: "openclaw-before-write-expand-", id: "before-write-expand", body: `export default { id: "before-write-expand", register(api) { api.on("before_message_write", (event) => { @@ -244,22 +236,10 @@ describe("before_message_write hook", () => { content: [{ type: "text", text: "z".repeat(5000) }], }, }; - }, { priority: 10 }); -} };`, + }, { priority: 10 }); + } };`, }); - const registry = loadOpenClawPlugins({ - cache: false, - workspaceDir: tmp, - config: { - plugins: { - load: { paths: [plugin] }, - allow: ["before-write-expand"], - }, - }, - }); - initializeGlobalHookRunner(registry); - const sm = guardSessionManager(SessionManager.inMemory(), { agentId: "main", sessionKey: "main", @@ -267,10 +247,6 @@ describe("before_message_write hook", () => { }); appendToolCallAndResult(sm); - const toolResult = getPersistedToolResult(sm); - const text = toolResult.content.find((block: { type: string }) => block.type === "text")?.text; - expect(typeof text).toBe("string"); - expect(text.length).toBeLessThanOrEqual(120); - expect(text).toContain("truncated"); + expectPersistedToolResultTextCapped(sm); }); });