Files
OmniRoute/tests/unit/apikey-policy-default-rate-limits.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

90 lines
3.6 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";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-default-rl-"));
const ORIGINAL_DATA_DIR = process.env.DATA_DIR;
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "default-rate-limit-test-secret";
// Mirror the constants in apiKeyPolicy.ts so the tests document the contract
// rather than re-deriving it from the implementation under test.
const LEGACY_DEFAULT = [
{ limit: 1000, window: 86400 },
{ limit: 5000, window: 604800 },
{ limit: 20000, window: 2592000 },
];
test.after(async () => {
const coreDb = await import("../../src/lib/db/core.ts");
coreDb.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;
}
});
test("apiKeyPolicy exposes no implicit default rate limits (#2289)", async () => {
const { DEFAULT_RATE_LIMITS } = await import("../../src/shared/utils/apiKeyPolicy.ts");
assert.deepEqual(DEFAULT_RATE_LIMITS, []);
});
test("buildDefaultRateLimits: unset / empty env disables implicit fallback limits", async () => {
const { buildDefaultRateLimits } = await import("../../src/shared/utils/apiKeyPolicy.ts");
// No env override means UI-unrestricted API keys really stay unrestricted.
assert.deepEqual(buildDefaultRateLimits(undefined), []);
assert.deepEqual(buildDefaultRateLimits(""), []);
assert.deepEqual(buildDefaultRateLimits(" "), []);
});
test("ENVIRONMENT.md documents unset DEFAULT_RATE_LIMIT_PER_DAY as unlimited (#11017)", () => {
const md = fs.readFileSync(
new URL("../../docs/reference/ENVIRONMENT.md", import.meta.url),
"utf8"
);
const row = md.split("\n").find((line) => line.includes("`DEFAULT_RATE_LIMIT_PER_DAY`"));
assert.ok(row, "ENVIRONMENT.md must document DEFAULT_RATE_LIMIT_PER_DAY");
assert.match(
row,
/unset|empty|unlimited|no implicit/i,
"live env table must match buildDefaultRateLimits() (#2289)"
);
assert.doesNotMatch(
row,
/unset\/empty\/malformed\) keeps the legacy 1000/,
"pre-#2289 1000/day default must not remain in the live table"
);
});
test("buildDefaultRateLimits: explicit '0' opts out — no fallback rules", async () => {
const { buildDefaultRateLimits } = await import("../../src/shared/utils/apiKeyPolicy.ts");
// Explicit "0" remains accepted for deployments that already set it.
assert.deepEqual(buildDefaultRateLimits("0"), []);
});
test("buildDefaultRateLimits: positive N yields N/day, 5N/week, 20N/month", async () => {
const { buildDefaultRateLimits } = await import("../../src/shared/utils/apiKeyPolicy.ts");
assert.deepEqual(buildDefaultRateLimits("100"), [
{ limit: 100, window: 86400 },
{ limit: 500, window: 604800 },
{ limit: 2000, window: 2592000 },
]);
});
test("buildDefaultRateLimits: malformed non-empty input falls back to the legacy default", async () => {
const { buildDefaultRateLimits } = await import("../../src/shared/utils/apiKeyPolicy.ts");
// A typo in an explicit deployment config should not silently disable rate limits.
assert.deepEqual(buildDefaultRateLimits("-5"), LEGACY_DEFAULT);
assert.deepEqual(buildDefaultRateLimits("not-a-number"), LEGACY_DEFAULT);
assert.deepEqual(buildDefaultRateLimits("1000 requests"), LEGACY_DEFAULT);
assert.deepEqual(buildDefaultRateLimits("3.14"), LEGACY_DEFAULT);
});