mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-17 20:32:25 +03:00
* test: close the database before removing temp DATA_DIR (#13290) Tests that set their own DATA_DIR and removed it in test.after() failed on Windows with EPERM: nothing closed the SQLite connection, so the directory still had an open handle and the -shm/-wal sidecars kept it locked. maxRetries could not help because every retry hit the same open handle. Adds tests/_setup/tempDataDir.ts with cleanupTempDataDir()/createTempDataDir(), which close the DB singleton (lazily imported, so tests that never touch the database do not pull in the DB layer) and then remove the directory best-effort. Applies it to the five suites confirmed failing. The helper's own test proves the ordering matters: skipping the close makes it fail with 'cleanup must remove the directory'. * test: close the database before removing temp DATA_DIR (15 more suites) Converts the suites that measurably emitted EPERM during a full run to the shared cleanupTempDataDir helper from #13292. Measured on the same 15 files: base -> 22 fail, 40 EPERM lines branch -> 7 fail, 10 EPERM lines The 7 remaining failures are pre-existing and unrelated to teardown: rtk-learn-discover-routes and executor-map-golden already fail on a clean base (6 and 3 failures respectively). * test: close the database before removing temp DATA_DIR (final 9 suites) Completes the #13290 sweep. Two teardown shapes needed the helper: - after()/t.after() hooks that removed DATA_DIR directly - beforeEach() hooks that wiped DATA_DIR between tests while the previous test's connection was still open. These failed *before* the test body ran, so every test in the file reported the same EPERM path. Three of them already called core.resetDbInstance() right before rmSync and still leaked, which is the product-side connection leak tracked in #13303. Measured per file, EPERM lines now 0 across all nine. Remaining failures are pre-existing on a clean base (firefly 4->1, driverFactory 1, responses-* 1 each) and unrelated to teardown. * test: add the missing cleanupTempDataDir import to two responses suites The previous commit swapped rmSync for cleanupTempDataDir in these two files but did not add the import, so both suites died with ReferenceError: cleanupTempDataDir is not defined before running any test. responses-parse-once-4041: 0 pass / 1 fail -> 4 pass / 0 fail responses-route-early-keepalive-wiring: 0 pass / 1 fail -> 3 pass / 0 fail Both now report 0 EPERM. * test: close SQLite handles in three silently-leaking suites These three suites requested DATA_DIR cleanup but the delete failed on Windows because a SQLite connection was still open. They pass today, so the leak is invisible: they carry state between tests and would surface later as an unrelated-looking assertion, as #13303 already did in the Firefly suite (a 500 instead of a 401). agentbridge-mitm-router-key-6403 and agent-bridge-bypass-flow removed their own temp dir in test.after() without closing the DB first; both now use the shared cleanupTempDataDir helper, which closes the singleton before removing the directory. issue-agent-route-execution is a different case: it has no teardown at all, so the connection stayed open until process exit and the isolateDataDir cleanup hook then hit EPERM. It now closes the DB in test.after(). Verified with a probe on fs.rmSync: all three reported a failed delete before, and zero across three consecutive runs after, while the same probe still reports four leaks in the Firefly suite. * test: remove temp DATA_DIR in five suites that never cleaned up These five suites create their own mkdtemp DATA_DIR, open the SQLite DB and never remove the directory, so every run leaves a storage.sqlite behind in the OS temp dir. Each dir is private to its suite, so this leaked disk space rather than corrupting results - but the churn is pointless. Each now closes the DB and removes its directory through the shared cleanupTempDataDir helper. Verified with an exit-time probe that lists storage.sqlite* still present in DATA_DIR: it fired for these suites before the change and is silent after, with the same test counts (22/14/5/3/3 passing).
305 lines
13 KiB
TypeScript
305 lines
13 KiB
TypeScript
/**
|
|
* Unit tests for src/lib/db/agenticConversations.ts CRUD.
|
|
*/
|
|
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import { mkdtempSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { tmpdir } from "node:os";
|
|
import { cleanupTempDataDir } from "../_setup/tempDataDir.ts";
|
|
|
|
process.env.DATA_DIR = mkdtempSync(join(tmpdir(), "omniroute-agentic-conv-db-"));
|
|
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "agentic-conversations-test-secret";
|
|
|
|
// Dynamic imports (not static) are required here: a static `import` of a module
|
|
// that reads process.env.DATA_DIR at its own top level (src/lib/db/core.ts's
|
|
// `export const DATA_DIR = ...`) is evaluated before this file's own top-level
|
|
// code runs — ESM instantiates the whole dependency graph, dependencies first,
|
|
// regardless of source-line order — so the override above would silently miss
|
|
// and the module would resolve the real host DATA_DIR instead of the temp dir.
|
|
const {
|
|
createAgenticConversation,
|
|
findAgenticConversationsByFingerprint,
|
|
updateAgenticConversation,
|
|
touchOrCreateExternalConversation,
|
|
listMultiTurnConversations,
|
|
getConversationTurnIndex,
|
|
insertConversationTurnNodes,
|
|
getConversationTurnPage,
|
|
resolveCallLogIdsByCorrelationIds,
|
|
} = await import("../../src/lib/db/agenticConversations.ts");
|
|
const { getDbInstance } = await import("../../src/lib/db/core.ts");
|
|
|
|
test("createAgenticConversation + findAgenticConversationsByFingerprint round-trip", () => {
|
|
const row = createAgenticConversation({
|
|
apiKeyId: "key-a",
|
|
fingerprintHash: "fp-round-trip",
|
|
});
|
|
|
|
assert.match(row.id, /^conv_/);
|
|
assert.equal(row.turnCount, 1);
|
|
|
|
const found = findAgenticConversationsByFingerprint("fp-round-trip");
|
|
assert.equal(found.length, 1);
|
|
assert.equal(found[0].id, row.id);
|
|
assert.equal(found[0].apiKeyId, "key-a");
|
|
});
|
|
|
|
test("findAgenticConversationsByFingerprint returns multiple rows for a shared fingerprint", () => {
|
|
createAgenticConversation({ apiKeyId: "key-b", fingerprintHash: "fp-shared" });
|
|
createAgenticConversation({ apiKeyId: "key-b", fingerprintHash: "fp-shared" });
|
|
|
|
const found = findAgenticConversationsByFingerprint("fp-shared");
|
|
assert.equal(found.length, 2);
|
|
});
|
|
|
|
test("updateAgenticConversation updates turn count", () => {
|
|
const row = createAgenticConversation({ apiKeyId: "key-c", fingerprintHash: "fp-update" });
|
|
|
|
updateAgenticConversation(row.id, { turnCount: 3 });
|
|
|
|
const found = findAgenticConversationsByFingerprint("fp-update");
|
|
assert.equal(found[0].turnCount, 3);
|
|
});
|
|
|
|
test("insertConversationTurnNodes + getConversationTurnIndex round-trip", () => {
|
|
const row = createAgenticConversation({ apiKeyId: "key-nodes", fingerprintHash: "fp-nodes" });
|
|
|
|
insertConversationTurnNodes(row.id, "corr-1", [
|
|
{ id: "node-a", parentId: null, role: "user", contentHash: "hash-a" },
|
|
{ id: "node-b", parentId: "node-a", role: "assistant", contentHash: "hash-b" },
|
|
]);
|
|
|
|
const index = getConversationTurnIndex(row.id);
|
|
assert.equal(index.nodeIds.size, 2);
|
|
assert.ok(index.nodeIds.has("node-a"));
|
|
assert.ok(index.nodeIds.has("node-b"));
|
|
assert.deepEqual(index.byContentHash.get("hash-a"), ["node-a"]);
|
|
assert.deepEqual(index.byContentHash.get("hash-b"), ["node-b"]);
|
|
|
|
// A different conversation's nodes must never leak into this index.
|
|
const other = createAgenticConversation({
|
|
apiKeyId: "key-nodes-2",
|
|
fingerprintHash: "fp-nodes-2",
|
|
});
|
|
insertConversationTurnNodes(other.id, "corr-2", [
|
|
{ id: "node-c", parentId: null, role: "user", contentHash: "hash-c" },
|
|
]);
|
|
const reReadIndex = getConversationTurnIndex(row.id);
|
|
assert.equal(reReadIndex.nodeIds.size, 2);
|
|
assert.equal(reReadIndex.byContentHash.has("hash-c"), false);
|
|
});
|
|
|
|
test("getConversationTurnIndex groups multiple node ids under the same content hash (duplicate turn text at different tree positions)", () => {
|
|
const row = createAgenticConversation({ apiKeyId: "key-dup-content", fingerprintHash: "fp-dup" });
|
|
|
|
insertConversationTurnNodes(row.id, "corr-1", [
|
|
{ id: "node-1", parentId: null, role: "user", contentHash: "hash-ok" },
|
|
{ id: "node-2", parentId: "node-1", role: "assistant", contentHash: "hash-reply" },
|
|
// Same content ("ok") recurs later in the same tree, at a different node.
|
|
{ id: "node-3", parentId: "node-2", role: "user", contentHash: "hash-ok" },
|
|
]);
|
|
|
|
const index = getConversationTurnIndex(row.id);
|
|
const matches = index.byContentHash.get("hash-ok");
|
|
assert.equal(matches?.length, 2);
|
|
assert.deepEqual([...matches!].sort(), ["node-1", "node-3"]);
|
|
});
|
|
|
|
test("insertConversationTurnNodes is idempotent for already-existing node ids (INSERT OR IGNORE)", () => {
|
|
const row = createAgenticConversation({ apiKeyId: "key-idem", fingerprintHash: "fp-idem" });
|
|
|
|
insertConversationTurnNodes(row.id, "corr-1", [
|
|
{ id: "node-dup", parentId: null, role: "user", contentHash: "hash-dup" },
|
|
]);
|
|
// Re-insert the same id — must not throw, must not duplicate.
|
|
insertConversationTurnNodes(row.id, "corr-2", [
|
|
{ id: "node-dup", parentId: null, role: "user", contentHash: "hash-dup" },
|
|
]);
|
|
|
|
const tree = getConversationTurnPage(row.id, { limit: 500 }).nodes;
|
|
assert.equal(tree.length, 1);
|
|
});
|
|
|
|
test("getConversationTurnPage returns the full chain with parent/child structure and content hash", () => {
|
|
const row = createAgenticConversation({ apiKeyId: "key-tree", fingerprintHash: "fp-tree" });
|
|
|
|
insertConversationTurnNodes(row.id, "corr-tree", [
|
|
{ id: "root-turn", parentId: null, role: "user", contentHash: "hash-hello" },
|
|
{ id: "child-turn", parentId: "root-turn", role: "assistant", contentHash: "hash-hi" },
|
|
]);
|
|
// A sibling branch off the same parent.
|
|
insertConversationTurnNodes(row.id, "corr-tree-2", [
|
|
{ id: "sibling-turn", parentId: "root-turn", role: "assistant", contentHash: "hash-hey" },
|
|
]);
|
|
|
|
const tree = getConversationTurnPage(row.id, { limit: 500 }).nodes;
|
|
assert.equal(tree.length, 3);
|
|
|
|
const root = tree.find((n) => n.id === "root-turn");
|
|
const children = tree.filter((n) => n.parentId === "root-turn");
|
|
assert.equal(root?.parentId, null);
|
|
assert.equal(root?.contentHash, "hash-hello");
|
|
assert.equal(children.length, 2);
|
|
assert.deepEqual(children.map((c) => c.id).sort(), ["child-turn", "sibling-turn"]);
|
|
});
|
|
|
|
test("getConversationTurnPage: initial load returns only the last `limit` turns, oldest-first, with hasMore", () => {
|
|
const row = createAgenticConversation({ apiKeyId: "key-page", fingerprintHash: "fp-page" });
|
|
const nodes = Array.from({ length: 25 }, (_, i) => ({
|
|
id: `n${i}`,
|
|
parentId: i === 0 ? null : `n${i - 1}`,
|
|
role: i % 2 === 0 ? "user" : "assistant",
|
|
contentHash: `hash-${i}`,
|
|
}));
|
|
insertConversationTurnNodes(row.id, "corr-page", nodes);
|
|
|
|
const page = getConversationTurnPage(row.id, { limit: 20 });
|
|
assert.equal(page.nodes.length, 20);
|
|
assert.equal(page.hasMore, true);
|
|
// Oldest-first within the page, and it's the LAST 20 (n5..n24).
|
|
assert.equal(page.nodes[0].id, "n5");
|
|
assert.equal(page.nodes[19].id, "n24");
|
|
});
|
|
|
|
test("getConversationTurnPage: beforeSeq loads the previous page (older turns), with correct hasMore", () => {
|
|
const row = createAgenticConversation({ apiKeyId: "key-page-2", fingerprintHash: "fp-page-2" });
|
|
const nodes = Array.from({ length: 25 }, (_, i) => ({
|
|
id: `m${i}`,
|
|
parentId: i === 0 ? null : `m${i - 1}`,
|
|
role: "user",
|
|
contentHash: `hash-m${i}`,
|
|
}));
|
|
insertConversationTurnNodes(row.id, "corr-page-2", nodes);
|
|
|
|
const firstPage = getConversationTurnPage(row.id, { limit: 20 });
|
|
const oldestSeqInFirstPage = firstPage.nodes[0].seq;
|
|
|
|
const olderPage = getConversationTurnPage(row.id, { limit: 20, beforeSeq: oldestSeqInFirstPage });
|
|
assert.equal(olderPage.nodes.length, 5, "only 5 turns (0-4) exist before the first page");
|
|
assert.equal(olderPage.hasMore, false);
|
|
assert.equal(olderPage.nodes[0].id, "m0");
|
|
assert.equal(olderPage.nodes[4].id, "m4");
|
|
});
|
|
|
|
test("getConversationTurnPage: afterSeq returns only turns newer than the cursor (for polling), uncapped", () => {
|
|
const row = createAgenticConversation({ apiKeyId: "key-page-3", fingerprintHash: "fp-page-3" });
|
|
insertConversationTurnNodes(row.id, "corr-page-3", [
|
|
{ id: "p0", parentId: null, role: "user", contentHash: "h0" },
|
|
{ id: "p1", parentId: "p0", role: "assistant", contentHash: "h1" },
|
|
]);
|
|
const firstPage = getConversationTurnPage(row.id, { limit: 20 });
|
|
const newestSeq = firstPage.nodes[firstPage.nodes.length - 1].seq;
|
|
|
|
// Nothing new yet.
|
|
assert.equal(getConversationTurnPage(row.id, { afterSeq: newestSeq }).nodes.length, 0);
|
|
|
|
// A new turn arrives (e.g. a later request continuing this conversation).
|
|
insertConversationTurnNodes(row.id, "corr-page-3b", [
|
|
{ id: "p2", parentId: "p1", role: "user", contentHash: "h2" },
|
|
]);
|
|
const polled = getConversationTurnPage(row.id, { afterSeq: newestSeq });
|
|
assert.equal(polled.nodes.length, 1);
|
|
assert.equal(polled.nodes[0].id, "p2");
|
|
assert.equal(polled.hasMore, false);
|
|
});
|
|
|
|
test("touchOrCreateExternalConversation creates then increments turn_count on repeat calls", () => {
|
|
const id = "ext-conv-test-id";
|
|
touchOrCreateExternalConversation(id, { apiKeyId: "key-d" });
|
|
|
|
const db = getDbInstance();
|
|
const afterCreate = db
|
|
.prepare("SELECT turn_count FROM agentic_conversations WHERE id = ?")
|
|
.get(id) as { turn_count: number };
|
|
assert.equal(afterCreate.turn_count, 1);
|
|
|
|
touchOrCreateExternalConversation(id, { apiKeyId: "key-d" });
|
|
const afterTouch = db
|
|
.prepare("SELECT turn_count FROM agentic_conversations WHERE id = ?")
|
|
.get(id) as { turn_count: number };
|
|
assert.equal(afterTouch.turn_count, 2);
|
|
});
|
|
|
|
test("listMultiTurnConversations only returns conversations with >= 2 actual turn nodes, joined to their latest call_logs row", () => {
|
|
const db = getDbInstance();
|
|
|
|
createAgenticConversation({
|
|
id: "conv-single-turn",
|
|
apiKeyId: null,
|
|
fingerprintHash: "fp-single",
|
|
});
|
|
insertConversationTurnNodes("conv-single-turn", "corr-single", [
|
|
{ id: "single-node-1", parentId: null, role: "user", contentHash: "hash-single-1" },
|
|
]);
|
|
|
|
const multi = createAgenticConversation({
|
|
id: "conv-multi-turn",
|
|
apiKeyId: null,
|
|
fingerprintHash: "fp-multi",
|
|
});
|
|
// turn_count deliberately left at its default of 1 here: it tracks
|
|
// requests-touched, not node count, and a freshly-minted conversation can
|
|
// already carry many turn nodes from a single insert (see the doc comment
|
|
// on listMultiTurnConversations) — the filter must key off actual node
|
|
// count, not turn_count, for this conversation to be listed at all.
|
|
insertConversationTurnNodes(multi.id, "corr-multi", [
|
|
{ id: "multi-node-1", parentId: null, role: "user", contentHash: "hash-multi-1" },
|
|
{
|
|
id: "multi-node-2",
|
|
parentId: "multi-node-1",
|
|
role: "assistant",
|
|
contentHash: "hash-multi-2",
|
|
},
|
|
]);
|
|
|
|
db.prepare(
|
|
`INSERT INTO call_logs (id, timestamp, method, path, status, model, provider, session_tag)
|
|
VALUES (?, ?, 'POST', '/v1/chat/completions', 200, 'big-pickle', 'opencode-zen', ?)`
|
|
).run("multi-turn-1", "2026-03-01T00:00:00.000Z", "conv-multi-turn");
|
|
db.prepare(
|
|
`INSERT INTO call_logs (id, timestamp, method, path, status, model, provider, session_tag)
|
|
VALUES (?, ?, 'POST', '/v1/chat/completions', 200, 'gemma-4', 'gemini', ?)`
|
|
).run("multi-turn-2", "2026-03-01T00:01:00.000Z", "conv-multi-turn");
|
|
|
|
const { rows, total } = listMultiTurnConversations();
|
|
const ids = rows.map((r) => r.id);
|
|
assert.ok(ids.includes("conv-multi-turn"));
|
|
assert.ok(!ids.includes("conv-single-turn"));
|
|
assert.ok(total >= 1);
|
|
|
|
const found = rows.find((r) => r.id === "conv-multi-turn");
|
|
assert.equal(found?.lastCallLogId, "multi-turn-2");
|
|
assert.equal(found?.lastModel, "gemma-4");
|
|
assert.equal(found?.lastProvider, "gemini");
|
|
});
|
|
|
|
test("resolveCallLogIdsByCorrelationIds bulk-resolves correlation_id to call_logs.id", () => {
|
|
const db = getDbInstance();
|
|
|
|
db.prepare(
|
|
`INSERT INTO call_logs (id, timestamp, method, path, status, model, correlation_id)
|
|
VALUES (?, ?, 'POST', '/v1/chat/completions', 200, 'big-pickle', ?)`
|
|
).run("call-corr-1", "2026-04-01T00:00:00.000Z", "corr-a");
|
|
db.prepare(
|
|
`INSERT INTO call_logs (id, timestamp, method, path, status, model, correlation_id)
|
|
VALUES (?, ?, 'POST', '/v1/chat/completions', 200, 'big-pickle', ?)`
|
|
).run("call-corr-2", "2026-04-01T00:01:00.000Z", "corr-b");
|
|
|
|
const resolved = resolveCallLogIdsByCorrelationIds(["corr-a", "corr-b", "corr-missing"]);
|
|
assert.equal(resolved.get("corr-a"), "call-corr-1");
|
|
assert.equal(resolved.get("corr-b"), "call-corr-2");
|
|
assert.equal(resolved.has("corr-missing"), false);
|
|
});
|
|
|
|
test("resolveCallLogIdsByCorrelationIds returns an empty map for an empty/all-falsy input", () => {
|
|
assert.equal(resolveCallLogIdsByCorrelationIds([]).size, 0);
|
|
assert.equal(resolveCallLogIdsByCorrelationIds(["", ""]).size, 0);
|
|
});
|
|
|
|
test.after(async () => {
|
|
await cleanupTempDataDir(process.env.DATA_DIR!);
|
|
});
|