Files
openclaw/extensions/memory-wiki/src/source-sync.test.ts
2026-04-06 23:45:18 +01:00

98 lines
3.1 KiB
TypeScript

import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { syncMemoryWikiImportedSources } from "./source-sync.js";
import { createMemoryWikiTestHarness } from "./test-helpers.js";
const { createVault } = createMemoryWikiTestHarness();
describe("syncMemoryWikiImportedSources", () => {
let suiteRoot = "";
let caseId = 0;
beforeAll(async () => {
suiteRoot = await fs.mkdtemp(path.join(os.tmpdir(), "memory-wiki-source-sync-suite-"));
});
afterAll(async () => {
if (suiteRoot) {
await fs.rm(suiteRoot, { recursive: true, force: true });
}
});
function nextCaseRoot() {
return path.join(suiteRoot, `case-${caseId++}`);
}
it("refreshes indexes when imported sources change and skips when they do not", async () => {
const caseRoot = nextCaseRoot();
const privateDir = path.join(caseRoot, "private");
const sourcePath = path.join(privateDir, "alpha.md");
await fs.mkdir(privateDir, { recursive: true });
await fs.writeFile(sourcePath, "# Alpha\n", "utf8");
const { rootDir: vaultDir, config } = await createVault({
rootDir: path.join(caseRoot, "vault"),
config: {
vaultMode: "unsafe-local",
unsafeLocal: {
allowPrivateMemoryCoreAccess: true,
paths: [sourcePath],
},
},
});
const first = await syncMemoryWikiImportedSources({ config });
expect(first.indexesRefreshed).toBe(true);
expect(first.indexRefreshReason).toBe("import-changed");
await expect(fs.readFile(path.join(vaultDir, "index.md"), "utf8")).resolves.toContain(
"Unsafe Local Import: alpha.md",
);
const second = await syncMemoryWikiImportedSources({ config });
expect(second.indexesRefreshed).toBe(false);
expect(second.indexRefreshReason).toBe("no-import-changes");
await fs.rm(path.join(vaultDir, "sources", "index.md"));
const third = await syncMemoryWikiImportedSources({ config });
expect(third.indexesRefreshed).toBe(true);
expect(third.indexRefreshReason).toBe("missing-indexes");
await expect(
fs.readFile(path.join(vaultDir, "sources", "index.md"), "utf8"),
).resolves.toContain("Unsafe Local Import: alpha.md");
});
it("respects ingest.autoCompile=false", async () => {
const caseRoot = nextCaseRoot();
const privateDir = path.join(caseRoot, "private");
const sourcePath = path.join(privateDir, "alpha.md");
await fs.mkdir(privateDir, { recursive: true });
await fs.writeFile(sourcePath, "# Alpha\n", "utf8");
const { config } = await createVault({
rootDir: path.join(caseRoot, "vault"),
config: {
vaultMode: "unsafe-local",
unsafeLocal: {
allowPrivateMemoryCoreAccess: true,
paths: [sourcePath],
},
ingest: {
autoCompile: false,
},
},
});
const result = await syncMemoryWikiImportedSources({ config });
expect(result.indexesRefreshed).toBe(false);
expect(result.indexRefreshReason).toBe("auto-compile-disabled");
});
});