Files
OmniRoute/tests/unit/api/compression-preview-engine.test.ts
diegosouzapw b3f6f697ef 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.
2026-09-01 00:16:48 -03:00

169 lines
6.6 KiB
TypeScript

/**
* TDD: per-engine compression preview via engineId param.
*
* Auth pattern mirrors tests/unit/compression/compression-preview-auth.test.ts:
* - Use makeManagementSessionRequest to create a JWT-auth'd Request.
* - Set DATA_DIR to a temp dir and run the DB setup before importing the route.
*/
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";
import { makeManagementSessionRequest } from "../../helpers/managementSession.ts";
// ─── temp DB isolation ────────────────────────────────────────────────────────
const TEST_DATA_DIR = fs.mkdtempSync(
path.join(os.tmpdir(), "omniroute-compression-preview-engine-")
);
const originalDataDir = process.env.DATA_DIR;
const originalJwtSecret = process.env.JWT_SECRET;
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../../src/lib/db/core.ts");
const settingsDb = await import("../../../src/lib/db/settings.ts");
const previewRoute = await import("../../../src/app/api/compression/preview/route.ts");
// ─── helpers ──────────────────────────────────────────────────────────────────
async function setupAuth(): Promise<void> {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
await settingsDb.updateSettings({
requireLogin: true,
setupComplete: true,
password: "test-password-hash",
});
}
/**
* Build a message array whose content is a pretty-printed JSON string of a homogeneous
* array of ≥8 rows. headroom's crushText() tries the whole string as a JSON array when
* it starts with "[", so passing JSON.stringify(rows, null, 2) as content triggers
* compaction. Pretty-printed JSON has whitespace so countTokens() also gives a
* meaningful baseline to compare against the compact tabular output.
*/
function buildHeadroomMessages(): Array<{ role: string; content: string }> {
const rows = Array.from({ length: 10 }, (_, i) => ({
id: i + 1,
name: `item-${i + 1}`,
value: (i + 1) * 100,
active: true,
}));
// Pretty-print so word-split countTokens() produces a non-trivial baseline count
return [{ role: "user", content: JSON.stringify(rows, null, 2) }];
}
// ─── lifecycle ────────────────────────────────────────────────────────────────
test.beforeEach(async () => {
process.env.DATA_DIR = TEST_DATA_DIR;
await setupAuth();
});
test.after(() => {
if (originalDataDir === undefined) delete process.env.DATA_DIR;
else process.env.DATA_DIR = originalDataDir;
if (originalJwtSecret === undefined) delete process.env.JWT_SECRET;
else process.env.JWT_SECRET = originalJwtSecret;
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
// ─── tests ────────────────────────────────────────────────────────────────────
test("POST /api/compression/preview with engineId=headroom returns 200 with token savings", async () => {
const messages = buildHeadroomMessages();
const request = await makeManagementSessionRequest("http://localhost/api/compression/preview", {
method: "POST",
body: {
engineId: "headroom",
messages,
mode: "stacked",
},
});
const response = await previewRoute.POST(request);
assert.equal(response.status, 200, `Expected 200, got ${response.status}`);
const body = (await response.json()) as {
originalTokens: number;
compressedTokens: number;
tokensSaved: number;
savingsPct: number;
techniquesUsed: string[];
original: string;
compressed: string;
mode: string;
};
assert.ok(
typeof body.originalTokens === "number" && body.originalTokens > 0,
`originalTokens should be > 0, got: ${body.originalTokens}`
);
assert.ok(
typeof body.compressedTokens === "number",
`compressedTokens should be a number, got: ${body.compressedTokens}`
);
assert.ok(
body.compressedTokens < body.originalTokens,
`compressedTokens (${body.compressedTokens}) should be < originalTokens (${body.originalTokens}) — headroom should compact a 10-row homogeneous JSON array`
);
assert.ok(body.tokensSaved > 0, `tokensSaved should be > 0, got: ${body.tokensSaved}`);
assert.ok(body.savingsPct > 0, `savingsPct should be > 0, got: ${body.savingsPct}`);
// Response shape completeness checks
assert.ok(typeof body.original === "string", "should have original field");
assert.ok(typeof body.compressed === "string", "should have compressed field");
assert.ok(Array.isArray(body.techniquesUsed), "techniquesUsed should be an array");
});
test("POST /api/compression/preview with engineId but no mode still works (mode defaults to stacked)", async () => {
const messages = buildHeadroomMessages();
const request = await makeManagementSessionRequest("http://localhost/api/compression/preview", {
method: "POST",
body: {
engineId: "headroom",
messages,
// mode intentionally omitted — should default to "stacked" when engineId present
},
});
const response = await previewRoute.POST(request);
assert.equal(
response.status,
200,
`Expected 200 when mode omitted with engineId, got ${response.status}`
);
const body = (await response.json()) as { tokensSaved: number };
assert.ok(body.tokensSaved > 0, `tokensSaved should be > 0 even without explicit mode`);
});
test("POST /api/compression/preview without engineId still works (existing path untouched)", async () => {
const request = await makeManagementSessionRequest("http://localhost/api/compression/preview", {
method: "POST",
body: {
messages: [{ role: "user", content: "Hello world" }],
mode: "standard",
},
});
const response = await previewRoute.POST(request);
assert.equal(
response.status,
200,
`Existing path should still return 200, got ${response.status}`
);
const body = (await response.json()) as { original: string; mode: string };
assert.ok(typeof body.original === "string", "should have original field");
assert.equal(body.mode, "standard", "mode field should reflect the requested mode");
});