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

156 lines
5.4 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";
import { makeManagementSessionRequest } from "../../helpers/managementSession.ts";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-compression-preview-"));
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");
const languagePacksRoute = await import("../../../src/app/api/compression/language-packs/route.ts");
const rulesRoute = await import("../../../src/app/api/compression/rules/route.ts");
type ErrorResponseBody = {
error: string | { message?: string };
};
async function resetAuthRequiredStorage(): 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",
});
}
test.beforeEach(async () => {
process.env.DATA_DIR = TEST_DATA_DIR;
await resetAuthRequiredStorage();
});
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 });
});
test("compression preview requires management auth before reading preview input", async () => {
const response = await previewRoute.POST(
new Request("http://localhost/api/compression/preview", {
method: "POST",
body: JSON.stringify({
messages: [{ role: "tool", content: "same\nsame\nsame" }],
mode: "rtk",
}),
})
);
const body = (await response.json()) as ErrorResponseBody;
assert.equal(response.status, 401);
assert.equal(
typeof body.error === "object" ? body.error.message : null,
"Authentication required"
);
});
test("compression preview rejects bearer tokens without a dashboard session", async () => {
const response = await previewRoute.POST(
new Request("http://localhost/api/compression/preview", {
method: "POST",
headers: { authorization: "Bearer invalid-management-token" },
body: JSON.stringify({
messages: [{ role: "tool", content: "same\nsame\nsame" }],
mode: "rtk",
}),
})
);
const body = (await response.json()) as ErrorResponseBody;
assert.equal(response.status, 403);
assert.equal(
typeof body.error === "object" ? body.error.message : null,
"Invalid management token"
);
});
test("compression preview still runs for authenticated management sessions", async () => {
const request = await makeManagementSessionRequest("http://localhost/api/compression/preview", {
method: "POST",
body: {
messages: [{ role: "tool", content: "same\nsame\nsame" }],
mode: "rtk",
config: {
rtkConfig: {
intensity: "standard",
},
},
},
});
const response = await previewRoute.POST(request);
const body = (await response.json()) as { mode?: string; original?: string };
assert.equal(response.status, 200);
assert.equal(body.mode, "rtk");
assert.match(body.original || "", /same/);
});
test("compression language pack metadata requires management auth", async () => {
const response = await languagePacksRoute.GET(
new Request("http://localhost/api/compression/language-packs")
);
const body = (await response.json()) as ErrorResponseBody;
assert.equal(response.status, 401);
assert.equal(
typeof body.error === "object" ? body.error.message : null,
"Authentication required"
);
});
test("compression rules metadata rejects bearer tokens without a dashboard session", async () => {
const response = await rulesRoute.GET(
new Request("http://localhost/api/compression/rules", {
headers: { authorization: "Bearer invalid-management-token" },
})
);
const body = (await response.json()) as ErrorResponseBody;
assert.equal(response.status, 403);
assert.equal(
typeof body.error === "object" ? body.error.message : null,
"Invalid management token"
);
});
test("compression metadata routes return data for authenticated management sessions", async () => {
const languageRequest = await makeManagementSessionRequest(
"http://localhost/api/compression/language-packs"
);
const rulesRequest = await makeManagementSessionRequest("http://localhost/api/compression/rules");
const languageResponse = await languagePacksRoute.GET(languageRequest);
const rulesResponse = await rulesRoute.GET(rulesRequest);
const languageBody = (await languageResponse.json()) as { languages?: unknown; packs?: unknown };
const rulesBody = (await rulesResponse.json()) as { rules?: unknown };
assert.equal(languageResponse.status, 200);
assert.ok(Array.isArray(languageBody.languages));
assert.ok(Array.isArray(languageBody.packs));
assert.equal(rulesResponse.status, 200);
assert.ok(Array.isArray(rulesBody.rules));
});