test: share lancedb temp fixtures

This commit is contained in:
Peter Steinberger
2026-04-20 21:51:06 +01:00
parent 5272a94a19
commit eb5f33a5c6
3 changed files with 29 additions and 50 deletions

View File

@@ -8,10 +8,7 @@
* - Auto-capture filtering
*/
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, test, expect, beforeEach, afterEach, vi } from "vitest";
import { describe, test, expect, vi } from "vitest";
import memoryPlugin, {
detectCategory,
formatRelevantMemoriesContext,
@@ -19,6 +16,7 @@ import memoryPlugin, {
shouldCapture,
} from "./index.js";
import { createLanceDbRuntimeLoader, type LanceDbRuntimeLogger } from "./lancedb-runtime.js";
import { installTmpDirHarness } from "./test-helpers.js";
const OPENAI_API_KEY = process.env.OPENAI_API_KEY ?? "test-key";
type MemoryPluginTestConfig = {
@@ -51,27 +49,6 @@ type RuntimeManifest = {
dependencies: Record<string, string>;
};
function installTmpDirHarness(params: { prefix: string }) {
let tmpDir = "";
let dbPath = "";
beforeEach(async () => {
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), params.prefix));
dbPath = path.join(tmpDir, "lancedb");
});
afterEach(async () => {
if (tmpDir) {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
return {
getTmpDir: () => tmpDir,
getDbPath: () => dbPath,
};
}
function createMockModule(): LanceDbModule {
return {
connect: vi.fn(),

View File

@@ -1,34 +1,11 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { describe, expect, test } from "vitest";
import { installTmpDirHarness } from "./test-helpers.js";
const OPENAI_API_KEY = process.env.OPENAI_API_KEY ?? "";
const HAS_OPENAI_KEY = Boolean(process.env.OPENAI_API_KEY);
const liveEnabled = HAS_OPENAI_KEY && process.env.OPENCLAW_LIVE_TEST === "1";
const describeLive = liveEnabled ? describe : describe.skip;
function installTmpDirHarness(params: { prefix: string }) {
let tmpDir = "";
let dbPath = "";
beforeEach(async () => {
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), params.prefix));
dbPath = path.join(tmpDir, "lancedb");
});
afterEach(async () => {
if (tmpDir) {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
return {
getTmpDir: () => tmpDir,
getDbPath: () => dbPath,
};
}
// Live tests that require OpenAI API key and actually use LanceDB
describeLive("memory plugin live tests", () => {
const { getDbPath } = installTmpDirHarness({ prefix: "openclaw-memory-live-" });

View File

@@ -0,0 +1,25 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach } from "vitest";
export function installTmpDirHarness(params: { prefix: string }) {
let tmpDir = "";
let dbPath = "";
beforeEach(async () => {
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), params.prefix));
dbPath = path.join(tmpDir, "lancedb");
});
afterEach(async () => {
if (tmpDir) {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
return {
getTmpDir: () => tmpDir,
getDbPath: () => dbPath,
};
}