diff --git a/src/hooks/bundled/session-memory/handler.test.ts b/src/hooks/bundled/session-memory/handler.test.ts index e93476937..57d300f82 100644 --- a/src/hooks/bundled/session-memory/handler.test.ts +++ b/src/hooks/bundled/session-memory/handler.test.ts @@ -94,6 +94,23 @@ async function runNewWithPreviousSession(params: { return { tempDir, files, memoryContent }; } +function makeSessionMemoryConfig(tempDir: string, messages?: number): OpenClawConfig { + return { + agents: { defaults: { workspace: tempDir } }, + ...(typeof messages === "number" + ? { + hooks: { + internal: { + entries: { + "session-memory": { enabled: true, messages }, + }, + }, + }, + } + : {}), + } satisfies OpenClawConfig; +} + describe("session-memory hook", () => { it("skips non-command events", async () => { const tempDir = await makeTempWorkspace("openclaw-session-memory-"); @@ -214,16 +231,7 @@ describe("session-memory hook", () => { const sessionContent = createMockSessionContent(entries); const { memoryContent } = await runNewWithPreviousSession({ sessionContent, - cfg: (tempDir) => ({ - agents: { defaults: { workspace: tempDir } }, - hooks: { - internal: { - entries: { - "session-memory": { enabled: true, messages: 3 }, - }, - }, - }, - }), + cfg: (tempDir) => makeSessionMemoryConfig(tempDir, 3), }); // Only last 3 messages should be present @@ -252,16 +260,7 @@ describe("session-memory hook", () => { const sessionContent = createMockSessionContent(entries); const { memoryContent } = await runNewWithPreviousSession({ sessionContent, - cfg: (tempDir) => ({ - agents: { defaults: { workspace: tempDir } }, - hooks: { - internal: { - entries: { - "session-memory": { enabled: true, messages: 3 }, - }, - }, - }, - }), + cfg: (tempDir) => makeSessionMemoryConfig(tempDir, 3), }); // Should have exactly 3 user/assistant messages (the last 3) @@ -320,24 +319,15 @@ describe("session-memory hook", () => { ]), }); - const cfg = { - agents: { defaults: { workspace: tempDir } }, - } satisfies OpenClawConfig; - - const event = createHookEvent("command", "new", "agent:main:main", { - cfg, + const { files, memoryContent } = await runNewWithPreviousSessionEntry({ + tempDir, + cfg: makeSessionMemoryConfig(tempDir), previousSessionEntry: { sessionId, sessionFile: resetSessionFile, }, }); - - await handler(event); - - const memoryDir = path.join(tempDir, "memory"); - const files = await fs.readdir(memoryDir); expect(files.length).toBe(1); - const memoryContent = await fs.readFile(path.join(memoryDir, files[0]), "utf-8"); expect(memoryContent).toContain("user: Message from reset pointer"); expect(memoryContent).toContain("assistant: Recovered directly from reset file"); @@ -363,23 +353,14 @@ describe("session-memory hook", () => { ]), }); - const cfg = { - agents: { defaults: { workspace: tempDir } }, - } satisfies OpenClawConfig; - - const event = createHookEvent("command", "new", "agent:main:main", { - cfg, + const { files, memoryContent } = await runNewWithPreviousSessionEntry({ + tempDir, + cfg: makeSessionMemoryConfig(tempDir), previousSessionEntry: { sessionId, }, }); - - await handler(event); - - const memoryDir = path.join(tempDir, "memory"); - const files = await fs.readdir(memoryDir); expect(files.length).toBe(1); - const memoryContent = await fs.readFile(path.join(memoryDir, files[0]), "utf-8"); expect(memoryContent).toContain("user: Recovered with missing sessionFile pointer"); expect(memoryContent).toContain("assistant: Recovered by sessionId fallback");