Files
OmniRoute/tests/unit/api/compression/compression-api.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

229 lines
8.8 KiB
TypeScript

import { describe, it, beforeEach, afterEach, after } from "node:test";
import assert from "node:assert/strict";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Top-level awaits MUST run before any describe registers: under
// --test-force-exit (the CI runner flag) the process exits when the already-
// registered tests finish, so a mid-file `await import` raced the runner and the
// whole second describe died as "Promise resolution is still pending" on slow
// CI machines (base-reds round 3, #9985).
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-compression-route-"));
const ORIGINAL_DATA_DIR = process.env.DATA_DIR;
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../../../src/lib/db/core.ts");
const route = await import("../../../../src/app/api/settings/compression/route.ts");
describe("Compression Settings API Schema Validation", () => {
const compressionModeValues = [
"off",
"lite",
"standard",
"aggressive",
"ultra",
"rtk",
"stacked",
];
it("should validate all compression mode values", () => {
assert.deepStrictEqual(compressionModeValues, [
"off",
"lite",
"standard",
"aggressive",
"ultra",
"rtk",
"stacked",
]);
});
it("should validate caveman config structure", () => {
const defaultCavemanConfig = {
enabled: true,
compressRoles: ["user"],
skipRules: [],
minMessageLength: 50,
preservePatterns: [],
};
assert.equal(defaultCavemanConfig.enabled, true);
assert.deepStrictEqual(defaultCavemanConfig.compressRoles, ["user"]);
assert.equal(Array.isArray(defaultCavemanConfig.skipRules), true);
assert.equal(defaultCavemanConfig.minMessageLength, 50);
assert.equal(Array.isArray(defaultCavemanConfig.preservePatterns), true);
});
it("should validate full compression config structure", () => {
const defaultConfig = {
enabled: false,
defaultMode: "off",
autoTriggerTokens: 0,
cacheMinutes: 5,
preserveSystemPrompt: true,
comboOverrides: {},
cavemanConfig: {
enabled: true,
compressRoles: ["user"],
skipRules: [],
minMessageLength: 50,
preservePatterns: [],
},
ultra: {
enabled: false,
compressionRate: 0.5,
minScoreThreshold: 0.3,
slmFallbackToAggressive: true,
maxTokensPerMessage: 0,
},
};
assert.equal(defaultConfig.enabled, false);
assert.ok(compressionModeValues.includes(defaultConfig.defaultMode));
assert.equal(typeof defaultConfig.autoTriggerTokens, "number");
assert.equal(typeof defaultConfig.cacheMinutes, "number");
assert.equal(typeof defaultConfig.preserveSystemPrompt, "boolean");
assert.equal(typeof defaultConfig.comboOverrides, "object");
assert.equal(typeof defaultConfig.cavemanConfig, "object");
assert.equal(typeof defaultConfig.ultra, "object");
assert.equal(defaultConfig.ultra.compressionRate, 0.5);
});
it("should validate all caveman compression rules are defined", async () => {
const { CAVEMAN_RULES } =
await import("../../../../open-sse/services/compression/cavemanRules.ts");
assert.ok(Array.isArray(CAVEMAN_RULES));
assert.ok(CAVEMAN_RULES.length >= 29, `Expected >= 29 rules, got ${CAVEMAN_RULES.length}`);
for (const rule of CAVEMAN_RULES) {
assert.ok(rule.name && typeof rule.name === "string", `Rule must have a name`);
assert.ok(rule.pattern instanceof RegExp, `Rule ${rule.name} must have a RegExp pattern`);
assert.ok(
typeof rule.replacement === "string" || typeof rule.replacement === "function",
`Rule ${rule.name} must have string or function replacement`
);
assert.ok(
rule.pattern.source !== "^$" || rule.replacement !== "",
`Rule ${rule.name} must not be a no-op (empty pattern + empty replacement)`
);
}
});
it("should validate compression modes cover all CavemanConfig roles", () => {
const validRoles = ["user", "assistant", "system"];
for (const role of validRoles) {
assert.ok(validRoles.includes(role), `Role ${role} should be valid`);
}
assert.equal(validRoles.length, 3);
});
});
// ─── Route round-trip: engines map + activeComboId ─────────────────────────
// Mirrors the mcp-accessibility-config test harness: allocate a temp DATA_DIR,
// import route + DB modules, tear down in after().
function makeRequest(method: string, body?: unknown): Request {
return new Request("http://localhost/api/settings/compression", {
method,
headers: body !== undefined ? { "content-type": "application/json" } : {},
body: body !== undefined ? JSON.stringify(body) : undefined,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as any;
}
describe("settings/compression route — engines + activeComboId", () => {
beforeEach(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
});
after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
if (ORIGINAL_DATA_DIR === undefined) delete process.env.DATA_DIR;
else process.env.DATA_DIR = ORIGINAL_DATA_DIR;
});
it("PUT engines map persists and GET returns engines + activeComboId", async () => {
const putRes = await route.PUT(
makeRequest("PUT", { engines: { rtk: { enabled: true, level: "standard" } } })
);
assert.equal(putRes.status, 200);
// Fresh DB handle so we read from storage, not from the write-path return value.
core.resetDbInstance();
const getRes = await route.GET(makeRequest("GET"));
assert.equal(getRes.status, 200);
const body = await getRes.json();
assert.equal(body.engines?.rtk?.enabled, true, "engines.rtk.enabled should be true after PUT");
assert.equal(
body.engines?.rtk?.level,
"standard",
"engines.rtk.level should be 'standard' after PUT"
);
// activeComboId is always present (null by default)
assert.ok("activeComboId" in body, "GET response must include activeComboId");
});
it("PUT activeComboId persists and is returned by GET", async () => {
const putRes = await route.PUT(makeRequest("PUT", { activeComboId: "combo-abc" }));
assert.equal(putRes.status, 200);
core.resetDbInstance();
const getRes = await route.GET(makeRequest("GET"));
assert.equal(getRes.status, 200);
const body = await getRes.json();
assert.equal(body.activeComboId, "combo-abc");
});
it("PUT activeComboId:null clears the active combo", async () => {
// First set it, then clear.
await route.PUT(makeRequest("PUT", { activeComboId: "combo-to-clear" }));
core.resetDbInstance();
await route.PUT(makeRequest("PUT", { activeComboId: null }));
core.resetDbInstance();
const getRes = await route.GET(makeRequest("GET"));
assert.equal(getRes.status, 200);
const body = await getRes.json();
assert.equal(body.activeComboId, null);
});
it("PUT with invalid engines shape is rejected by schema validation (400)", async () => {
// engines values must have an `enabled` boolean — passing a string should fail the schema.
const putRes = await route.PUT(makeRequest("PUT", { engines: { rtk: { enabled: "yes" } } }));
assert.equal(putRes.status, 400);
const body = await putRes.json();
// Validation failures use { error: { message, details } } via validateBody helper.
assert.ok(body.error !== null && typeof body.error === "object", "error should be an object");
const errorMessage: string =
typeof body.error === "string"
? body.error
: (body.error?.message ?? JSON.stringify(body.error));
assert.ok(!errorMessage.includes("at /"), "error must not contain a stack trace");
});
it("PUT accepts enginesExplicit (round-tripped from GET response)", async () => {
// Regression: the GET handler injects `enginesExplicit` (compression.ts:632) so the
// hub/panel can round-trip the full settings object. The previous .strict() PUT schema
// rejected it with 400 ("Unrecognized key: enginesExplicit"), causing every toggle on
// the Compression Hub / Panel to revert. Allow it through.
const putRes = await route.PUT(makeRequest("PUT", { enabled: true, enginesExplicit: true }));
assert.equal(putRes.status, 200);
core.resetDbInstance();
const putRes2 = await route.PUT(makeRequest("PUT", { enabled: false, enginesExplicit: false }));
assert.equal(putRes2.status, 200);
});
});