Skip to content

Commit 7eb943e

Browse files
committed
fix(tests): migrate promotion embedding stub off process-global mock.module (#279)
2 parents fa61774 + 9e936a4 commit 7eb943e

1 file changed

Lines changed: 50 additions & 27 deletions

File tree

‎packages/plugin/src/features/magic-context/memory/promotion.test.ts‎

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
11
/// <reference types="bun-types" />
22

33
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from "bun:test";
4+
45
import { Database } from "../../../shared/sqlite";
56
import { closeQuietly } from "../../../shared/sqlite-helpers";
67
import { CATEGORY_DEFAULT_TTL } from "./constants";
7-
// The real module is imported (and evaluated) here BEFORE the mock registers,
8-
// so it captures every real export. Bun's mock.module is process-global and
9-
// persists across files in one test run, so a PARTIAL mock leaks into sibling
10-
// tests that import the omitted exports (e.g. embedding-backfill.test.ts /
11-
// embedding-cache.test.ts fail with "Export named 'embedBatchForProject' not
12-
// found" under a whole-dir run). Spreading the real module keeps the mock
13-
// complete — only the three functions this file needs stubbed are overridden.
14-
import * as realEmbedding from "./embedding";
8+
import type { EmbeddingProvider } from "./embedding-provider";
159
import { computeNormalizedHash } from "./normalize-hash";
1610

17-
const mockEmbedText = mock(async () => null);
11+
const mockEmbedText = mock(async (): Promise<{ vector: Float32Array } | null> => null);
1812
const mockLog = mock(() => {});
1913

20-
mock.module("./embedding", () => ({
21-
...realEmbedding,
22-
embedText: mockEmbedText,
23-
embedTextForProject: mockEmbedText,
24-
getEmbeddingModelId: () => "mock:model",
25-
}));
26-
2714
mock.module("../../../shared/logger", () => ({
2815
log: mockLog,
2916
sessionLog: mockLog,
3017
getLogFilePath: () => "/tmp/test.log",
3118
}));
3219

20+
const {
21+
_resetProjectEmbeddingRegistryForTests,
22+
_setTestProviderFactoryForProject,
23+
registerProjectEmbedding,
24+
} = await import("../project-embedding-registry");
3325
const {
3426
archiveMemory,
3527
getMemoryByHash,
@@ -40,8 +32,31 @@ const {
4032
} = await import("./storage-memory");
4133
const { embedPromotedFacts, promoteSessionFactsDurable } = await import("./promotion");
4234

35+
const TEST_PROJECT_PATH = "/repo/project";
4336
let db: Database | null = null;
4437

38+
class TestEmbeddingProvider implements EmbeddingProvider {
39+
readonly modelId = "mock:model";
40+
41+
async initialize(): Promise<boolean> {
42+
return true;
43+
}
44+
45+
async embed(text: string): Promise<Float32Array | null> {
46+
return (await mockEmbedText(text))?.vector ?? null;
47+
}
48+
49+
async embedBatch(texts: string[]): Promise<(Float32Array | null)[]> {
50+
return Promise.all(texts.map((text) => this.embed(text)));
51+
}
52+
53+
async dispose(): Promise<void> {}
54+
55+
isLoaded(): boolean {
56+
return true;
57+
}
58+
}
59+
4560
function makeMemoryDatabase(): Database {
4661
const database = new Database(":memory:");
4762
database.exec(`
@@ -101,14 +116,26 @@ function makeMemoryDatabase(): Database {
101116
return database;
102117
}
103118

119+
function registerTestEmbeddingProvider(database: Database): string {
120+
return registerProjectEmbedding(
121+
database,
122+
TEST_PROJECT_PATH,
123+
{ provider: "local", model: "mock:model" },
124+
{ memoryEnabled: false, gitCommitEnabled: false },
125+
TEST_PROJECT_PATH,
126+
).modelId;
127+
}
128+
104129
beforeEach(() => {
130+
_setTestProviderFactoryForProject(() => new TestEmbeddingProvider());
105131
mockEmbedText.mockReset();
106132
mockEmbedText.mockImplementation(async () => null);
107133
mockLog.mockReset();
108134
mockLog.mockImplementation(() => {});
109135
});
110136

111137
afterEach(() => {
138+
_resetProjectEmbeddingRegistryForTests();
112139
if (db) {
113140
try {
114141
closeQuietly(db);
@@ -405,35 +432,34 @@ describe("promotion", () => {
405432
it("stores the vector under the registered model when the memory is unchanged", async () => {
406433
db = makeMemoryDatabase();
407434
const memory = insertMemory(db, {
408-
projectPath: "/repo/project",
435+
projectPath: TEST_PROJECT_PATH,
409436
category: "ARCHITECTURE_DECISIONS",
410437
content: "Embed promoted facts eagerly",
411438
});
439+
const registeredModelId = registerTestEmbeddingProvider(db);
412440
mockEmbedText.mockImplementation(async () => ({
413441
vector: new Float32Array([1, 2]),
414-
modelId: "mock:model",
415-
chunkModelId: "mock:chunk",
416-
generation: 1,
417442
}));
418443

419-
await embedPromotedFacts(db, "ses-1", "/repo/project", [
444+
await embedPromotedFacts(db, "ses-1", TEST_PROJECT_PATH, [
420445
{ memoryId: memory.id, content: memory.content },
421446
]);
422447

423448
const rows = db.prepare("SELECT model_id FROM memory_embeddings").all() as Array<{
424449
model_id: string;
425450
}>;
426451
expect(rows).toHaveLength(1);
427-
expect(rows[0].model_id).toBe("mock:model");
452+
expect(rows[0].model_id).toBe(registeredModelId);
428453
});
429454

430455
it("discards the stale vector when the memory is edited while embedding is in flight", async () => {
431456
db = makeMemoryDatabase();
432457
const memory = insertMemory(db, {
433-
projectPath: "/repo/project",
458+
projectPath: TEST_PROJECT_PATH,
434459
category: "ARCHITECTURE_DECISIONS",
435460
content: "Original promoted content",
436461
});
462+
registerTestEmbeddingProvider(db);
437463
let release: (() => void) | undefined;
438464
const started = new Promise<void>((resolve) => {
439465
mockEmbedText.mockImplementation(async () => {
@@ -443,14 +469,11 @@ describe("promotion", () => {
443469
});
444470
return {
445471
vector: new Float32Array([1, 2]),
446-
modelId: "mock:model",
447-
chunkModelId: "mock:chunk",
448-
generation: 1,
449472
};
450473
});
451474
});
452475

453-
const inFlight = embedPromotedFacts(db, "ses-1", "/repo/project", [
476+
const inFlight = embedPromotedFacts(db, "ses-1", TEST_PROJECT_PATH, [
454477
{ memoryId: memory.id, content: memory.content },
455478
]);
456479
await started;

0 commit comments

Comments
 (0)