Files
OmniRoute/tests/unit/quota-phase2.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

112 lines
3.7 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(), "omni-quota-phase2-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const coreDb = await import("../../src/lib/db/core.ts");
const { parseProviderQuotaHeaders, applyQuotaHeadersToState } = await import(
"../../src/lib/quota/quotaAdapters"
);
const { getQuotaAnalyticsSummary } = await import("../../src/lib/quota/quotaAnalytics");
const { getActiveQuotaResetItems, resetExpiredQuotaWindows } = await import(
"../../src/lib/quota/quotaResetTimers"
);
const { recordProviderQuotaUsage, getProviderQuota } = await import(
"../../src/lib/quota/providerQuotaState"
);
const { getDbInstance } = coreDb;
async function resetStorage() {
coreDb.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
test.beforeEach(async () => {
await resetStorage();
});
test.after(() => {
coreDb.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test("parseProviderQuotaHeaders: parses OpenAI rate limit headers", () => {
const headers = new Headers({
"x-ratelimit-limit-tokens": "100000",
"x-ratelimit-remaining-tokens": "80000",
"x-ratelimit-reset-tokens": "60s",
});
const parsed = parseProviderQuotaHeaders(headers, "openai");
assert.ok(parsed);
assert.equal(parsed?.tokenLimit, 100000);
assert.equal(parsed?.tokensRemaining, 80000);
assert.equal(parsed?.tokensUsed, 20000);
assert.equal(parsed?.windowResetMs, 60000);
});
test("parseProviderQuotaHeaders: parses Anthropic rate limit headers", () => {
const headers = new Headers({
"anthropic-ratelimit-input-tokens-limit": "50000",
"anthropic-ratelimit-input-tokens-remaining": "10000",
"anthropic-ratelimit-input-tokens-reset": "30s",
});
const parsed = parseProviderQuotaHeaders(headers, "anthropic");
assert.ok(parsed);
assert.equal(parsed?.tokenLimit, 50000);
assert.equal(parsed?.tokensRemaining, 10000);
assert.equal(parsed?.tokensUsed, 40000);
assert.equal(parsed?.windowResetMs, 30000);
});
test("applyQuotaHeadersToState & getQuotaAnalyticsSummary: records and aggregates quota analytics", () => {
const connId = "test-conn-p2-01";
const model = "gpt-4o";
const headers = {
"x-ratelimit-limit-tokens": "100000",
"x-ratelimit-remaining-tokens": "20000",
"x-ratelimit-reset-tokens": "120s",
};
applyQuotaHeadersToState(connId, model, headers, "openai");
const snapshot = getProviderQuota(connId, model);
assert.ok(snapshot);
assert.equal(snapshot?.tokensUsed, 80000);
assert.equal(snapshot?.tokenLimit, 100000);
const analytics = getQuotaAnalyticsSummary();
assert.ok(analytics.totalConnectionsTracked > 0);
assert.ok(analytics.connections.some((c) => c.connectionId === connId));
});
test("quotaResetTimers: tracks active reset items and purges expired windows", () => {
const connId = "test-conn-expired";
const model = "claude-sonnet-4-6";
const now = Date.now();
// Seed an already-expired window directly (recordProviderQuotaUsage always
// computes windows from Date.now(), so it cannot create a past window).
const db = getDbInstance();
db.prepare(
`INSERT OR REPLACE INTO provider_quota_state
(connection_id, model, tokens_used, token_limit, window_start, window_reset, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)`
).run(
connId,
model,
5000,
5000,
now - 10_000,
now - 1_000,
new Date().toISOString()
);
const expiredCount = resetExpiredQuotaWindows();
assert.ok(expiredCount >= 1);
});