Files
memind/scripts/resolve-llm-embedding-credentials.test.mjs
T
john fb3a442e73 Improve MemFuse recall via hybrid ranking and candidate generation.
Add RRF fusion with English word-level lexical scoring, tiered keyword fetch, and vector margin expansion (0.15/200) to fix pre-rank truncation; wire DashScope embedding bench path and update baseline to 28.8% recall@20.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-02 13:41:12 +08:00

81 lines
2.6 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { encryptSecret } from '../llm-providers.mjs';
import { resolveLlmEmbeddingCredentials } from './resolve-llm-embedding-credentials.mjs';
function makePool(rowsByQuery) {
return {
async query(sql, params = []) {
const text = String(sql);
if (text.includes('WHERE id = ?')) {
const row = rowsByQuery.byId?.get(params[0]);
return [row ? [row] : []];
}
if (text.includes('is_selected = 1')) {
return [rowsByQuery.selected ?? []];
}
if (text.includes('dashscope.aliyuncs.com')) {
return [rowsByQuery.dashscope ?? []];
}
return [[]];
},
async end() {},
};
}
test('resolveLlmEmbeddingCredentials decrypts selected DashScope key', async () => {
const secret = encryptSecret('dashscope-test-key', 'unit-test-secret');
const row = {
id: 'key-1',
name: 'qwen3-max',
provider_id: 'custom_qwen3-max_1',
api_url: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
default_model: 'qwen3-max',
api_key_ciphertext: secret.ciphertext,
api_key_iv: secret.iv,
api_key_tag: secret.tag,
};
const resolved = await resolveLlmEmbeddingCredentials({
pool: makePool({ selected: [row] }),
env: { TKMIND_SERVER__SECRET_KEY: 'unit-test-secret' },
});
assert.equal(resolved.available, true);
assert.equal(resolved.apiKey, 'dashscope-test-key');
assert.equal(resolved.baseUrl, 'https://dashscope.aliyuncs.com/compatible-mode/v1');
assert.equal(resolved.keyName, 'qwen3-max');
});
test('resolveLlmEmbeddingCredentials falls back when selected key is not DashScope', async () => {
const secret = encryptSecret('dashscope-fallback-key', 'unit-test-secret');
const dashRow = {
id: 'ds-1',
name: 'qwen chat',
provider_id: 'custom_qwen_chat',
api_url: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
default_model: 'qwen-plus',
api_key_ciphertext: secret.ciphertext,
api_key_iv: secret.iv,
api_key_tag: secret.tag,
};
const resolved = await resolveLlmEmbeddingCredentials({
pool: makePool({
selected: [{
id: 'deepseek',
name: 'DeepSeek',
provider_id: 'custom_deepseek',
api_url: null,
default_model: 'deepseek-v4-pro',
api_key_ciphertext: secret.ciphertext,
api_key_iv: secret.iv,
api_key_tag: secret.tag,
}],
dashscope: [dashRow],
}),
env: { TKMIND_SERVER__SECRET_KEY: 'unit-test-secret' },
});
assert.equal(resolved.available, true);
assert.equal(resolved.apiKey, 'dashscope-fallback-key');
assert.equal(resolved.keyName, 'qwen chat');
});