mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 11:51:23 +00:00
perf(test): speed up suites and reduce fs churn
This commit is contained in:
@@ -46,10 +46,8 @@ describe("memory index", () => {
|
||||
let workspaceDir = "";
|
||||
let memoryDir = "";
|
||||
let extraDir = "";
|
||||
let indexBasicPath = "";
|
||||
let indexCachePath = "";
|
||||
let indexHybridPath = "";
|
||||
let indexVectorPath = "";
|
||||
let indexMainPath = "";
|
||||
let indexExtraPath = "";
|
||||
|
||||
// Perf: keep managers open across tests, but only reset the one a test uses.
|
||||
@@ -61,13 +59,15 @@ describe("memory index", () => {
|
||||
workspaceDir = path.join(fixtureRoot, "workspace");
|
||||
memoryDir = path.join(workspaceDir, "memory");
|
||||
extraDir = path.join(workspaceDir, "extra");
|
||||
indexBasicPath = path.join(workspaceDir, "index-basic.sqlite");
|
||||
indexCachePath = path.join(workspaceDir, "index-cache.sqlite");
|
||||
indexHybridPath = path.join(workspaceDir, "index-hybrid.sqlite");
|
||||
indexMainPath = path.join(workspaceDir, "index-main.sqlite");
|
||||
indexVectorPath = path.join(workspaceDir, "index-vector.sqlite");
|
||||
indexExtraPath = path.join(workspaceDir, "index-extra.sqlite");
|
||||
|
||||
await fs.mkdir(memoryDir, { recursive: true });
|
||||
await fs.writeFile(
|
||||
path.join(memoryDir, "2026-01-12.md"),
|
||||
"# Log\nAlpha memory line.\nZebra memory line.",
|
||||
);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
@@ -83,10 +83,6 @@ describe("memory index", () => {
|
||||
|
||||
// Keep the workspace stable to allow manager reuse across tests.
|
||||
await fs.mkdir(memoryDir, { recursive: true });
|
||||
await fs.writeFile(
|
||||
path.join(memoryDir, "2026-01-12.md"),
|
||||
"# Log\nAlpha memory line.\nZebra memory line.",
|
||||
);
|
||||
|
||||
// Clean additional paths that may have been created by earlier cases.
|
||||
await fs.rm(extraDir, { recursive: true, force: true });
|
||||
@@ -105,6 +101,38 @@ describe("memory index", () => {
|
||||
|
||||
type TestCfg = Parameters<typeof getMemorySearchManager>[0]["cfg"];
|
||||
|
||||
function createCfg(params: {
|
||||
storePath: string;
|
||||
extraPaths?: string[];
|
||||
model?: string;
|
||||
vectorEnabled?: boolean;
|
||||
cacheEnabled?: boolean;
|
||||
hybrid?: { enabled: boolean; vectorWeight?: number; textWeight?: number };
|
||||
}): TestCfg {
|
||||
return {
|
||||
agents: {
|
||||
defaults: {
|
||||
workspace: workspaceDir,
|
||||
memorySearch: {
|
||||
provider: "openai",
|
||||
model: params.model ?? "mock-embed",
|
||||
store: { path: params.storePath, vector: { enabled: params.vectorEnabled ?? false } },
|
||||
// Perf: keep test indexes to a single chunk to reduce sqlite work.
|
||||
chunking: { tokens: 4000, overlap: 0 },
|
||||
sync: { watch: false, onSessionStart: false, onSearch: true },
|
||||
query: {
|
||||
minScore: 0,
|
||||
hybrid: params.hybrid ?? { enabled: false },
|
||||
},
|
||||
cache: params.cacheEnabled ? { enabled: true } : undefined,
|
||||
extraPaths: params.extraPaths,
|
||||
},
|
||||
},
|
||||
list: [{ id: "main", default: true }],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function getPersistentManager(cfg: TestCfg): Promise<MemoryIndexManager> {
|
||||
const storePath = cfg.agents?.defaults?.memorySearch?.store?.path;
|
||||
if (!storePath) {
|
||||
@@ -128,24 +156,14 @@ describe("memory index", () => {
|
||||
return manager;
|
||||
}
|
||||
|
||||
it("indexes memory files and searches by vector", async () => {
|
||||
const cfg = {
|
||||
agents: {
|
||||
defaults: {
|
||||
workspace: workspaceDir,
|
||||
memorySearch: {
|
||||
provider: "openai",
|
||||
model: "mock-embed",
|
||||
store: { path: indexBasicPath, vector: { enabled: false } },
|
||||
sync: { watch: false, onSessionStart: false, onSearch: true },
|
||||
query: { minScore: 0, hybrid: { enabled: false } },
|
||||
},
|
||||
},
|
||||
list: [{ id: "main", default: true }],
|
||||
},
|
||||
};
|
||||
it("indexes memory files and searches", async () => {
|
||||
const cfg = createCfg({
|
||||
storePath: indexMainPath,
|
||||
hybrid: { enabled: true, vectorWeight: 0.5, textWeight: 0.5 },
|
||||
});
|
||||
const manager = await getPersistentManager(cfg);
|
||||
await manager.sync({ reason: "test" });
|
||||
expect(embedBatchCalls).toBeGreaterThan(0);
|
||||
const results = await manager.search("alpha");
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results[0]?.path).toContain("memory/2026-01-12.md");
|
||||
@@ -162,26 +180,8 @@ describe("memory index", () => {
|
||||
});
|
||||
|
||||
it("keeps dirty false in status-only manager after prior indexing", async () => {
|
||||
const indexStatusPath = path.join(workspaceDir, "index-status.sqlite");
|
||||
await fs.rm(indexStatusPath, { force: true });
|
||||
await fs.rm(`${indexStatusPath}-shm`, { force: true });
|
||||
await fs.rm(`${indexStatusPath}-wal`, { force: true });
|
||||
|
||||
const cfg = {
|
||||
agents: {
|
||||
defaults: {
|
||||
workspace: workspaceDir,
|
||||
memorySearch: {
|
||||
provider: "openai",
|
||||
model: "mock-embed",
|
||||
store: { path: indexStatusPath, vector: { enabled: false } },
|
||||
sync: { watch: false, onSessionStart: false, onSearch: true },
|
||||
query: { minScore: 0, hybrid: { enabled: false } },
|
||||
},
|
||||
},
|
||||
list: [{ id: "main", default: true }],
|
||||
},
|
||||
};
|
||||
const indexStatusPath = path.join(workspaceDir, `index-status-${Date.now()}.sqlite`);
|
||||
const cfg = createCfg({ storePath: indexStatusPath });
|
||||
|
||||
const first = await getMemorySearchManager({ cfg, agentId: "main" });
|
||||
expect(first.manager).not.toBeNull();
|
||||
@@ -207,25 +207,8 @@ describe("memory index", () => {
|
||||
});
|
||||
|
||||
it("reindexes when the embedding model changes", async () => {
|
||||
const indexModelPath = path.join(workspaceDir, "index-model-change.sqlite");
|
||||
await fs.rm(indexModelPath, { force: true });
|
||||
await fs.rm(`${indexModelPath}-shm`, { force: true });
|
||||
await fs.rm(`${indexModelPath}-wal`, { force: true });
|
||||
|
||||
const base = {
|
||||
agents: {
|
||||
defaults: {
|
||||
workspace: workspaceDir,
|
||||
memorySearch: {
|
||||
provider: "openai",
|
||||
store: { path: indexModelPath },
|
||||
sync: { watch: false, onSessionStart: false, onSearch: true },
|
||||
query: { minScore: 0, hybrid: { enabled: false } },
|
||||
},
|
||||
},
|
||||
list: [{ id: "main", default: true }],
|
||||
},
|
||||
};
|
||||
const indexModelPath = path.join(workspaceDir, `index-model-change-${Date.now()}.sqlite`);
|
||||
const base = createCfg({ storePath: indexModelPath });
|
||||
|
||||
const first = await getMemorySearchManager({
|
||||
cfg: {
|
||||
@@ -279,24 +262,11 @@ describe("memory index", () => {
|
||||
});
|
||||
|
||||
it("reuses cached embeddings on forced reindex", async () => {
|
||||
const cfg = {
|
||||
agents: {
|
||||
defaults: {
|
||||
workspace: workspaceDir,
|
||||
memorySearch: {
|
||||
provider: "openai",
|
||||
model: "mock-embed",
|
||||
store: { path: indexCachePath, vector: { enabled: false } },
|
||||
sync: { watch: false, onSessionStart: false, onSearch: false },
|
||||
query: { minScore: 0, hybrid: { enabled: false } },
|
||||
cache: { enabled: true },
|
||||
},
|
||||
},
|
||||
list: [{ id: "main", default: true }],
|
||||
},
|
||||
};
|
||||
const cfg = createCfg({ storePath: indexMainPath, cacheEnabled: true });
|
||||
const manager = await getPersistentManager(cfg);
|
||||
await manager.sync({ force: true });
|
||||
// Seed the embedding cache once, then ensure a forced reindex doesn't
|
||||
// re-embed when the cache is enabled.
|
||||
await manager.sync({ reason: "test" });
|
||||
const afterFirst = embedBatchCalls;
|
||||
expect(afterFirst).toBeGreaterThan(0);
|
||||
|
||||
@@ -305,24 +275,10 @@ describe("memory index", () => {
|
||||
});
|
||||
|
||||
it("finds keyword matches via hybrid search when query embedding is zero", async () => {
|
||||
const cfg = {
|
||||
agents: {
|
||||
defaults: {
|
||||
workspace: workspaceDir,
|
||||
memorySearch: {
|
||||
provider: "openai",
|
||||
model: "mock-embed",
|
||||
store: { path: indexHybridPath, vector: { enabled: false } },
|
||||
sync: { watch: false, onSessionStart: false, onSearch: true },
|
||||
query: {
|
||||
minScore: 0,
|
||||
hybrid: { enabled: true, vectorWeight: 0, textWeight: 1 },
|
||||
},
|
||||
},
|
||||
},
|
||||
list: [{ id: "main", default: true }],
|
||||
},
|
||||
};
|
||||
const cfg = createCfg({
|
||||
storePath: indexMainPath,
|
||||
hybrid: { enabled: true, vectorWeight: 0, textWeight: 1 },
|
||||
});
|
||||
const manager = await getPersistentManager(cfg);
|
||||
|
||||
const status = manager.status();
|
||||
@@ -337,20 +293,7 @@ describe("memory index", () => {
|
||||
});
|
||||
|
||||
it("reports vector availability after probe", async () => {
|
||||
const cfg = {
|
||||
agents: {
|
||||
defaults: {
|
||||
workspace: workspaceDir,
|
||||
memorySearch: {
|
||||
provider: "openai",
|
||||
model: "mock-embed",
|
||||
store: { path: indexVectorPath, vector: { enabled: true } },
|
||||
sync: { watch: false, onSessionStart: false, onSearch: false },
|
||||
},
|
||||
},
|
||||
list: [{ id: "main", default: true }],
|
||||
},
|
||||
};
|
||||
const cfg = createCfg({ storePath: indexVectorPath, vectorEnabled: true });
|
||||
const manager = await getPersistentManager(cfg);
|
||||
const available = await manager.probeVectorAvailability();
|
||||
const status = manager.status();
|
||||
@@ -360,21 +303,7 @@ describe("memory index", () => {
|
||||
});
|
||||
|
||||
it("rejects reading non-memory paths", async () => {
|
||||
const cfg = {
|
||||
agents: {
|
||||
defaults: {
|
||||
workspace: workspaceDir,
|
||||
memorySearch: {
|
||||
provider: "openai",
|
||||
model: "mock-embed",
|
||||
store: { path: indexBasicPath, vector: { enabled: false } },
|
||||
sync: { watch: false, onSessionStart: false, onSearch: true },
|
||||
query: { minScore: 0, hybrid: { enabled: false } },
|
||||
},
|
||||
},
|
||||
list: [{ id: "main", default: true }],
|
||||
},
|
||||
};
|
||||
const cfg = createCfg({ storePath: indexMainPath });
|
||||
const manager = await getPersistentManager(cfg);
|
||||
await expect(manager.readFile({ relPath: "NOTES.md" })).rejects.toThrow("path required");
|
||||
});
|
||||
@@ -383,22 +312,7 @@ describe("memory index", () => {
|
||||
await fs.mkdir(extraDir, { recursive: true });
|
||||
await fs.writeFile(path.join(extraDir, "extra.md"), "Extra content.");
|
||||
|
||||
const cfg = {
|
||||
agents: {
|
||||
defaults: {
|
||||
workspace: workspaceDir,
|
||||
memorySearch: {
|
||||
provider: "openai",
|
||||
model: "mock-embed",
|
||||
store: { path: indexExtraPath, vector: { enabled: false } },
|
||||
sync: { watch: false, onSessionStart: false, onSearch: true },
|
||||
query: { minScore: 0, hybrid: { enabled: false } },
|
||||
extraPaths: [extraDir],
|
||||
},
|
||||
},
|
||||
list: [{ id: "main", default: true }],
|
||||
},
|
||||
};
|
||||
const cfg = createCfg({ storePath: indexExtraPath, extraPaths: [extraDir] });
|
||||
const manager = await getPersistentManager(cfg);
|
||||
await expect(manager.readFile({ relPath: "extra/extra.md" })).resolves.toEqual({
|
||||
path: "extra/extra.md",
|
||||
|
||||
Reference in New Issue
Block a user