|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { readMemoryHostEvents } from "openclaw/plugin-sdk/memory-host-events"; |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | +import { writeDailyDreamingPhaseBlock } from "./dreaming-markdown.js"; |
| 7 | +import { |
| 8 | + applyShortTermPromotions, |
| 9 | + rankShortTermPromotionCandidates, |
| 10 | + recordShortTermRecalls, |
| 11 | +} from "./short-term-promotion.js"; |
| 12 | + |
| 13 | +const tempDirs: string[] = []; |
| 14 | + |
| 15 | +afterEach(async () => { |
| 16 | + await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true }))); |
| 17 | +}); |
| 18 | + |
| 19 | +describe("memory host event journal integration", () => { |
| 20 | + it("records recall and promotion events from short-term promotion flows", async () => { |
| 21 | + const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "memory-core-events-")); |
| 22 | + tempDirs.push(workspaceDir); |
| 23 | + await fs.mkdir(path.join(workspaceDir, "memory"), { recursive: true }); |
| 24 | + await fs.writeFile( |
| 25 | + path.join(workspaceDir, "memory", "2026-04-05.md"), |
| 26 | + "# Daily\n\nalpha\nbeta\ngamma\n", |
| 27 | + "utf8", |
| 28 | + ); |
| 29 | + |
| 30 | + await recordShortTermRecalls({ |
| 31 | + workspaceDir, |
| 32 | + query: "alpha memory", |
| 33 | + results: [ |
| 34 | + { |
| 35 | + path: "memory/2026-04-05.md", |
| 36 | + startLine: 3, |
| 37 | + endLine: 4, |
| 38 | + score: 0.92, |
| 39 | + snippet: "alpha beta", |
| 40 | + source: "memory", |
| 41 | + }, |
| 42 | + ], |
| 43 | + nowMs: Date.UTC(2026, 3, 5, 12, 0, 0), |
| 44 | + }); |
| 45 | + |
| 46 | + const candidates = await rankShortTermPromotionCandidates({ |
| 47 | + workspaceDir, |
| 48 | + minScore: 0, |
| 49 | + minRecallCount: 0, |
| 50 | + minUniqueQueries: 0, |
| 51 | + nowMs: Date.UTC(2026, 3, 5, 12, 5, 0), |
| 52 | + }); |
| 53 | + const applied = await applyShortTermPromotions({ |
| 54 | + workspaceDir, |
| 55 | + candidates, |
| 56 | + minScore: 0, |
| 57 | + minRecallCount: 0, |
| 58 | + minUniqueQueries: 0, |
| 59 | + nowMs: Date.UTC(2026, 3, 5, 12, 10, 0), |
| 60 | + }); |
| 61 | + |
| 62 | + expect(applied.applied).toBe(1); |
| 63 | + |
| 64 | + const events = await readMemoryHostEvents({ workspaceDir }); |
| 65 | + |
| 66 | + expect(events.map((event) => event.type)).toEqual([ |
| 67 | + "memory.recall.recorded", |
| 68 | + "memory.promotion.applied", |
| 69 | + ]); |
| 70 | + expect(events[0]).toMatchObject({ |
| 71 | + type: "memory.recall.recorded", |
| 72 | + resultCount: 1, |
| 73 | + query: "alpha memory", |
| 74 | + }); |
| 75 | + expect(events[1]).toMatchObject({ |
| 76 | + type: "memory.promotion.applied", |
| 77 | + applied: 1, |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it("records dreaming completion events when phase artifacts are written", async () => { |
| 82 | + const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "memory-core-dream-events-")); |
| 83 | + tempDirs.push(workspaceDir); |
| 84 | + |
| 85 | + const written = await writeDailyDreamingPhaseBlock({ |
| 86 | + workspaceDir, |
| 87 | + phase: "light", |
| 88 | + bodyLines: ["- staged note", "- second note"], |
| 89 | + nowMs: Date.UTC(2026, 3, 5, 13, 0, 0), |
| 90 | + storage: { mode: "both", separateReports: true }, |
| 91 | + }); |
| 92 | + |
| 93 | + const events = await readMemoryHostEvents({ workspaceDir }); |
| 94 | + |
| 95 | + expect(written.inlinePath).toBeTruthy(); |
| 96 | + expect(written.reportPath).toBeTruthy(); |
| 97 | + expect(events).toHaveLength(1); |
| 98 | + expect(events[0]).toMatchObject({ |
| 99 | + type: "memory.dream.completed", |
| 100 | + phase: "light", |
| 101 | + lineCount: 2, |
| 102 | + storageMode: "both", |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments