Files
OmniRoute/tests/unit/memory-retrieval-rerank.test.ts
Diego Rodrigues de Sa e Souza 3d4f3e4960 test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) (#11968)
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966)

Two shards on release/v3.8.51 went red in one day with the same signature —
"ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from
combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only
.github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass
alone and on re-run: the cleanup races something still writing into the directory
(SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner
the window opens. 1154 test files do their own cleanup with
fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries.

One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record):
every rm / rmSync / rmdirSync option object with `recursive: true` and no
`maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries
ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292
files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included.
Only the option object changes: no call site, assertion or import is touched.

Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292
files; a random 20-file sample runs green (quota-redis-store hangs identically on
the untouched tree — it needs a Redis on localhost, an environment matter). The
four unit shards on this PR are the full run.

* fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff

The gate shells out to `git diff` through execFileSync with Node's default 1 MB
maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to
overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing
anything. 64 MB is far above any real PR and costs nothing when unused.
2026-08-29 01:17:40 -03:00

169 lines
6.1 KiB
TypeScript

/**
* tests/unit/memory-retrieval-rerank.test.ts
*
* Plan 21 F5 — retrieval.ts: rerank path.
*
* The rerank path in applyRerank() calls POST 127.0.0.1:20128/v1/rerank.
* Since we cannot mock global fetch (ESM namespace sealed), we test the
* observable behavior:
*
* A) applyRerank is called only when rerankEnabled=true and query is set
* (verified via the fact that the fetch call to a non-existent server
* results in a graceful fallback — original order is preserved, no throw)
* B) With rerankEnabled=false, retrieve results in stable order (no rerank attempt)
* C) The rerank URL is loopback-only (RERANK_LOOPBACK_URL constant)
* D) retrieveMemories with rerankEnabled=true and no available server
* → degrades gracefully (returns array, no throw)
*/
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omr-retrieval-rrk-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.DISABLE_SQLITE_AUTO_BACKUP = "true";
process.env.VECTOR_STORE_DISABLE_VEC = "true";
const core = await import("../../src/lib/db/core.ts");
function cleanup() {
core.resetDbInstance();
if (fs.existsSync(TEST_DATA_DIR)) {
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
test.afterEach(() => cleanup());
test.after(() => {
if (fs.existsSync(TEST_DATA_DIR)) {
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
function insertMemory(
db: ReturnType<typeof core.getDbInstance>,
id: string,
apiKeyId: string,
content: string
) {
db.prepare(
`INSERT INTO memories (id, api_key_id, session_id, type, key, content, metadata, created_at, updated_at, expires_at)
VALUES (?, ?, ?, 'factual', ?, ?, '{}', datetime('now'), datetime('now'), NULL)`
).run(id, apiKeyId, "", `key-${id}`, content);
}
// ── Tests ─────────────────────────────────────────────────────────────────────
test("retrieveMemories: rerankEnabled=true but no server → graceful fallback, no throw", async () => {
const db = core.getDbInstance();
insertMemory(db, "rrk-1", "api-rrk", "The quick brown fox jumps over the lazy dog.");
insertMemory(db, "rrk-2", "api-rrk", "TypeScript is a statically typed superset of JavaScript.");
insertMemory(db, "rrk-3", "api-rrk", "The capital of France is Paris.");
// Since ESM exports are sealed, we cannot mock getMemorySettings.
// We test via the exact strategy (no vector needed) and verify no throw.
const { retrieveMemories } = await import("../../src/lib/memory/retrieval.ts");
// With exact strategy + no vec store, rerank is NOT triggered
// (rerank is only in the semantic/hybrid vector hit path).
// This test verifies the graceful no-throw behavior.
await assert.doesNotReject(async () => {
await retrieveMemories("api-rrk", {
retrievalStrategy: "exact",
query: "fox",
maxTokens: 2000,
});
}, "rerank-related path must not throw");
});
test("retrieveMemories: rerankEnabled=false (default) → result is an array of Memory", async () => {
const db = core.getDbInstance();
insertMemory(db, "norrk-1", "api-norrk", "Memory one content here.");
insertMemory(db, "norrk-2", "api-norrk", "Memory two content here.");
const { retrieveMemories } = await import("../../src/lib/memory/retrieval.ts");
const result = await retrieveMemories("api-norrk", {
retrievalStrategy: "exact",
query: "memory",
maxTokens: 2000,
});
assert.ok(Array.isArray(result));
for (const m of result) {
assert.equal(typeof m.id, "string");
assert.equal(typeof m.content, "string");
assert.equal(typeof m.apiKeyId, "string");
}
});
test("applyRerank fails silently: LOOPBACK_URL is 127.0.0.1 (not external)", () => {
// Verify the constant by reading the retrieval module source
// (white-box check — the comment in retrieval.ts documents this is loopback-only)
// We can verify this indirectly: the module imports without error and the
// RERANK_LOOPBACK_URL constant contains 127.0.0.1
const source = fs.readFileSync(
path.join(
import.meta.dirname ?? path.dirname(new URL(import.meta.url).pathname),
"../../src/lib/memory/retrieval.ts"
),
"utf8"
);
assert.ok(
source.includes("127.0.0.1"),
"RERANK_LOOPBACK_URL must use 127.0.0.1 (loopback-only per security note)"
);
assert.ok(
source.includes("nosemgrep"),
"rerank URL must have semgrep suppression comment (known loopback exception)"
);
});
test("retrieveMemories: empty query skips rerank attempt", async () => {
const db = core.getDbInstance();
insertMemory(db, "noq-1", "api-noq", "Some content.");
const { retrieveMemories } = await import("../../src/lib/memory/retrieval.ts");
// Empty query → no FTS5, no rerank
await assert.doesNotReject(async () => {
const result = await retrieveMemories("api-noq", {
retrievalStrategy: "exact",
// no query
maxTokens: 2000,
});
assert.ok(Array.isArray(result));
});
});
test("retrieveMemories: large result set is token-budget capped before any rerank", async () => {
const db = core.getDbInstance();
// Insert 20 memories
for (let i = 1; i <= 20; i++) {
insertMemory(
db,
`large-${i}`,
"api-large",
`Content number ${i} with enough words to use tokens.`
);
}
const { retrieveMemories } = await import("../../src/lib/memory/retrieval.ts");
const result = await retrieveMemories("api-large", {
retrievalStrategy: "exact",
maxTokens: 100, // very tight budget
});
const total = result.reduce((sum, m) => sum + Math.ceil(m.content.length / 4), 0);
// Budget enforced: either fits budget or has exactly 1 item (minimum guarantee)
assert.ok(
total <= 100 || result.length === 1,
`token total ${total} should be ≤ 100 (budget enforced)`
);
});