Skip to content

Commit 1cc374b

Browse files
authored
test(llama-cpp): consolidate provider fixtures (#118426)
1 parent 7aaf302 commit 1cc374b

2 files changed

Lines changed: 196 additions & 405 deletions

File tree

‎extensions/llama-cpp/index.test.ts‎

Lines changed: 90 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import { llamaCppEmbeddingProviderAdapter } from "./src/embedding-provider.js";
3232

3333
const DEFAULT_LLAMA_CPP_EMBEDDING_MODEL =
3434
"hf:ggml-org/embeddinggemma-300m-qat-q8_0-GGUF/embeddinggemma-300m-qat-Q8_0.gguf";
35+
const DEFAULT_LLAMA_CPP_EMBEDDING_CACHE_FILE = "hf_ggml-org_embeddinggemma-300m-qat-Q8_0.gguf";
36+
const DEFAULT_LLAMA_CPP_EMBEDDING_CACHE_DIR = path.join(os.homedir(), ".node-llama-cpp", "models");
3537
type AdapterCreateOptions = Parameters<typeof llamaCppEmbeddingProviderAdapter.create>[0];
3638
type MemoryCreateTestOptions = AdapterCreateOptions & {
3739
fallback?: "none";
@@ -67,6 +69,45 @@ async function createLlamaCppMemoryEmbeddingProvider(options: MemoryCreateTestOp
6769
});
6870
}
6971

