Files
OmniRoute/tests/unit/chatcore-compression-cache-stats.test.ts
Diego Rodrigues de Sa e Souza 93265eede3 test(infra): retry recursive temp-dir removal on main (main twin of #11968) (#12246)
* test(infra): retry recursive temp-dir removal on main (main twin of #11968)

`main` has been red since b342c1a361 on the vitest and integration gates:

  ✖ tests/unit/autoCombo/provider-family-combos.test.ts > auto/<family>
  ✖ chat pipeline applies Codex OAuth fingerprint and priority tier inside combos

Both call resetStorage() from beforeEach, which does an fs.rmSync(TEST_DATA_DIR,
{recursive: true, force: true}) with no retry, and intermittently loses the race
with a not-yet-released SQLite handle (ENOTEMPTY).

release/v3.8.51 fixed this in #11968 with a mechanical codemod adding
maxRetries/retryDelay to every recursive rm/rmSync/rmdirSync under tests/, but
that PR landed only on the release branch. Because main only receives work at
the release squash, it stayed broken for the whole cycle — and repo-wide gates
then turn every open PR into main red on checks unrelated to their diff.

This is the --base main twin: re-runs the same codemod that already shipped on
the release branch (scripts/ad-hoc/codemod-rm-maxretries.mjs), so the two
branches converge on identical test-teardown semantics. Test-only; no product
logic is touched.

The remaining three failures reported on #12133 (unit full suite exceeding its
4800s ceiling, package-artifact exceeding 1200s, and the boot-smoke that is
skipped as a consequence) are runner-contention timeouts, not code defects —
validate-release-green.mjs runs those heavy gates concurrently on one shared
hosted runner. There is no fix to port for those.

* chore(scripts): carry the rm-maxretries codemod onto main alongside its output

The codemod that generated the previous commit lives in the repo on
release/v3.8.51 (added by #11968) but was never on main. Bringing it over keeps
the tool next to the change it produced, so the transformation stays
reproducible and auditable from either branch.
2026-09-01 01:48:00 -03:00

91 lines
3.0 KiB
TypeScript

// Characterization of recordCompressionCacheStats — the compression cache-stats hook extracted
// from handleChatCore's request-setup compression path (chatCore god-file decomposition, #3501).
// Fire-and-forget; uses a real temp DB and polls compression_cache_stats. Locks: a compressed
// prompt records a cache-stats row with the resolved provider/mode/tokens-saved, and the helper
// returns synchronously without throwing (fail-open).
import { test, before, after } 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 testDataDir = fs.mkdtempSync(path.join(os.tmpdir(), "omni-comp-cache-test-"));
process.env.DATA_DIR = testDataDir;
const coreDb = await import("../../src/lib/db/core.ts");
const { recordCompressionCacheStats } = await import(
"../../open-sse/handlers/chatCore/compressionCacheStats.ts"
);
function rowsFor(provider: string): Array<Record<string, unknown>> {
return coreDb
.getDbInstance()
.prepare(
"SELECT provider, compression_mode AS mode, tokens_saved_compression AS saved FROM compression_cache_stats WHERE provider = ?"
)
.all(provider) as Array<Record<string, unknown>>;
}
async function waitForRows(provider: string, min: number, timeoutMs = 3000) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (rowsFor(provider).length >= min) break;
await new Promise((r) => setTimeout(r, 25));
}
return rowsFor(provider);
}
before(async () => {
await coreDb.ensureDbInitialized();
});
after(() => {
coreDb.resetDbInstance();
try {
fs.rmSync(testDataDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
} catch {
// best-effort cleanup
}
});
test("returns synchronously without throwing (fire-and-forget)", () => {
assert.doesNotThrow(() =>
recordCompressionCacheStats({
compressionInputBody: { messages: [{ role: "user", content: "hi" }] },
provider: "openai",
targetFormat: "openai",
effectiveModel: "gpt-x",
mode: "balanced",
stats: { originalTokens: 100, compressedTokens: 60 },
})
);
});
test("records a cache-stats row with the resolved provider/mode/tokens-saved", async () => {
recordCompressionCacheStats({
compressionInputBody: { messages: [{ role: "user", content: "hello world" }] },
provider: "ccs-prov",
targetFormat: "openai",
effectiveModel: "gpt-ccs",
mode: "aggressive",
stats: { originalTokens: 200, compressedTokens: 75 },
});
const rows = await waitForRows("ccs-prov", 1);
assert.equal(rows.length, 1);
assert.equal(rows[0].mode, "aggressive");
assert.equal(rows[0].saved, 125);
});
test("clamps negative tokens-saved to 0", async () => {
recordCompressionCacheStats({
compressionInputBody: { messages: [] },
provider: "ccs-neg",
targetFormat: "openai",
effectiveModel: "gpt-x",
mode: "balanced",
stats: { originalTokens: 50, compressedTokens: 80 },
});
const rows = await waitForRows("ccs-neg", 1);
assert.equal(rows[0].saved, 0);
});