mirror of https://github.com/openclaw/openclaw.git
test(memory-core): reuse narrative workspace helper
This commit is contained in:
parent
b0f11f4eef
commit
f02f16db01
|
|
@ -1,7 +1,6 @@
|
|||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
appendNarrativeEntry,
|
||||
buildDiaryEntry,
|
||||
|
|
@ -11,18 +10,9 @@ import {
|
|||
generateAndAppendDreamNarrative,
|
||||
type NarrativePhaseData,
|
||||
} from "./dreaming-narrative.js";
|
||||
import { createMemoryCoreTestHarness } from "./test-helpers.js";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
async function createTempWorkspace(): Promise<string> {
|
||||
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-dreaming-narrative-"));
|
||||
tempDirs.push(workspaceDir);
|
||||
return workspaceDir;
|
||||
}
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
|
||||
});
|
||||
const { createTempWorkspace } = createMemoryCoreTestHarness();
|
||||
|
||||
describe("buildNarrativePrompt", () => {
|
||||
it("builds a prompt from snippets only", () => {
|
||||
|
|
@ -129,7 +119,7 @@ describe("buildDiaryEntry", () => {
|
|||
|
||||
describe("appendNarrativeEntry", () => {
|
||||
it("creates DREAMS.md with diary header on fresh workspace", async () => {
|
||||
const workspaceDir = await createTempWorkspace();
|
||||
const workspaceDir = await createTempWorkspace("openclaw-dreaming-narrative-");
|
||||
const dreamsPath = await appendNarrativeEntry({
|
||||
workspaceDir,
|
||||
narrative: "Fragments of authentication logic kept surfacing.",
|
||||
|
|
@ -145,7 +135,7 @@ describe("appendNarrativeEntry", () => {
|
|||
});
|
||||
|
||||
it("appends a second entry within the diary markers", async () => {
|
||||
const workspaceDir = await createTempWorkspace();
|
||||
const workspaceDir = await createTempWorkspace("openclaw-dreaming-narrative-");
|
||||
await appendNarrativeEntry({
|
||||
workspaceDir,
|
||||
narrative: "First dream.",
|
||||
|
|
@ -172,7 +162,7 @@ describe("appendNarrativeEntry", () => {
|
|||
});
|
||||
|
||||
it("prepends diary before existing managed blocks", async () => {
|
||||
const workspaceDir = await createTempWorkspace();
|
||||
const workspaceDir = await createTempWorkspace("openclaw-dreaming-narrative-");
|
||||
const dreamsPath = path.join(workspaceDir, "DREAMS.md");
|
||||
await fs.writeFile(
|
||||
dreamsPath,
|
||||
|
|
@ -194,7 +184,7 @@ describe("appendNarrativeEntry", () => {
|
|||
});
|
||||
|
||||
it("reuses existing dreams file when present", async () => {
|
||||
const workspaceDir = await createTempWorkspace();
|
||||
const workspaceDir = await createTempWorkspace("openclaw-dreaming-narrative-");
|
||||
const dreamsPath = path.join(workspaceDir, "DREAMS.md");
|
||||
await fs.writeFile(dreamsPath, "# Existing\n", "utf-8");
|
||||
const result = await appendNarrativeEntry({
|
||||
|
|
@ -235,7 +225,7 @@ describe("generateAndAppendDreamNarrative", () => {
|
|||
}
|
||||
|
||||
it("generates narrative and writes diary entry", async () => {
|
||||
const workspaceDir = await createTempWorkspace();
|
||||
const workspaceDir = await createTempWorkspace("openclaw-dreaming-narrative-");
|
||||
const subagent = createMockSubagent("The repository whispered of forgotten endpoints.");
|
||||
const logger = createMockLogger();
|
||||
|
||||
|
|
@ -264,7 +254,7 @@ describe("generateAndAppendDreamNarrative", () => {
|
|||
});
|
||||
|
||||
it("skips narrative when no snippets are available", async () => {
|
||||
const workspaceDir = await createTempWorkspace();
|
||||
const workspaceDir = await createTempWorkspace("openclaw-dreaming-narrative-");
|
||||
const subagent = createMockSubagent("Should not appear.");
|
||||
const logger = createMockLogger();
|
||||
|
||||
|
|
@ -284,7 +274,7 @@ describe("generateAndAppendDreamNarrative", () => {
|
|||
});
|
||||
|
||||
it("handles subagent timeout gracefully", async () => {
|
||||
const workspaceDir = await createTempWorkspace();
|
||||
const workspaceDir = await createTempWorkspace("openclaw-dreaming-narrative-");
|
||||
const subagent = createMockSubagent("");
|
||||
subagent.waitForRun.mockResolvedValue({ status: "timeout" });
|
||||
const logger = createMockLogger();
|
||||
|
|
@ -306,7 +296,7 @@ describe("generateAndAppendDreamNarrative", () => {
|
|||
});
|
||||
|
||||
it("handles subagent error gracefully", async () => {
|
||||
const workspaceDir = await createTempWorkspace();
|
||||
const workspaceDir = await createTempWorkspace("openclaw-dreaming-narrative-");
|
||||
const subagent = createMockSubagent("");
|
||||
subagent.run.mockRejectedValue(new Error("connection failed"));
|
||||
const logger = createMockLogger();
|
||||
|
|
@ -323,7 +313,7 @@ describe("generateAndAppendDreamNarrative", () => {
|
|||
});
|
||||
|
||||
it("cleans up session even on failure", async () => {
|
||||
const workspaceDir = await createTempWorkspace();
|
||||
const workspaceDir = await createTempWorkspace("openclaw-dreaming-narrative-");
|
||||
const subagent = createMockSubagent("");
|
||||
subagent.getSessionMessages.mockRejectedValue(new Error("fetch failed"));
|
||||
const logger = createMockLogger();
|
||||
|
|
|
|||
Loading…
Reference in New Issue