mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-03 03:03:24 -04:00
refactor(test): share memory embedding mocks
This commit is contained in:
50
src/memory/embedding.test-mocks.ts
Normal file
50
src/memory/embedding.test-mocks.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { vi } from "vitest";
|
||||
|
||||
// Avoid exporting vitest mock types (TS2742 under pnpm + d.ts emit).
|
||||
// oxlint-disable-next-line typescript/no-explicit-any
|
||||
type AnyMock = any;
|
||||
|
||||
const hoisted = vi.hoisted(() => ({
|
||||
embedBatch: vi.fn(async (texts: string[]) => texts.map(() => [0, 1, 0])),
|
||||
embedQuery: vi.fn(async () => [0, 1, 0]),
|
||||
}));
|
||||
|
||||
export function getEmbedBatchMock(): AnyMock {
|
||||
return hoisted.embedBatch;
|
||||
}
|
||||
|
||||
export function getEmbedQueryMock(): AnyMock {
|
||||
return hoisted.embedQuery;
|
||||
}
|
||||
|
||||
export function resetEmbeddingMocks(): void {
|
||||
hoisted.embedBatch.mockReset();
|
||||
hoisted.embedQuery.mockReset();
|
||||
hoisted.embedBatch.mockImplementation(async (texts: string[]) => texts.map(() => [0, 1, 0]));
|
||||
hoisted.embedQuery.mockImplementation(async () => [0, 1, 0]);
|
||||
}
|
||||
|
||||
// Unit tests: avoid importing the real chokidar implementation (native fsevents, etc.).
|
||||
vi.mock("chokidar", () => ({
|
||||
default: {
|
||||
watch: () => ({ on: () => {}, close: async () => {} }),
|
||||
},
|
||||
watch: () => ({ on: () => {}, close: async () => {} }),
|
||||
}));
|
||||
|
||||
vi.mock("./sqlite-vec.js", () => ({
|
||||
loadSqliteVecExtension: async () => ({ ok: false, error: "sqlite-vec disabled in tests" }),
|
||||
}));
|
||||
|
||||
vi.mock("./embeddings.js", () => ({
|
||||
createEmbeddingProvider: async () => ({
|
||||
requestedProvider: "openai",
|
||||
provider: {
|
||||
id: "mock",
|
||||
model: "mock-embed",
|
||||
maxInputTokens: 8192,
|
||||
embedQuery: hoisted.embedQuery,
|
||||
embedBatch: hoisted.embedBatch,
|
||||
},
|
||||
}),
|
||||
}));
|
||||
@@ -2,34 +2,10 @@ import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { getEmbedBatchMock, resetEmbeddingMocks } from "./embedding.test-mocks.js";
|
||||
import { getMemorySearchManager, type MemoryIndexManager } from "./index.js";
|
||||
|
||||
const embedBatch = vi.fn(async (texts: string[]) => texts.map(() => [0, 1, 0]));
|
||||
const embedQuery = vi.fn(async () => [0, 1, 0]);
|
||||
|
||||
// Unit tests: avoid importing the real chokidar implementation (native fsevents, etc.).
|
||||
vi.mock("chokidar", () => ({
|
||||
default: {
|
||||
watch: () => ({ on: () => {}, close: async () => {} }),
|
||||
},
|
||||
watch: () => ({ on: () => {}, close: async () => {} }),
|
||||
}));
|
||||
|
||||
vi.mock("./sqlite-vec.js", () => ({
|
||||
loadSqliteVecExtension: async () => ({ ok: false, error: "sqlite-vec disabled in tests" }),
|
||||
}));
|
||||
|
||||
vi.mock("./embeddings.js", () => ({
|
||||
createEmbeddingProvider: async () => ({
|
||||
requestedProvider: "openai",
|
||||
provider: {
|
||||
id: "mock",
|
||||
model: "mock-embed",
|
||||
embedQuery,
|
||||
embedBatch,
|
||||
},
|
||||
}),
|
||||
}));
|
||||
const embedBatch = getEmbedBatchMock();
|
||||
|
||||
describe("memory embedding batches", () => {
|
||||
let fixtureRoot: string;
|
||||
@@ -109,10 +85,7 @@ describe("memory embedding batches", () => {
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
embedBatch.mockClear();
|
||||
embedQuery.mockClear();
|
||||
embedBatch.mockImplementation(async (texts: string[]) => texts.map(() => [0, 1, 0]));
|
||||
embedQuery.mockImplementation(async () => [0, 1, 0]);
|
||||
resetEmbeddingMocks();
|
||||
|
||||
await fs.rm(memoryDir, { recursive: true, force: true });
|
||||
await fs.mkdir(memoryDir, { recursive: true });
|
||||
|
||||
@@ -1,36 +1,11 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
||||
import { getEmbedBatchMock, resetEmbeddingMocks } from "./embedding.test-mocks.js";
|
||||
import { getMemorySearchManager, type MemoryIndexManager } from "./index.js";
|
||||
|
||||
const embedBatch = vi.fn(async (texts: string[]) => texts.map(() => [0, 1, 0]));
|
||||
const embedQuery = vi.fn(async () => [0, 1, 0]);
|
||||
|
||||
// Unit tests: avoid importing the real chokidar implementation (native fsevents, etc.).
|
||||
vi.mock("chokidar", () => ({
|
||||
default: {
|
||||
watch: () => ({ on: () => {}, close: async () => {} }),
|
||||
},
|
||||
watch: () => ({ on: () => {}, close: async () => {} }),
|
||||
}));
|
||||
|
||||
vi.mock("./sqlite-vec.js", () => ({
|
||||
loadSqliteVecExtension: async () => ({ ok: false, error: "sqlite-vec disabled in tests" }),
|
||||
}));
|
||||
|
||||
vi.mock("./embeddings.js", () => ({
|
||||
createEmbeddingProvider: async () => ({
|
||||
requestedProvider: "openai",
|
||||
provider: {
|
||||
id: "mock",
|
||||
model: "mock-embed",
|
||||
maxInputTokens: 8192,
|
||||
embedQuery,
|
||||
embedBatch,
|
||||
},
|
||||
}),
|
||||
}));
|
||||
const embedBatch = getEmbedBatchMock();
|
||||
|
||||
describe("memory embedding token limits", () => {
|
||||
let fixtureRoot: string;
|
||||
@@ -102,10 +77,7 @@ describe("memory embedding token limits", () => {
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
embedBatch.mockReset();
|
||||
embedQuery.mockReset();
|
||||
embedBatch.mockImplementation(async (texts: string[]) => texts.map(() => [0, 1, 0]));
|
||||
embedQuery.mockImplementation(async () => [0, 1, 0]);
|
||||
resetEmbeddingMocks();
|
||||
|
||||
await fs.rm(memoryDir, { recursive: true, force: true });
|
||||
await fs.mkdir(memoryDir, { recursive: true });
|
||||
|
||||
Reference in New Issue
Block a user