72+
function mockLocalEmbeddingProvider(model = DEFAULT_LLAMA_CPP_EMBEDDING_MODEL) {
73+
memoryHostEmbeddingMocks.createLocalEmbeddingProvider.mockResolvedValue({
74+
id: "local",
75+
model,
76+
embedQuery: vi.fn(),
77+
embedBatch: vi.fn(),
78+
});
79+
}
80+
81+
async function createMemoryProvider(
82+
model: string,
83+
local: NonNullable<AdapterCreateOptions["local"]> = { modelPath: model },
84+
) {
85+
return await createLlamaCppMemoryEmbeddingProvider({
86+
config: {},
87+
provider: "local",
88+
fallback: "none",
89+
model,
90+
local,
91+
});
92+
}
93+
94+
function cacheKeyData(model = DEFAULT_LLAMA_CPP_EMBEDDING_MODEL) {
95+
return { provider: "local", model };
96+
}
97+
98+
function identityAliases(...models: string[]) {
99+
return models.map((model) => ({ model, cacheKeyData: cacheKeyData(model) }));
100+
}
101+
102+
function resolveIndexIdentity(modelPath: string, modelCacheDir?: string) {
103+
return llamaCppEmbeddingProviderAdapter.resolveIndexIdentity?.({
104+
config: {},
105+
provider: "local",
106+
model: modelPath,
107+
local: { modelPath, ...(modelCacheDir ? { modelCacheDir } : {}) },
108+
});
109+
}
110+
70111
afterEach(() => {
71112
clearEmbeddingProviders();
72113
clearMemoryEmbeddingProviders();
@@ -223,12 +264,7 @@ describe("llama.cpp provider plugin", () => {
223264
});
224265

225266
it("includes output dimensionality in local cache and index identities", async () => {
226-
memoryHostEmbeddingMocks.createLocalEmbeddingProvider.mockResolvedValue({
227-
id: "local",
228-
model: DEFAULT_LLAMA_CPP_EMBEDDING_MODEL,
229-
embedQuery: vi.fn(),
230-
embedBatch: vi.fn(),
231-
});
267+
mockLocalEmbeddingProvider();
232268

233269
const result = await createLlamaCppMemoryEmbeddingProvider({
234270
config: {},
@@ -264,76 +300,22 @@ describe("llama.cpp provider plugin", () => {
264300

265301
it("keeps the default model identity when configured with its exact cache artifact path", async () => {
266302
const modelPath = path.join(
267-
os.homedir(),
268-
".node-llama-cpp",
269-
"models",
270-
"hf_ggml-org_embeddinggemma-300m-qat-Q8_0.gguf",
303+
DEFAULT_LLAMA_CPP_EMBEDDING_CACHE_DIR,
304+
DEFAULT_LLAMA_CPP_EMBEDDING_CACHE_FILE,
271305
);
272-
memoryHostEmbeddingMocks.createLocalEmbeddingProvider.mockResolvedValue({
273-
id: "local",
274-
model: modelPath,
275-
embedQuery: vi.fn(),
276-
embedBatch: vi.fn(),
277-
});
306+
mockLocalEmbeddingProvider(modelPath);
278307

279-
const result = await createLlamaCppMemoryEmbeddingProvider({
280-
config: {},
281-
provider: "local",
282-
fallback: "none",
283-
model: modelPath,
284-
local: { modelPath },
285-
});
308+
const result = await createMemoryProvider(modelPath);
286309

287310
expect(result.provider?.model).toBe(DEFAULT_LLAMA_CPP_EMBEDDING_MODEL);
288-
expect(result.runtime?.cacheKeyData).toEqual({
289-
provider: "local",
290-
model: DEFAULT_LLAMA_CPP_EMBEDDING_MODEL,
291-
});
292-
expect(result.runtime?.indexIdentityAliases).toEqual([
293-
{
294-
model: modelPath,
295-
cacheKeyData: {
296-
provider: "local",
297-
model: modelPath,
298-
},
299-
},
300-
{
301-
model: "hf_ggml-org_embeddinggemma-300m-qat-Q8_0.gguf",
302-
cacheKeyData: {
303-
provider: "local",
304-
model: "hf_ggml-org_embeddinggemma-300m-qat-Q8_0.gguf",
305-
},
306-
},
307-
]);
308-
expect(
309-
llamaCppEmbeddingProviderAdapter.resolveIndexIdentity?.({
310-
config: {},
311-
provider: "local",
312-
model: modelPath,
313-
local: { modelPath },
314-
}),
315-
).toEqual({
311+
expect(result.runtime?.cacheKeyData).toEqual(cacheKeyData());
312+
expect(result.runtime?.indexIdentityAliases).toEqual(
313+
identityAliases(modelPath, DEFAULT_LLAMA_CPP_EMBEDDING_CACHE_FILE),
314+
);
315+
expect(resolveIndexIdentity(modelPath)).toEqual({
316316
model: DEFAULT_LLAMA_CPP_EMBEDDING_MODEL,
317-
cacheKeyData: {
318-
provider: "local",
319-
model: DEFAULT_LLAMA_CPP_EMBEDDING_MODEL,
320-
},
321-
aliases: [
322-
{
323-
model: modelPath,
324-
cacheKeyData: {
325-
provider: "local",
326-
model: modelPath,
327-
},
328-
},
329-
{
330-
model: "hf_ggml-org_embeddinggemma-300m-qat-Q8_0.gguf",
331-
cacheKeyData: {
332-
provider: "local",
333-
model: "hf_ggml-org_embeddinggemma-300m-qat-Q8_0.gguf",
334-
},
335-
},
336-
],
317+
cacheKeyData: cacheKeyData(),
318+
aliases: identityAliases(modelPath, DEFAULT_LLAMA_CPP_EMBEDDING_CACHE_FILE),
337319
});
338320
expect(memoryHostEmbeddingMocks.createLocalEmbeddingProvider).toHaveBeenCalledWith(
339321
expect.objectContaining({
@@ -346,70 +328,35 @@ describe("llama.cpp provider plugin", () => {
346328
);
347329
});
348330

349-
it("keeps an arbitrary same-basename model path as a distinct identity", async () => {
350-
const modelPath = path.join(
351-
os.tmpdir(),
352-
"custom-models",
353-
DEFAULT_LLAMA_CPP_EMBEDDING_MODEL.split("/").at(-1)!,
354-
);
355-
memoryHostEmbeddingMocks.createLocalEmbeddingProvider.mockResolvedValue({
356-
id: "local",
357-
model: modelPath,
358-
embedQuery: vi.fn(),
359-
embedBatch: vi.fn(),
360-
});
361-
362-
const result = await createLlamaCppMemoryEmbeddingProvider({
363-
config: {},
364-
provider: "local",
365-
fallback: "none",
366-
model: modelPath,
367-
local: { modelPath },
368-
});
369-
370-
expect(result.provider?.model).toBe(modelPath);
371-
expect(result.runtime?.cacheKeyData).toEqual({
372-
provider: "local",
373-
model: modelPath,
374-
});
375-
expect(result.runtime).not.toHaveProperty("indexIdentityAliases");
376-
});
377-
378-
it("keeps a bare same-basename file in the default cache as a distinct identity", async () => {
379-
const modelPath = path.join(
380-
os.homedir(),
381-
".node-llama-cpp",
382-
"models",
383-
DEFAULT_LLAMA_CPP_EMBEDDING_MODEL.split("/").at(-1)!,
384-
);
385-
memoryHostEmbeddingMocks.createLocalEmbeddingProvider.mockResolvedValue({
386-
id: "local",
387-
model: modelPath,
388-
embedQuery: vi.fn(),
389-
embedBatch: vi.fn(),
390-
});
391-
392-
const result = await createLlamaCppMemoryEmbeddingProvider({
393-
config: {},
394-
provider: "local",
395-
fallback: "none",
396-
model: modelPath,
397-
local: { modelPath },
398-
});
331+
it.each([
332+
[
333+
"keeps an arbitrary same-basename model path as a distinct identity",
334+
path.join(os.tmpdir(), "custom-models", DEFAULT_LLAMA_CPP_EMBEDDING_MODEL.split("/").at(-1)!),
335+
true,
336+
],
337+
[
338+
"keeps a bare same-basename file in the default cache as a distinct identity",
339+
path.join(
340+
DEFAULT_LLAMA_CPP_EMBEDDING_CACHE_DIR,
341+
DEFAULT_LLAMA_CPP_EMBEDDING_MODEL.split("/").at(-1)!,
342+
),
343+
false,
344+
],
345+
])("%s", async (_name, modelPath, checksCacheKey) => {
346+
mockLocalEmbeddingProvider(modelPath);
347+
const result = await createMemoryProvider(modelPath);
399348

400349
expect(result.provider?.model).toBe(modelPath);
350+
if (checksCacheKey) {
351+
expect(result.runtime?.cacheKeyData).toEqual(cacheKeyData(modelPath));
352+
}
401353
expect(result.runtime).not.toHaveProperty("indexIdentityAliases");
402354
});
403355

404356
it("keeps the default model identity with a custom cache directory", async () => {
405357
const modelCacheDir = path.join(os.tmpdir(), "llama-cpp-model-cache");
406-
const modelPath = path.join(modelCacheDir, "hf_ggml-org_embeddinggemma-300m-qat-Q8_0.gguf");
407-
memoryHostEmbeddingMocks.createLocalEmbeddingProvider.mockResolvedValue({
408-
id: "local",
409-
model: modelPath,
410-
embedQuery: vi.fn(),
411-
embedBatch: vi.fn(),
412-
});
358+
const modelPath = path.join(modelCacheDir, DEFAULT_LLAMA_CPP_EMBEDDING_CACHE_FILE);
359+
mockLocalEmbeddingProvider(modelPath);
413360

414361
const result = await createLlamaCppMemoryEmbeddingProvider({
415362
config: {},
@@ -420,26 +367,10 @@ describe("llama.cpp provider plugin", () => {
420367
});
421368

422369
expect(result.provider?.model).toBe(DEFAULT_LLAMA_CPP_EMBEDDING_MODEL);
423-
expect(result.runtime?.cacheKeyData).toEqual({
424-
provider: "local",
425-
model: DEFAULT_LLAMA_CPP_EMBEDDING_MODEL,
426-
});
427-
expect(result.runtime?.indexIdentityAliases).toEqual([
428-
{
429-
model: modelPath,
430-
cacheKeyData: {
431-
provider: "local",
432-
model: modelPath,
433-
},
434-
},
435-
{
436-
model: "hf_ggml-org_embeddinggemma-300m-qat-Q8_0.gguf",
437-
cacheKeyData: {
438-
provider: "local",
439-
model: "hf_ggml-org_embeddinggemma-300m-qat-Q8_0.gguf",
440-
},
441-
},
442-
]);
370+
expect(result.runtime?.cacheKeyData).toEqual(cacheKeyData());
371+
expect(result.runtime?.indexIdentityAliases).toEqual(
372+
identityAliases(modelPath, DEFAULT_LLAMA_CPP_EMBEDDING_CACHE_FILE),
373+
);
443374
});
444375

445376
it.each([
@@ -449,81 +380,32 @@ describe("llama.cpp provider plugin", () => {
449380
},
450381
{
451382
direction: "exact relative cache artifact to default URI",
452-
modelPath: "hf_ggml-org_embeddinggemma-300m-qat-Q8_0.gguf",
383+
modelPath: DEFAULT_LLAMA_CPP_EMBEDDING_CACHE_FILE,
453384
},
454385
])("keeps $direction compatible", ({ modelPath }) => {
455386
const modelCacheDir = path.join(os.tmpdir(), "llama-cpp-relative-model-cache");
456-
const relativeModelPath = "hf_ggml-org_embeddinggemma-300m-qat-Q8_0.gguf";
387+
const relativeModelPath = DEFAULT_LLAMA_CPP_EMBEDDING_CACHE_FILE;
457388
const resolvedModelPath = path.join(modelCacheDir, relativeModelPath);
458389

459-
expect(
460-
llamaCppEmbeddingProviderAdapter.resolveIndexIdentity?.({
461-
config: {},
462-
provider: "local",
463-
model: modelPath,
464-
local: { modelPath, modelCacheDir },
465-
}),
466-
).toEqual({
390+
expect(resolveIndexIdentity(modelPath, modelCacheDir)).toEqual({
467391
model: DEFAULT_LLAMA_CPP_EMBEDDING_MODEL,
468-
cacheKeyData: {
469-
provider: "local",
470-
model: DEFAULT_LLAMA_CPP_EMBEDDING_MODEL,
471-
},
472-
aliases: [
473-
{
474-
model: resolvedModelPath,
475-
cacheKeyData: {
476-
provider: "local",
477-
model: resolvedModelPath,
478-
},
479-
},
480-
{
481-
model: relativeModelPath,
482-
cacheKeyData: {
483-
provider: "local",
484-
model: relativeModelPath,
485-
},
486-
},
487-
],
392+
cacheKeyData: cacheKeyData(),
393+
aliases: identityAliases(resolvedModelPath, relativeModelPath),
488394
});
489395
});
490396

491397
it("keeps the default model identity for its exact relative cache artifact", async () => {
492398
const modelCacheDir = path.join(os.tmpdir(), "llama-cpp-relative-model-cache");
493-
const modelPath = "hf_ggml-org_embeddinggemma-300m-qat-Q8_0.gguf";
399+
const modelPath = DEFAULT_LLAMA_CPP_EMBEDDING_CACHE_FILE;
494400
const resolvedModelPath = path.join(modelCacheDir, modelPath);
495-
memoryHostEmbeddingMocks.createLocalEmbeddingProvider.mockResolvedValue({
496-
id: "local",
497-
model: modelPath,
498-
embedQuery: vi.fn(),
499-
embedBatch: vi.fn(),
500-
});
401+
mockLocalEmbeddingProvider(modelPath);
501402

502-
const result = await createLlamaCppMemoryEmbeddingProvider({
503-
config: {},
504-
provider: "local",
505-
fallback: "none",
506-
model: modelPath,
507-
local: { modelPath, modelCacheDir },
508-
});
403+
const result = await createMemoryProvider(modelPath, { modelPath, modelCacheDir });
509404

510405
expect(result.provider?.model).toBe(DEFAULT_LLAMA_CPP_EMBEDDING_MODEL);
511-
expect(result.runtime?.indexIdentityAliases).toEqual([
512-
{
513-
model: resolvedModelPath,
514-
cacheKeyData: {
515-
provider: "local",
516-
model: resolvedModelPath,
517-
},
518-
},
519-
{
520-
model: modelPath,
521-
cacheKeyData: {
522-
provider: "local",
523-
model: modelPath,
524-
},
525-
},
526-
]);
406+
expect(result.runtime?.indexIdentityAliases).toEqual(
407+
identityAliases(resolvedModelPath, modelPath),
408+
);
527409
});
528410

529411
it("formats missing runtime errors with the plugin install command", () => {

0 commit comments

Comments
 (0)