mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 19:02:17 +03:00
* 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.
152 lines
5.6 KiB
TypeScript
152 lines
5.6 KiB
TypeScript
/**
|
|
* TDD: getPerEngineAnalytics — per-engine aggregation from compression_analytics.
|
|
*
|
|
* DB isolation pattern mirrors tests/unit/db/api-keys.test.ts:
|
|
* - Temp DATA_DIR, resetDbInstance() before/after, close in test.after().
|
|
*/
|
|
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";
|
|
|
|
// ─── isolated temp DB ─────────────────────────────────────────────────────────
|
|
|
|
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-per-engine-analytics-"));
|
|
const originalDataDir = process.env.DATA_DIR;
|
|
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
|
|
const core = await import("../../../src/lib/db/core.ts");
|
|
core.resetDbInstance();
|
|
|
|
const { insertCompressionAnalyticsRow, getPerEngineAnalytics } =
|
|
await import("../../../src/lib/db/compressionAnalytics.ts");
|
|
|
|
// ─── helpers ──────────────────────────────────────────────────────────────────
|
|
|
|
function resetDb(): void {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
}
|
|
|
|
// ─── lifecycle ────────────────────────────────────────────────────────────────
|
|
|
|
test.beforeEach(() => {
|
|
resetDb();
|
|
});
|
|
|
|
test.after(() => {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
if (originalDataDir === undefined) delete process.env.DATA_DIR;
|
|
else process.env.DATA_DIR = originalDataDir;
|
|
});
|
|
|
|
// ─── tests ────────────────────────────────────────────────────────────────────
|
|
|
|
test("getPerEngineAnalytics returns runs=0 and zeroed metrics for unknown engine", () => {
|
|
const result = getPerEngineAnalytics("unknown-engine");
|
|
assert.equal(result.engineId, "unknown-engine");
|
|
assert.equal(result.runs, 0);
|
|
assert.equal(result.tokensSaved, 0);
|
|
assert.equal(result.avgSavingsPercent, 0);
|
|
assert.equal(result.days, 7);
|
|
});
|
|
|
|
test("getPerEngineAnalytics returns correct aggregation for headroom rows only", () => {
|
|
const now = new Date().toISOString();
|
|
|
|
// 2 headroom rows: original=1000/compressed=800/saved=200 and original=500/compressed=350/saved=150
|
|
insertCompressionAnalyticsRow({
|
|
timestamp: now,
|
|
mode: "stacked",
|
|
engine: "headroom",
|
|
original_tokens: 1000,
|
|
compressed_tokens: 800,
|
|
tokens_saved: 200,
|
|
});
|
|
insertCompressionAnalyticsRow({
|
|
timestamp: now,
|
|
mode: "stacked",
|
|
engine: "headroom",
|
|
original_tokens: 500,
|
|
compressed_tokens: 350,
|
|
tokens_saved: 150,
|
|
});
|
|
|
|
// 1 caveman row that must NOT appear in headroom results
|
|
insertCompressionAnalyticsRow({
|
|
timestamp: now,
|
|
mode: "stacked",
|
|
engine: "caveman",
|
|
original_tokens: 800,
|
|
compressed_tokens: 600,
|
|
tokens_saved: 200,
|
|
});
|
|
|
|
const result = getPerEngineAnalytics("headroom");
|
|
|
|
assert.equal(result.engineId, "headroom");
|
|
assert.equal(result.runs, 2, "should count only the 2 headroom rows");
|
|
assert.equal(result.tokensSaved, 350, "should sum tokens_saved for headroom rows");
|
|
|
|
// avgSavingsPercent = round(((1500 - 1150) / 1500) * 1000) / 10
|
|
// = round((350/1500) * 1000) / 10
|
|
// = round(233.33...) / 10
|
|
// = 233 / 10 = 23.3
|
|
assert.equal(result.avgSavingsPercent, 23.3, `expected 23.3%, got ${result.avgSavingsPercent}`);
|
|
assert.equal(result.days, 7);
|
|
});
|
|
|
|
test("getPerEngineAnalytics excludes rows outside the days window", () => {
|
|
const recent = new Date().toISOString();
|
|
// 30 days ago — outside the default 7-day window
|
|
const old = new Date(Date.now() - 30 * 86400_000).toISOString();
|
|
|
|
insertCompressionAnalyticsRow({
|
|
timestamp: recent,
|
|
mode: "stacked",
|
|
engine: "headroom",
|
|
original_tokens: 1000,
|
|
compressed_tokens: 800,
|
|
tokens_saved: 200,
|
|
});
|
|
insertCompressionAnalyticsRow({
|
|
timestamp: old,
|
|
mode: "stacked",
|
|
engine: "headroom",
|
|
original_tokens: 2000,
|
|
compressed_tokens: 1000,
|
|
tokens_saved: 1000,
|
|
});
|
|
|
|
const result = getPerEngineAnalytics("headroom", 7);
|
|
assert.equal(result.runs, 1, "should only count the recent row within 7 days");
|
|
assert.equal(result.tokensSaved, 200);
|
|
});
|
|
|
|
test("getPerEngineAnalytics falls back to mode column when engine is null (COALESCE behaviour)", () => {
|
|
const now = new Date().toISOString();
|
|
|
|
// Insert a row where engine is explicitly omitted — insertCompressionAnalyticsRow
|
|
// writes engine = row.engine ?? row.mode, so we cannot get NULL via the helper.
|
|
// Instead, insert raw via the DB to simulate a pre-engine row.
|
|
const db = core.getDbInstance();
|
|
db.prepare(
|
|
`INSERT INTO compression_analytics (timestamp, mode, engine, original_tokens, compressed_tokens, tokens_saved)
|
|
VALUES (?, ?, NULL, ?, ?, ?)`
|
|
).run(now, "caveman", 600, 400, 200);
|
|
|
|
// COALESCE(engine, mode) = 'caveman' for the row above
|
|
const result = getPerEngineAnalytics("caveman");
|
|
assert.equal(result.runs, 1, "COALESCE fallback should match engine=NULL, mode=caveman");
|
|
assert.equal(result.tokensSaved, 200);
|
|
});
|
|
|
|
test("getPerEngineAnalytics accepts custom days parameter", () => {
|
|
const result = getPerEngineAnalytics("headroom", 30);
|
|
assert.equal(result.days, 30);
|
|
});
|