mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 03:51:25 +00:00
test(hooks): dedupe session-memory handler test setup
This commit is contained in:
@@ -94,6 +94,23 @@ async function runNewWithPreviousSession(params: {
|
|||||||
return { tempDir, files, memoryContent };
|
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", () => {
|
describe("session-memory hook", () => {
|
||||||
it("skips non-command events", async () => {
|
it("skips non-command events", async () => {
|
||||||
const tempDir = await makeTempWorkspace("openclaw-session-memory-");
|
const tempDir = await makeTempWorkspace("openclaw-session-memory-");
|
||||||
@@ -214,16 +231,7 @@ describe("session-memory hook", () => {
|
|||||||
const sessionContent = createMockSessionContent(entries);
|
const sessionContent = createMockSessionContent(entries);
|
||||||
const { memoryContent } = await runNewWithPreviousSession({
|
const { memoryContent } = await runNewWithPreviousSession({
|
||||||
sessionContent,
|
sessionContent,
|
||||||
cfg: (tempDir) => ({
|
cfg: (tempDir) => makeSessionMemoryConfig(tempDir, 3),
|
||||||
agents: { defaults: { workspace: tempDir } },
|
|
||||||
hooks: {
|
|
||||||
internal: {
|
|
||||||
entries: {
|
|
||||||
"session-memory": { enabled: true, messages: 3 },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Only last 3 messages should be present
|
// Only last 3 messages should be present
|
||||||
@@ -252,16 +260,7 @@ describe("session-memory hook", () => {
|
|||||||
const sessionContent = createMockSessionContent(entries);
|
const sessionContent = createMockSessionContent(entries);
|
||||||
const { memoryContent } = await runNewWithPreviousSession({
|
const { memoryContent } = await runNewWithPreviousSession({
|
||||||
sessionContent,
|
sessionContent,
|
||||||
cfg: (tempDir) => ({
|
cfg: (tempDir) => makeSessionMemoryConfig(tempDir, 3),
|
||||||
agents: { defaults: { workspace: tempDir } },
|
|
||||||
hooks: {
|
|
||||||
internal: {
|
|
||||||
entries: {
|
|
||||||
"session-memory": { enabled: true, messages: 3 },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Should have exactly 3 user/assistant messages (the last 3)
|
// Should have exactly 3 user/assistant messages (the last 3)
|
||||||
@@ -320,24 +319,15 @@ describe("session-memory hook", () => {
|
|||||||
]),
|
]),
|
||||||
});
|
});
|
||||||
|
|
||||||
const cfg = {
|
const { files, memoryContent } = await runNewWithPreviousSessionEntry({
|
||||||
agents: { defaults: { workspace: tempDir } },
|
tempDir,
|
||||||
} satisfies OpenClawConfig;
|
cfg: makeSessionMemoryConfig(tempDir),
|
||||||
|
|
||||||
const event = createHookEvent("command", "new", "agent:main:main", {
|
|
||||||
cfg,
|
|
||||||
previousSessionEntry: {
|
previousSessionEntry: {
|
||||||
sessionId,
|
sessionId,
|
||||||
sessionFile: resetSessionFile,
|
sessionFile: resetSessionFile,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await handler(event);
|
|
||||||
|
|
||||||
const memoryDir = path.join(tempDir, "memory");
|
|
||||||
const files = await fs.readdir(memoryDir);
|
|
||||||
expect(files.length).toBe(1);
|
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("user: Message from reset pointer");
|
||||||
expect(memoryContent).toContain("assistant: Recovered directly from reset file");
|
expect(memoryContent).toContain("assistant: Recovered directly from reset file");
|
||||||
@@ -363,23 +353,14 @@ describe("session-memory hook", () => {
|
|||||||
]),
|
]),
|
||||||
});
|
});
|
||||||
|
|
||||||
const cfg = {
|
const { files, memoryContent } = await runNewWithPreviousSessionEntry({
|
||||||
agents: { defaults: { workspace: tempDir } },
|
tempDir,
|
||||||
} satisfies OpenClawConfig;
|
cfg: makeSessionMemoryConfig(tempDir),
|
||||||
|
|
||||||
const event = createHookEvent("command", "new", "agent:main:main", {
|
|
||||||
cfg,
|
|
||||||
previousSessionEntry: {
|
previousSessionEntry: {
|
||||||
sessionId,
|
sessionId,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await handler(event);
|
|
||||||
|
|
||||||
const memoryDir = path.join(tempDir, "memory");
|
|
||||||
const files = await fs.readdir(memoryDir);
|
|
||||||
expect(files.length).toBe(1);
|
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("user: Recovered with missing sessionFile pointer");
|
||||||
expect(memoryContent).toContain("assistant: Recovered by sessionId fallback");
|
expect(memoryContent).toContain("assistant: Recovered by sessionId fallback");
|
||||||
|
|||||||
Reference in New Issue
Block a user