Files
OmniRoute/tests/unit/log-retention.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

254 lines
8.9 KiB
TypeScript

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(), "omniroute-log-retention-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.APP_LOG_RETENTION_DAYS = "2";
process.env.CALL_LOG_RETENTION_DAYS = "1";
process.env.CALL_LOGS_TABLE_MAX_ROWS = "5";
process.env.PROXY_LOGS_TABLE_MAX_ROWS = "5";
const core = await import("../../src/lib/db/core.ts");
const compliance = await import("../../src/lib/compliance/index.ts");
function resetStorage() {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
test.beforeEach(() => {
resetStorage();
});
test.after(() => {
resetStorage();
});
test("cleanupExpiredLogs uses separate APP and CALL retention windows", async () => {
compliance.initAuditLog();
const db = core.getDbInstance();
const oldCallTs = new Date(Date.now() - 3 * 24 * 60 * 60 * 1000).toISOString();
const freshCallTs = new Date().toISOString();
const oldAppTs = new Date(Date.now() - 5 * 24 * 60 * 60 * 1000).toISOString();
const freshAppTs = new Date().toISOString();
db.prepare(
"INSERT INTO usage_history (provider, model, tokens_input, tokens_output, success, latency_ms, ttft_ms, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
).run("openai", "old-usage", 1, 1, 1, 1, 1, oldCallTs);
db.prepare(
"INSERT INTO usage_history (provider, model, tokens_input, tokens_output, success, latency_ms, ttft_ms, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
).run("openai", "fresh-usage", 1, 1, 1, 1, 1, freshCallTs);
db.prepare(
"INSERT INTO call_logs (id, timestamp, method, path, status, model, provider, account, duration, tokens_in, tokens_out, has_pipeline_details) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
).run(
"old-call",
oldCallTs,
"POST",
"/v1/chat/completions",
200,
"old",
"openai",
"-",
1,
1,
1,
0
);
db.prepare(
"INSERT INTO call_logs (id, timestamp, method, path, status, model, provider, account, duration, tokens_in, tokens_out, has_pipeline_details) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
).run(
"fresh-call",
freshCallTs,
"POST",
"/v1/chat/completions",
200,
"fresh",
"openai",
"-",
1,
1,
1,
0
);
db.prepare(
"INSERT INTO proxy_logs (id, timestamp, status, level, latency_ms) VALUES (?, ?, ?, ?, ?)"
).run("old-proxy", oldCallTs, "success", "direct", 1);
db.prepare(
"INSERT INTO proxy_logs (id, timestamp, status, level, latency_ms) VALUES (?, ?, ?, ?, ?)"
).run("fresh-proxy", freshCallTs, "success", "direct", 1);
db.prepare("INSERT INTO request_detail_logs (id, timestamp, duration_ms) VALUES (?, ?, ?)").run(
"old-detail",
oldCallTs,
1
);
db.prepare("INSERT INTO request_detail_logs (id, timestamp, duration_ms) VALUES (?, ?, ?)").run(
"fresh-detail",
freshCallTs,
1
);
db.prepare("INSERT INTO audit_log (timestamp, action, actor) VALUES (?, ?, ?)").run(
oldAppTs,
"old-audit",
"system"
);
db.prepare("INSERT INTO audit_log (timestamp, action, actor) VALUES (?, ?, ?)").run(
freshAppTs,
"fresh-audit",
"system"
);
db.prepare("INSERT INTO mcp_tool_audit (tool_name, success, created_at) VALUES (?, ?, ?)").run(
"old-tool",
1,
oldAppTs
);
db.prepare("INSERT INTO mcp_tool_audit (tool_name, success, created_at) VALUES (?, ?, ?)").run(
"fresh-tool",
1,
freshAppTs
);
const result = await compliance.cleanupExpiredLogs();
assert.equal(result.deletedUsage, 1);
assert.equal(result.deletedCallLogs, 1);
assert.equal(result.deletedProxyLogs, 1);
assert.equal(result.deletedRequestDetailLogs, 1);
assert.equal(result.deletedAuditLogs, 1);
assert.equal(result.deletedMcpAuditLogs, 1);
assert.deepEqual(compliance.getRetentionDays(), { app: 2, call: 1 });
assert.equal((db.prepare("SELECT COUNT(*) AS cnt FROM usage_history").get() as any).cnt, 1);
assert.equal((db.prepare("SELECT COUNT(*) AS cnt FROM call_logs").get() as any).cnt, 1);
assert.equal((db.prepare("SELECT COUNT(*) AS cnt FROM proxy_logs").get() as any).cnt, 1);
assert.equal((db.prepare("SELECT COUNT(*) AS cnt FROM request_detail_logs").get() as any).cnt, 1);
assert.equal((db.prepare("SELECT COUNT(*) AS cnt FROM audit_log").get() as any).cnt, 2);
assert.equal((db.prepare("SELECT COUNT(*) AS cnt FROM mcp_tool_audit").get() as any).cnt, 1);
});
test("cleanupExpiredLogs honors the dashboard usageHistory retention when env is unset (#4354)", async () => {
// Operator did NOT set the retention env vars; the dashboard configured 90 days.
// The startup cleanup must respect the dashboard value, not the 7-day env default.
const savedCall = process.env.CALL_LOG_RETENTION_DAYS;
const savedApp = process.env.APP_LOG_RETENTION_DAYS;
delete process.env.CALL_LOG_RETENTION_DAYS;
delete process.env.APP_LOG_RETENTION_DAYS;
try {
compliance.initAuditLog();
const db = core.getDbInstance();
const { updateDatabaseSettings } = await import("../../src/lib/db/databaseSettings.ts");
updateDatabaseSettings({ retention: { usageHistory: 90, callLogs: 90 } } as any);
const oldTs = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString();
db.prepare(
"INSERT INTO usage_history (provider, model, tokens_input, tokens_output, success, latency_ms, ttft_ms, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
).run("openai", "old-usage", 1, 1, 1, 1, 1, oldTs);
const result = await compliance.cleanupExpiredLogs();
// 30 days < the configured 90-day dashboard retention → must be kept.
// With the old env-default (7d) behavior this row would be deleted.
assert.equal(
result.deletedUsage,
0,
"30-day usage_history must survive a 90-day dashboard retention"
);
assert.equal((db.prepare("SELECT COUNT(*) AS cnt FROM usage_history").get() as any).cnt, 1);
} finally {
if (savedCall !== undefined) process.env.CALL_LOG_RETENTION_DAYS = savedCall;
else delete process.env.CALL_LOG_RETENTION_DAYS;
if (savedApp !== undefined) process.env.APP_LOG_RETENTION_DAYS = savedApp;
else delete process.env.APP_LOG_RETENTION_DAYS;
}
});
test("cleanupExpiredLogs enforces row count limits", async () => {
compliance.initAuditLog();
const db = core.getDbInstance();
const now = new Date().toISOString();
for (let i = 0; i < 10; i++) {
db.prepare(
"INSERT INTO call_logs (id, timestamp, method, path, status, model, provider, account, duration, tokens_in, tokens_out, has_pipeline_details) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
).run(
`call-${i}`,
now,
"POST",
"/v1/chat/completions",
200,
"model",
"provider",
"-",
1,
1,
1,
0
);
}
for (let i = 0; i < 10; i++) {
db.prepare(
"INSERT INTO proxy_logs (id, timestamp, status, level, latency_ms) VALUES (?, ?, ?, ?, ?)"
).run(`proxy-${i}`, now, "success", "direct", 1);
}
assert.equal((db.prepare("SELECT COUNT(*) AS cnt FROM call_logs").get() as any).cnt, 10);
assert.equal((db.prepare("SELECT COUNT(*) AS cnt FROM proxy_logs").get() as any).cnt, 10);
const result = await compliance.cleanupExpiredLogs();
assert.equal(result.trimmedCallLogs, 5);
assert.equal(result.trimmedProxyLogs, 5);
assert.equal(result.callLogsMaxRows, 5);
assert.equal(result.proxyLogsMaxRows, 5);
assert.equal((db.prepare("SELECT COUNT(*) AS cnt FROM call_logs").get() as any).cnt, 5);
assert.equal((db.prepare("SELECT COUNT(*) AS cnt FROM proxy_logs").get() as any).cnt, 5);
});
test("getCallLogsTableMaxRows returns configured value", async () => {
const { getCallLogsTableMaxRows, getProxyLogsTableMaxRows } =
await import("../../src/lib/logEnv.ts");
assert.equal(getCallLogsTableMaxRows(), 5);
assert.equal(getProxyLogsTableMaxRows(), 5);
});
test("call log pipeline env helpers parse stream chunk flag and size cap", async () => {
const originalCapture = process.env.CALL_LOG_PIPELINE_CAPTURE_STREAM_CHUNKS;
const originalMaxSize = process.env.CALL_LOG_PIPELINE_MAX_SIZE_KB;
const { getCallLogPipelineCaptureStreamChunks, getCallLogPipelineMaxSizeBytes } =
await import("../../src/lib/logEnv.ts");
try {
process.env.CALL_LOG_PIPELINE_CAPTURE_STREAM_CHUNKS = "false";
process.env.CALL_LOG_PIPELINE_MAX_SIZE_KB = "256";
assert.equal(getCallLogPipelineCaptureStreamChunks(), false);
assert.equal(getCallLogPipelineMaxSizeBytes(), 256 * 1024);
} finally {
if (originalCapture === undefined) {
delete process.env.CALL_LOG_PIPELINE_CAPTURE_STREAM_CHUNKS;
} else {
process.env.CALL_LOG_PIPELINE_CAPTURE_STREAM_CHUNKS = originalCapture;
}
if (originalMaxSize === undefined) {
delete process.env.CALL_LOG_PIPELINE_MAX_SIZE_KB;
} else {
process.env.CALL_LOG_PIPELINE_MAX_SIZE_KB = originalMaxSize;
}
}
});